MediaWiki  1.23.2
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->mDescription = "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 = wfGetDB( DB_MASTER );
55  if ( $this->db->getType() == 'postgres' ) {
56  $this->error( "This script is not needed when using Postgres.\n", true );
57  }
58 
59  $this->db = wfGetDB( DB_MASTER );
60  if ( $this->db->getType() == 'sqlite' ) {
62  $this->error( "Your version of SQLite module for PHP doesn't support full-text search (FTS3).\n", true );
63  }
64  if ( !$this->db->checkForEnabledSearch() ) {
65  $this->error( "Your database schema is not configured for full-text search support. Run update.php.\n", true );
66  }
67  }
68 
69  if ( $this->db->getType() == 'mysql' ) {
70  $this->dropMysqlTextIndex();
71  $this->populateSearchIndex();
72  $this->createMysqlTextIndex();
73  } else {
74  $this->clearSearchIndex();
75  $this->populateSearchIndex();
76  }
77 
78  $this->output( "Done.\n" );
79  }
80 
84  protected function populateSearchIndex() {
85  $res = $this->db->select( 'page', 'MAX(page_id) AS count' );
86  $s = $this->db->fetchObject( $res );
87  $count = $s->count;
88  $this->output( "Rebuilding index fields for {$count} pages...\n" );
89  $n = 0;
90 
91  $fields = array_merge(
95  );
96 
97  while ( $n < $count ) {
98  if ( $n ) {
99  $this->output( $n . "\n" );
100  }
101  $end = $n + self::RTI_CHUNK_SIZE - 1;
102 
103  $res = $this->db->select( array( 'page', 'revision', 'text' ), $fields,
104  array( "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ),
105  __METHOD__
106  );
107 
108  foreach ( $res as $s ) {
109  try {
110  $title = Title::makeTitle( $s->page_namespace, $s->page_title );
111 
112  $rev = new Revision( $s );
113  $content = $rev->getContent();
114 
115  $u = new SearchUpdate( $s->page_id, $title, $content );
116  $u->doUpdate();
117  } catch ( MWContentSerializationException $ex ) {
118  $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
119  . "`" . $title->getPrefixedDBkey() . "`!\n" );
120  }
121  }
123  }
124  }
125 
129  private function dropMysqlTextIndex() {
130  $searchindex = $this->db->tableName( 'searchindex' );
131  if ( $this->db->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
132  $this->output( "Dropping index...\n" );
133  $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
134  $this->db->query( $sql, __METHOD__ );
135  }
136  }
137 
141  private function createMysqlTextIndex() {
142  $searchindex = $this->db->tableName( 'searchindex' );
143  $this->output( "\nRebuild the index...\n" );
144  $sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
145  "ADD FULLTEXT si_text (si_text)";
146  $this->db->query( $sql, __METHOD__ );
147  }
148 
152  private function clearSearchIndex() {
153  $this->output( 'Clearing searchindex table...' );
154  $this->db->delete( 'searchindex', '*', __METHOD__ );
155  $this->output( "Done\n" );
156  }
157 }
158 
159 $maintClass = "RebuildTextIndex";
160 require_once RUN_MAINTENANCE_IF_MAIN;
RebuildTextIndex\__construct
__construct()
Default constructor.
Definition: rebuildtextindex.php:42
Title\makeTitle
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:398
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
RebuildTextIndex\dropMysqlTextIndex
dropMysqlTextIndex()
(MySQL only) Drops fulltext index before populating the table.
Definition: rebuildtextindex.php:128
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
$n
$n
Definition: RandomTest.php:76
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$s
$s
Definition: mergeMessageFileList.php:156
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
Revision\selectTextFields
static selectTextFields()
Return the list of text fields that should be selected to read the revision text.
Definition: Revision.php:467
Revision
Definition: Revision.php:26
RebuildTextIndex
Maintenance script that rebuilds search index table from scratch.
Definition: rebuildtextindex.php:35
RebuildTextIndex\execute
execute()
Do the actual work.
Definition: rebuildtextindex.php:51
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: ContentHandler.php:33
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
Maintenance\DB_ADMIN
const DB_ADMIN
Definition: Maintenance.php:59
Revision\selectPageFields
static selectPageFields()
Return the list of page fields that should be selected from page table.
Definition: Revision.php:478
RebuildTextIndex\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: rebuildtextindex.php:47
$title
presenting them properly to the user as errors is done by the caller $title
Definition: hooks.txt:1324
DatabaseBase
Database abstraction object.
Definition: Database.php:219
RebuildTextIndex\clearSearchIndex
clearSearchIndex()
Deletes everything from search index.
Definition: rebuildtextindex.php:151
$count
$count
Definition: UtfNormalTest2.php:96
$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:1337
RebuildTextIndex\populateSearchIndex
populateSearchIndex()
Populates the search index with content from all pages.
Definition: rebuildtextindex.php:83
$maintClass
$maintClass
Definition: rebuildtextindex.php:158
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:9
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
RebuildTextIndex\$db
DatabaseBase $db
Definition: rebuildtextindex.php:40
Revision\selectFields
static selectFields()
Return the list of revision fields that should be selected to create a new revision.
Definition: Revision.php:405
$res
$res
Definition: database.txt:21
RebuildTextIndex\RTI_CHUNK_SIZE
const RTI_CHUNK_SIZE
Definition: rebuildtextindex.php:36
DatabaseSqlite\getFulltextSearchModule
static getFulltextSearchModule()
Returns version of currently supported SQLite fulltext search module or false if none present.
Definition: DatabaseSqlite.php:196