MediaWiki  master
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;
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:39
populateSearchIndex()
Populates the search index with content from all pages.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:86
clearSearchIndex()
Deletes everything from search index.
const DB_MASTER
Definition: defines.php:26
addDescription( $text)
Set the description text.
const DB_ADMIN
Definition: Maintenance.php:93
dropMysqlTextIndex()
(MySQL only) Drops fulltext index before populating the table.
static getQueryInfo( $options=[])
Return the tables, fields, and join conditions to be selected to create a new revision object...
Definition: Revision.php:316
Maintenance script that rebuilds search index table from scratch.
output( $out, $channel=null)
Throw some output to the user.
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:584
$revQuery
createMysqlTextIndex()
(MySQL only) Adds back fulltext index after populating the table.
$content
Definition: router.php:78
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.
Exception representing a failure to serialize or unserialize a content object.