MediaWiki  1.23.2
updateSearchIndex.php
Go to the documentation of this file.
1 <?php
31 require_once __DIR__ . '/Maintenance.php';
32 
39 
40  public function __construct() {
41  parent::__construct();
42  $this->mDescription = "Script for periodic off-peak updating of the search index";
43  $this->addOption( 's', 'starting timestamp', false, true );
44  $this->addOption( 'e', 'Ending timestamp', false, true );
45  $this->addOption( 'p', 'File for saving/loading timestamps, searchUpdate.WIKI_ID.pos by default', false, true );
46  $this->addOption( 'l', 'How long the searchindex and revision tables will be locked for', false, true );
47  }
48 
49  public function getDbType() {
50  return Maintenance::DB_ADMIN;
51  }
52 
53  public function execute() {
54  $posFile = $this->getOption( 'p', 'searchUpdate.' . wfWikiId() . '.pos' );
55  $end = $this->getOption( 'e', wfTimestampNow() );
56  if ( $this->hasOption( 's' ) ) {
57  $start = $this->getOption( 's' );
58  } elseif ( is_readable( 'searchUpdate.pos' ) ) {
59  # B/c to the old position file name which was hardcoded
60  # We can safely delete the file when we're done though.
61  $start = file_get_contents( 'searchUpdate.pos' );
62  unlink( 'searchUpdate.pos' );
63  } elseif ( is_readable( $posFile ) ) {
64  $start = file_get_contents( $posFile );
65  } else {
66  $start = wfTimestamp( TS_MW, time() - 86400 );
67  }
68  $lockTime = $this->getOption( 'l', 20 );
69 
70  $this->doUpdateSearchIndex( $start, $end, $lockTime );
71  if ( is_writable( dirname( realpath( $posFile ) ) ) ) {
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  } else {
80  $this->error( "*** Couldn't write to the $posFile!\n" );
81  }
82  }
83 
84  private function doUpdateSearchIndex( $start, $end, $maxLockTime ) {
85  global $wgDisableSearchUpdate;
86 
87  $wgDisableSearchUpdate = false;
88 
89  $dbw = wfGetDB( DB_MASTER );
90  $recentchanges = $dbw->tableName( 'recentchanges' );
91 
92  $this->output( "Updating searchindex between $start and $end\n" );
93 
94  # Select entries from recentchanges which are on top and between the specified times
95  $start = $dbw->timestamp( $start );
96  $end = $dbw->timestamp( $end );
97 
98  $page = $dbw->tableName( 'page' );
99  $sql = "SELECT rc_cur_id FROM $recentchanges
100  JOIN $page ON rc_cur_id=page_id AND rc_this_oldid=page_latest
101  WHERE rc_type != " . RC_LOG . " AND rc_timestamp BETWEEN '$start' AND '$end'";
102  $res = $dbw->query( $sql, __METHOD__ );
103 
104  $this->updateSearchIndex( $maxLockTime, array( $this, 'searchIndexUpdateCallback' ), $dbw, $res );
105 
106  $this->output( "Done\n" );
107  }
108 
109  public function searchIndexUpdateCallback( $dbw, $row ) {
110  $this->updateSearchIndexForPage( $dbw, $row->rc_cur_id );
111  }
112 }
113 
114 $maintClass = "UpdateSearchIndex";
115 require_once RUN_MAINTENANCE_IF_MAIN;
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
wfTimestamp
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
Definition: GlobalFunctions.php:2483
UpdateSearchIndex\searchIndexUpdateCallback
searchIndexUpdateCallback( $dbw, $row)
Definition: updateSearchIndex.php:109
RC_LOG
const RC_LOG
Definition: Defines.php:181
Maintenance\addOption
addOption( $name, $description, $required=false, $withArg=false, $shortName=false)
Add a parameter to the script.
Definition: Maintenance.php:169
RUN_MAINTENANCE_IF_MAIN
require_once RUN_MAINTENANCE_IF_MAIN
Definition: maintenance.txt:50
$maintClass
$maintClass
Definition: updateSearchIndex.php:114
Maintenance
Abstract maintenance class for quickly writing and churning out maintenance scripts with minimal effo...
Definition: maintenance.txt:39
Maintenance\updateSearchIndex
updateSearchIndex( $maxLockTime, $callback, $dbw, $results)
Perform a search index update with locking.
Definition: Maintenance.php:1059
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
wfTimestampNow
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
Definition: GlobalFunctions.php:2514
UpdateSearchIndex\execute
execute()
Do the actual work.
Definition: updateSearchIndex.php:53
Maintenance\DB_ADMIN
const DB_ADMIN
Definition: Maintenance.php:59
TS_MW
const TS_MW
MediaWiki concatenated string timestamp (YYYYMMDDHHMMSS)
Definition: GlobalFunctions.php:2431
UpdateSearchIndex\getDbType
getDbType()
Does the script need different DB access? By default, we give Maintenance scripts normal rights to th...
Definition: updateSearchIndex.php:49
Maintenance\updateSearchIndexForPage
updateSearchIndexForPage( $dbw, $pageId)
Update the searchindex table for a given pageid.
Definition: Maintenance.php:1097
$file
if(PHP_SAPI !='cli') $file
Definition: UtfNormalTest2.php:30
Maintenance\getOption
getOption( $name, $default=null)
Get an option, or return the default.
Definition: Maintenance.php:191
UpdateSearchIndex\doUpdateSearchIndex
doUpdateSearchIndex( $start, $end, $maxLockTime)
Definition: updateSearchIndex.php:84
Maintenance\error
error( $err, $die=0)
Throw an error to the user.
Definition: Maintenance.php:333
Maintenance\output
output( $out, $channel=null)
Throw some output to the user.
Definition: Maintenance.php:314
UpdateSearchIndex\__construct
__construct()
Default constructor.
Definition: updateSearchIndex.php:40
Maintenance\hasOption
hasOption( $name)
Checks to see if a particular param exists.
Definition: Maintenance.php:181
$res
$res
Definition: database.txt:21
UpdateSearchIndex
Maintenance script for periodic off-peak updating of the search index.
Definition: updateSearchIndex.php:38