MediaWiki master
rebuildtextindex.php
Go to the documentation of this file.
1<?php
7// @codeCoverageIgnoreStart
8require_once __DIR__ . '/Maintenance.php';
9// @codeCoverageIgnoreEnd
10
17
28 private const RTI_CHUNK_SIZE = 500;
29
30 public function __construct() {
31 parent::__construct();
32 $this->addDescription( 'Rebuild search index table from scratch' );
33 }
34
36 public function getDbType() {
37 return Maintenance::DB_ADMIN;
38 }
39
40 public function execute() {
41 // Shouldn't be needed for Postgres
42 $dbw = $this->getPrimaryDB();
43 if ( $dbw->getType() == 'postgres' ) {
44 $this->fatalError( "This script is not needed when using Postgres.\n" );
45 }
46
47 if ( $dbw->getType() == 'sqlite' ) {
48 if ( !DatabaseSqlite::getFulltextSearchModule() ) {
49 $this->fatalError( "Your version of SQLite module for PHP doesn't "
50 . "support full-text search (FTS3).\n" );
51 }
52 }
53
54 if ( $dbw->getType() == 'mysql' ) {
55 $this->dropMysqlTextIndex();
56 $this->clearSearchIndex();
57 $this->populateSearchIndex();
58 $this->createMysqlTextIndex();
59 } else {
60 $this->clearSearchIndex();
61 $this->populateSearchIndex();
62 }
63
64 $this->output( "Done.\n" );
65 }
66
70 protected function populateSearchIndex() {
71 $dbw = $this->getPrimaryDB();
72 $res = $dbw->newSelectQueryBuilder()
73 ->select( [ 'count' => 'MAX(page_id)' ] )
74 ->from( 'page' )
75 ->caller( __METHOD__ )->fetchResultSet();
76 $s = $res->fetchObject();
77 $count = $s->count;
78 $this->output( "Rebuilding index fields for {$count} pages...\n" );
79 $n = 0;
80
81 $revStore = $this->getServiceContainer()->getRevisionStore();
82 $queryBuilderTemplate = $revStore->newSelectQueryBuilder( $dbw )
83 ->joinPage()
84 ->joinComment();
85
86 while ( $n < $count ) {
87 if ( $n ) {
88 $this->output( $n . "\n" );
89 }
90 $end = $n + self::RTI_CHUNK_SIZE - 1;
91 $queryBuilder = clone $queryBuilderTemplate;
92 $res = $queryBuilder->where( [
93 $dbw->expr( 'page_id', '>=', $n )->and( 'page_id', '<=', $end ),
94 'page_latest = rev_id'
95 ] )->caller( __METHOD__ )->fetchResultSet();
96
97 foreach ( $res as $s ) {
98
99 // T268673 Prevent failure of WikiPage.php: Invalid or virtual namespace -1 given
100 if ( $s->page_namespace < 0 ) {
101 continue;
102 }
103
104 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
105 try {
106 $revRecord = $revStore->newRevisionFromRow( $s );
107 $content = $revRecord->getContent( SlotRecord::MAIN );
108
109 $u = new SearchUpdate( $s->page_id, $title, $content );
110 $u->doUpdate();
112 $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
113 . "`" . $title->getPrefixedDBkey() . "`!\n" );
114 }
115 }
116 $n += self::RTI_CHUNK_SIZE;
117 }
118 }
119
123 private function dropMysqlTextIndex() {
124 $dbw = $this->getDB( DB_PRIMARY );
125 $searchindex = $dbw->tableName( 'searchindex' );
126 $this->output( "Dropping index...\n" );
127 $sql = <<<SQL
128 ALTER TABLE $searchindex
129 DROP INDEX IF EXISTS si_title,
130 DROP INDEX IF EXISTS si_text
131 SQL;
132 $dbw->query( $sql, __METHOD__ );
133 }
134
138 private function createMysqlTextIndex() {
139 $dbw = $this->getPrimaryDB();
140 $searchindex = $dbw->tableName( 'searchindex' );
141 $this->output( "\nRebuild the index...\n" );
142 foreach ( [ 'si_title', 'si_text' ] as $field ) {
143 $sql = "ALTER TABLE $searchindex ADD FULLTEXT $field ($field)";
144 $dbw->query( $sql, __METHOD__ );
145 }
146 }
147
151 private function clearSearchIndex() {
152 $dbw = $this->getPrimaryDB();
153 $this->output( 'Clearing searchindex table...' );
154 $dbw->newDeleteQueryBuilder()
155 ->deleteFrom( 'searchindex' )
156 ->where( '*' )
157 ->caller( __METHOD__ )->execute();
158 $this->output( "Done\n" );
159 }
160}
161
162// @codeCoverageIgnoreStart
163$maintClass = RebuildTextIndex::class;
164require_once RUN_MAINTENANCE_IF_MAIN;
165// @codeCoverageIgnoreEnd
const DB_PRIMARY
Definition defines.php:28
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...
output( $out, $channel=null)
Throw some output to the user.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
getServiceContainer()
Returns the main service container.
getPrimaryDB(string|false $virtualDomain=false)
addDescription( $text)
Set the description text.
Value object representing a content slot associated with a page revision.
Database independent search index updater.
Represents a title within MediaWiki.
Definition Title.php:69
Rebuild 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.