3 * Rebuild search index table from scratch. This may take several
4 * hours, depending on the database size and server configuration.
6 * Postgres is trigger-based and should never need rebuilding.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
24 * @ingroup Maintenance
28 require_once __DIR__
. '/Maintenance.php';
30 use Wikimedia\Rdbms\IMaintainableDatabase
;
31 use Wikimedia\Rdbms\DatabaseSqlite
;
34 * Maintenance script that rebuilds search index table from scratch.
36 * @ingroup Maintenance
38 class RebuildTextIndex
extends Maintenance
{
39 const RTI_CHUNK_SIZE
= 500;
42 * @var IMaintainableDatabase
46 public function __construct() {
47 parent
::__construct();
48 $this->addDescription( 'Rebuild search index table from scratch' );
51 public function getDbType() {
52 return Maintenance
::DB_ADMIN
;
55 public function execute() {
56 // Shouldn't be needed for Postgres
57 $this->db
= $this->getDB( DB_MASTER
);
58 if ( $this->db
->getType() == 'postgres' ) {
59 $this->fatalError( "This script is not needed when using Postgres.\n" );
62 if ( $this->db
->getType() == 'sqlite' ) {
63 if ( !DatabaseSqlite
::getFulltextSearchModule() ) {
64 $this->fatalError( "Your version of SQLite module for PHP doesn't "
65 . "support full-text search (FTS3).\n" );
67 if ( !$this->db
->checkForEnabledSearch() ) {
68 $this->fatalError( "Your database schema is not configured for "
69 . "full-text search support. Run update.php.\n" );
73 if ( $this->db
->getType() == 'mysql' ) {
74 $this->dropMysqlTextIndex();
75 $this->clearSearchIndex();
76 $this->populateSearchIndex();
77 $this->createMysqlTextIndex();
79 $this->clearSearchIndex();
80 $this->populateSearchIndex();
83 $this->output( "Done.\n" );
87 * Populates the search index with content from all pages
89 protected function populateSearchIndex() {
90 $res = $this->db
->select( 'page', 'MAX(page_id) AS count' );
91 $s = $this->db
->fetchObject( $res );
93 $this->output( "Rebuilding index fields for {$count} pages...\n" );
96 $revQuery = Revision
::getQueryInfo( [ 'page', 'text' ] );
98 while ( $n < $count ) {
100 $this->output( $n . "\n" );
102 $end = $n + self
::RTI_CHUNK_SIZE
- 1;
104 $res = $this->db
->select( $revQuery['tables'], $revQuery['fields'],
105 [ "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ],
109 foreach ( $res as $s ) {
110 $title = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
112 $rev = new Revision( $s );
113 $content = $rev->getContent();
115 $u = new SearchUpdate( $s->page_id
, $title, $content );
117 } catch ( MWContentSerializationException
$ex ) {
118 $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
119 . "`" . $title->getPrefixedDBkey() . "`!\n" );
122 $n +
= self
::RTI_CHUNK_SIZE
;
127 * (MySQL only) Drops fulltext index before populating the table.
129 private function dropMysqlTextIndex() {
130 $searchindex = $this->db
->tableName( 'searchindex' );
131 if ( $this->db
->indexExists( 'searchindex', 'si_title', __METHOD__
) ) {
132 $this->output( "Dropping index...\n" );
133 $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
134 $this->db
->query( $sql, __METHOD__
);
139 * (MySQL only) Adds back fulltext index after populating the table.
141 private function createMysqlTextIndex() {
142 $searchindex = $this->db
->tableName( 'searchindex' );
143 $this->output( "\nRebuild the index...\n" );
144 foreach ( [ 'si_title', 'si_text' ] as $field ) {
145 $sql = "ALTER TABLE $searchindex ADD FULLTEXT $field ($field)";
146 $this->db
->query( $sql, __METHOD__
);
151 * Deletes everything from search index.
153 private function clearSearchIndex() {
154 $this->output( 'Clearing searchindex table...' );
155 $this->db
->delete( 'searchindex', '*', __METHOD__
);
156 $this->output( "Done\n" );
160 $maintClass = RebuildTextIndex
::class;
161 require_once RUN_MAINTENANCE_IF_MAIN
;