MediaWiki master
updateSearchIndex.php
Go to the documentation of this file.
1<?php
27
28// @codeCoverageIgnoreStart
29require_once __DIR__ . '/Maintenance.php';
30// @codeCoverageIgnoreEnd
31
46
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
57 );
58 $this->addOption(
59 'l',
60 'Deprecated, has no effect (formerly lock time)',
61 false,
62 true
63 );
64 }
65
67 public function getDbType() {
68 return Maintenance::DB_ADMIN;
69 }
70
71 public function execute() {
72 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
73 $posFile = $this->getOption( 'p', 'searchUpdate.' . rawurlencode( $dbDomain ) . '.pos' );
74 $end = $this->getOption( 'e', wfTimestampNow() );
75 if ( $this->hasOption( 's' ) ) {
76 $start = $this->getOption( 's' );
77 } elseif ( is_readable( $posFile ) ) {
78 $start = file_get_contents( $posFile );
79 } else {
80 $start = wfTimestamp( TS_MW, time() - 86400 );
81 }
82
83 $this->doUpdateSearchIndex( $start, $end );
84 $file = fopen( $posFile, 'w' );
85 if ( $file !== false ) {
86 fwrite( $file, $end );
87 fclose( $file );
88 } else {
89 $this->error( "*** Couldn't write to the $posFile!\n" );
90 }
91 }
92
93 private function doUpdateSearchIndex( string $start, string $end ) {
95
97
98 $dbw = $this->getPrimaryDB();
99
100 $this->output( "Updating searchindex between $start and $end\n" );
101
102 # Select entries from recentchanges which are on top and between the specified times
103 $start = $dbw->timestamp( $start );
104 $end = $dbw->timestamp( $end );
105
106 $res = $dbw->newSelectQueryBuilder()
107 ->select( 'rc_cur_id' )
108 ->from( 'recentchanges' )
109 ->join( 'page', null, 'rc_cur_id=page_id AND rc_this_oldid=page_latest' )
110 ->where( [
111 $dbw->expr( 'rc_type', '!=', RC_LOG ),
112 $dbw->expr( 'rc_timestamp', '>=', $start ),
113 $dbw->expr( 'rc_timestamp', '<=', $end ),
114 ] )
115 ->caller( __METHOD__ )->fetchResultSet();
116
117 foreach ( $res as $row ) {
118 $this->updateSearchIndexForPage( (int)$row->rc_cur_id );
119 }
120 $this->output( "Done\n" );
121 }
122
128 private function updateSearchIndexForPage( int $pageId ) {
129 // Get current revision
130 $rev = $this->getServiceContainer()
131 ->getRevisionLookup()
132 ->getRevisionByPageId( $pageId, 0, IDBAccessObject::READ_LATEST );
133 $title = null;
134 if ( $rev ) {
135 $titleObj = Title::newFromPageIdentity( $rev->getPage() );
136 $title = $titleObj->getPrefixedDBkey();
137 $this->output( "$title..." );
138 # Update searchindex
139 $u = new SearchUpdate( $pageId, $titleObj, $rev->getContent( SlotRecord::MAIN ) );
140 $u->doUpdate();
141 $this->output( "\n" );
142 }
143
144 return $title;
145 }
146}
147
148// @codeCoverageIgnoreStart
149$maintClass = UpdateSearchIndex::class;
150require_once RUN_MAINTENANCE_IF_MAIN;
151// @codeCoverageIgnoreEnd
const RC_LOG
Definition Defines.php:119
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
output( $out, $channel=null)
Throw some output to the user.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
hasOption( $name)
Checks to see if a particular option was set.
getOption( $name, $default=null)
Get an option, or return the default.
error( $err, $die=0)
Throw an error to the user.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
Value object representing a content slot associated with a page revision.
Database independent search index updater.
Represents a title within MediaWiki.
Definition Title.php:78
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:31
Periodic off-peak updating of the search index.
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
execute()
Do the actual work.
__construct()
Default constructor.
$wgDisableSearchUpdate
Config variable stub for the DisableSearchUpdate setting, for use by phpdoc and IDEs.
Interface for database access objects.