MediaWiki  master
updateSearchIndex.php
Go to the documentation of this file.
1 <?php
35 
36 require_once __DIR__ . '/Maintenance.php';
37 
44 
45  public function __construct() {
46  parent::__construct();
47  $this->addDescription( 'Script for periodic off-peak updating of the search index' );
48  $this->addOption( 's', 'Starting timestamp', false, true );
49  $this->addOption( 'e', 'Ending timestamp', false, true );
50  $this->addOption(
51  'p',
52  'File for saving/loading timestamps, searchUpdate.WIKI_ID.pos by default',
53  false,
54  true
55  );
56  $this->addOption(
57  'l',
58  'Deprecated, has no effect (formerly lock time)',
59  false,
60  true
61  );
62  }
63 
64  public function getDbType() {
65  return Maintenance::DB_ADMIN;
66  }
67 
68  public function execute() {
69  $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
70  $posFile = $this->getOption( 'p', 'searchUpdate.' . rawurlencode( $dbDomain ) . '.pos' );
71  $end = $this->getOption( 'e', wfTimestampNow() );
72  if ( $this->hasOption( 's' ) ) {
73  $start = $this->getOption( 's' );
74  } elseif ( is_readable( $posFile ) ) {
75  $start = file_get_contents( $posFile );
76  } else {
77  $start = wfTimestamp( TS_MW, time() - 86400 );
78  }
79 
80  $this->doUpdateSearchIndex( $start, $end );
81  $file = fopen( $posFile, 'w' );
82  if ( $file !== false ) {
83  fwrite( $file, $end );
84  fclose( $file );
85  } else {
86  $this->error( "*** Couldn't write to the $posFile!\n" );
87  }
88  }
89 
90  private function doUpdateSearchIndex( $start, $end ) {
92 
93  $wgDisableSearchUpdate = false;
94 
95  $dbw = $this->getDB( DB_PRIMARY );
96 
97  $this->output( "Updating searchindex between $start and $end\n" );
98 
99  # Select entries from recentchanges which are on top and between the specified times
100  $start = $dbw->timestamp( $start );
101  $end = $dbw->timestamp( $end );
102 
103  $res = $dbw->select(
104  [ 'recentchanges', 'page' ],
105  'rc_cur_id',
106  [
107  'rc_type != ' . $dbw->addQuotes( RC_LOG ),
108  'rc_timestamp BETWEEN ' . $dbw->addQuotes( $start ) . ' AND ' . $dbw->addQuotes( $end )
109  ],
110  __METHOD__,
111  [],
112  [
113  'page' => [ 'JOIN', 'rc_cur_id=page_id AND rc_this_oldid=page_latest' ]
114  ]
115  );
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 = MediaWikiServices::getInstance()
131  ->getRevisionLookup()
132  ->getRevisionByPageId( $pageId, 0, IDBAccessObject::READ_LATEST );
133  $title = null;
134  if ( $rev ) {
135  $titleObj = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
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 $maintClass = UpdateSearchIndex::class;
149 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.
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.
Service locator for MediaWiki core services.
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
Represents a title within MediaWiki.
Definition: Title.php:82
Helper tools for dealing with other locally-hosted wikis.
Definition: WikiMap.php:33
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