MediaWiki  REL1_21
Job.php
Go to the documentation of this file.
00001 <?php
00030 abstract class Job {
00034         var $title;
00035 
00036         var $command,
00037                 $params,
00038                 $id,
00039                 $removeDuplicates,
00040                 $error;
00041 
00043         public $metadata = array();
00044 
00045         /*-------------------------------------------------------------------------
00046          * Abstract functions
00047          *------------------------------------------------------------------------*/
00048 
00053         abstract public function run();
00054 
00055         /*-------------------------------------------------------------------------
00056          * Static functions
00057          *------------------------------------------------------------------------*/
00058 
00069         public static function factory( $command, Title $title, $params = false, $id = 0 ) {
00070                 global $wgJobClasses;
00071                 if( isset( $wgJobClasses[$command] ) ) {
00072                         $class = $wgJobClasses[$command];
00073                         return new $class( $title, $params, $id );
00074                 }
00075                 throw new MWException( "Invalid job command `{$command}`" );
00076         }
00077 
00089         public static function batchInsert( $jobs ) {
00090                 return JobQueueGroup::singleton()->push( $jobs );
00091         }
00092 
00104         public static function safeBatchInsert( $jobs ) {
00105                 return JobQueueGroup::singleton()->push( $jobs, JobQueue::QoS_Atomic );
00106         }
00107 
00117         public static function pop_type( $type ) {
00118                 return JobQueueGroup::singleton()->get( $type )->pop();
00119         }
00120 
00128         public static function pop() {
00129                 return JobQueueGroup::singleton()->pop();
00130         }
00131 
00132         /*-------------------------------------------------------------------------
00133          * Non-static functions
00134          *------------------------------------------------------------------------*/
00135 
00142         public function __construct( $command, $title, $params = false, $id = 0 ) {
00143                 $this->command = $command;
00144                 $this->title = $title;
00145                 $this->params = $params;
00146                 $this->id = $id;
00147 
00148                 $this->removeDuplicates = false; // expensive jobs may set this to true
00149         }
00150 
00154         public function getId() {
00155                 return $this->id;
00156         }
00157 
00161         public function getType() {
00162                 return $this->command;
00163         }
00164 
00168         public function getTitle() {
00169                 return $this->title;
00170         }
00171 
00175         public function getParams() {
00176                 return $this->params;
00177         }
00178 
00182         public function ignoreDuplicates() {
00183                 return $this->removeDuplicates;
00184         }
00185 
00189         public function allowRetries() {
00190                 return true;
00191         }
00192 
00198         public function getDeduplicationInfo() {
00199                 $info = array(
00200                         'type'      => $this->getType(),
00201                         'namespace' => $this->getTitle()->getNamespace(),
00202                         'title'     => $this->getTitle()->getDBkey(),
00203                         'params'    => $this->getParams()
00204                 );
00205                 // Identical jobs with different "root" jobs should count as duplicates
00206                 if ( is_array( $info['params'] ) ) {
00207                         unset( $info['params']['rootJobSignature'] );
00208                         unset( $info['params']['rootJobTimestamp'] );
00209                 }
00210                 return $info;
00211         }
00212 
00217         public static function newRootJobParams( $key ) {
00218                 return array(
00219                         'rootJobSignature' => sha1( $key ),
00220                         'rootJobTimestamp' => wfTimestampNow()
00221                 );
00222         }
00223 
00227         public function getRootJobParams() {
00228                 return array(
00229                         'rootJobSignature' => isset( $this->params['rootJobSignature'] )
00230                                 ? $this->params['rootJobSignature']
00231                                 : null,
00232                         'rootJobTimestamp' => isset( $this->params['rootJobTimestamp'] )
00233                                 ? $this->params['rootJobTimestamp']
00234                                 : null
00235                 );
00236         }
00237 
00243         public function insert() {
00244                 return JobQueueGroup::singleton()->push( $this );
00245         }
00246 
00250         public function toString() {
00251                 $paramString = '';
00252                 if ( $this->params ) {
00253                         foreach ( $this->params as $key => $value ) {
00254                                 if ( $paramString != '' ) {
00255                                         $paramString .= ' ';
00256                                 }
00257                                 if ( is_array( $value ) ) {
00258                                         $value = "array(" . count( $value ) . ")";
00259                                 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
00260                                         $value = "object(" . get_class( $value ) . ")";
00261                                 }
00262                                 $value = (string)$value;
00263                                 if ( mb_strlen( $value ) > 1024 ) {
00264                                         $value = "string(" . mb_strlen( $value ) . ")";
00265                                 }
00266 
00267                                 $paramString .= "$key=$value";
00268                         }
00269                 }
00270 
00271                 if ( is_object( $this->title ) ) {
00272                         $s = "{$this->command} " . $this->title->getPrefixedDBkey();
00273                         if ( $paramString !== '' ) {
00274                                 $s .= ' ' . $paramString;
00275                         }
00276                         return $s;
00277                 } else {
00278                         return "{$this->command} $paramString";
00279                 }
00280         }
00281 
00282         protected function setLastError( $error ) {
00283                 $this->error = $error;
00284         }
00285 
00286         public function getLastError() {
00287                 return $this->error;
00288         }
00289 }