2004-02-18 02:15:00 +00:00
|
|
|
<?php
|
2004-09-03 20:33:01 +00:00
|
|
|
/**
|
2009-10-29 16:19:35 +00:00
|
|
|
* Rebuild search index table from scratch. This may take several
|
2004-09-03 20:33:01 +00:00
|
|
|
* hours, depending on the database size and server configuration.
|
|
|
|
|
*
|
2008-07-21 16:58:06 +00:00
|
|
|
* Postgres is trigger-based and should never need rebuilding.
|
2007-05-14 20:24:10 +00:00
|
|
|
*
|
2009-08-02 19:35:17 +00:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
|
* http://www.gnu.org/copyleft/gpl.html
|
|
|
|
|
*
|
2012-08-09 16:06:18 +00:00
|
|
|
* @file
|
WARNING: HUGE COMMIT
Doxygen documentation update:
* Changed alls @addtogroup to @ingroup. @addtogroup adds the comment to the group description, but doesn't add the file, class, function, ... to the group like @ingroup does. See for example http://svn.wikimedia.org/doc/group__SpecialPage.html where it's impossible to see related files, classes, ... that should belong to that group.
* Added @file to file description, it seems that it should be explicitely decalred for file descriptions, otherwise doxygen will think that the comment document the first class, variabled, function, ... that is in that file.
* Removed some empty comments
* Removed some ?>
Added following groups:
* ExternalStorage
* JobQueue
* MaintenanceLanguage
One more thing: there are still a lot of warnings when generating the doc.
2008-05-20 17:13:28 +00:00
|
|
|
* @ingroup Maintenance
|
2009-08-02 19:35:17 +00:00
|
|
|
* @todo document
|
2006-01-07 13:09:30 +00:00
|
|
|
*/
|
2003-05-02 22:55:37 +00:00
|
|
|
|
2013-05-17 00:16:59 +00:00
|
|
|
require_once __DIR__ . '/Maintenance.php';
|
2003-05-02 22:55:37 +00:00
|
|
|
|
2012-08-09 16:06:18 +00:00
|
|
|
/**
|
|
|
|
|
* Maintenance script that rebuilds search index table from scratch.
|
|
|
|
|
*
|
|
|
|
|
* @ingroup Maintenance
|
|
|
|
|
*/
|
2009-08-02 19:35:17 +00:00
|
|
|
class RebuildTextIndex extends Maintenance {
|
2010-12-04 03:20:14 +00:00
|
|
|
const RTI_CHUNK_SIZE = 500;
|
2011-07-01 02:25:19 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var DatabaseBase
|
|
|
|
|
*/
|
2009-10-29 16:19:35 +00:00
|
|
|
private $db;
|
2007-05-14 20:24:10 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
public function __construct() {
|
|
|
|
|
parent::__construct();
|
|
|
|
|
$this->mDescription = "Rebuild search index table from scratch";
|
|
|
|
|
}
|
2003-05-02 22:55:37 +00:00
|
|
|
|
2010-03-10 12:59:44 +00:00
|
|
|
public function getDbType() {
|
2009-08-09 15:22:34 +00:00
|
|
|
return Maintenance::DB_ADMIN;
|
|
|
|
|
}
|
|
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
public function execute() {
|
2009-10-29 16:19:35 +00:00
|
|
|
// Shouldn't be needed for Postgres
|
2010-12-10 13:56:17 +00:00
|
|
|
$this->db = wfGetDB( DB_MASTER );
|
|
|
|
|
if ( $this->db->getType() == 'postgres' ) {
|
2009-11-15 18:53:41 +00:00
|
|
|
$this->error( "This script is not needed when using Postgres.\n", true );
|
2009-10-29 16:19:35 +00:00
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-10-29 16:19:35 +00:00
|
|
|
$this->db = wfGetDB( DB_MASTER );
|
2010-11-01 20:14:46 +00:00
|
|
|
if ( $this->db->getType() == 'sqlite' ) {
|
2011-04-11 17:16:41 +00:00
|
|
|
if ( !DatabaseSqlite::getFulltextSearchModule() ) {
|
2014-04-22 20:55:50 +00:00
|
|
|
$this->error( "Your version of SQLite module for PHP doesn't "
|
|
|
|
|
. "support full-text search (FTS3).\n", true );
|
2010-11-01 20:14:46 +00:00
|
|
|
}
|
|
|
|
|
if ( !$this->db->checkForEnabledSearch() ) {
|
2014-04-22 20:55:50 +00:00
|
|
|
$this->error( "Your database schema is not configured for "
|
|
|
|
|
. "full-text search support. Run update.php.\n", true );
|
2010-11-01 20:14:46 +00:00
|
|
|
}
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2010-11-01 20:14:46 +00:00
|
|
|
if ( $this->db->getType() == 'mysql' ) {
|
2009-10-29 16:19:35 +00:00
|
|
|
$this->dropMysqlTextIndex();
|
|
|
|
|
$this->populateSearchIndex();
|
|
|
|
|
$this->createMysqlTextIndex();
|
|
|
|
|
} else {
|
|
|
|
|
$this->clearSearchIndex();
|
|
|
|
|
$this->populateSearchIndex();
|
|
|
|
|
}
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
$this->output( "Done.\n" );
|
|
|
|
|
}
|
2003-05-02 22:55:37 +00:00
|
|
|
|
2009-10-29 16:19:35 +00:00
|
|
|
/**
|
|
|
|
|
* Populates the search index with content from all pages
|
|
|
|
|
*/
|
|
|
|
|
protected function populateSearchIndex() {
|
|
|
|
|
$res = $this->db->select( 'page', 'MAX(page_id) AS count' );
|
2010-05-22 16:50:39 +00:00
|
|
|
$s = $this->db->fetchObject( $res );
|
2009-08-02 19:35:17 +00:00
|
|
|
$count = $s->count;
|
|
|
|
|
$this->output( "Rebuilding index fields for {$count} pages...\n" );
|
|
|
|
|
$n = 0;
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2012-12-13 20:48:36 +00:00
|
|
|
$fields = array_merge(
|
|
|
|
|
Revision::selectPageFields(),
|
|
|
|
|
Revision::selectFields(),
|
|
|
|
|
Revision::selectTextFields()
|
|
|
|
|
);
|
|
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
while ( $n < $count ) {
|
2010-11-01 19:38:08 +00:00
|
|
|
if ( $n ) {
|
|
|
|
|
$this->output( $n . "\n" );
|
|
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
$end = $n + self::RTI_CHUNK_SIZE - 1;
|
2009-10-29 16:19:35 +00:00
|
|
|
|
2012-12-13 20:48:36 +00:00
|
|
|
$res = $this->db->select( array( 'page', 'revision', 'text' ), $fields,
|
2009-10-29 16:19:35 +00:00
|
|
|
array( "page_id BETWEEN $n AND $end", 'page_latest = rev_id', 'rev_text_id = old_id' ),
|
|
|
|
|
__METHOD__
|
2012-12-13 20:48:36 +00:00
|
|
|
);
|
2010-12-04 03:20:14 +00:00
|
|
|
|
2010-05-22 16:50:39 +00:00
|
|
|
foreach ( $res as $s ) {
|
2012-12-13 20:48:36 +00:00
|
|
|
try {
|
|
|
|
|
$title = Title::makeTitle( $s->page_namespace, $s->page_title );
|
|
|
|
|
|
|
|
|
|
$rev = new Revision( $s );
|
|
|
|
|
$content = $rev->getContent();
|
|
|
|
|
|
2013-06-19 17:55:33 +00:00
|
|
|
$u = new SearchUpdate( $s->page_id, $title, $content );
|
2012-12-13 20:48:36 +00:00
|
|
|
$u->doUpdate();
|
|
|
|
|
} catch ( MWContentSerializationException $ex ) {
|
|
|
|
|
$this->output( "Failed to deserialize content of revision {$s->rev_id} of page "
|
|
|
|
|
. "`" . $title->getPrefixedDBkey() . "`!\n" );
|
|
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
|
|
|
|
$n += self::RTI_CHUNK_SIZE;
|
|
|
|
|
}
|
|
|
|
|
}
|
2009-10-29 16:19:35 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* (MySQL only) Drops fulltext index before populating the table.
|
|
|
|
|
*/
|
|
|
|
|
private function dropMysqlTextIndex() {
|
|
|
|
|
$searchindex = $this->db->tableName( 'searchindex' );
|
2011-11-10 20:44:37 +00:00
|
|
|
if ( $this->db->indexExists( 'searchindex', 'si_title', __METHOD__ ) ) {
|
2009-10-29 16:19:35 +00:00
|
|
|
$this->output( "Dropping index...\n" );
|
|
|
|
|
$sql = "ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
|
2010-05-22 16:50:39 +00:00
|
|
|
$this->db->query( $sql, __METHOD__ );
|
2009-10-29 16:19:35 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* (MySQL only) Adds back fulltext index after populating the table.
|
|
|
|
|
*/
|
|
|
|
|
private function createMysqlTextIndex() {
|
|
|
|
|
$searchindex = $this->db->tableName( 'searchindex' );
|
|
|
|
|
$this->output( "\nRebuild the index...\n" );
|
|
|
|
|
$sql = "ALTER TABLE $searchindex ADD FULLTEXT si_title (si_title), " .
|
2013-04-18 18:48:44 +00:00
|
|
|
"ADD FULLTEXT si_text (si_text)";
|
2009-10-29 16:19:35 +00:00
|
|
|
$this->db->query( $sql, __METHOD__ );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Deletes everything from search index.
|
|
|
|
|
*/
|
|
|
|
|
private function clearSearchIndex() {
|
|
|
|
|
$this->output( 'Clearing searchindex table...' );
|
|
|
|
|
$this->db->delete( 'searchindex', '*', __METHOD__ );
|
|
|
|
|
$this->output( "Done\n" );
|
|
|
|
|
}
|
2009-08-02 19:35:17 +00:00
|
|
|
}
|
2007-06-29 01:19:14 +00:00
|
|
|
|
2009-08-02 19:35:17 +00:00
|
|
|
$maintClass = "RebuildTextIndex";
|
2013-05-07 23:00:15 +00:00
|
|
|
require_once RUN_MAINTENANCE_IF_MAIN;
|