Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
48.44% covered (danger)
48.44%
31 / 64
33.33% covered (danger)
33.33%
4 / 12
CRAP
0.00% covered (danger)
0.00%
0 / 1
JobSpecification
49.21% covered (danger)
49.21%
31 / 63
33.33% covered (danger)
33.33%
4 / 12
99.48
0.00% covered (danger)
0.00%
0 / 1
 __construct
68.75% covered (warning)
68.75%
11 / 16
0.00% covered (danger)
0.00%
0 / 1
3.27
 validateParams
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
5.20
 getType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getParams
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getReleaseTimestamp
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
6
 ignoreDuplicates
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDeduplicationInfo
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
1 / 1
4
 getRootJobParams
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 hasRootJobParams
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
6
 isRootJob
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
6
 toSerializableArray
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 newFromArray
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21namespace MediaWiki\JobQueue;
22
23use MediaWiki\Http\Telemetry;
24use MediaWiki\Page\PageReference;
25use MediaWiki\Page\PageReferenceValue;
26use UnexpectedValueException;
27
28/**
29 * Job queue task description base code.
30 *
31 * Example usage:
32 * @code
33 * $job = new JobSpecification(
34 *        'null',
35 *        [ 'lives' => 1, 'usleep' => 100, 'pi' => 3.141569 ],
36 *        [ 'removeDuplicates' => 1 ]
37 * );
38 * MediaWikiServices::getInstance()->getJobQueueGroup()->push( $job )
39 * @endcode
40 *
41 * @newable
42 * @since 1.23
43 * @ingroup JobQueue
44 */
45class JobSpecification implements IJobSpecification {
46    /** @var string */
47    protected $type;
48
49    /** @var array Array of job parameters or false if none */
50    protected $params;
51
52    /** @var PageReference */
53    protected $page;
54
55    /** @var array */
56    protected $opts;
57
58    /**
59     * @param string $type
60     * @param array $params Map of key/values
61     *   'requestId' - The request ID, as obtained from {@link Telemetry::getRequestId}. If not set,
62     *   the value will be populated from the current instance of {@link Telemetry}.
63     * @param array $opts Map of key/values
64     *   'removeDuplicates' key - whether to remove duplicate jobs
65     *   'removeDuplicatesIgnoreParams' key - array with parameters to ignore for deduplication
66     * @param PageReference|null $page
67     */
68    public function __construct(
69        $type, array $params, array $opts = [], ?PageReference $page = null
70    ) {
71        $params += [
72            'requestId' => Telemetry::getInstance()->getRequestId(),
73        ];
74        $this->validateParams( $params );
75        $this->validateParams( $opts );
76
77        $this->type = $type;
78        if ( $page ) {
79            // Make sure JobQueue classes can pull the title from parameters alone
80            if ( $page->getDBkey() !== '' ) {
81                $params += [
82                    'namespace' => $page->getNamespace(),
83                    'title' => $page->getDBkey()
84                ];
85            }
86        } else {
87            // We aim to remove the page from job specification and all we need
88            // is namespace/dbkey, so use LOCAL no matter what.
89            $page = PageReferenceValue::localReference( NS_SPECIAL, 'Badtitle/' . __CLASS__ );
90        }
91        $this->params = $params;
92        $this->page = $page;
93        $this->opts = $opts;
94    }
95
96    protected function validateParams( array $params ) {
97        foreach ( $params as $p => $v ) {
98            if ( is_array( $v ) ) {
99                $this->validateParams( $v );
100            } elseif ( !is_scalar( $v ) && $v !== null ) {
101                throw new UnexpectedValueException( "Job parameter $p is not JSON serializable." );
102            }
103        }
104    }
105
106    public function getType() {
107        return $this->type;
108    }
109
110    public function getParams() {
111        return $this->params;
112    }
113
114    public function getReleaseTimestamp() {
115        return isset( $this->params['jobReleaseTimestamp'] )
116            ? wfTimestampOrNull( TS_UNIX, $this->params['jobReleaseTimestamp'] )
117            : null;
118    }
119
120    public function ignoreDuplicates() {
121        return !empty( $this->opts['removeDuplicates'] );
122    }
123
124    public function getDeduplicationInfo() {
125        $info = [
126            'type' => $this->getType(),
127            'params' => $this->getParams()
128        ];
129        if ( is_array( $info['params'] ) ) {
130            // Identical jobs with different "root" jobs should count as duplicates
131            unset( $info['params']['rootJobSignature'] );
132            unset( $info['params']['rootJobTimestamp'] );
133            // Likewise for jobs with different delay times
134            unset( $info['params']['jobReleaseTimestamp'] );
135            // Identical jobs from different requests should count as duplicates
136            unset( $info['params']['requestId'] );
137            if ( isset( $this->opts['removeDuplicatesIgnoreParams'] ) ) {
138                foreach ( $this->opts['removeDuplicatesIgnoreParams'] as $field ) {
139                    unset( $info['params'][$field] );
140                }
141            }
142        }
143
144        return $info;
145    }
146
147    public function getRootJobParams() {
148        return [
149            'rootJobSignature' => $this->params['rootJobSignature'] ?? null,
150            'rootJobTimestamp' => $this->params['rootJobTimestamp'] ?? null
151        ];
152    }
153
154    public function hasRootJobParams() {
155        return isset( $this->params['rootJobSignature'] )
156            && isset( $this->params['rootJobTimestamp'] );
157    }
158
159    public function isRootJob() {
160        return $this->hasRootJobParams() && !empty( $this->params['rootJobIsSelf'] );
161    }
162
163    /**
164     * @deprecated since 1.41
165     * @return array Field/value map that can immediately be serialized
166     * @since 1.25
167     */
168    public function toSerializableArray() {
169        wfDeprecated( __METHOD__, '1.41' );
170        return [
171            'type'   => $this->type,
172            'params' => $this->params,
173            'opts'   => $this->opts,
174            'title'  => [
175                'ns'  => $this->page->getNamespace(),
176                'key' => $this->page->getDBkey()
177            ]
178        ];
179    }
180
181    /**
182     * @param array $map Field/value map
183     * @return JobSpecification
184     * @since 1.25
185     */
186    public static function newFromArray( array $map ) {
187        return new self(
188            $map['type'],
189            $map['params'],
190            $map['opts'],
191            PageReferenceValue::localReference( $map['title']['ns'], $map['title']['key'] )
192        );
193    }
194}
195
196/** @deprecated class alias since 1.44 */
197class_alias( JobSpecification::class, 'JobSpecification' );