2006-02-24 01:56:31 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
if ( !defined( 'MEDIAWIKI' ) ) {
|
|
|
|
|
die( "This file is part of MediaWiki, it is not a valid entry point\n" );
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-05 12:44:55 +00:00
|
|
|
require_once('UserMailer.php');
|
|
|
|
|
|
2007-04-04 05:22:37 +00:00
|
|
|
/**
|
2007-04-21 12:33:41 +00:00
|
|
|
* Class to both describe a background job and handle jobs.
|
2007-04-04 05:22:37 +00:00
|
|
|
*/
|
2006-06-18 12:42:16 +00:00
|
|
|
abstract class Job {
|
2006-05-11 22:40:38 +00:00
|
|
|
var $command,
|
2006-02-24 01:56:31 +00:00
|
|
|
$title,
|
2006-03-11 17:13:49 +00:00
|
|
|
$params,
|
2006-02-25 01:38:06 +00:00
|
|
|
$id,
|
2006-03-11 17:13:49 +00:00
|
|
|
$removeDuplicates,
|
2006-02-24 01:56:31 +00:00
|
|
|
$error;
|
|
|
|
|
|
2007-04-21 12:33:41 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
* Abstract functions
|
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Run the job
|
|
|
|
|
* @return boolean success
|
|
|
|
|
*/
|
|
|
|
|
abstract function run();
|
|
|
|
|
|
2006-02-24 01:56:31 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
* Static functions
|
|
|
|
|
*------------------------------------------------------------------------*/
|
2006-06-18 12:42:16 +00:00
|
|
|
|
2007-04-21 12:33:41 +00:00
|
|
|
/**
|
2006-06-18 12:42:16 +00:00
|
|
|
* @deprecated use LinksUpdate::queueRecursiveJobs()
|
|
|
|
|
*/
|
2006-02-24 01:56:31 +00:00
|
|
|
/**
|
2006-06-18 12:42:16 +00:00
|
|
|
* static function queueLinksJobs( $titles ) {}
|
2006-02-24 01:56:31 +00:00
|
|
|
*/
|
|
|
|
|
|
2007-05-11 07:36:05 +00:00
|
|
|
/**
|
|
|
|
|
* Pop a job of a certain type. This tries less hard than pop() to
|
|
|
|
|
* actually find a job; it may be adversely affected by concurrent job
|
|
|
|
|
* runners.
|
|
|
|
|
*/
|
|
|
|
|
static function pop_type($type) {
|
|
|
|
|
wfProfilein( __METHOD__ );
|
|
|
|
|
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
$row = $dbw->selectRow( 'job', '*', array( 'job_cmd' => $type ), __METHOD__,
|
|
|
|
|
array( 'LIMIT' => 1 ));
|
|
|
|
|
|
|
|
|
|
if ($row === false) {
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Ensure we "own" this row */
|
|
|
|
|
$dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
|
|
|
|
|
$affected = $dbw->affectedRows();
|
|
|
|
|
|
|
|
|
|
if ($affected == 0) {
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$namespace = $row->job_namespace;
|
|
|
|
|
$dbkey = $row->job_title;
|
|
|
|
|
$title = Title::makeTitleSafe( $namespace, $dbkey );
|
|
|
|
|
$job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ), $row->job_id );
|
|
|
|
|
|
|
|
|
|
$dbw->delete( 'job', $job->insertFields(), __METHOD__ );
|
|
|
|
|
$dbw->immediateCommit();
|
|
|
|
|
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return $job;
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-24 01:56:31 +00:00
|
|
|
/**
|
|
|
|
|
* Pop a job off the front of the queue
|
|
|
|
|
* @static
|
2007-03-25 15:55:39 +00:00
|
|
|
* @param $offset Number of jobs to skip
|
2006-02-24 01:56:31 +00:00
|
|
|
* @return Job or false if there's no jobs
|
|
|
|
|
*/
|
2007-03-25 15:55:39 +00:00
|
|
|
static function pop($offset=0) {
|
2006-06-18 12:42:16 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2006-02-24 01:56:31 +00:00
|
|
|
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbr = wfGetDB( DB_SLAVE );
|
2006-02-25 01:12:35 +00:00
|
|
|
|
2007-03-25 15:55:39 +00:00
|
|
|
/* Get a job from the slave, start with an offset,
|
|
|
|
|
scan full set afterwards, avoid hitting purged rows
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2007-03-25 15:55:39 +00:00
|
|
|
NB: If random fetch previously was used, offset
|
|
|
|
|
will always be ahead of few entries
|
|
|
|
|
*/
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2007-03-25 15:55:39 +00:00
|
|
|
$row = $dbr->selectRow( 'job', '*', "job_id >= ${offset}", __METHOD__,
|
|
|
|
|
array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2007-03-25 15:55:39 +00:00
|
|
|
// Refetching without offset is needed as some of job IDs could have had delayed commits
|
|
|
|
|
// and have lower IDs than jobs already executed, blame concurrency :)
|
2007-04-21 12:33:41 +00:00
|
|
|
//
|
2007-03-25 20:09:21 +00:00
|
|
|
if ( $row === false) {
|
|
|
|
|
if ($offset!=0)
|
|
|
|
|
$row = $dbr->selectRow( 'job', '*', '', __METHOD__,
|
|
|
|
|
array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 ));
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2007-03-25 15:55:39 +00:00
|
|
|
if ($row === false ) {
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
|
return false;
|
2007-04-21 12:33:41 +00:00
|
|
|
}
|
|
|
|
|
}
|
2007-03-25 15:55:39 +00:00
|
|
|
$offset = $row->job_id;
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2006-02-25 01:12:35 +00:00
|
|
|
// Try to delete it from the master
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2006-06-18 12:42:16 +00:00
|
|
|
$dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
|
2006-02-24 01:56:31 +00:00
|
|
|
$affected = $dbw->affectedRows();
|
|
|
|
|
$dbw->immediateCommit();
|
|
|
|
|
|
2006-02-25 01:12:35 +00:00
|
|
|
if ( !$affected ) {
|
|
|
|
|
// Failed, someone else beat us to it
|
|
|
|
|
// Try getting a random row
|
2006-03-11 17:13:49 +00:00
|
|
|
$row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
|
2007-03-25 15:55:39 +00:00
|
|
|
'MAX(job_id) as maxjob' ), "job_id >= $offset", __METHOD__ );
|
2006-02-25 01:12:35 +00:00
|
|
|
if ( $row === false || is_null( $row->minjob ) || is_null( $row->maxjob ) ) {
|
|
|
|
|
// No jobs to get
|
2006-06-18 12:42:16 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2006-02-25 01:12:35 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Get the random row
|
2006-03-11 17:13:49 +00:00
|
|
|
$row = $dbw->selectRow( 'job', '*',
|
2007-03-25 15:55:39 +00:00
|
|
|
'job_id >= ' . mt_rand( $row->minjob, $row->maxjob ), __METHOD__ );
|
2006-02-25 01:12:35 +00:00
|
|
|
if ( $row === false ) {
|
|
|
|
|
// Random job gone before we got the chance to select it
|
|
|
|
|
// Give up
|
2006-06-18 12:42:16 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2006-02-25 01:12:35 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
// Delete the random row
|
2006-06-18 12:42:16 +00:00
|
|
|
$dbw->delete( 'job', array( 'job_id' => $row->job_id ), __METHOD__ );
|
2006-02-25 01:12:35 +00:00
|
|
|
$affected = $dbw->affectedRows();
|
|
|
|
|
$dbw->immediateCommit();
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2006-02-25 01:12:35 +00:00
|
|
|
if ( !$affected ) {
|
|
|
|
|
// Random job gone before we exclusively deleted it
|
|
|
|
|
// Give up
|
2006-06-18 12:42:16 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2006-02-25 01:12:35 +00:00
|
|
|
return false;
|
2006-03-11 17:13:49 +00:00
|
|
|
}
|
2006-02-24 01:56:31 +00:00
|
|
|
}
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2006-02-25 01:12:35 +00:00
|
|
|
// If execution got to here, there's a row in $row that has been deleted from the database
|
|
|
|
|
// by this thread. Hence the concurrent pop was successful.
|
|
|
|
|
$namespace = $row->job_namespace;
|
|
|
|
|
$dbkey = $row->job_title;
|
2006-02-24 01:56:31 +00:00
|
|
|
$title = Title::makeTitleSafe( $namespace, $dbkey );
|
2006-06-18 12:42:16 +00:00
|
|
|
$job = Job::factory( $row->job_cmd, $title, Job::extractBlob( $row->job_params ), $row->job_id );
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2006-05-31 23:56:41 +00:00
|
|
|
// Remove any duplicates it may have later in the queue
|
2006-06-18 12:42:16 +00:00
|
|
|
$dbw->delete( 'job', $job->insertFields(), __METHOD__ );
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2006-06-18 12:42:16 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2006-02-24 01:56:31 +00:00
|
|
|
return $job;
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-21 12:33:41 +00:00
|
|
|
/**
|
2006-06-18 12:42:16 +00:00
|
|
|
* Create an object of a subclass
|
|
|
|
|
*/
|
|
|
|
|
static function factory( $command, $title, $params = false, $id = 0 ) {
|
|
|
|
|
switch ( $command ) {
|
|
|
|
|
case 'refreshLinks':
|
|
|
|
|
return new RefreshLinksJob( $title, $params, $id );
|
2006-06-19 02:29:21 +00:00
|
|
|
case 'htmlCacheUpdate':
|
|
|
|
|
case 'html_cache_update': # BC
|
2006-06-18 12:42:16 +00:00
|
|
|
return new HTMLCacheUpdateJob( $title, $params['table'], $params['start'], $params['end'], $id );
|
2007-05-05 12:44:55 +00:00
|
|
|
case 'sendMail':
|
2007-06-21 14:12:50 +00:00
|
|
|
return new EmaillingJob( $params );
|
2007-05-11 16:42:18 +00:00
|
|
|
case 'enotifNotify':
|
2007-06-21 14:12:50 +00:00
|
|
|
return new EnotifNotifyJob( $title, $params );
|
|
|
|
|
}
|
|
|
|
|
// OK, check if this is a custom job
|
|
|
|
|
global $wgCustomJobs;
|
|
|
|
|
wfLoadAllExtensions(); // This may be for an extension
|
|
|
|
|
|
|
|
|
|
if( isset($wgCustomJobs[$command]) ) {
|
|
|
|
|
$class = $wgCustomJobs[$command];
|
|
|
|
|
return new $class($title, $params, $id);
|
|
|
|
|
} else {
|
|
|
|
|
throw new MWException( "Invalid job command \"$command\"" );
|
2006-06-18 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function makeBlob( $params ) {
|
|
|
|
|
if ( $params !== false ) {
|
|
|
|
|
return serialize( $params );
|
|
|
|
|
} else {
|
|
|
|
|
return '';
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static function extractBlob( $blob ) {
|
|
|
|
|
if ( (string)$blob !== '' ) {
|
|
|
|
|
return unserialize( $blob );
|
|
|
|
|
} else {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-21 12:33:41 +00:00
|
|
|
/**
|
|
|
|
|
* Batch-insert a group of jobs into the queue.
|
|
|
|
|
* This will be wrapped in a transaction with a forced commit.
|
|
|
|
|
*
|
|
|
|
|
* This may add duplicate at insert time, but they will be
|
|
|
|
|
* removed later on, when the first one is popped.
|
|
|
|
|
*
|
|
|
|
|
* @param $jobs array of Job objects
|
|
|
|
|
*/
|
|
|
|
|
static function batchInsert( $jobs ) {
|
|
|
|
|
if( count( $jobs ) ) {
|
|
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
|
|
|
|
$dbw->begin();
|
|
|
|
|
foreach( $jobs as $job ) {
|
|
|
|
|
$rows[] = $job->insertFields();
|
|
|
|
|
}
|
|
|
|
|
$dbw->insert( 'job', $rows, __METHOD__, 'IGNORE' );
|
|
|
|
|
$dbw->commit();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2006-02-24 01:56:31 +00:00
|
|
|
/*-------------------------------------------------------------------------
|
|
|
|
|
* Non-static functions
|
|
|
|
|
*------------------------------------------------------------------------*/
|
|
|
|
|
|
2006-06-18 12:42:16 +00:00
|
|
|
function __construct( $command, $title, $params = false, $id = 0 ) {
|
2006-02-24 01:56:31 +00:00
|
|
|
$this->command = $command;
|
|
|
|
|
$this->title = $title;
|
|
|
|
|
$this->params = $params;
|
2006-02-25 01:38:06 +00:00
|
|
|
$this->id = $id;
|
2006-02-24 01:56:31 +00:00
|
|
|
|
|
|
|
|
// A bit of premature generalisation
|
|
|
|
|
// Oh well, the whole class is premature generalisation really
|
|
|
|
|
$this->removeDuplicates = true;
|
|
|
|
|
}
|
|
|
|
|
|
2006-05-31 23:56:41 +00:00
|
|
|
/**
|
|
|
|
|
* Insert a single job into the queue.
|
|
|
|
|
*/
|
2006-02-24 01:56:31 +00:00
|
|
|
function insert() {
|
2006-05-31 23:56:41 +00:00
|
|
|
$fields = $this->insertFields();
|
2006-02-24 01:56:31 +00:00
|
|
|
|
2007-01-22 23:50:42 +00:00
|
|
|
$dbw = wfGetDB( DB_MASTER );
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2006-02-24 01:56:31 +00:00
|
|
|
if ( $this->removeDuplicates ) {
|
2006-06-18 12:42:16 +00:00
|
|
|
$res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__ );
|
2006-02-25 00:29:15 +00:00
|
|
|
if ( $dbw->numRows( $res ) ) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2006-02-24 01:56:31 +00:00
|
|
|
}
|
|
|
|
|
$fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
|
2006-06-18 12:42:16 +00:00
|
|
|
$dbw->insert( 'job', $fields, __METHOD__ );
|
2006-02-24 01:56:31 +00:00
|
|
|
}
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2006-05-31 23:56:41 +00:00
|
|
|
protected function insertFields() {
|
|
|
|
|
return array(
|
|
|
|
|
'job_cmd' => $this->command,
|
|
|
|
|
'job_namespace' => $this->title->getNamespace(),
|
|
|
|
|
'job_title' => $this->title->getDBkey(),
|
2006-06-18 12:42:16 +00:00
|
|
|
'job_params' => Job::makeBlob( $this->params )
|
2006-05-31 23:56:41 +00:00
|
|
|
);
|
|
|
|
|
}
|
2006-02-24 01:56:31 +00:00
|
|
|
|
2006-06-18 12:42:16 +00:00
|
|
|
function toString() {
|
|
|
|
|
$paramString = '';
|
|
|
|
|
if ( $this->params ) {
|
|
|
|
|
foreach ( $this->params as $key => $value ) {
|
|
|
|
|
if ( $paramString != '' ) {
|
|
|
|
|
$paramString .= ' ';
|
2006-06-15 06:35:24 +00:00
|
|
|
}
|
2006-06-18 12:42:16 +00:00
|
|
|
$paramString .= "$key=$value";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ( is_object( $this->title ) ) {
|
|
|
|
|
$s = "{$this->command} " . $this->title->getPrefixedDBkey();
|
|
|
|
|
if ( $paramString !== '' ) {
|
|
|
|
|
$s .= ' ' . $paramString;
|
|
|
|
|
}
|
|
|
|
|
return $s;
|
|
|
|
|
} else {
|
|
|
|
|
return "{$this->command} $paramString";
|
2006-02-24 01:56:31 +00:00
|
|
|
}
|
2006-06-18 12:42:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function getLastError() {
|
|
|
|
|
return $this->error;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-04-04 05:22:37 +00:00
|
|
|
|
|
|
|
|
/**
|
2007-04-21 12:33:41 +00:00
|
|
|
* Background job to update links for a given title.
|
2007-04-04 05:22:37 +00:00
|
|
|
*/
|
2006-06-18 12:42:16 +00:00
|
|
|
class RefreshLinksJob extends Job {
|
|
|
|
|
function __construct( $title, $params = '', $id = 0 ) {
|
|
|
|
|
parent::__construct( 'refreshLinks', $title, $params, $id );
|
2006-02-24 01:56:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Run a refreshLinks job
|
|
|
|
|
* @return boolean success
|
|
|
|
|
*/
|
2006-06-18 12:42:16 +00:00
|
|
|
function run() {
|
2006-02-24 01:56:31 +00:00
|
|
|
global $wgParser;
|
2006-06-18 12:42:16 +00:00
|
|
|
wfProfileIn( __METHOD__ );
|
2006-05-01 10:53:59 +00:00
|
|
|
|
2006-02-24 01:56:31 +00:00
|
|
|
$linkCache =& LinkCache::singleton();
|
|
|
|
|
$linkCache->clear();
|
2007-04-21 12:33:41 +00:00
|
|
|
|
2006-02-24 01:56:31 +00:00
|
|
|
if ( is_null( $this->title ) ) {
|
|
|
|
|
$this->error = "refreshLinks: Invalid title";
|
2006-06-18 12:42:16 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2006-02-24 01:56:31 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$revision = Revision::newFromTitle( $this->title );
|
|
|
|
|
if ( !$revision ) {
|
|
|
|
|
$this->error = 'refreshLinks: Article not found "' . $this->title->getPrefixedDBkey() . '"';
|
2006-06-18 12:42:16 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
2006-02-24 01:56:31 +00:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2006-06-18 12:42:16 +00:00
|
|
|
wfProfileIn( __METHOD__.'-parse' );
|
2006-02-24 01:56:31 +00:00
|
|
|
$options = new ParserOptions;
|
|
|
|
|
$parserOutput = $wgParser->parse( $revision->getText(), $this->title, $options, true, true, $revision->getId() );
|
2006-06-18 12:42:16 +00:00
|
|
|
wfProfileOut( __METHOD__.'-parse' );
|
|
|
|
|
wfProfileIn( __METHOD__.'-update' );
|
2006-02-25 01:31:07 +00:00
|
|
|
$update = new LinksUpdate( $this->title, $parserOutput, false );
|
2006-02-24 01:56:31 +00:00
|
|
|
$update->doUpdate();
|
2006-06-18 12:42:16 +00:00
|
|
|
wfProfileOut( __METHOD__.'-update' );
|
|
|
|
|
wfProfileOut( __METHOD__ );
|
2006-02-24 01:56:31 +00:00
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
}
|
2006-06-18 12:42:16 +00:00
|
|
|
|
2007-05-05 12:44:55 +00:00
|
|
|
class EmaillingJob extends Job {
|
|
|
|
|
function __construct($params) {
|
|
|
|
|
parent::__construct('sendMail', Title::newMainPage(), $params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function run() {
|
|
|
|
|
userMailer($this->params['to'], $this->params['from'], $this->params['subj'],
|
|
|
|
|
$this->params['body'], $this->params['replyto']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-11 16:42:18 +00:00
|
|
|
class EnotifNotifyJob extends Job {
|
|
|
|
|
function __construct($title, $params) {
|
|
|
|
|
parent::__construct('enotifNotify', $title, $params);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function run() {
|
|
|
|
|
$enotif = new EmailNotification();
|
|
|
|
|
$enotif->actuallyNotifyOnPageChange( User::newFromName($this->params['editor'], false),
|
|
|
|
|
$this->title, $this->params['timestamp'],
|
|
|
|
|
$this->params['summary'], $this->params['minorEdit'],
|
|
|
|
|
$this->params['oldid']);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2006-03-07 13:32:27 +00:00
|
|
|
?>
|