MediaWiki REL1_41
rebuildtextindex.php
Go to the documentation of this file.
1<?php
28require_once __DIR__ . '/Maintenance.php';
29
33
40 private const RTI_CHUNK_SIZE = 500;
41
42 public function __construct() {
43 parent::__construct();
44 $this->addDescription( 'Rebuild search index table from scratch' );
45 }
46
47 public function getDbType() {
49 }
50
51 public function execute() {
52 // Shouldn't be needed for Postgres
53 $dbw = $this->getDB( DB_PRIMARY );
54 if ( $dbw->getType() == 'postgres' ) {
55 $this->fatalError( "This script is not needed when using Postgres.\n" );
56 }
57
58 if ( $dbw->getType() == 'sqlite' ) {
59 if ( !DatabaseSqlite::getFulltextSearchModule() ) {
60 $this->fatalError( "Your version of SQLite module for PHP doesn't "
61 . "support full-text search (FTS3).\n" );
62 }
63 }
64
65 if ( $dbw->getType() == 'mysql' ) {
66 $this->dropMysqlTextIndex();
67 $this->clearSearchIndex();
68 $this->populateSearchIndex();
69 $this->createMysqlTextIndex();
70 } else {
71 $this->clearSearchIndex();
72 $this->populateSearchIndex();
73 }
74
75 $this->output( "Done.\n" );
76 }
77
81 protected function populateSearchIndex() {
82 $dbw = $this->getDB( DB_PRIMARY );
83 $res = $dbw->newSelectQueryBuilder()
84 ->select( 'MAX(page_id) AS count' )
85 ->from( 'page' )
86 ->caller( __METHOD__ )->fetchResultSet();
87 $s = $res->fetchObject();
88 $count = $s->count;
89 $this->output( "Rebuilding index fields for {$count} pages...\n" );
90 $n = 0;
91
92 $revStore = $this->getServiceContainer()->getRevisionStore();
93 $queryBuilderTemplate = $revStore->newSelectQueryBuilder( $dbw )
94 ->joinPage()
95 ->joinComment();
96
97 while ( $n < $count ) {
98 if ( $n ) {
99 $this->output( $n . "\n" );
100 }
101 $end = $n + self::RTI_CHUNK_SIZE - 1;
102 $queryBuilder = clone $queryBuilderTemplate;
103 $res = $queryBuilder->where( [ "page_id BETWEEN $n AND $end", 'page_latest = rev_id' ] )
104 ->caller( __METHOD__ )->fetchResultSet();
105
106 foreach ( $res as $s ) {
107
108 // T268673 Prevent failure of WikiPage.php: Invalid or virtual namespace -1 given
109 if ( $s->page_namespace < 0 ) {
110 continue;
111 }
112
113 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
114 try {
115 $revRecord = $revStore->newRevisionFromRow( $s );
116 $content = $revRecord->getContent( SlotRecord::MAIN );
117
118 $u = new SearchUpdate( $s->page_id, $title, $content );
119 $u->doUpdate();
120 } catch ( MWContentSerializationException $ex ) {
121 $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
122 . "`" . $title->getPrefixedDBkey() . "`!\n" );
123 }
124 }
125 $n += self::RTI_CHUNK_SIZE;
126 }
127 }
128
132 private function dropMysqlTextIndex() {
133 $dbw = $this->getDB( DB_PRIMARY );
134 $searchindex = $dbw->tableName( 'searchindex' );
135 if ( $dbw->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 $dbw->query( $sql, __METHOD__ );
139 }
140 }
141
145 private function createMysqlTextIndex() {
146 $dbw = $this->getDB( DB_PRIMARY );
147 $searchindex = $dbw->tableName( 'searchindex' );
148 $this->output( "\nRebuild the index...\n" );
149 foreach ( [ 'si_title', 'si_text' ] as $field ) {
150 $sql = "ALTER TABLE $searchindex ADD FULLTEXT $field ($field)";
151 $dbw->query( $sql, __METHOD__ );
152 }
153 }
154
158 private function clearSearchIndex() {
159 $dbw = $this->getDB( DB_PRIMARY );
160 $this->output( 'Clearing searchindex table...' );
161 $dbw->delete( 'searchindex', '*', __METHOD__ );
162 $this->output( "Done\n" );
163 }
164}
165
166$maintClass = RebuildTextIndex::class;
167require_once RUN_MAINTENANCE_IF_MAIN;
getDB()
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.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
fatalError( $msg, $exitCode=1)
Output a message and terminate the current script.
Value object representing a content slot associated with a page revision.
Represents a title within MediaWiki.
Definition Title.php:76
Maintenance script that rebuilds search index table from scratch.
execute()
Do the actual work.
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.
Database independent search index updater.
This is the SQLite database abstraction layer.
const DB_PRIMARY
Definition defines.php:28
$content
Definition router.php:76