MediaWiki  REL1_24
Job.php
Go to the documentation of this file.
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         JobQueueGroup::singleton()->push( $jobs );
00096         return true;
00097     }
00098 
00110     public static function safeBatchInsert( $jobs ) {
00111         JobQueueGroup::singleton()->push( $jobs, JobQueue::QOS_ATOMIC );
00112         return true;
00113     }
00114 
00124     public static function pop_type( $type ) {
00125         return JobQueueGroup::singleton()->get( $type )->pop();
00126     }
00127 
00135     public static function pop() {
00136         return JobQueueGroup::singleton()->pop();
00137     }
00138 
00139     /*-------------------------------------------------------------------------
00140      * Non-static functions
00141      *------------------------------------------------------------------------*/
00142 
00148     public function __construct( $command, $title, $params = false ) {
00149         $this->command = $command;
00150         $this->title = $title;
00151         $this->params = $params;
00152 
00153         // expensive jobs may set this to true
00154         $this->removeDuplicates = false;
00155     }
00156 
00160     public function getType() {
00161         return $this->command;
00162     }
00163 
00167     public function getTitle() {
00168         return $this->title;
00169     }
00170 
00174     public function getParams() {
00175         return $this->params;
00176     }
00177 
00182     public function getReleaseTimestamp() {
00183         return isset( $this->params['jobReleaseTimestamp'] )
00184             ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
00185             : null;
00186     }
00187 
00191     public function ignoreDuplicates() {
00192         return $this->removeDuplicates;
00193     }
00194 
00199     public function allowRetries() {
00200         return true;
00201     }
00202 
00208     public function workItemCount() {
00209         return 1;
00210     }
00211 
00221     public function getDeduplicationInfo() {
00222         $info = array(
00223             'type' => $this->getType(),
00224             'namespace' => $this->getTitle()->getNamespace(),
00225             'title' => $this->getTitle()->getDBkey(),
00226             'params' => $this->getParams()
00227         );
00228         if ( is_array( $info['params'] ) ) {
00229             // Identical jobs with different "root" jobs should count as duplicates
00230             unset( $info['params']['rootJobSignature'] );
00231             unset( $info['params']['rootJobTimestamp'] );
00232             // Likewise for jobs with different delay times
00233             unset( $info['params']['jobReleaseTimestamp'] );
00234         }
00235 
00236         return $info;
00237     }
00238 
00247     public static function newRootJobParams( $key ) {
00248         return array(
00249             'rootJobSignature' => sha1( $key ),
00250             'rootJobTimestamp' => wfTimestampNow()
00251         );
00252     }
00253 
00259     public function getRootJobParams() {
00260         return array(
00261             'rootJobSignature' => isset( $this->params['rootJobSignature'] )
00262                 ? $this->params['rootJobSignature']
00263                 : null,
00264             'rootJobTimestamp' => isset( $this->params['rootJobTimestamp'] )
00265                 ? $this->params['rootJobTimestamp']
00266                 : null
00267         );
00268     }
00269 
00275     public function hasRootJobParams() {
00276         return isset( $this->params['rootJobSignature'] )
00277             && isset( $this->params['rootJobTimestamp'] );
00278     }
00279 
00285     public function insert() {
00286         JobQueueGroup::singleton()->push( $this );
00287         return true;
00288     }
00289 
00293     public function toString() {
00294         $truncFunc = function ( $value ) {
00295             $value = (string)$value;
00296             if ( mb_strlen( $value ) > 1024 ) {
00297                 $value = "string(" . mb_strlen( $value ) . ")";
00298             }
00299             return $value;
00300         };
00301 
00302         $paramString = '';
00303         if ( $this->params ) {
00304             foreach ( $this->params as $key => $value ) {
00305                 if ( $paramString != '' ) {
00306                     $paramString .= ' ';
00307                 }
00308                 if ( is_array( $value ) ) {
00309                     $filteredValue = array();
00310                     foreach ( $value as $k => $v ) {
00311                         if ( is_scalar( $v ) ) {
00312                             $filteredValue[$k] = $truncFunc( $v );
00313                         } else {
00314                             $filteredValue = null;
00315                             break;
00316                         }
00317                     }
00318                     if ( $filteredValue ) {
00319                         $value = FormatJson::encode( $filteredValue );
00320                     } else {
00321                         $value = "array(" . count( $value ) . ")";
00322                     }
00323                 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
00324                     $value = "object(" . get_class( $value ) . ")";
00325                 }
00326 
00327                 $paramString .= "$key={$truncFunc( $value )}";
00328             }
00329         }
00330 
00331         if ( is_object( $this->title ) ) {
00332             $s = "{$this->command} " . $this->title->getPrefixedDBkey();
00333             if ( $paramString !== '' ) {
00334                 $s .= ' ' . $paramString;
00335             }
00336 
00337             return $s;
00338         } else {
00339             return "{$this->command} $paramString";
00340         }
00341     }
00342 
00343     protected function setLastError( $error ) {
00344         $this->error = $error;
00345     }
00346 
00347     public function getLastError() {
00348         return $this->error;
00349     }
00350 }