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 $this->
output(
"Dropping index...\n" );
128 ALTER TABLE $searchindex
129 DROP INDEX IF EXISTS si_title,
130 DROP INDEX IF EXISTS si_text
132 $dbw->query( $sql, __METHOD__ );
138 private function createMysqlTextIndex() {
140 $searchindex = $dbw->tableName(
'searchindex' );
141 $this->
output(
"\nRebuild the index...\n" );
142 foreach ( [
'si_title',
'si_text' ] as $field ) {
143 $sql =
"ALTER TABLE $searchindex ADD FULLTEXT $field ($field)";
144 $dbw->query( $sql, __METHOD__ );
151 private function clearSearchIndex() {
153 $this->
output(
'Clearing searchindex table...' );
154 $dbw->newDeleteQueryBuilder()
155 ->deleteFrom(
'searchindex' )
157 ->caller( __METHOD__ )->execute();
158 $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.