28 private const RTI_CHUNK_SIZE = 500;
31 parent::__construct();
32 $this->
addDescription(
'Rebuild search index table from scratch' );
37 return Maintenance::DB_ADMIN;
43 if ( $dbw->getType() ==
'postgres' ) {
44 $this->
fatalError(
"This script is not needed when using Postgres.\n" );
47 if ( $dbw->getType() ==
'sqlite' ) {
48 if ( !DatabaseSqlite::getFulltextSearchModule() ) {
49 $this->
fatalError(
"Your version of SQLite module for PHP doesn't "
50 .
"support full-text search (FTS3).\n" );
54 if ( $dbw->getType() ==
'mysql' ) {
55 $this->dropMysqlTextIndex();
56 $this->clearSearchIndex();
58 $this->createMysqlTextIndex();
60 $this->clearSearchIndex();
64 $this->
output(
"Done.\n" );
72 $res = $dbw->newSelectQueryBuilder()
73 ->select( [
'count' =>
'MAX(page_id)' ] )
75 ->caller( __METHOD__ )->fetchResultSet();
76 $s = $res->fetchObject();
78 $this->
output(
"Rebuilding index fields for {$count} pages...\n" );
82 $queryBuilderTemplate = $revStore->newSelectQueryBuilder( $dbw )
86 while ( $n < $count ) {
88 $this->
output( $n .
"\n" );
90 $end = $n + self::RTI_CHUNK_SIZE - 1;
91 $queryBuilder = clone $queryBuilderTemplate;
92 $res = $queryBuilder->where( [
93 $dbw->expr(
'page_id',
'>=', $n )->and(
'page_id',
'<=', $end ),
94 'page_latest = rev_id'
95 ] )->caller( __METHOD__ )->fetchResultSet();
97 foreach ( $res as $s ) {
100 if ( $s->page_namespace < 0 ) {
104 $title = Title::makeTitle( $s->page_namespace, $s->page_title );
106 $revRecord = $revStore->newRevisionFromRow( $s );
107 $content = $revRecord->getContent( SlotRecord::MAIN );
112 $this->
output(
"Failed to deserialize content of revision {$s->rev_id} of page "
113 .
"`" . $title->getPrefixedDBkey() .
"`!\n" );
116 $n += self::RTI_CHUNK_SIZE;
123 private function dropMysqlTextIndex() {
125 $searchindex = $dbw->tableName(
'searchindex' );
126 if ( $dbw->indexExists(
'searchindex',
'si_title', __METHOD__ ) ) {
127 $this->
output(
"Dropping index...\n" );
128 $sql =
"ALTER TABLE $searchindex DROP INDEX si_title, DROP INDEX si_text";
129 $dbw->query( $sql, __METHOD__ );
136 private function createMysqlTextIndex() {
138 $searchindex = $dbw->tableName(
'searchindex' );
139 $this->
output(
"\nRebuild the index...\n" );
140 foreach ( [
'si_title',
'si_text' ] as $field ) {
141 $sql =
"ALTER TABLE $searchindex ADD FULLTEXT $field ($field)";
142 $dbw->query( $sql, __METHOD__ );
149 private function clearSearchIndex() {
151 $this->
output(
'Clearing searchindex table...' );
152 $dbw->newDeleteQueryBuilder()
153 ->deleteFrom(
'searchindex' )
155 ->caller( __METHOD__ )->execute();
156 $this->
output(
"Done\n" );
Rebuild search index table from scratch.
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
populateSearchIndex()
Populates the search index with content from all pages.