MediaWiki  master
updateSearchIndex.php
Go to the documentation of this file.
1 <?php
34 
35 require_once __DIR__ . '/Maintenance.php';
36 
43 
44  public function __construct() {
45  parent::__construct();
46  $this->addDescription( 'Script for periodic off-peak updating of the search index' );
47  $this->addOption( 's', 'Starting timestamp', false, true );
48  $this->addOption( 'e', 'Ending timestamp', false, true );
49  $this->addOption(
50  'p',
51  'File for saving/loading timestamps, searchUpdate.WIKI_ID.pos by default',
52  false,
53  true
54  );
55  $this->addOption(
56  'l',
57  'Deprecated, has no effect (formerly lock time)',
58  false,
59  true
60  );
61  }
62 
63  public function getDbType() {
64  return Maintenance::DB_ADMIN;
65  }
66 
67  public function execute() {
68  $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
69  $posFile = $this->getOption( 'p', 'searchUpdate.' . rawurlencode( $dbDomain ) . '.pos' );
70  $end = $this->getOption( 'e', wfTimestampNow() );
71  if ( $this->hasOption( 's' ) ) {
72  $start = $this->getOption( 's' );
73  } elseif ( is_readable( $posFile ) ) {
74  $start = file_get_contents( $posFile );
75  } else {
76  $start = wfTimestamp( TS_MW, time() - 86400 );
77  }
78 
79  $this->doUpdateSearchIndex( $start, $end );
80  $file = fopen( $posFile, 'w' );
81  if ( $file !== false ) {
82  fwrite( $file, $end );
83  fclose( $file );
84  } else {
85  $this->error( "*** Couldn't write to the $posFile!\n" );
86  }
87  }
88 
89  private function doUpdateSearchIndex( $start, $end ) {
91 
92  $wgDisableSearchUpdate = false;
93 
94  $dbw = $this->getDB( DB_PRIMARY );
95 
96  $this->output( "Updating searchindex between $start and $end\n" );
97 
98  # Select entries from recentchanges which are on top and between the specified times
99  $start = $dbw->timestamp( $start );
100  $end = $dbw->timestamp( $end );
101 
102  $res = $dbw->newSelectQueryBuilder()
103  ->select( 'rc_cur_id' )
104  ->from( 'recentchanges' )
105  ->join( 'page', null, 'rc_cur_id=page_id AND rc_this_oldid=page_latest' )
106  ->where( [
107  'rc_type != ' . $dbw->addQuotes( RC_LOG ),
108  'rc_timestamp BETWEEN ' . $dbw->addQuotes( $start ) . ' AND ' . $dbw->addQuotes( $end )
109  ] )
110  ->caller( __METHOD__ )->fetchResultSet();
111 
112  foreach ( $res as $row ) {
113  $this->updateSearchIndexForPage( (int)$row->rc_cur_id );
114  }
115  $this->output( "Done\n" );
116  }
117 
123  private function updateSearchIndexForPage( int $pageId ) {
124  // Get current revision
125  $rev = $this->getServiceContainer()
126  ->getRevisionLookup()
127  ->getRevisionByPageId( $pageId, 0, IDBAccessObject::READ_LATEST );
128  $title = null;
129  if ( $rev ) {
130  $titleObj = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
131  $title = $titleObj->getPrefixedDBkey();
132  $this->output( "$title..." );
133  # Update searchindex
134  $u = new SearchUpdate( $pageId, $titleObj, $rev->getContent( SlotRecord::MAIN ) );
135  $u->doUpdate();
136  $this->output( "\n" );
137  }
138 
139  return $title;
140  }
141 }
142 
143 $maintClass = UpdateSearchIndex::class;
144 require_once RUN_MAINTENANCE_IF_MAIN;
const RC_LOG
Definition: Defines.php:118
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...
Definition: Maintenance.php:66
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
error( $err, $die=0)
Throw an error to the user.
output( $out, $channel=null)
Throw some output to the user.
const DB_ADMIN
Definition: Maintenance.php:73
hasOption( $name)
Checks to see if a particular option was set.
getServiceContainer()
Returns the main service container.
addDescription( $text)
Set the description text.
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
getOption( $name, $default=null)
Get an option, or return the default.
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
Represents a title within MediaWiki.
Definition: Title.php:76
Tools for dealing with other locally-hosted wikis.
Definition: WikiMap.php:31
Database independent search index updater.
Maintenance script for 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.
const DB_PRIMARY
Definition: defines.php:28
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42