MediaWiki  1.28.0
rebuildtextindex.php
Go to the documentation of this file.
1 <?php
28 require_once __DIR__ . '/Maintenance.php';
29 
36  const RTI_CHUNK_SIZE = 500;
37 
41  private $db;
42 
43  public function __construct() {
44  parent::__construct();
45  $this->addDescription( 'Rebuild search index table from scratch' );
46  }
47 
48  public function getDbType() {
49  return Maintenance::DB_ADMIN;
50  }
51 
52  public function execute() {
53  // Shouldn't be needed for Postgres
54  $this->db = $this->getDB( DB_MASTER );
55  if ( $this->db->getType() == 'postgres' ) {
56  $this->error( "This script is not needed when using Postgres.\n", true );
57  }
58 
59  if ( $this->db->getType() == 'sqlite' ) {
61  $this->error( "Your version of SQLite module for PHP doesn't "
62  . "support full-text search (FTS3).\n", true );
63  }
64  if ( !$this->db->checkForEnabledSearch() ) {
65  $this->error( "Your database schema is not configured for "
66  . "full-text search support. Run update.php.\n", true );
67  }
68  }
69 
70  if ( $this->db->getType() == 'mysql' ) {
71  $this->dropMysqlTextIndex();
72  $this->clearSearchIndex();
73  $this->populateSearchIndex();
74  $this->createMysqlTextIndex();
75  } else {
76  $this->clearSearchIndex();
77  $this->populateSearchIndex();
78  }
79 
80  $this->output( "Done.\n" );
81  }
82 
86  protected function populateSearchIndex() {
87  $res = $this->db->select( 'page', 'MAX(page_id) AS count' );
88  $s = $this->db->fetchObject( $res );
89  $count = $s->count;
90  $this->output( "Rebuilding index fields for {$count} pages...\n" );
91  $n = 0;
92 
93  $fields = array_merge(
97  );
98 
99  while ( $n < $count ) {
100  if ( $n ) {
101  $this->output( $n . "\n" );
102  }
103  $end = $n + self::RTI_CHUNK_SIZE - 1;
104 
105  $res = $this->db->select( [ 'page', 'revision', 'text' ], $fields,
106  [ "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ],
107  __METHOD__
108  );
109 
110  foreach ( $res as $s ) {
111  try {
112  $title = Title::makeTitle( $s->page_namespace, $s->page_title );
113 
114  $rev = new Revision( $s );
115  $content = $rev->getContent();
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  $searchindex = $this->db->tableName( 'searchindex' );
133  if ( $this->db->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
134  $this->output( "Dropping index...\n" );
135  $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
136  $this->db->query( $sql, __METHOD__ );
137  }
138  }
139 
143  private function createMysqlTextIndex() {
144  $searchindex = $this->db->tableName( 'searchindex' );
145  $this->output( "\nRebuild the index...\n" );
146  $sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
147  "ADD FULLTEXT si_text (si_text)";
148  $this->db->query( $sql, __METHOD__ );
149  }
150 
154  private function clearSearchIndex() {
155  $this->output( 'Clearing searchindex table...' );
156  $this->db->delete( 'searchindex', '*', __METHOD__ );
157  $this->output( "Done\n" );
158  }
159 }
160 
161 $maintClass = "RebuildTextIndex";
162 require_once RUN_MAINTENANCE_IF_MAIN;
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.txt:39
static getFulltextSearchModule()
Returns version of currently supported SQLite fulltext search module or false if none present...
getDB($db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
clearSearchIndex()
Deletes everything from search index.
const DB_MASTER
Definition: defines.php:23
Database independant search index updater.
$res
Definition: database.txt:21
const DB_ADMIN
Definition: Maintenance.php:59
dropMysqlTextIndex()
(MySQL only) Drops fulltext index before populating the table.
static selectFields()
Return the list of revision fields that should be selected to create a new revision.
Definition: Revision.php:442
static selectTextFields()
Return the list of text fields that should be selected to read the revision text. ...
Definition: Revision.php:504
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:953
Maintenance script that rebuilds search index table from scratch.
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:1721
addDescription($text)
Set the description text.
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:9
output($out, $channel=null)
Throw some output to the user.
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:35
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content $content
Definition: hooks.txt:1046
error($err, $die=0)
Throw an error to the user.
$count
static selectPageFields()
Return the list of page fields that should be selected from page table.
Definition: Revision.php:515
createMysqlTextIndex()
(MySQL only) Adds back fulltext index after populating the table.
static makeTitle($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:511
Exception representing a failure to serialize or unserialize a content object.