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