MediaWiki  REL1_31
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 
44  private $db;
45 
46  public function __construct() {
47  parent::__construct();
48  $this->addDescription( 'Rebuild search index table from scratch' );
49  }
50 
51  public function getDbType() {
52  return Maintenance::DB_ADMIN;
53  }
54 
55  public function execute() {
56  // Shouldn't be needed for Postgres
57  $this->db = $this->getDB( DB_MASTER );
58  if ( $this->db->getType() == 'postgres' ) {
59  $this->fatalError( "This script is not needed when using Postgres.\n" );
60  }
61 
62  if ( $this->db->getType() == 'sqlite' ) {
63  if ( !DatabaseSqlite::getFulltextSearchModule() ) {
64  $this->fatalError( "Your version of SQLite module for PHP doesn't "
65  . "support full-text search (FTS3).\n" );
66  }
67  if ( !$this->db->checkForEnabledSearch() ) {
68  $this->fatalError( "Your database schema is not configured for "
69  . "full-text search support. Run update.php.\n" );
70  }
71  }
72 
73  if ( $this->db->getType() == 'mysql' ) {
74  $this->dropMysqlTextIndex();
75  $this->clearSearchIndex();
76  $this->populateSearchIndex();
77  $this->createMysqlTextIndex();
78  } else {
79  $this->clearSearchIndex();
80  $this->populateSearchIndex();
81  }
82 
83  $this->output( "Done.\n" );
84  }
85 
89  protected function populateSearchIndex() {
90  $res = $this->db->select( 'page', 'MAX(page_id) AS count' );
91  $s = $this->db->fetchObject( $res );
92  $count = $s->count;
93  $this->output( "Rebuilding index fields for {$count} pages...\n" );
94  $n = 0;
95 
96  $revQuery = Revision::getQueryInfo( [ 'page', 'text' ] );
97 
98  while ( $n < $count ) {
99  if ( $n ) {
100  $this->output( $n . "\n" );
101  }
102  $end = $n + self::RTI_CHUNK_SIZE - 1;
103 
104  $res = $this->db->select(
105  $revQuery['tables'],
106  $revQuery['fields'],
107  [ "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ],
108  __METHOD__,
109  [],
110  $revQuery['joins']
111  );
112 
113  foreach ( $res as $s ) {
114  $title = Title::makeTitle( $s->page_namespace, $s->page_title );
115  try {
116  $rev = new Revision( $s );
117  $content = $rev->getContent();
118 
119  $u = new SearchUpdate( $s->page_id, $title, $content );
120  $u->doUpdate();
121  } catch ( MWContentSerializationException $ex ) {
122  $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
123  . "`" . $title->getPrefixedDBkey() . "`!\n" );
124  }
125  }
126  $n += self::RTI_CHUNK_SIZE;
127  }
128  }
129 
133  private function dropMysqlTextIndex() {
134  $searchindex = $this->db->tableName( 'searchindex' );
135  if ( $this->db->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
136  $this->output( "Dropping index...\n" );
137  $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
138  $this->db->query( $sql, __METHOD__ );
139  }
140  }
141 
145  private function createMysqlTextIndex() {
146  $searchindex = $this->db->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  $this->db->query( $sql, __METHOD__ );
151  }
152  }
153 
157  private function clearSearchIndex() {
158  $this->output( 'Clearing searchindex table...' );
159  $this->db->delete( 'searchindex', '*', __METHOD__ );
160  $this->output( "Done\n" );
161  }
162 }
163 
165 require_once RUN_MAINTENANCE_IF_MAIN;
RebuildTextIndex\__construct
__construct()
Default constructor.
Definition: rebuildtextindex.php:46
Wikimedia\Rdbms\DatabaseSqlite
Definition: DatabaseSqlite.php:37
RebuildTextIndex\dropMysqlTextIndex
dropMysqlTextIndex()
(MySQL only) Drops fulltext index before populating the table.
Definition: rebuildtextindex.php:133
use
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Definition: APACHE-LICENSE-2.0.txt:10
Maintenance\fatalError
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Definition: Maintenance.php:439
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:291
SearchUpdate
Database independant search index updater.
Definition: SearchUpdate.php:33
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$s
$s
Definition: mergeMessageFileList.php:187
$res
$res
Definition: database.txt:21
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
$revQuery
$revQuery
Definition: testCompression.php:51
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:37
Revision
Definition: Revision.php:41
RebuildTextIndex
Maintenance script that rebuilds search index table from scratch.
Definition: rebuildtextindex.php:38
Revision\getQueryInfo
static getQueryInfo( $options=[])
Return the tables, fields, and join conditions to be selected to create a new revision object.
Definition: Revision.php:492
RebuildTextIndex\execute
execute()
Do the actual work.
Definition: rebuildtextindex.php:55
RebuildTextIndex\createMysqlTextIndex
createMysqlTextIndex()
(MySQL only) Adds back fulltext index after populating the table.
Definition: rebuildtextindex.php:145
MWContentSerializationException
Exception representing a failure to serialize or unserialize a content object.
Definition: MWContentSerializationException.php:7
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:964
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:534
DB_MASTER
const DB_MASTER
Definition: defines.php:29
Maintenance\DB_ADMIN
const DB_ADMIN
Definition: Maintenance.php:68
RebuildTextIndex\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: rebuildtextindex.php:51
RebuildTextIndex\clearSearchIndex
clearSearchIndex()
Deletes everything from search index.
Definition: rebuildtextindex.php:157
$rev
presenting them properly to the user as errors is done by the caller return true use this to change the list i e etc $rev
Definition: hooks.txt:1777
RebuildTextIndex\populateSearchIndex
populateSearchIndex()
Populates the search index with content from all pages.
Definition: rebuildtextindex.php:89
RebuildTextIndex\$db
IMaintainableDatabase $db
Definition: rebuildtextindex.php:44
$maintClass
$maintClass
Definition: rebuildtextindex.php:164
as
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:22
Maintenance\getDB
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1309
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:388
class
you have access to all of the normal MediaWiki so you can get a DB use the etc For full docs on the Maintenance class
Definition: maintenance.txt:56
Wikimedia\Rdbms\IMaintainableDatabase
Advanced database interface for IDatabase handles that include maintenance methods.
Definition: IMaintainableDatabase.php:38
RebuildTextIndex\RTI_CHUNK_SIZE
const RTI_CHUNK_SIZE
Definition: rebuildtextindex.php:39