MediaWiki  REL1_22
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 
00155     public function getId() {
00156         return $this->id;
00157     }
00158 
00162     public function getType() {
00163         return $this->command;
00164     }
00165 
00169     public function getTitle() {
00170         return $this->title;
00171     }
00172 
00176     public function getParams() {
00177         return $this->params;
00178     }
00179 
00184     public function getReleaseTimestamp() {
00185         return isset( $this->params['jobReleaseTimestamp'] )
00186             ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
00187             : null;
00188     }
00189 
00193     public function ignoreDuplicates() {
00194         return $this->removeDuplicates;
00195     }
00196 
00201     public function allowRetries() {
00202         return true;
00203     }
00204 
00214     public function getDeduplicationInfo() {
00215         $info = array(
00216             'type' => $this->getType(),
00217             'namespace' => $this->getTitle()->getNamespace(),
00218             'title' => $this->getTitle()->getDBkey(),
00219             'params' => $this->getParams()
00220         );
00221         if ( is_array( $info['params'] ) ) {
00222             // Identical jobs with different "root" jobs should count as duplicates
00223             unset( $info['params']['rootJobSignature'] );
00224             unset( $info['params']['rootJobTimestamp'] );
00225             // Likewise for jobs with different delay times
00226             unset( $info['params']['jobReleaseTimestamp'] );
00227         }
00228         return $info;
00229     }
00230 
00237     public static function newRootJobParams( $key ) {
00238         return array(
00239             'rootJobSignature' => sha1( $key ),
00240             'rootJobTimestamp' => wfTimestampNow()
00241         );
00242     }
00243 
00249     public function getRootJobParams() {
00250         return array(
00251             'rootJobSignature' => isset( $this->params['rootJobSignature'] )
00252                 ? $this->params['rootJobSignature']
00253                 : null,
00254             'rootJobTimestamp' => isset( $this->params['rootJobTimestamp'] )
00255                 ? $this->params['rootJobTimestamp']
00256                 : null
00257         );
00258     }
00259 
00265     public function hasRootJobParams() {
00266         return isset( $this->params['rootJobSignature'] )
00267             && isset( $this->params['rootJobTimestamp'] );
00268     }
00269 
00275     public function insert() {
00276         return JobQueueGroup::singleton()->push( $this );
00277     }
00278 
00282     public function toString() {
00283         $paramString = '';
00284         if ( $this->params ) {
00285             foreach ( $this->params as $key => $value ) {
00286                 if ( $paramString != '' ) {
00287                     $paramString .= ' ';
00288                 }
00289                 if ( is_array( $value ) ) {
00290                     $value = "array(" . count( $value ) . ")";
00291                 } elseif ( is_object( $value ) && !method_exists( $value, '__toString' ) ) {
00292                     $value = "object(" . get_class( $value ) . ")";
00293                 }
00294                 $value = (string)$value;
00295                 if ( mb_strlen( $value ) > 1024 ) {
00296                     $value = "string(" . mb_strlen( $value ) . ")";
00297                 }
00298 
00299                 $paramString .= "$key=$value";
00300             }
00301         }
00302 
00303         if ( is_object( $this->title ) ) {
00304             $s = "{$this->command} " . $this->title->getPrefixedDBkey();
00305             if ( $paramString !== '' ) {
00306                 $s .= ' ' . $paramString;
00307             }
00308             return $s;
00309         } else {
00310             return "{$this->command} $paramString";
00311         }
00312     }
00313 
00314     protected function setLastError( $error ) {
00315         $this->error = $error;
00316     }
00317 
00318     public function getLastError() {
00319         return $this->error;
00320     }
00321 }