MediaWiki  master
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 
00042         /*-------------------------------------------------------------------------
00043          * Abstract functions
00044          *------------------------------------------------------------------------*/
00045 
00050         abstract public function run();
00051 
00052         /*-------------------------------------------------------------------------
00053          * Static functions
00054          *------------------------------------------------------------------------*/
00055 
00066         public static function factory( $command, Title $title, $params = false, $id = 0 ) {
00067                 global $wgJobClasses;
00068                 if( isset( $wgJobClasses[$command] ) ) {
00069                         $class = $wgJobClasses[$command];
00070                         return new $class( $title, $params, $id );
00071                 }
00072                 throw new MWException( "Invalid job command `{$command}`" );
00073         }
00074 
00085         public static function batchInsert( $jobs ) {
00086                 return JobQueueGroup::singleton()->push( $jobs );
00087         }
00088 
00099         public static function safeBatchInsert( $jobs ) {
00100                 return JobQueueGroup::singleton()->push( $jobs, JobQueue::QoS_Atomic );
00101         }
00102 
00112         public static function pop_type( $type ) {
00113                 return JobQueueGroup::singleton()->get( $type )->pop();
00114         }
00115 
00123         public static function pop() {
00124                 return JobQueueGroup::singleton()->pop();
00125         }
00126 
00127         /*-------------------------------------------------------------------------
00128          * Non-static functions
00129          *------------------------------------------------------------------------*/
00130 
00137         public function __construct( $command, $title, $params = false, $id = 0 ) {
00138                 $this->command = $command;
00139                 $this->title = $title;
00140                 $this->params = $params;
00141                 $this->id = $id;
00142 
00143                 $this->removeDuplicates = false; // expensive jobs may set this to true
00144         }
00145 
00149         public function getId() {
00150                 return $this->id;
00151         }
00152 
00156         public function getType() {
00157                 return $this->command;
00158         }
00159 
00163         public function getTitle() {
00164                 return $this->title;
00165         }
00166 
00170         public function getParams() {
00171                 return $this->params;
00172         }
00173 
00177         public function ignoreDuplicates() {
00178                 return $this->removeDuplicates;
00179         }
00180 
00186         public function insert() {
00187                 return JobQueueGroup::singleton()->push( $this );
00188         }
00189 
00193         public function toString() {
00194                 $paramString = '';
00195                 if ( $this->params ) {
00196                         foreach ( $this->params as $key => $value ) {
00197                                 if ( $paramString != '' ) {
00198                                         $paramString .= ' ';
00199                                 }
00200                                 $paramString .= "$key=$value";
00201                         }
00202                 }
00203 
00204                 if ( is_object( $this->title ) ) {
00205                         $s = "{$this->command} " . $this->title->getPrefixedDBkey();
00206                         if ( $paramString !== '' ) {
00207                                 $s .= ' ' . $paramString;
00208                         }
00209                         return $s;
00210                 } else {
00211                         return "{$this->command} $paramString";
00212                 }
00213         }
00214 
00215         protected function setLastError( $error ) {
00216                 $this->error = $error;
00217         }
00218 
00219         public function getLastError() {
00220                 return $this->error;
00221         }
00222 }