Merge "Remove useless PHP version comparison from wfDebugBacktrace()"
[lhc/web/wiklou.git] / includes / jobqueue / Job.php
index b971bd5..37e3d0f 100644 (file)
@@ -36,7 +36,7 @@ abstract class Job implements IJobSpecification {
        public $params;
 
        /** @var array Additional queue metadata */
-       public $metadata = array();
+       public $metadata = [];
 
        /** @var Title */
        protected $title;
@@ -62,14 +62,19 @@ abstract class Job implements IJobSpecification {
         * @throws MWException
         * @return Job
         */
-       public static function factory( $command, Title $title, $params = array() ) {
+       public static function factory( $command, Title $title, $params = [] ) {
                global $wgJobClasses;
+
                if ( isset( $wgJobClasses[$command] ) ) {
                        $class = $wgJobClasses[$command];
 
-                       return new $class( $title, $params );
+                       $job = new $class( $title, $params );
+                       $job->command = $command;
+
+                       return $job;
                }
-               throw new MWException( "Invalid job command `{$command}`" );
+
+               throw new InvalidArgumentException( "Invalid job command '{$command}'" );
        }
 
        /**
@@ -80,7 +85,7 @@ abstract class Job implements IJobSpecification {
        public function __construct( $command, $title, $params = false ) {
                $this->command = $command;
                $this->title = $title;
-               $this->params = is_array( $params ) ? $params : array(); // sanity
+               $this->params = is_array( $params ) ? $params : []; // sanity
 
                // expensive jobs may set this to true
                $this->removeDuplicates = false;
@@ -144,6 +149,14 @@ abstract class Job implements IJobSpecification {
                        : null;
        }
 
+       /**
+        * @return int|null UNIX timestamp of when the job was runnable, or null
+        * @since 1.26
+        */
+       public function getReadyTimestamp() {
+               return $this->getReleaseTimestamp() ?: $this->getQueuedTimestamp();
+       }
+
        /**
         * Whether the queue should reject insertion of this job if a duplicate exists
         *
@@ -186,12 +199,12 @@ abstract class Job implements IJobSpecification {
         * @since 1.21
         */
        public function getDeduplicationInfo() {
-               $info = array(
+               $info = [
                        'type' => $this->getType(),
                        'namespace' => $this->getTitle()->getNamespace(),
                        'title' => $this->getTitle()->getDBkey(),
                        'params' => $this->getParams()
-               );
+               ];
                if ( is_array( $info['params'] ) ) {
                        // Identical jobs with different "root" jobs should count as duplicates
                        unset( $info['params']['rootJobSignature'] );
@@ -206,18 +219,30 @@ abstract class Job implements IJobSpecification {
        }
 
        /**
+        * Get "root job" parameters for a task
+        *
+        * This is used to no-op redundant jobs, including child jobs of jobs,
+        * as long as the children inherit the root job parameters. When a job
+        * with root job parameters and "rootJobIsSelf" set is pushed, the
+        * deduplicateRootJob() method is automatically called on it. If the
+        * root job is only virtual and not actually pushed (e.g. the sub-jobs
+        * are inserted directly), then call deduplicateRootJob() directly.
+        *
         * @see JobQueue::deduplicateRootJob()
+        *
         * @param string $key A key that identifies the task
         * @return array Map of:
+        *   - rootJobIsSelf    : true
         *   - rootJobSignature : hash (e.g. SHA1) that identifies the task
         *   - rootJobTimestamp : TS_MW timestamp of this instance of the task
         * @since 1.21
         */
        public static function newRootJobParams( $key ) {
-               return array(
+               return [
+                       'rootJobIsSelf'    => true,
                        'rootJobSignature' => sha1( $key ),
                        'rootJobTimestamp' => wfTimestampNow()
-               );
+               ];
        }
 
        /**
@@ -226,14 +251,14 @@ abstract class Job implements IJobSpecification {
         * @since 1.21
         */
        public function getRootJobParams() {
-               return array(
+               return [
                        'rootJobSignature' => isset( $this->params['rootJobSignature'] )
                                ? $this->params['rootJobSignature']
                                : null,
                        'rootJobTimestamp' => isset( $this->params['rootJobTimestamp'] )
                                ? $this->params['rootJobTimestamp']
                                : null
-               );
+               ];
        }
 
        /**
@@ -246,6 +271,14 @@ abstract class Job implements IJobSpecification {
                        && isset( $this->params['rootJobTimestamp'] );
        }
 
+       /**
+        * @see JobQueue::deduplicateRootJob()
+        * @return bool Whether this is job is a root job
+        */
+       public function isRootJob() {
+               return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
+       }
+
        /**
         * Insert a single job into the queue.
         * @return bool True on success
@@ -275,7 +308,7 @@ abstract class Job implements IJobSpecification {
                                        $paramString .= ' ';
                                }
                                if ( is_array( $value ) ) {
-                                       $filteredValue = array();
+                                       $filteredValue = [];
                                        foreach ( $value as $k => $v ) {
                                                if ( is_scalar( $v ) ) {
                                                        $filteredValue[$k] = $truncFunc( $v );