MediaWiki REL1_30
rebuildtextindex.php
Go to the documentation of this file.
1<?php
28require_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() {
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->error( "This script is not needed when using Postgres.\n", true );
60 }
61
62 if ( $this->db->getType() == 'sqlite' ) {
63 if ( !DatabaseSqlite::getFulltextSearchModule() ) {
64 $this->error( "Your version of SQLite module for PHP doesn't "
65 . "support full-text search (FTS3).\n", true );
66 }
67 if ( !$this->db->checkForEnabledSearch() ) {
68 $this->error( "Your database schema is not configured for "
69 . "full-text search support. Run update.php.\n", true );
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 $fields = array_merge(
100 );
101
102 while ( $n < $count ) {
103 if ( $n ) {
104 $this->output( $n . "\n" );
105 }
106 $end = $n + self::RTI_CHUNK_SIZE - 1;
107
108 $res = $this->db->select( [ 'page', 'revision', 'text' ], $fields,
109 [ "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ],
110 __METHOD__
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 }
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 $sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
149 "ADD FULLTEXT si_text (si_text)";
150 $this->db->query( $sql, __METHOD__ );
151 }
152
156 private function clearSearchIndex() {
157 $this->output( 'Clearing searchindex table...' );
158 $this->db->delete( 'searchindex', '*', __METHOD__ );
159 $this->output( "Done\n" );
160 }
161}
162
163$maintClass = "RebuildTextIndex";
164require_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...
getDB( $db, $groups=[], $wiki=false)
Returns a database to be used by current maintenance script.
addDescription( $text)
Set the description text.
Maintenance script that rebuilds search index table from scratch.
execute()
Do the actual work.
dropMysqlTextIndex()
(MySQL only) Drops fulltext index before populating the table.
createMysqlTextIndex()
(MySQL only) Adds back fulltext index after populating the table.
clearSearchIndex()
Deletes everything from search index.
IMaintainableDatabase $db
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.
static selectTextFields()
Return the list of text fields that should be selected to read the revision text.
Definition Revision.php:518
static selectPageFields()
Return the list of page fields that should be selected from page table.
Definition Revision.php:529
static selectFields()
Return the list of revision fields that should be selected to create a new revision.
Definition Revision.php:452
Database independant search index updater.
$res
Definition database.txt:21
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add in any and then calling output() to send it all. It could be easily changed to send incrementally if that becomes useful
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults error
Definition hooks.txt:2581
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:1760
Advanced database interface for IDatabase handles that include maintenance methods.
require_once RUN_MAINTENANCE_IF_MAIN
const DB_MASTER
Definition defines.php:26