MediaWiki  master
updateSearchIndex.php
Go to the documentation of this file.
1 <?php
33 
34 require_once __DIR__ . '/Maintenance.php';
35 
42 
43  public function __construct() {
44  parent::__construct();
45  $this->addDescription( 'Script for periodic off-peak updating of the search index' );
46  $this->addOption( 's', 'Starting timestamp', false, true );
47  $this->addOption( 'e', 'Ending timestamp', false, true );
48  $this->addOption(
49  'p',
50  'File for saving/loading timestamps, searchUpdate.WIKI_ID.pos by default',
51  false,
52  true
53  );
54  $this->addOption(
55  'l',
56  'Deprecated, has no effect (formerly lock time)',
57  false,
58  true
59  );
60  }
61 
62  public function getDbType() {
63  return Maintenance::DB_ADMIN;
64  }
65 
66  public function execute() {
67  $dbDomain = WikiMap::getCurrentWikiDbDomain()->getId();
68  $posFile = $this->getOption( 'p', 'searchUpdate.' . rawurlencode( $dbDomain ) . '.pos' );
69  $end = $this->getOption( 'e', wfTimestampNow() );
70  if ( $this->hasOption( 's' ) ) {
71  $start = $this->getOption( 's' );
72  } elseif ( is_readable( $posFile ) ) {
73  $start = file_get_contents( $posFile );
74  } else {
75  $start = wfTimestamp( TS_MW, time() - 86400 );
76  }
77 
78  $this->doUpdateSearchIndex( $start, $end );
79  $file = fopen( $posFile, 'w' );
80  if ( $file !== false ) {
81  fwrite( $file, $end );
82  fclose( $file );
83  } else {
84  $this->error( "*** Couldn't write to the $posFile!\n" );
85  }
86  }
87 
88  private function doUpdateSearchIndex( $start, $end ) {
90 
91  $wgDisableSearchUpdate = false;
92 
93  $dbw = $this->getDB( DB_PRIMARY );
94 
95  $this->output( "Updating searchindex between $start and $end\n" );
96 
97  # Select entries from recentchanges which are on top and between the specified times
98  $start = $dbw->timestamp( $start );
99  $end = $dbw->timestamp( $end );
100 
101  $res = $dbw->select(
102  [ 'recentchanges', 'page' ],
103  'rc_cur_id',
104  [
105  'rc_type != ' . $dbw->addQuotes( RC_LOG ),
106  'rc_timestamp BETWEEN ' . $dbw->addQuotes( $start ) . ' AND ' . $dbw->addQuotes( $end )
107  ],
108  __METHOD__,
109  [],
110  [
111  'page' => [ 'JOIN', 'rc_cur_id=page_id AND rc_this_oldid=page_latest' ]
112  ]
113  );
114 
115  foreach ( $res as $row ) {
116  $this->updateSearchIndexForPage( (int)$row->rc_cur_id );
117  }
118  $this->output( "Done\n" );
119  }
120 
126  private function updateSearchIndexForPage( int $pageId ) {
127  // Get current revision
128  $rev = MediaWikiServices::getInstance()
129  ->getRevisionLookup()
130  ->getRevisionByPageId( $pageId, 0, IDBAccessObject::READ_LATEST );
131  $title = null;
132  if ( $rev ) {
133  $titleObj = Title::newFromLinkTarget( $rev->getPageAsLinkTarget() );
134  $title = $titleObj->getPrefixedDBkey();
135  $this->output( "$title..." );
136  # Update searchindex
137  $u = new SearchUpdate( $pageId, $titleObj, $rev->getContent( SlotRecord::MAIN ) );
138  $u->doUpdate();
139  $this->output( "\n" );
140  }
141 
142  return $title;
143  }
144 }
145 
146 $maintClass = UpdateSearchIndex::class;
147 require_once RUN_MAINTENANCE_IF_MAIN;
UpdateSearchIndex\updateSearchIndexForPage
updateSearchIndexForPage(int $pageId)
Update the searchindex table for a given pageid.
Definition: updateSearchIndex.php:126
WikiMap\getCurrentWikiDbDomain
static getCurrentWikiDbDomain()
Definition: WikiMap.php:293
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:195
Maintenance\addDescription
addDescription( $text)
Set the description text.
Definition: Maintenance.php:329
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:1668
SearchUpdate
Database independent search index updater.
Definition: SearchUpdate.php:36
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
$maintClass
$maintClass
Definition: updateSearchIndex.php:146
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: Maintenance.php:59
$res
$res
Definition: testCompression.php:57
RC_LOG
const RC_LOG
Definition: Defines.php:117
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false, $multiOccurrence=false)
Add a parameter to the script.
Definition: Maintenance.php:249
$title
$title
Definition: testCompression.php:38
UpdateSearchIndex\doUpdateSearchIndex
doUpdateSearchIndex( $start, $end)
Definition: updateSearchIndex.php:88
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:1697
UpdateSearchIndex\execute
execute()
Do the actual work.
Definition: updateSearchIndex.php:66
Maintenance\DB_ADMIN
const DB_ADMIN
Definition: Maintenance.php:66
UpdateSearchIndex\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: updateSearchIndex.php:62
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
Maintenance\getDB
getDB( $db, $groups=[], $dbDomain=false)
Returns a database to be used by current maintenance script.
Definition: Maintenance.php:1362
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:294
$wgDisableSearchUpdate
$wgDisableSearchUpdate
If you've disabled search semi-permanently, this also disables updates to the table.
Definition: DefaultSettings.php:7674
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:286
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:464
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:435
UpdateSearchIndex\__construct
__construct()
Default constructor.
Definition: updateSearchIndex.php:43
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular option was set.
Definition: Maintenance.php:271
UpdateSearchIndex
Maintenance script for periodic off-peak updating of the search index.
Definition: updateSearchIndex.php:41
MediaWiki\Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40