3 if ( !defined( 'MEDIAWIKI' ) ) {
4 die( "This file is part of MediaWiki, it is not a valid entry point\n" );
15 /*-------------------------------------------------------------------------
17 *------------------------------------------------------------------------*/
20 * @deprecated use LinksUpdate::queueRecursiveJobs()
23 * static function queueLinksJobs( $titles ) {}
27 * Pop a job off the front of the queue
29 * @return Job or false if there's no jobs
31 static function pop() {
32 wfProfileIn( __METHOD__
);
34 $dbr =& wfGetDB( DB_SLAVE
);
36 // Get a job from the slave
37 $row = $dbr->selectRow( 'job', '*', '', __METHOD__
,
38 array( 'ORDER BY' => 'job_id', 'LIMIT' => 1 )
41 if ( $row === false ) {
42 wfProfileOut( __METHOD__
);
46 // Try to delete it from the master
47 $dbw =& wfGetDB( DB_MASTER
);
48 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
49 $affected = $dbw->affectedRows();
50 $dbw->immediateCommit();
53 // Failed, someone else beat us to it
54 // Try getting a random row
55 $row = $dbw->selectRow( 'job', array( 'MIN(job_id) as minjob',
56 'MAX(job_id) as maxjob' ), '', __METHOD__
);
57 if ( $row === false ||
is_null( $row->minjob
) ||
is_null( $row->maxjob
) ) {
59 wfProfileOut( __METHOD__
);
63 $row = $dbw->selectRow( 'job', '*',
64 array( 'job_id' => mt_rand( $row->minjob
, $row->maxjob
) ), __METHOD__
);
65 if ( $row === false ) {
66 // Random job gone before we got the chance to select it
68 wfProfileOut( __METHOD__
);
71 // Delete the random row
72 $dbw->delete( 'job', array( 'job_id' => $row->job_id
), __METHOD__
);
73 $affected = $dbw->affectedRows();
74 $dbw->immediateCommit();
77 // Random job gone before we exclusively deleted it
79 wfProfileOut( __METHOD__
);
84 // If execution got to here, there's a row in $row that has been deleted from the database
85 // by this thread. Hence the concurrent pop was successful.
86 $namespace = $row->job_namespace
;
87 $dbkey = $row->job_title
;
88 $title = Title
::makeTitleSafe( $namespace, $dbkey );
89 $job = Job
::factory( $row->job_cmd
, $title, Job
::extractBlob( $row->job_params
), $row->job_id
);
91 // Remove any duplicates it may have later in the queue
92 $dbw->delete( 'job', $job->insertFields(), __METHOD__
);
94 wfProfileOut( __METHOD__
);
99 * Create an object of a subclass
101 static function factory( $command, $title, $params = false, $id = 0 ) {
102 switch ( $command ) {
104 return new RefreshLinksJob( $title, $params, $id );
105 case 'htmlCacheUpdate':
106 case 'html_cache_update': # BC
107 return new HTMLCacheUpdateJob( $title, $params['table'], $params['start'], $params['end'], $id );
109 throw new MWException( "Invalid job command \"$command\"" );
113 static function makeBlob( $params ) {
114 if ( $params !== false ) {
115 return serialize( $params );
121 static function extractBlob( $blob ) {
122 if ( (string)$blob !== '' ) {
123 return unserialize( $blob );
129 /*-------------------------------------------------------------------------
130 * Non-static functions
131 *------------------------------------------------------------------------*/
133 function __construct( $command, $title, $params = false, $id = 0 ) {
134 $this->command
= $command;
135 $this->title
= $title;
136 $this->params
= $params;
139 // A bit of premature generalisation
140 // Oh well, the whole class is premature generalisation really
141 $this->removeDuplicates
= true;
145 * Insert a single job into the queue.
148 $fields = $this->insertFields();
150 $dbw =& wfGetDB( DB_MASTER
);
152 if ( $this->removeDuplicates
) {
153 $res = $dbw->select( 'job', array( '1' ), $fields, __METHOD__
);
154 if ( $dbw->numRows( $res ) ) {
158 $fields['job_id'] = $dbw->nextSequenceValue( 'job_job_id_seq' );
159 $dbw->insert( 'job', $fields, __METHOD__
);
162 protected function insertFields() {
164 'job_cmd' => $this->command
,
165 'job_namespace' => $this->title
->getNamespace(),
166 'job_title' => $this->title
->getDBkey(),
167 'job_params' => Job
::makeBlob( $this->params
)
172 * Batch-insert a group of jobs into the queue.
173 * This will be wrapped in a transaction with a forced commit.
175 * This may add duplicate at insert time, but they will be
176 * removed later on, when the first one is popped.
178 * @param $jobs array of Job objects
180 static function batchInsert( $jobs ) {
181 if( count( $jobs ) ) {
182 $dbw = wfGetDB( DB_MASTER
);
184 foreach( $jobs as $job ) {
185 $rows[] = $job->insertFields();
187 $dbw->insert( 'job', $rows, __METHOD__
, 'IGNORE' );
194 * @return boolean success
196 abstract function run();
198 function toString() {
200 if ( $this->params
) {
201 foreach ( $this->params
as $key => $value ) {
202 if ( $paramString != '' ) {
205 $paramString .= "$key=$value";
209 if ( is_object( $this->title
) ) {
210 $s = "{$this->command} " . $this->title
->getPrefixedDBkey();
211 if ( $paramString !== '' ) {
212 $s .= ' ' . $paramString;
216 return "{$this->command} $paramString";
220 function getLastError() {
225 class RefreshLinksJob
extends Job
{
226 function __construct( $title, $params = '', $id = 0 ) {
227 parent
::__construct( 'refreshLinks', $title, $params, $id );
231 * Run a refreshLinks job
232 * @return boolean success
236 wfProfileIn( __METHOD__
);
238 $linkCache =& LinkCache
::singleton();
241 if ( is_null( $this->title
) ) {
242 $this->error
= "refreshLinks: Invalid title";
243 wfProfileOut( __METHOD__
);
247 $revision = Revision
::newFromTitle( $this->title
);
249 $this->error
= 'refreshLinks: Article not found "' . $this->title
->getPrefixedDBkey() . '"';
250 wfProfileOut( __METHOD__
);
254 wfProfileIn( __METHOD__
.'-parse' );
255 $options = new ParserOptions
;
256 $parserOutput = $wgParser->parse( $revision->getText(), $this->title
, $options, true, true, $revision->getId() );
257 wfProfileOut( __METHOD__
.'-parse' );
258 wfProfileIn( __METHOD__
.'-update' );
259 $update = new LinksUpdate( $this->title
, $parserOutput, false );
261 wfProfileOut( __METHOD__
.'-update' );
262 wfProfileOut( __METHOD__
);