Merge "Remove old workaround for HHVM"
[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\IMaintainableDatabase;
31 use Wikimedia\Rdbms\DatabaseSqlite;
32
33 /**
34 * Maintenance script that rebuilds search index table from scratch.
35 *
36 * @ingroup Maintenance
37 */
38 class RebuildTextIndex extends Maintenance {
39 const RTI_CHUNK_SIZE = 500;
40
41 /**
42 * @var IMaintainableDatabase
43 */
44 private $db;
45
46 public function __construct() {
47 parent::__construct();
48 $this->addDescription( 'Rebuild search index table from scratch' );
49 }
50
51 public function getDbType() {
52 return Maintenance::DB_ADMIN;
53 }
54
55 public function execute() {
56 // Shouldn't be needed for Postgres
57 $this->db = $this->getDB( DB_MASTER );
58 if ( $this->db->getType() == 'postgres' ) {
59 $this->error( "This script is not needed when using Postgres.\n", true );
60 }
61
62 if ( $this->db->getType() == 'sqlite' ) {
63 if ( !DatabaseSqlite::getFulltextSearchModule() ) {
64 $this->error( "Your version of SQLite module for PHP doesn't "
65 . "support full-text search (FTS3).\n", true );
66 }
67 if ( !$this->db->checkForEnabledSearch() ) {
68 $this->error( "Your database schema is not configured for "
69 . "full-text search support. Run update.php.\n", true );
70 }
71 }
72
73 if ( $this->db->getType() == 'mysql' ) {
74 $this->dropMysqlTextIndex();
75 $this->clearSearchIndex();
76 $this->populateSearchIndex();
77 $this->createMysqlTextIndex();
78 } else {
79 $this->clearSearchIndex();
80 $this->populateSearchIndex();
81 }
82
83 $this->output( "Done.\n" );
84 }
85
86 /**
87 * Populates the search index with content from all pages
88 */
89 protected function populateSearchIndex() {
90 $res = $this->db->select( 'page', 'MAX(page_id) AS count' );
91 $s = $this->db->fetchObject( $res );
92 $count = $s->count;
93 $this->output( "Rebuilding index fields for {$count} pages...\n" );
94 $n = 0;
95
96 $fields = array_merge(
97 Revision::selectPageFields(),
98 Revision::selectFields(),
99 Revision::selectTextFields()
100 );
101
102 while ( $n < $count ) {
103 if ( $n ) {
104 $this->output( $n . "\n" );
105 }
106 $end = $n + self::RTI_CHUNK_SIZE - 1;
107
108 $res = $this->db->select( [ 'page', 'revision', 'text' ], $fields,
109 [ "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ],
110 __METHOD__
111 );
112
113 foreach ( $res as $s ) {
114 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
115 try {
116 $rev = new Revision( $s );
117 $content = $rev->getContent();
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 $searchindex = $this->db->tableName( 'searchindex' );
135 if ( $this->db->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
136 $this->output( "Dropping index...\n" );
137 $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
138 $this->db->query( $sql, __METHOD__ );
139 }
140 }
141
142 /**
143 * (MySQL only) Adds back fulltext index after populating the table.
144 */
145 private function createMysqlTextIndex() {
146 $searchindex = $this->db->tableName( 'searchindex' );
147 $this->output( "\nRebuild the index...\n" );
148 foreach ( [ 'si_title', 'si_text' ] as $field ) {
149 $sql = "ALTER TABLE $searchindex ADD FULLTEXT $field ($field)";
150 $this->db->query( $sql, __METHOD__ );
151 }
152 }
153
154 /**
155 * Deletes everything from search index.
156 */
157 private function clearSearchIndex() {
158 $this->output( 'Clearing searchindex table...' );
159 $this->db->delete( 'searchindex', '*', __METHOD__ );
160 $this->output( "Done\n" );
161 }
162 }
163
164 $maintClass = "RebuildTextIndex";
165 require_once RUN_MAINTENANCE_IF_MAIN;