Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / maintenance / rebuildtextindex.php
blobb7f07629ecc9a4d85480e21a1b76d6f8d5af86f9
1 <?php
2 /**
3 * Rebuild search index table from scratch. This may take several
4 * hours, depending on the database size and server configuration.
6 * Postgres is trigger-based and should never need rebuilding.
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.
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.
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
23 * @file
24 * @ingroup Maintenance
25 * @todo document
28 require_once __DIR__ . '/Maintenance.php';
30 /**
31 * Maintenance script that rebuilds search index table from scratch.
33 * @ingroup Maintenance
35 class RebuildTextIndex extends Maintenance {
36 const RTI_CHUNK_SIZE = 500;
38 /**
39 * @var Database
41 private $db;
43 public function __construct() {
44 parent::__construct();
45 $this->addDescription( 'Rebuild search index table from scratch' );
48 public function getDbType() {
49 return Maintenance::DB_ADMIN;
52 public function execute() {
53 // Shouldn't be needed for Postgres
54 $this->db = $this->getDB( DB_MASTER );
55 if ( $this->db->getType() == 'postgres' ) {
56 $this->error( "This script is not needed when using Postgres.\n", true );
59 if ( $this->db->getType() == 'sqlite' ) {
60 if ( !DatabaseSqlite::getFulltextSearchModule() ) {
61 $this->error( "Your version of SQLite module for PHP doesn't "
62 . "support full-text search (FTS3).\n", true );
64 if ( !$this->db->checkForEnabledSearch() ) {
65 $this->error( "Your database schema is not configured for "
66 . "full-text search support. Run update.php.\n", true );
70 if ( $this->db->getType() == 'mysql' ) {
71 $this->dropMysqlTextIndex();
72 $this->clearSearchIndex();
73 $this->populateSearchIndex();
74 $this->createMysqlTextIndex();
75 } else {
76 $this->clearSearchIndex();
77 $this->populateSearchIndex();
80 $this->output( "Done.\n" );
83 /**
84 * Populates the search index with content from all pages
86 protected function populateSearchIndex() {
87 $res = $this->db->select( 'page', 'MAX(page_id) AS count' );
88 $s = $this->db->fetchObject( $res );
89 $count = $s->count;
90 $this->output( "Rebuilding index fields for {$count} pages...\n" );
91 $n = 0;
93 $fields = array_merge(
94 Revision::selectPageFields(),
95 Revision::selectFields(),
96 Revision::selectTextFields()
99 while ( $n < $count ) {
100 if ( $n ) {
101 $this->output( $n . "\n" );
103 $end = $n + self::RTI_CHUNK_SIZE - 1;
105 $res = $this->db->select( [ 'page', 'revision', 'text' ], $fields,
106 [ "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ],
107 __METHOD__
110 foreach ( $res as $s ) {
111 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
112 try {
113 $rev = new Revision( $s );
114 $content = $rev->getContent();
116 $u = new SearchUpdate( $s->page_id, $title, $content );
117 $u->doUpdate();
118 } catch ( MWContentSerializationException $ex ) {
119 $this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
120 . "`" . $title->getPrefixedDBkey() . "`!\n" );
123 $n += self::RTI_CHUNK_SIZE;
128 * (MySQL only) Drops fulltext index before populating the table.
130 private function dropMysqlTextIndex() {
131 $searchindex = $this->db->tableName( 'searchindex' );
132 if ( $this->db->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
133 $this->output( "Dropping index...\n" );
134 $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
135 $this->db->query( $sql, __METHOD__ );
140 * (MySQL only) Adds back fulltext index after populating the table.
142 private function createMysqlTextIndex() {
143 $searchindex = $this->db->tableName( 'searchindex' );
144 $this->output( "\nRebuild the index...\n" );
145 $sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
146 "ADD FULLTEXT si_text (si_text)";
147 $this->db->query( $sql, __METHOD__ );
151 * Deletes everything from search index.
153 private function clearSearchIndex() {
154 $this->output( 'Clearing searchindex table...' );
155 $this->db->delete( 'searchindex', '*', __METHOD__ );
156 $this->output( "Done\n" );
160 $maintClass = "RebuildTextIndex";
161 require_once RUN_MAINTENANCE_IF_MAIN;