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\DatabaseSqlite
;
33 * Maintenance script that rebuilds search index table from scratch.
35 * @ingroup Maintenance
37 class RebuildTextIndex
extends Maintenance
{
38 const RTI_CHUNK_SIZE
= 500;
40 public function __construct() {
41 parent
::__construct();
42 $this->addDescription( 'Rebuild search index table from scratch' );
45 public function getDbType() {
46 return Maintenance
::DB_ADMIN
;
49 public function execute() {
50 // Shouldn't be needed for Postgres
51 $dbw = $this->getDB( DB_MASTER
);
52 if ( $dbw->getType() == 'postgres' ) {
53 $this->fatalError( "This script is not needed when using Postgres.\n" );
56 if ( $dbw->getType() == 'sqlite' ) {
57 if ( !DatabaseSqlite
::getFulltextSearchModule() ) {
58 $this->fatalError( "Your version of SQLite module for PHP doesn't "
59 . "support full-text search (FTS3).\n" );
63 if ( $dbw->getType() == 'mysql' ) {
64 $this->dropMysqlTextIndex();
65 $this->clearSearchIndex();
66 $this->populateSearchIndex();
67 $this->createMysqlTextIndex();
69 $this->clearSearchIndex();
70 $this->populateSearchIndex();
73 $this->output( "Done.\n" );
77 * Populates the search index with content from all pages
79 protected function populateSearchIndex() {
80 $dbw = $this->getDB( DB_MASTER
);
81 $res = $dbw->select( 'page', 'MAX(page_id) AS count' );
82 $s = $dbw->fetchObject( $res );
84 $this->output( "Rebuilding index fields for {$count} pages...\n" );
87 $revQuery = Revision
::getQueryInfo( [ 'page' ] );
89 while ( $n < $count ) {
91 $this->output( $n . "\n" );
93 $end = $n + self
::RTI_CHUNK_SIZE
- 1;
98 [ "page_id BETWEEN $n AND $end", 'page_latest = rev_id' ],
104 foreach ( $res as $s ) {
105 $title = Title
::makeTitle( $s->page_namespace
, $s->page_title
);
107 $rev = new Revision( $s );
108 $content = $rev->getContent();
110 $u = new SearchUpdate( $s->page_id
, $title, $content );
112 } catch ( MWContentSerializationException
$ex ) {
113 $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
114 . "`" . $title->getPrefixedDBkey() . "`!\n" );
117 $n +
= self
::RTI_CHUNK_SIZE
;
122 * (MySQL only) Drops fulltext index before populating the table.
124 private function dropMysqlTextIndex() {
125 $dbw = $this->getDB( DB_MASTER
);
126 $searchindex = $dbw->tableName( 'searchindex' );
127 if ( $dbw->indexExists( 'searchindex', 'si_title', __METHOD__
) ) {
128 $this->output( "Dropping index...\n" );
129 $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
130 $dbw->query( $sql, __METHOD__
);
135 * (MySQL only) Adds back fulltext index after populating the table.
137 private function createMysqlTextIndex() {
138 $dbw = $this->getDB( DB_MASTER
);
139 $searchindex = $dbw->tableName( 'searchindex' );
140 $this->output( "\nRebuild the index...\n" );
141 foreach ( [ 'si_title', 'si_text' ] as $field ) {
142 $sql = "ALTER TABLE $searchindex ADD FULLTEXT $field ($field)";
143 $dbw->query( $sql, __METHOD__
);
148 * Deletes everything from search index.
150 private function clearSearchIndex() {
151 $dbw = $this->getDB( DB_MASTER
);
152 $this->output( 'Clearing searchindex table...' );
153 $dbw->delete( 'searchindex', '*', __METHOD__
);
154 $this->output( "Done\n" );
158 $maintClass = RebuildTextIndex
::class;
159 require_once RUN_MAINTENANCE_IF_MAIN
;