maintenance: Make use of BatchRowIterator::setCaller
[mediawiki.git] / maintenance / rebuildtextindex.php
blob346deb2377eb6b127bddad9322d1dea6cc5b8426
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\MediaWikiServices;
31 use MediaWiki\Revision\SlotRecord;
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_MASTER );
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_MASTER );
83 $res = $dbw->select( 'page', 'MAX(page_id) AS count', [], __METHOD__ );
84 $s = $dbw->fetchObject( $res );
85 $count = $s->count;
86 $this->output( "Rebuilding index fields for {$count} pages...\n" );
87 $n = 0;
89 $revStore = MediaWikiServices::getInstance()->getRevisionStore();
90 $revQuery = $revStore->getQueryInfo( [ 'page' ] );
92 while ( $n < $count ) {
93 if ( $n ) {
94 $this->output( $n . "\n" );
96 $end = $n + self::RTI_CHUNK_SIZE - 1;
98 $res = $dbw->select(
99 $revQuery['tables'],
100 $revQuery['fields'],
101 [ "page_id BETWEEN $n AND $end", 'page_latest = rev_id' ],
102 __METHOD__,
104 $revQuery['joins']
107 foreach ( $res as $s ) {
108 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
109 try {
110 $revRecord = $revStore->newRevisionFromRow( $s );
111 $content = $revRecord->getContent( SlotRecord::MAIN );
113 $u = new SearchUpdate( $s->page_id, $title, $content );
114 $u->doUpdate();
115 } catch ( MWContentSerializationException $ex ) {
116 $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
117 . "`" . $title->getPrefixedDBkey() . "`!\n" );
120 $n += self::RTI_CHUNK_SIZE;
125 * (MySQL only) Drops fulltext index before populating the table.
127 private function dropMysqlTextIndex() {
128 $dbw = $this->getDB( DB_MASTER );
129 $searchindex = $dbw->tableName( 'searchindex' );
130 if ( $dbw->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
131 $this->output( "Dropping index...\n" );
132 $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
133 $dbw->query( $sql, __METHOD__ );
138 * (MySQL only) Adds back fulltext index after populating the table.
140 private function createMysqlTextIndex() {
141 $dbw = $this->getDB( DB_MASTER );
142 $searchindex = $dbw->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 $dbw->query( $sql, __METHOD__ );
151 * Deletes everything from search index.
153 private function clearSearchIndex() {
154 $dbw = $this->getDB( DB_MASTER );
155 $this->output( 'Clearing searchindex table...' );
156 $dbw->delete( 'searchindex', '*', __METHOD__ );
157 $this->output( "Done\n" );
161 $maintClass = RebuildTextIndex::class;
162 require_once RUN_MAINTENANCE_IF_MAIN;