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