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