MediaWiki  master
rebuildtextindex.php
Go to the documentation of this file.
1 <?php
28 require_once __DIR__ . '/Maintenance.php';
29 
33 
40  private const RTI_CHUNK_SIZE = 500;
41 
42  public function __construct() {
43  parent::__construct();
44  $this->addDescription( 'Rebuild search index table from scratch' );
45  }
46 
47  public function getDbType() {
48  return Maintenance::DB_ADMIN;
49  }
50 
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" );
56  }
57 
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" );
62  }
63  }
64 
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();
73  }
74 
75  $this->output( "Done.\n" );
76  }
77 
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;
88 
89  $revStore = MediaWikiServices::getInstance()->getRevisionStore();
90  $revQuery = $revStore->getQueryInfo( [ 'page' ] );
91 
92  while ( $n < $count ) {
93  if ( $n ) {
94  $this->output( $n . "\n" );
95  }
96  $end = $n + self::RTI_CHUNK_SIZE - 1;
97 
98  $res = $dbw->select(
99  $revQuery['tables'],
100  $revQuery['fields'],
101  [ "page_id BETWEEN $n AND $end", 'page_latest = rev_id' ],
102  __METHOD__,
103  [],
104  $revQuery['joins']
105  );
106 
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 );
112 
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" );
118  }
119  }
120  $n += self::RTI_CHUNK_SIZE;
121  }
122  }
123 
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__ );
134  }
135  }
136 
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__ );
147  }
148  }
149 
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" );
158  }
159 }
160 
161 $maintClass = RebuildTextIndex::class;
162 require_once RUN_MAINTENANCE_IF_MAIN;
RebuildTextIndex\__construct
__construct()
Default constructor.
Definition: rebuildtextindex.php:42
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:38
Wikimedia\Rdbms\DatabaseSqlite
Definition: DatabaseSqlite.php:37
RebuildTextIndex\dropMysqlTextIndex
dropMysqlTextIndex()
(MySQL only) Drops fulltext index before populating the table.
Definition: rebuildtextindex.php:127
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:152
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:487
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:327
SearchUpdate
Database independant search index updater.
Definition: SearchUpdate.php:33
$s
$s
Definition: mergeMessageFileList.php:185
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:55
$res
$res
Definition: testCompression.php:57
$revQuery
$revQuery
Definition: testCompression.php:56
RebuildTextIndex
Maintenance script that rebuilds search index table from scratch.
Definition: rebuildtextindex.php:39
RebuildTextIndex\execute
execute()
Do the actual work.
Definition: rebuildtextindex.php:51
RebuildTextIndex\createMysqlTextIndex
createMysqlTextIndex()
(MySQL only) Adds back fulltext index after populating the table.
Definition: rebuildtextindex.php:140
MWContentSerializationException
Exception representing a failure to serialize or unserialize a content object.
Definition: MWContentSerializationException.php:8
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:592
DB_MASTER
const DB_MASTER
Definition: defines.php:26
$revStore
$revStore
Definition: testCompression.php:55
Maintenance\DB_ADMIN
const DB_ADMIN
Definition: Maintenance.php:62
RebuildTextIndex\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: rebuildtextindex.php:47
$content
$content
Definition: router.php:76
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1366
RebuildTextIndex\clearSearchIndex
clearSearchIndex()
Deletes everything from search index.
Definition: rebuildtextindex.php:153
RebuildTextIndex\populateSearchIndex
populateSearchIndex()
Populates the search index with content from all pages.
Definition: rebuildtextindex.php:81
$maintClass
$maintClass
Definition: rebuildtextindex.php:161
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:434
RebuildTextIndex\RTI_CHUNK_SIZE
const RTI_CHUNK_SIZE
Definition: rebuildtextindex.php:40
Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:39