MediaWiki  master
rebuildtextindex.php
Go to the documentation of this file.
1 <?php
28 require_once __DIR__ . '/Maintenance.php';
29 
32 
39  const RTI_CHUNK_SIZE = 500;
40 
41  public function __construct() {
42  parent::__construct();
43  $this->addDescription( 'Rebuild search index table from scratch' );
44  }
45 
46  public function getDbType() {
47  return Maintenance::DB_ADMIN;
48  }
49 
50  public function execute() {
51  // Shouldn't be needed for Postgres
52  $dbw = $this->getDB( DB_MASTER );
53  if ( $dbw->getType() == 'postgres' ) {
54  $this->fatalError( "This script is not needed when using Postgres.\n" );
55  }
56 
57  if ( $dbw->getType() == 'sqlite' ) {
58  if ( !DatabaseSqlite::getFulltextSearchModule() ) {
59  $this->fatalError( "Your version of SQLite module for PHP doesn't "
60  . "support full-text search (FTS3).\n" );
61  }
62  }
63 
64  if ( $dbw->getType() == 'mysql' ) {
65  $this->dropMysqlTextIndex();
66  $this->clearSearchIndex();
67  $this->populateSearchIndex();
68  $this->createMysqlTextIndex();
69  } else {
70  $this->clearSearchIndex();
71  $this->populateSearchIndex();
72  }
73 
74  $this->output( "Done.\n" );
75  }
76 
80  protected function populateSearchIndex() {
81  $dbw = $this->getDB( DB_MASTER );
82  $res = $dbw->select( 'page', 'MAX(page_id) AS count' );
83  $s = $dbw->fetchObject( $res );
84  $count = $s->count;
85  $this->output( "Rebuilding index fields for {$count} pages...\n" );
86  $n = 0;
87 
88  $revQuery = MediaWikiServices::getInstance()
89  ->getRevisionStore()
90  ->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  $rev = new Revision( $s );
111  $content = $rev->getContent();
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:41
RUN_MAINTENANCE_IF_MAIN
const RUN_MAINTENANCE_IF_MAIN
Definition: Maintenance.php:42
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:137
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:511
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:355
$s
$s
Definition: mergeMessageFileList.php:185
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:89
$res
$res
Definition: testCompression.php:57
$revQuery
$revQuery
Definition: testCompression.php:56
Revision
Definition: Revision.php:40
RebuildTextIndex
Maintenance script that rebuilds search index table from scratch.
Definition: rebuildtextindex.php:38
RebuildTextIndex\execute
execute()
Do the actual work.
Definition: rebuildtextindex.php:50
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:7
$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:595
DB_MASTER
const DB_MASTER
Definition: defines.php:26
Maintenance\DB_ADMIN
const DB_ADMIN
Definition: Maintenance.php:96
RebuildTextIndex\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: rebuildtextindex.php:46
$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:1384
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:80
$maintClass
$maintClass
Definition: rebuildtextindex.php:161
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:460
RebuildTextIndex\RTI_CHUNK_SIZE
const RTI_CHUNK_SIZE
Definition: rebuildtextindex.php:39