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_PRIMARY );
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_PRIMARY );
83  $res = $dbw->newSelectQueryBuilder()
84  ->select( 'MAX(page_id) AS count' )
85  ->from( 'page' )
86  ->caller( __METHOD__ )->fetchResultSet();
87  $s = $res->fetchObject();
88  $count = $s->count;
89  $this->output( "Rebuilding index fields for {$count} pages...\n" );
90  $n = 0;
91 
92  $revStore = $this->getServiceContainer()->getRevisionStore();
93  $queryBuilderTemplate = $revStore->newSelectQueryBuilder( $dbw )
94  ->joinPage();
95 
96  while ( $n < $count ) {
97  if ( $n ) {
98  $this->output( $n . "\n" );
99  }
100  $end = $n + self::RTI_CHUNK_SIZE - 1;
101  $queryBuilder = clone $queryBuilderTemplate;
102  $res = $queryBuilder->where( [ "page_id BETWEEN $n AND $end", 'page_latest = rev_id' ] )
103  ->caller( __METHOD__ )->fetchResultSet();
104 
105  foreach ( $res as $s ) {
106 
107  // T268673 Prevent failure of WikiPage.php: Invalid or virtual namespace -1 given
108  if ( $s->page_namespace < 0 ) {
109  continue;
110  }
111 
112  $title = Title::makeTitle( $s->page_namespace, $s->page_title );
113  try {
114  $revRecord = $revStore->newRevisionFromRow( $s );
115  $content = $revRecord->getContent( SlotRecord::MAIN );
116 
117  $u = new SearchUpdate( $s->page_id, $title, $content );
118  $u->doUpdate();
119  } catch ( MWContentSerializationException $ex ) {
120  $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
121  . "`" . $title->getPrefixedDBkey() . "`!\n" );
122  }
123  }
124  $n += self::RTI_CHUNK_SIZE;
125  }
126  }
127 
131  private function dropMysqlTextIndex() {
132  $dbw = $this->getDB( DB_PRIMARY );
133  $searchindex = $dbw->tableName( 'searchindex' );
134  if ( $dbw->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
135  $this->output( "Dropping index...\n" );
136  $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
137  $dbw->query( $sql, __METHOD__ );
138  }
139  }
140 
144  private function createMysqlTextIndex() {
145  $dbw = $this->getDB( DB_PRIMARY );
146  $searchindex = $dbw->tableName( 'searchindex' );
147  $this->output( "\nRebuild the index...\n" );
148  foreach ( [ 'si_title', 'si_text' ] as $field ) {
149  $sql = "ALTER TABLE $searchindex ADD FULLTEXT $field ($field)";
150  $dbw->query( $sql, __METHOD__ );
151  }
152  }
153 
157  private function clearSearchIndex() {
158  $dbw = $this->getDB( DB_PRIMARY );
159  $this->output( 'Clearing searchindex table...' );
160  $dbw->delete( 'searchindex', '*', __METHOD__ );
161  $this->output( "Done\n" );
162  }
163 }
164 
165 $maintClass = RebuildTextIndex::class;
166 require_once RUN_MAINTENANCE_IF_MAIN;
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...
Definition: Maintenance.php:66
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
output( $out, $channel=null)
Throw some output to the user.
const DB_ADMIN
Definition: Maintenance.php:73
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
Represents a title within MediaWiki.
Definition: Title.php:76
Maintenance script that rebuilds 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.
Database independent search index updater.
This is the SQLite database abstraction layer.
const DB_PRIMARY
Definition: defines.php:28
$content
Definition: router.php:76