build: Upgrade mediawiki-codesniffer from 26.0.0 to 28.0.0
[lhc/web/wiklou.git] / maintenance / rebuildtextindex.php
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 require_once __DIR__ . '/Maintenance.php';
29
30 use Wikimedia\Rdbms\DatabaseSqlite;
31
32 /**
33 * Maintenance script that rebuilds search index table from scratch.
34 *
35 * @ingroup Maintenance
36 */
37 class RebuildTextIndex extends Maintenance {
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() {
46 return Maintenance::DB_ADMIN;
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
76 /**
77 * Populates the search index with content from all pages
78 */
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 }
117 $n += self::RTI_CHUNK_SIZE;
118 }
119 }
120
121 /**
122 * (MySQL only) Drops fulltext index before populating the table.
123 */
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
134 /**
135 * (MySQL only) Adds back fulltext index after populating the table.
136 */
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
147 /**
148 * Deletes everything from search index.
149 */
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;
159 require_once RUN_MAINTENANCE_IF_MAIN;