MediaWiki  1.34.0
rebuildtextindex.php
Go to the documentation of this file.
1 <?php
28 require_once __DIR__ . '/Maintenance.php';
29 
31 
38  const RTI_CHUNK_SIZE = 500;
39 
40  public function __construct() {
41  parent::__construct();
42  $this->addDescription( 'Rebuild search index table from scratch' );
43  }
44 
45  public function getDbType() {
46  return Maintenance::DB_ADMIN;
47  }
48 
49  public function execute() {
50  // Shouldn't be needed for Postgres
51  $dbw = $this->getDB( DB_MASTER );
52  if ( $dbw->getType() == 'postgres' ) {
53  $this->fatalError( "This script is not needed when using Postgres.\n" );
54  }
55 
56  if ( $dbw->getType() == 'sqlite' ) {
57  if ( !DatabaseSqlite::getFulltextSearchModule() ) {
58  $this->fatalError( "Your version of SQLite module for PHP doesn't "
59  . "support full-text search (FTS3).\n" );
60  }
61  }
62 
63  if ( $dbw->getType() == 'mysql' ) {
64  $this->dropMysqlTextIndex();
65  $this->clearSearchIndex();
66  $this->populateSearchIndex();
67  $this->createMysqlTextIndex();
68  } else {
69  $this->clearSearchIndex();
70  $this->populateSearchIndex();
71  }
72 
73  $this->output( "Done.\n" );
74  }
75 
79  protected function populateSearchIndex() {
80  $dbw = $this->getDB( DB_MASTER );
81  $res = $dbw->select( 'page', 'MAX(page_id) AS count' );
82  $s = $dbw->fetchObject( $res );
83  $count = $s->count;
84  $this->output( "Rebuilding index fields for {$count} pages...\n" );
85  $n = 0;
86 
87  $revQuery = Revision::getQueryInfo( [ 'page' ] );
88 
89  while ( $n < $count ) {
90  if ( $n ) {
91  $this->output( $n . "\n" );
92  }
93  $end = $n + self::RTI_CHUNK_SIZE - 1;
94 
95  $res = $dbw->select(
96  $revQuery['tables'],
97  $revQuery['fields'],
98  [ "page_id BETWEEN $n AND $end", 'page_latest = rev_id' ],
99  __METHOD__,
100  [],
101  $revQuery['joins']
102  );
103 
104  foreach ( $res as $s ) {
105  $title = Title::makeTitle( $s->page_namespace, $s->page_title );
106  try {
107  $rev = new Revision( $s );
108  $content = $rev->getContent();
109 
110  $u = new SearchUpdate( $s->page_id, $title, $content );
111  $u->doUpdate();
112  } catch ( MWContentSerializationException $ex ) {
113  $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
114  . "`" . $title->getPrefixedDBkey() . "`!\n" );
115  }
116  }
117  $n += self::RTI_CHUNK_SIZE;
118  }
119  }
120 
124  private function dropMysqlTextIndex() {
125  $dbw = $this->getDB( DB_MASTER );
126  $searchindex = $dbw->tableName( 'searchindex' );
127  if ( $dbw->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
128  $this->output( "Dropping index...\n" );
129  $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
130  $dbw->query( $sql, __METHOD__ );
131  }
132  }
133 
137  private function createMysqlTextIndex() {
138  $dbw = $this->getDB( DB_MASTER );
139  $searchindex = $dbw->tableName( 'searchindex' );
140  $this->output( "\nRebuild the index...\n" );
141  foreach ( [ 'si_title', 'si_text' ] as $field ) {
142  $sql = "ALTER TABLE $searchindex ADD FULLTEXT $field ($field)";
143  $dbw->query( $sql, __METHOD__ );
144  }
145  }
146 
150  private function clearSearchIndex() {
151  $dbw = $this->getDB( DB_MASTER );
152  $this->output( 'Clearing searchindex table...' );
153  $dbw->delete( 'searchindex', '*', __METHOD__ );
154  $this->output( "Done\n" );
155  }
156 }
157 
158 $maintClass = RebuildTextIndex::class;
159 require_once RUN_MAINTENANCE_IF_MAIN;
RebuildTextIndex\__construct
__construct()
Default constructor.
Definition: rebuildtextindex.php:40
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
Wikimedia\Rdbms\DatabaseSqlite
Definition: DatabaseSqlite.php:38
RebuildTextIndex\dropMysqlTextIndex
dropMysqlTextIndex()
(MySQL only) Drops fulltext index before populating the table.
Definition: rebuildtextindex.php:124
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:504
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:348
$s
$s
Definition: mergeMessageFileList.php:185
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:82
$res
$res
Definition: testCompression.php:52
$revQuery
$revQuery
Definition: testCompression.php:51
Revision
Definition: Revision.php:40
RebuildTextIndex
Maintenance script that rebuilds search index table from scratch.
Definition: rebuildtextindex.php:37
Revision\getQueryInfo
static getQueryInfo( $options=[])
Return the tables, fields, and join conditions to be selected to create a new revision object.
Definition: Revision.php:315
RebuildTextIndex\execute
execute()
Do the actual work.
Definition: rebuildtextindex.php:49
RebuildTextIndex\createMysqlTextIndex
createMysqlTextIndex()
(MySQL only) Adds back fulltext index after populating the table.
Definition: rebuildtextindex.php:137
MWContentSerializationException
Exception representing a failure to serialize or unserialize a content object.
Definition: MWContentSerializationException.php:7
$title
$title
Definition: testCompression.php:34
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:586
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Maintenance\DB_ADMIN
const DB_ADMIN
Definition: Maintenance.php:89
RebuildTextIndex\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: rebuildtextindex.php:45
$content
$content
Definition: router.php:78
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1396
RebuildTextIndex\clearSearchIndex
clearSearchIndex()
Deletes everything from search index.
Definition: rebuildtextindex.php:150
RebuildTextIndex\populateSearchIndex
populateSearchIndex()
Populates the search index with content from all pages.
Definition: rebuildtextindex.php:79
$maintClass
$maintClass
Definition: rebuildtextindex.php:158
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:453
RebuildTextIndex\RTI_CHUNK_SIZE
const RTI_CHUNK_SIZE
Definition: rebuildtextindex.php:38