43 private const RTI_CHUNK_SIZE = 500;
46 parent::__construct();
47 $this->
addDescription(
'Rebuild search index table from scratch' );
57 if ( $dbw->getType() ==
'postgres' ) {
58 $this->
fatalError(
"This script is not needed when using Postgres.\n" );
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" );
68 if ( $dbw->getType() ==
'mysql' ) {
69 $this->dropMysqlTextIndex();
70 $this->clearSearchIndex();
72 $this->createMysqlTextIndex();
74 $this->clearSearchIndex();
78 $this->
output(
"Done.\n" );
86 $res = $dbw->newSelectQueryBuilder()
87 ->select( [
'count' =>
'MAX(page_id)' ] )
89 ->caller( __METHOD__ )->fetchResultSet();
90 $s = $res->fetchObject();
92 $this->
output(
"Rebuilding index fields for {$count} pages...\n" );
96 $queryBuilderTemplate = $revStore->newSelectQueryBuilder( $dbw )
100 while ( $n < $count ) {
102 $this->
output( $n .
"\n" );
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();
111 foreach ( $res as $s ) {
114 if ( $s->page_namespace < 0 ) {
118 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
120 $revRecord = $revStore->newRevisionFromRow( $s );
121 $content = $revRecord->getContent( SlotRecord::MAIN );
126 $this->
output(
"Failed to deserialize content of revision {$s->rev_id} of page "
127 .
"`" . $title->getPrefixedDBkey() .
"`!\n" );
130 $n += self::RTI_CHUNK_SIZE;
137 private function dropMysqlTextIndex() {
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__ );
150 private function createMysqlTextIndex() {
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__ );
163 private function clearSearchIndex() {
165 $this->
output(
'Clearing searchindex table...' );
166 $dbw->newDeleteQueryBuilder()
167 ->deleteFrom(
'searchindex' )
169 ->caller( __METHOD__ )->execute();
170 $this->
output(
"Done\n" );
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.
Maintenance script that rebuilds search index table from scratch.
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
populateSearchIndex()
Populates the search index with content from all pages.