Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 72
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
RebuildTextIndex
0.00% covered (danger)
0.00%
0 / 69
0.00% covered (danger)
0.00%
0 / 7
342
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 getDbType
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 1
30
 populateSearchIndex
0.00% covered (danger)
0.00%
0 / 32
0.00% covered (danger)
0.00%
0 / 1
42
 dropMysqlTextIndex
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 createMysqlTextIndex
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 clearSearchIndex
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
2
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
28require_once __DIR__ . '/Maintenance.php';
29
30use MediaWiki\Deferred\SearchUpdate;
31use MediaWiki\Revision\SlotRecord;
32use MediaWiki\Title\Title;
33use Wikimedia\Rdbms\DatabaseSqlite;
34
35/**
36 * Maintenance script that rebuilds search index table from scratch.
37 *
38 * @ingroup Maintenance
39 */
40class RebuildTextIndex extends Maintenance {
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() {
49        return Maintenance::DB_ADMIN;
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
79    /**
80     * Populates the search index with content from all pages
81     */
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( [ "page_id BETWEEN $n AND $end", 'page_latest = rev_id' ] )
105                ->caller( __METHOD__ )->fetchResultSet();
106
107            foreach ( $res as $s ) {
108
109                // T268673 Prevent failure of WikiPage.php: Invalid or virtual namespace -1 given
110                if ( $s->page_namespace < 0 ) {
111                    continue;
112                }
113
114                $title = Title::makeTitle( $s->page_namespace, $s->page_title );
115                try {
116                    $revRecord = $revStore->newRevisionFromRow( $s );
117                    $content = $revRecord->getContent( SlotRecord::MAIN );
118
119                    $u = new SearchUpdate( $s->page_id, $title, $content );
120                    $u->doUpdate();
121                } catch ( MWContentSerializationException $ex ) {
122                    $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
123                        . "`" . $title->getPrefixedDBkey() . "`!\n" );
124                }
125            }
126            $n += self::RTI_CHUNK_SIZE;
127        }
128    }
129
130    /**
131     * (MySQL only) Drops fulltext index before populating the table.
132     */
133    private function dropMysqlTextIndex() {
134        $dbw = $this->getDB( DB_PRIMARY );
135        $searchindex = $dbw->tableName( 'searchindex' );
136        if ( $dbw->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
137            $this->output( "Dropping index...\n" );
138            $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
139            $dbw->query( $sql, __METHOD__ );
140        }
141    }
142
143    /**
144     * (MySQL only) Adds back fulltext index after populating the table.
145     */
146    private function createMysqlTextIndex() {
147        $dbw = $this->getPrimaryDB();
148        $searchindex = $dbw->tableName( 'searchindex' );
149        $this->output( "\nRebuild the index...\n" );
150        foreach ( [ 'si_title', 'si_text' ] as $field ) {
151            $sql = "ALTER TABLE $searchindex ADD FULLTEXT $field ($field)";
152            $dbw->query( $sql, __METHOD__ );
153        }
154    }
155
156    /**
157     * Deletes everything from search index.
158     */
159    private function clearSearchIndex() {
160        $dbw = $this->getPrimaryDB();
161        $this->output( 'Clearing searchindex table...' );
162        $dbw->newDeleteQueryBuilder()
163            ->deleteFrom( 'searchindex' )
164            ->where( '*' )
165            ->caller( __METHOD__ )->execute();
166        $this->output( "Done\n" );
167    }
168}
169
170$maintClass = RebuildTextIndex::class;
171require_once RUN_MAINTENANCE_IF_MAIN;