MediaWiki
REL1_23
|
00001 <?php 00031 abstract class Job implements IJobSpecification { 00033 public $command; 00034 00036 public $params; 00037 00039 public $metadata = array(); 00040 00042 protected $title; 00043 00045 protected $removeDuplicates; 00046 00048 protected $error; 00049 00050 /*------------------------------------------------------------------------- 00051 * Abstract functions 00052 *------------------------------------------------------------------------*/ 00053 00058 abstract public function run(); 00059 00060 /*------------------------------------------------------------------------- 00061 * Static functions 00062 *------------------------------------------------------------------------*/ 00063 00073 public static function factory( $command, Title $title, $params = false ) { 00074 global $wgJobClasses; 00075 if ( isset( $wgJobClasses[$command] ) ) { 00076 $class = $wgJobClasses[$command]; 00077 00078 return new $class( $title, $params ); 00079 } 00080 throw new MWException( "Invalid job command `{$command}`" ); 00081 } 00082 00094 public static function batchInsert( $jobs ) { 00095 return JobQueueGroup::singleton()->push( $jobs ); 00096 } 00097 00109 public static function safeBatchInsert( $jobs ) { 00110 return JobQueueGroup::singleton()->push( $jobs, JobQueue::QOS_ATOMIC ); 00111 } 00112 00122 public static function pop_type( $type ) { 00123 return JobQueueGroup::singleton()->get( $type )->pop(); 00124 } 00125 00133 public static function pop() { 00134 return JobQueueGroup::singleton()->pop(); 00135 } 00136 00137 /*------------------------------------------------------------------------- 00138 * Non-static functions 00139 *------------------------------------------------------------------------*/ 00140 00146 public function __construct( $command, $title, $params = false ) { 00147 $this->command = $command; 00148 $this->title = $title; 00149 $this->params = $params; 00150 00151 // expensive jobs may set this to true 00152 $this->removeDuplicates = false; 00153 } 00154 00158 public function getType() { 00159 return $this->command; 00160 } 00161 00165 public function getTitle() { 00166 return $this->title; 00167 } 00168 00172 public function getParams() { 00173 return $this->params; 00174 } 00175 00180 public function getReleaseTimestamp() { 00181 return isset( $this->params['jobReleaseTimestamp'] ) 00182 ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] ) 00183 : null; 00184 } 00185 00189 public function ignoreDuplicates() { 00190 return $this->removeDuplicates; 00191 } 00192 00197 public function allowRetries() { 00198 return true; 00199 } 00200 00206 public function workItemCount() { 00207 return 1; 00208 } 00209 00219 public function getDeduplicationInfo() { 00220 $info = array( 00221 'type' => $this->getType(), 00222 'namespace' => $this->getTitle()->getNamespace(), 00223 'title' => $this->getTitle()->getDBkey(), 00224 'params' => $this->getParams() 00225 ); 00226 if ( is_array( $info['params'] ) ) { 00227 // Identical jobs with different "root" jobs should count as duplicates 00228 unset( $info['params']['rootJobSignature'] ); 00229 unset( $info['params']['rootJobTimestamp'] ); 00230 // Likewise for jobs with different delay times 00231 unset( $info['params']['jobReleaseTimestamp'] ); 00232 } 00233 00234 return $info; 00235 } 00236 00245 public static function newRootJobParams( $key ) { 00246 return array( 00247 'rootJobSignature' => sha1( $key ), 00248 'rootJobTimestamp' => wfTimestampNow() 00249 ); 00250 } 00251 00257 public function getRootJobParams() { 00258 return array( 00259 'rootJobSignature' => isset( $this->params['rootJobSignature'] ) 00260 ? $this->params['rootJobSignature'] 00261 : null, 00262 'rootJobTimestamp' => isset( $this->params['rootJobTimestamp'] ) 00263 ? $this->params['rootJobTimestamp'] 00264 : null 00265 ); 00266 } 00267 00273 public function hasRootJobParams() { 00274 return isset( $this->params['rootJobSignature'] ) 00275 && isset( $this->params['rootJobTimestamp'] ); 00276 } 00277 00283 public function insert() { 00284 return JobQueueGroup::singleton()->push( $this ); 00285 } 00286 00290 public function toString() { 00291 $paramString = ''; 00292 if ( $this->params ) { 00293 foreach ( $this->params as $key => $value ) { 00294 if ( $paramString != '' ) { 00295 $paramString .= ' '; 00296 } 00297 if ( is_array( $value ) ) { 00298 $value = "array(" . count( $value ) . ")"; 00299 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) { 00300 $value = "object(" . get_class( $value ) . ")"; 00301 } 00302 $value = (string)$value; 00303 if ( mb_strlen( $value ) > 1024 ) { 00304 $value = "string(" . mb_strlen( $value ) . ")"; 00305 } 00306 00307 $paramString .= "$key=$value"; 00308 } 00309 } 00310 00311 if ( is_object( $this->title ) ) { 00312 $s = "{$this->command} " . $this->title->getPrefixedDBkey(); 00313 if ( $paramString !== '' ) { 00314 $s .= ' ' . $paramString; 00315 } 00316 00317 return $s; 00318 } else { 00319 return "{$this->command} $paramString"; 00320 } 00321 } 00322 00323 protected function setLastError( $error ) { 00324 $this->error = $error; 00325 } 00326 00327 public function getLastError() { 00328 return $this->error; 00329 } 00330 }