Merge "doc: SpanInterface: more dev-friendly comments"
[mediawiki.git] / maintenance / updateSearchIndex.php
blob1d6ed61643a8a504060e4031f75be3f881f0a044
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
18 * @file
21 use MediaWiki\Maintenance\Maintenance;
22 use MediaWiki\Revision\SlotRecord;
23 use MediaWiki\Search\SearchUpdate;
24 use MediaWiki\Title\Title;
25 use MediaWiki\WikiMap\WikiMap;
26 use Wikimedia\Rdbms\IDBAccessObject;
28 // @codeCoverageIgnoreStart
29 require_once __DIR__ . '/Maintenance.php';
30 // @codeCoverageIgnoreEnd
32 /**
33 * Periodic off-peak updating of the search index.
35 * Usage: php updateSearchIndex.php [-s START] [-e END] [-p POSFILE] [-l LOCKTIME] [-q]
36 * Where START is the starting timestamp
37 * END is the ending timestamp
38 * POSFILE is a file to load timestamps from and save them to, searchUpdate.WIKI_ID.pos by default
39 * LOCKTIME is how long the searchindex and revision tables will be locked for
40 * -q means quiet
42 * @ingroup Search
43 * @ingroup Maintenance
45 class UpdateSearchIndex extends Maintenance {
47 public function __construct() {
48 parent::__construct();
49 $this->addDescription( 'Script for periodic off-peak updating of the search index' );
50 $this->addOption( 's', 'Starting timestamp', false, true );
51 $this->addOption( 'e', 'Ending timestamp', false, true );
52 $this->addOption(
53 'p',
54 'File for saving/loading timestamps, searchUpdate.WIKI_ID.pos by default',
55 false,
56 true
58 $this->addOption(
59 'l',
60 'Deprecated, has no effect (formerly lock time)',
61 false,
62 true
66 public function getDbType() {
67 return Maintenance::DB_ADMIN;
70 public function execute() {
71 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
72 $posFile = $this->getOption( 'p', 'searchUpdate.' . rawurlencode( $dbDomain ) . '.pos' );
73 $end = $this->getOption( 'e', wfTimestampNow() );
74 if ( $this->hasOption( 's' ) ) {
75 $start = $this->getOption( 's' );
76 } elseif ( is_readable( $posFile ) ) {
77 $start = file_get_contents( $posFile );
78 } else {
79 $start = wfTimestamp( TS_MW, time() - 86400 );
82 $this->doUpdateSearchIndex( $start, $end );
83 $file = fopen( $posFile, 'w' );
84 if ( $file !== false ) {
85 fwrite( $file, $end );
86 fclose( $file );
87 } else {
88 $this->error( "*** Couldn't write to the $posFile!\n" );
92 private function doUpdateSearchIndex( $start, $end ) {
93 global $wgDisableSearchUpdate;
95 $wgDisableSearchUpdate = false;
97 $dbw = $this->getPrimaryDB();
99 $this->output( "Updating searchindex between $start and $end\n" );
101 # Select entries from recentchanges which are on top and between the specified times
102 $start = $dbw->timestamp( $start );
103 $end = $dbw->timestamp( $end );
105 $res = $dbw->newSelectQueryBuilder()
106 ->select( 'rc_cur_id' )
107 ->from( 'recentchanges' )
108 ->join( 'page', null, 'rc_cur_id=page_id AND rc_this_oldid=page_latest' )
109 ->where( [
110 $dbw->expr( 'rc_type', '!=', RC_LOG ),
111 $dbw->expr( 'rc_timestamp', '>=', $start ),
112 $dbw->expr( 'rc_timestamp', '<=', $end ),
114 ->caller( __METHOD__ )->fetchResultSet();
116 foreach ( $res as $row ) {
117 $this->updateSearchIndexForPage( (int)$row->rc_cur_id );
119 $this->output( "Done\n" );
123 * Update the searchindex table for a given pageid
124 * @param int $pageId The page ID to update.
125 * @return null|string
127 private function updateSearchIndexForPage( int $pageId ) {
128 // Get current revision
129 $rev = $this->getServiceContainer()
130 ->getRevisionLookup()
131 ->getRevisionByPageId( $pageId, 0, IDBAccessObject::READ_LATEST );
132 $title = null;
133 if ( $rev ) {
134 $titleObj = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
135 $title = $titleObj->getPrefixedDBkey();
136 $this->output( "$title..." );
137 # Update searchindex
138 $u = new SearchUpdate( $pageId, $titleObj, $rev->getContent( SlotRecord::MAIN ) );
139 $u->doUpdate();
140 $this->output( "\n" );
143 return $title;
147 // @codeCoverageIgnoreStart
148 $maintClass = UpdateSearchIndex::class;
149 require_once RUN_MAINTENANCE_IF_MAIN;
150 // @codeCoverageIgnoreEnd