MediaWiki master
updateSearchIndex.php
Go to the documentation of this file.
1<?php
14use Wikimedia\Timestamp\TimestampFormat as TS;
15
16// @codeCoverageIgnoreStart
17require_once __DIR__ . '/Maintenance.php';
18// @codeCoverageIgnoreEnd
19
34
35 public function __construct() {
36 parent::__construct();
37 $this->addDescription( 'Script for periodic off-peak updating of the search index' );
38 $this->addOption( 's', 'Starting timestamp', false, true );
39 $this->addOption( 'e', 'Ending timestamp', false, true );
40 $this->addOption(
41 'p',
42 'File for saving/loading timestamps, searchUpdate.WIKI_ID.pos by default',
43 false,
44 true
45 );
46 $this->addOption(
47 'l',
48 'Deprecated, has no effect (formerly lock time)',
49 false,
50 true
51 );
52 }
53
55 public function getDbType() {
56 return Maintenance::DB_ADMIN;
57 }
58
59 public function execute() {
60 $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
61 $posFile = $this->getOption( 'p', 'searchUpdate.' . rawurlencode( $dbDomain ) . '.pos' );
62 $end = $this->getOption( 'e', wfTimestampNow() );
63 if ( $this->hasOption( 's' ) ) {
64 $start = $this->getOption( 's' );
65 } elseif ( is_readable( $posFile ) ) {
66 $start = file_get_contents( $posFile );
67 } else {
68 $start = wfTimestamp( TS::MW, time() - 86400 );
69 }
70
71 $this->doUpdateSearchIndex( $start, $end );
72 $file = fopen( $posFile, 'w' );
73 if ( $file !== false ) {
74 fwrite( $file, $end );
75 fclose( $file );
76 } else {
77 $this->error( "*** Couldn't write to the $posFile!\n" );
78 }
79 }
80
81 private function doUpdateSearchIndex( string $start, string $end ) {
83
85
86 $dbw = $this->getPrimaryDB();
87
88 $this->output( "Updating searchindex between $start and $end\n" );
89
90 # Select entries from recentchanges which are on top and between the specified times
91 $start = $dbw->timestamp( $start );
92 $end = $dbw->timestamp( $end );
93
94 $res = $dbw->newSelectQueryBuilder()
95 ->select( 'rc_cur_id' )
96 ->from( 'recentchanges' )
97 ->join( 'page', null, 'rc_cur_id=page_id AND rc_this_oldid=page_latest' )
98 ->where( [
99 $dbw->expr( 'rc_source', '!=', RecentChange::SRC_LOG ),
100 $dbw->expr( 'rc_timestamp', '>=', $start ),
101 $dbw->expr( 'rc_timestamp', '<=', $end ),
102 ] )
103 ->caller( __METHOD__ )->fetchResultSet();
104
105 foreach ( $res as $row ) {
106 $this->updateSearchIndexForPage( (int)$row->rc_cur_id );
107 }
108 $this->output( "Done\n" );
109 }
110
116 private function updateSearchIndexForPage( int $pageId ) {
117 // Get current revision
118 $rev = $this->getServiceContainer()
119 ->getRevisionLookup()
120 ->getRevisionByPageId( $pageId, 0, IDBAccessObject::READ_LATEST );
121 $title = null;
122 if ( $rev ) {
123 $titleObj = Title::newFromPageIdentity( $rev->getPage() );
124 $title = $titleObj->getPrefixedDBkey();
125 $this->output( "$title..." );
126 # Update searchindex
127 $u = new SearchUpdate( $pageId, $titleObj, $rev->getContent( SlotRecord::MAIN ) );
128 $u->doUpdate();
129 $this->output( "\n" );
130 }
131
132 return $title;
133 }
134}
135
136// @codeCoverageIgnoreStart
137$maintClass = UpdateSearchIndex::class;
138require_once RUN_MAINTENANCE_IF_MAIN;
139// @codeCoverageIgnoreEnd
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.
getPrimaryDB(string|false $virtualDomain=false)
addDescription( $text)
Set the description text.
Utility class for creating and reading rows in the recentchanges table.
Value object representing a content slot associated with a page revision.
Database independent search index updater.
Represents a title within MediaWiki.
Definition Title.php:69
Tools for dealing with other locally-hosted wikis.
Definition WikiMap.php:19
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.