Update git submodules
[mediawiki.git] / maintenance / rebuildtextindex.php
blob2d16430f99507db8a7047b2142c5a9a5083db101
1 <?php
2 /**
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
23 * @file
24 * @ingroup Maintenance
25 * @todo document
28 require_once __DIR__ . '/Maintenance.php';
30 use MediaWiki\Revision\SlotRecord;
31 use MediaWiki\Title\Title;
32 use Wikimedia\Rdbms\DatabaseSqlite;
34 /**
35 * Maintenance script that rebuilds search index table from scratch.
37 * @ingroup Maintenance
39 class RebuildTextIndex extends Maintenance {
40 private const RTI_CHUNK_SIZE = 500;
42 public function __construct() {
43 parent::__construct();
44 $this->addDescription( 'Rebuild search index table from scratch' );
47 public function getDbType() {
48 return Maintenance::DB_ADMIN;
51 public function execute() {
52 // Shouldn't be needed for Postgres
53 $dbw = $this->getDB( DB_PRIMARY );
54 if ( $dbw->getType() == 'postgres' ) {
55 $this->fatalError( "This script is not needed when using Postgres.\n" );
58 if ( $dbw->getType() == 'sqlite' ) {
59 if ( !DatabaseSqlite::getFulltextSearchModule() ) {
60 $this->fatalError( "Your version of SQLite module for PHP doesn't "
61 . "support full-text search (FTS3).\n" );
65 if ( $dbw->getType() == 'mysql' ) {
66 $this->dropMysqlTextIndex();
67 $this->clearSearchIndex();
68 $this->populateSearchIndex();
69 $this->createMysqlTextIndex();
70 } else {
71 $this->clearSearchIndex();
72 $this->populateSearchIndex();
75 $this->output( "Done.\n" );
78 /**
79 * Populates the search index with content from all pages
81 protected function populateSearchIndex() {
82 $dbw = $this->getDB( DB_PRIMARY );
83 $res = $dbw->newSelectQueryBuilder()
84 ->select( 'MAX(page_id) AS count' )
85 ->from( 'page' )
86 ->caller( __METHOD__ )->fetchResultSet();
87 $s = $res->fetchObject();
88 $count = $s->count;
89 $this->output( "Rebuilding index fields for {$count} pages...\n" );
90 $n = 0;
92 $revStore = $this->getServiceContainer()->getRevisionStore();
93 $queryBuilderTemplate = $revStore->newSelectQueryBuilder( $dbw )
94 ->joinPage()
95 ->joinComment();
97 while ( $n < $count ) {
98 if ( $n ) {
99 $this->output( $n . "\n" );
101 $end = $n + self::RTI_CHUNK_SIZE - 1;
102 $queryBuilder = clone $queryBuilderTemplate;
103 $res = $queryBuilder->where( [ "page_id BETWEEN $n AND $end", 'page_latest = rev_id' ] )
104 ->caller( __METHOD__ )->fetchResultSet();
106 foreach ( $res as $s ) {
108 // T268673 Prevent failure of WikiPage.php: Invalid or virtual namespace -1 given
109 if ( $s->page_namespace < 0 ) {
110 continue;
113 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
114 try {
115 $revRecord = $revStore->newRevisionFromRow( $s );
116 $content = $revRecord->getContent( SlotRecord::MAIN );
118 $u = new SearchUpdate( $s->page_id, $title, $content );
119 $u->doUpdate();
120 } catch ( MWContentSerializationException $ex ) {
121 $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
122 . "`" . $title->getPrefixedDBkey() . "`!\n" );
125 $n += self::RTI_CHUNK_SIZE;
130 * (MySQL only) Drops fulltext index before populating the table.
132 private function dropMysqlTextIndex() {
133 $dbw = $this->getDB( DB_PRIMARY );
134 $searchindex = $dbw->tableName( 'searchindex' );
135 if ( $dbw->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
136 $this->output( "Dropping index...\n" );
137 $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
138 $dbw->query( $sql, __METHOD__ );
143 * (MySQL only) Adds back fulltext index after populating the table.
145 private function createMysqlTextIndex() {
146 $dbw = $this->getDB( DB_PRIMARY );
147 $searchindex = $dbw->tableName( 'searchindex' );
148 $this->output( "\nRebuild the index...\n" );
149 foreach ( [ 'si_title', 'si_text' ] as $field ) {
150 $sql = "ALTER TABLE $searchindex ADD FULLTEXT $field ($field)";
151 $dbw->query( $sql, __METHOD__ );
156 * Deletes everything from search index.
158 private function clearSearchIndex() {
159 $dbw = $this->getDB( DB_PRIMARY );
160 $this->output( 'Clearing searchindex table...' );
161 $dbw->delete( 'searchindex', '*', __METHOD__ );
162 $this->output( "Done\n" );
166 $maintClass = RebuildTextIndex::class;
167 require_once RUN_MAINTENANCE_IF_MAIN;