Debian Codegraph @

maintenance/rebuildtextindex.php

raw symbols SCIP
1 <?php
2 /**
3 * Rebuild search index table from scratch. This may take several
4 * hours, depending on the database size and server configuration.
5 *
6 * Postgres is trigger-based and should never need rebuilding.
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * http://www.gnu.org/copyleft/gpl.html
22 *
23 * @file
24 * @ingroup Maintenance
25 * @todo document
26 */
27
28 // @codeCoverageIgnoreStart
29 require_once __DIR__ . '/Maintenance.php';
30 // @codeCoverageIgnoreEnd
31
32 use MediaWiki\Deferred\SearchUpdate;
33 use MediaWiki\Revision\SlotRecord;
34 use MediaWiki\Title\Title;
35 use Wikimedia\Rdbms\DatabaseSqlite;
36
37 /**
38 * Maintenance script that rebuilds search index table from scratch.
39 *
40 * @ingroup Maintenance
41 */
42 class RebuildTextIndex extends Maintenance {
43 private const RTI_CHUNK_SIZE = 500;
44
45 public function __construct() {
46 parent::__construct();
47 $this->addDescription( 'Rebuild search index table from scratch' );
48 }
49
50 public function getDbType() {
51 return Maintenance::DB_ADMIN;
52 }
53
54 public function execute() {
55 // Shouldn't be needed for Postgres
56 $dbw = $this->getPrimaryDB();
57 if ( $dbw->getType() == 'postgres' ) {
58 $this->fatalError( "This script is not needed when using Postgres.\n" );
59 }
60
61 if ( $dbw->getType() == 'sqlite' ) {
62 if ( !DatabaseSqlite::getFulltextSearchModule() ) {
63 $this->fatalError( "Your version of SQLite module for PHP doesn't "
64 . "support full-text search (FTS3).\n" );
65 }
66 }
67
68 if ( $dbw->getType() == 'mysql' ) {
69 $this->dropMysqlTextIndex();
70 $this->clearSearchIndex();
71 $this->populateSearchIndex();
72 $this->createMysqlTextIndex();
73 } else {
74 $this->clearSearchIndex();
75 $this->populateSearchIndex();
76 }
77
78 $this->output( "Done.\n" );
79 }
80
81 /**
82 * Populates the search index with content from all pages
83 */
84 protected function populateSearchIndex() {
85 $dbw = $this->getPrimaryDB();
86 $res = $dbw->newSelectQueryBuilder()
87 ->select( [ 'count' => 'MAX(page_id)' ] )
88 ->from( 'page' )
89 ->caller( __METHOD__ )->fetchResultSet();
90 $s = $res->fetchObject();
91 $count = $s->count;
92 $this->output( "Rebuilding index fields for {$count} pages...\n" );
93 $n = 0;
94
95 $revStore = $this->getServiceContainer()->getRevisionStore();
96 $queryBuilderTemplate = $revStore->newSelectQueryBuilder( $dbw )
97 ->joinPage()
98 ->joinComment();
99
100 while ( $n < $count ) {
101 if ( $n ) {
102 $this->output( $n . "\n" );
103 }
104 $end = $n + self::RTI_CHUNK_SIZE - 1;
105 $queryBuilder = clone $queryBuilderTemplate;
106 $res = $queryBuilder->where( [
107 $dbw->expr( 'page_id', '>=', $n )->and( 'page_id', '<=', $end ),
108 'page_latest = rev_id'
109 ] )->caller( __METHOD__ )->fetchResultSet();
110
111 foreach ( $res as $s ) {
112
113 // T268673 Prevent failure of WikiPage.php: Invalid or virtual namespace -1 given
114 if ( $s->page_namespace < 0 ) {
115 continue;
116 }
117
118 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
119 try {
120 $revRecord = $revStore->newRevisionFromRow( $s );
121 $content = $revRecord->getContent( SlotRecord::MAIN );
122
123 $u = new SearchUpdate( $s->page_id, $title, $content );
124 $u->doUpdate();
125 } catch ( MWContentSerializationException $ex ) {
126 $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
127 . "`" . $title->getPrefixedDBkey() . "`!\n" );
128 }
129 }
130 $n += self::RTI_CHUNK_SIZE;
131 }
132 }
133
134 /**
135 * (MySQL only) Drops fulltext index before populating the table.
136 */
137 private function dropMysqlTextIndex() {
138 $dbw = $this->getDB( DB_PRIMARY );
139 $searchindex = $dbw->tableName( 'searchindex' );
140 if ( $dbw->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
141 $this->output( "Dropping index...\n" );
142 $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
143 $dbw->query( $sql, __METHOD__ );
144 }
145 }
146
147 /**
148 * (MySQL only) Adds back fulltext index after populating the table.
149 */
150 private function createMysqlTextIndex() {
151 $dbw = $this->getPrimaryDB();
152 $searchindex = $dbw->tableName( 'searchindex' );
153 $this->output( "\nRebuild the index...\n" );
154 foreach ( [ 'si_title', 'si_text' ] as $field ) {
155 $sql = "ALTER TABLE $searchindex ADD FULLTEXT $field ($field)";
156 $dbw->query( $sql, __METHOD__ );
157 }
158 }
159
160 /**
161 * Deletes everything from search index.
162 */
163 private function clearSearchIndex() {
164 $dbw = $this->getPrimaryDB();
165 $this->output( 'Clearing searchindex table...' );
166 $dbw->newDeleteQueryBuilder()
167 ->deleteFrom( 'searchindex' )
168 ->where( '*' )
169 ->caller( __METHOD__ )->execute();
170 $this->output( "Done\n" );
171 }
172 }
173
174 // @codeCoverageIgnoreStart
175 $maintClass = RebuildTextIndex::class;
176 require_once RUN_MAINTENANCE_IF_MAIN;
177 // @codeCoverageIgnoreEnd