MediaWiki REL1_34
rebuildtextindex.php
Go to the documentation of this file.
1<?php
28require_once __DIR__ . '/Maintenance.php';
29
31
38 const RTI_CHUNK_SIZE = 500;
39
40 public function __construct() {
41 parent::__construct();
42 $this->addDescription( 'Rebuild search index table from scratch' );
43 }
44
45 public function getDbType() {
47 }
48
49 public function execute() {
50 // Shouldn't be needed for Postgres
51 $dbw = $this->getDB( DB_MASTER );
52 if ( $dbw->getType() == 'postgres' ) {
53 $this->fatalError( "This script is not needed when using Postgres.\n" );
54 }
55
56 if ( $dbw->getType() == 'sqlite' ) {
57 if ( !DatabaseSqlite::getFulltextSearchModule() ) {
58 $this->fatalError( "Your version of SQLite module for PHP doesn't "
59 . "support full-text search (FTS3).\n" );
60 }
61 }
62
63 if ( $dbw->getType() == 'mysql' ) {
64 $this->dropMysqlTextIndex();
65 $this->clearSearchIndex();
66 $this->populateSearchIndex();
67 $this->createMysqlTextIndex();
68 } else {
69 $this->clearSearchIndex();
70 $this->populateSearchIndex();
71 }
72
73 $this->output( "Done.\n" );
74 }
75
79 protected function populateSearchIndex() {
80 $dbw = $this->getDB( DB_MASTER );
81 $res = $dbw->select( 'page', 'MAX(page_id) AS count' );
82 $s = $dbw->fetchObject( $res );
83 $count = $s->count;
84 $this->output( "Rebuilding index fields for {$count} pages...\n" );
85 $n = 0;
86
87 $revQuery = Revision::getQueryInfo( [ 'page' ] );
88
89 while ( $n < $count ) {
90 if ( $n ) {
91 $this->output( $n . "\n" );
92 }
93 $end = $n + self::RTI_CHUNK_SIZE - 1;
94
95 $res = $dbw->select(
96 $revQuery['tables'],
97 $revQuery['fields'],
98 [ "page_id BETWEEN $n AND $end", 'page_latest = rev_id' ],
99 __METHOD__,
100 [],
101 $revQuery['joins']
102 );
103
104 foreach ( $res as $s ) {
105 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
106 try {
107 $rev = new Revision( $s );
108 $content = $rev->getContent();
109
110 $u = new SearchUpdate( $s->page_id, $title, $content );
111 $u->doUpdate();
112 } catch ( MWContentSerializationException $ex ) {
113 $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
114 . "`" . $title->getPrefixedDBkey() . "`!\n" );
115 }
116 }
118 }
119 }
120
124 private function dropMysqlTextIndex() {
125 $dbw = $this->getDB( DB_MASTER );
126 $searchindex = $dbw->tableName( 'searchindex' );
127 if ( $dbw->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
128 $this->output( "Dropping index...\n" );
129 $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
130 $dbw->query( $sql, __METHOD__ );
131 }
132 }
133
137 private function createMysqlTextIndex() {
138 $dbw = $this->getDB( DB_MASTER );
139 $searchindex = $dbw->tableName( 'searchindex' );
140 $this->output( "\nRebuild the index...\n" );
141 foreach ( [ 'si_title', 'si_text' ] as $field ) {
142 $sql = "ALTER TABLE $searchindex ADD FULLTEXT $field ($field)";
143 $dbw->query( $sql, __METHOD__ );
144 }
145 }
146
150 private function clearSearchIndex() {
151 $dbw = $this->getDB( DB_MASTER );
152 $this->output( 'Clearing searchindex table...' );
153 $dbw->delete( 'searchindex', '*', __METHOD__ );
154 $this->output( "Done\n" );
155 }
156}
157
158$maintClass = RebuildTextIndex::class;
159require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
const 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...
output( $out, $channel=null)
Throw some output to the user.
addDescription( $text)
Set the description text.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
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.
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 getQueryInfo( $options=[])
Return the tables, fields, and join conditions to be selected to create a new revision object.
Definition Revision.php:315
Database independant search index updater.
const DB_MASTER
Definition defines.php:26
$content
Definition router.php:78