MediaWiki master
rebuildtextindex.php
Go to the documentation of this file.
1<?php
28// @codeCoverageIgnoreStart
29require_once __DIR__ . '/Maintenance.php';
30// @codeCoverageIgnoreEnd
31
36
43 private const RTI_CHUNK_SIZE = 500;
44
45 public function __construct() {
46 parent::__construct();
47 $this->addDescription( 'Rebuild search index table from scratch' );
48 }
49
50 public function getDbType() {
52 }
53
54 public function execute() {
55 // Shouldn't be needed for Postgres
56 $dbw = $this->getPrimaryDB();
57 if ( $dbw->getType() == 'postgres' ) {
58 $this->fatalError( "This script is not needed when using Postgres.\n" );
59 }
60
61 if ( $dbw->getType() == 'sqlite' ) {
62 if ( !DatabaseSqlite::getFulltextSearchModule() ) {
63 $this->fatalError( "Your version of SQLite module for PHP doesn't "
64 . "support full-text search (FTS3).\n" );
65 }
66 }
67
68 if ( $dbw->getType() == 'mysql' ) {
69 $this->dropMysqlTextIndex();
70 $this->clearSearchIndex();
71 $this->populateSearchIndex();
72 $this->createMysqlTextIndex();
73 } else {
74 $this->clearSearchIndex();
75 $this->populateSearchIndex();
76 }
77
78 $this->output( "Done.\n" );
79 }
80
84 protected function populateSearchIndex() {
85 $dbw = $this->getPrimaryDB();
86 $res = $dbw->newSelectQueryBuilder()
87 ->select( [ 'count' => 'MAX(page_id)' ] )
88 ->from( 'page' )
89 ->caller( __METHOD__ )->fetchResultSet();
90 $s = $res->fetchObject();
91 $count = $s->count;
92 $this->output( "Rebuilding index fields for {$count} pages...\n" );
93 $n = 0;
94
95 $revStore = $this->getServiceContainer()->getRevisionStore();
96 $queryBuilderTemplate = $revStore->newSelectQueryBuilder( $dbw )
97 ->joinPage()
98 ->joinComment();
99
100 while ( $n < $count ) {
101 if ( $n ) {
102 $this->output( $n . "\n" );
103 }
104 $end = $n + self::RTI_CHUNK_SIZE - 1;
105 $queryBuilder = clone $queryBuilderTemplate;
106 $res = $queryBuilder->where( [
107 $dbw->expr( 'page_id', '>=', $n )->and( 'page_id', '<=', $end ),
108 'page_latest = rev_id'
109 ] )->caller( __METHOD__ )->fetchResultSet();
110
111 foreach ( $res as $s ) {
112
113 // T268673 Prevent failure of WikiPage.php: Invalid or virtual namespace -1 given
114 if ( $s->page_namespace < 0 ) {
115 continue;
116 }
117
118 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
119 try {
120 $revRecord = $revStore->newRevisionFromRow( $s );
121 $content = $revRecord->getContent( SlotRecord::MAIN );
122
123 $u = new SearchUpdate( $s->page_id, $title, $content );
124 $u->doUpdate();
125 } catch ( MWContentSerializationException $ex ) {
126 $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
127 . "`" . $title->getPrefixedDBkey() . "`!\n" );
128 }
129 }
130 $n += self::RTI_CHUNK_SIZE;
131 }
132 }
133
137 private function dropMysqlTextIndex() {
138 $dbw = $this->getDB( DB_PRIMARY );
139 $searchindex = $dbw->tableName( 'searchindex' );
140 if ( $dbw->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
141 $this->output( "Dropping index...\n" );
142 $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
143 $dbw->query( $sql, __METHOD__ );
144 }
145 }
146
150 private function createMysqlTextIndex() {
151 $dbw = $this->getPrimaryDB();
152 $searchindex = $dbw->tableName( 'searchindex' );
153 $this->output( "\nRebuild the index...\n" );
154 foreach ( [ 'si_title', 'si_text' ] as $field ) {
155 $sql = "ALTER TABLE $searchindex ADD FULLTEXT $field ($field)";
156 $dbw->query( $sql, __METHOD__ );
157 }
158 }
159
163 private function clearSearchIndex() {
164 $dbw = $this->getPrimaryDB();
165 $this->output( 'Clearing searchindex table...' );
166 $dbw->newDeleteQueryBuilder()
167 ->deleteFrom( 'searchindex' )
168 ->where( '*' )
169 ->caller( __METHOD__ )->execute();
170 $this->output( "Done\n" );
171 }
172}
173
174// @codeCoverageIgnoreStart
175$maintClass = RebuildTextIndex::class;
176require_once RUN_MAINTENANCE_IF_MAIN;
177// @codeCoverageIgnoreEnd
Exception representing a failure to serialize or unserialize a content object.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
output( $out, $channel=null)
Throw some output to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Database independent search index updater.
Value object representing a content slot associated with a page revision.
Represents a title within MediaWiki.
Definition Title.php:78
Maintenance script that rebuilds search index table from scratch.
execute()
Do the actual work.
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
__construct()
Default constructor.
populateSearchIndex()
Populates the search index with content from all pages.
This is the SQLite database abstraction layer.
const DB_PRIMARY
Definition defines.php:28