Fix concern raised by Brion in r74108 (but has really existed since the maintenance...
[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 * @ingroup Maintenance
24 * @todo document
25 */
26
27 require_once( dirname( __FILE__ ) . '/Maintenance.php' );
28
29 class RebuildTextIndex extends Maintenance {
30 const RTI_CHUNK_SIZE = 500;
31 private $db;
32
33 public function __construct() {
34 parent::__construct();
35 $this->mDescription = "Rebuild search index table from scratch";
36 }
37
38 public function getDbType() {
39 return Maintenance::DB_ADMIN;
40 }
41
42 public function execute() {
43 global $wgTitle;
44
45 // Shouldn't be needed for Postgres
46 $this->db = wfGetDB( DB_MASTER );
47 if ( $this->db->getType() == 'postgres' ) {
48 $this->error( "This script is not needed when using Postgres.\n", true );
49 }
50
51 $this->db = wfGetDB( DB_MASTER );
52 if ( $this->db->getType() == 'sqlite' ) {
53 if ( !$this->db->getFulltextSearchModule() ) {
54 $this->error( "Your version of SQLite module for PHP doesn't support full-text search (FTS3).\n" );
55 }
56 if ( !$this->db->checkForEnabledSearch() ) {
57 $this->error( "Your database schema is not configured for full-text search support. Run update.php.\n" );
58 }
59 }
60
61 $wgTitle = Title::newFromText( "Rebuild text index script" );
62
63 if ( $this->db->getType() == 'mysql' ) {
64 $this->dropMysqlTextIndex();
65 $this->populateSearchIndex();
66 $this->createMysqlTextIndex();
67 } else {
68 $this->clearSearchIndex();
69 $this->populateSearchIndex();
70 }
71
72 $this->output( "Done.\n" );
73 }
74
75 /**
76 * Populates the search index with content from all pages
77 */
78 protected function populateSearchIndex() {
79 $res = $this->db->select( 'page', 'MAX(page_id) AS count' );
80 $s = $this->db->fetchObject( $res );
81 $count = $s->count;
82 $this->output( "Rebuilding index fields for {$count} pages...\n" );
83 $n = 0;
84
85 while ( $n < $count ) {
86 if ( $n ) {
87 $this->output( $n . "\n" );
88 }
89 $end = $n + self::RTI_CHUNK_SIZE - 1;
90
91 $res = $this->db->select( array( 'page', 'revision', 'text' ),
92 array( 'page_id', 'page_namespace', 'page_title', 'old_flags', 'old_text' ),
93 array( "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ),
94 __METHOD__
95 );
96
97 foreach ( $res as $s ) {
98 $revtext = Revision::getRevisionText( $s );
99 $u = new SearchUpdate( $s->page_id, $s->page_title, $revtext );
100 $u->doUpdate();
101 }
102 $n += self::RTI_CHUNK_SIZE;
103 }
104 }
105
106 /**
107 * (MySQL only) Drops fulltext index before populating the table.
108 */
109 private function dropMysqlTextIndex() {
110 $searchindex = $this->db->tableName( 'searchindex' );
111 if ( $this->db->indexExists( 'searchindex', 'si_title' ) ) {
112 $this->output( "Dropping index...\n" );
113 $sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
114 $this->db->query( $sql, __METHOD__ );
115 }
116 }
117
118 /**
119 * (MySQL only) Adds back fulltext index after populating the table.
120 */
121 private function createMysqlTextIndex() {
122 $searchindex = $this->db->tableName( 'searchindex' );
123 $this->output( "\nRebuild the index...\n" );
124 $sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
125 "ADD FULLTEXT si_text (si_text)";
126 $this->db->query( $sql, __METHOD__ );
127 }
128
129 /**
130 * Deletes everything from search index.
131 */
132 private function clearSearchIndex() {
133 $this->output( 'Clearing searchindex table...' );
134 $this->db->delete( 'searchindex', '*', __METHOD__ );
135 $this->output( "Done\n" );
136 }
137 }
138
139 $maintClass = "RebuildTextIndex";
140 require_once( RUN_MAINTENANCE_IF_MAIN );