Merge "Add tags for undo edits"
[lhc/web/wiklou.git] / includes / jobqueue / jobs / EnqueueJob.php
1 <?php
2 /**
3 * Router job that takes jobs and enqueues them.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup JobQueue
22 */
23
24 /**
25 * Router job that takes jobs and enqueues them to their proper queues
26 *
27 * This can be used for getting sets of multiple jobs or sets of jobs intended for multiple
28 * queues to be inserted more robustly. This is a single job that, upon running, enqueues the
29 * wrapped jobs. If some of those fail to enqueue then the EnqueueJob will be retried. Due to
30 * the possibility of duplicate enqueues, the wrapped jobs should be idempotent.
31 *
32 * @ingroup JobQueue
33 * @since 1.25
34 */
35 final class EnqueueJob extends Job {
36 /**
37 * Callers should use the factory methods instead
38 *
39 * @param Title $title
40 * @param array $params Job parameters
41 */
42 function __construct( Title $title, array $params ) {
43 parent::__construct( 'enqueue', $title, $params );
44 }
45
46 /**
47 * @param JobSpecification|JobSpecification[] $jobs
48 * @return EnqueueJob
49 */
50 public static function newFromLocalJobs( $jobs ) {
51 $jobs = is_array( $jobs ) ? $jobs : [ $jobs ];
52
53 return self::newFromJobsByWiki( [ wfWikiID() => $jobs ] );
54 }
55
56 /**
57 * @param array $jobsByWiki Map of (wiki => JobSpecification list)
58 * @return EnqueueJob
59 */
60 public static function newFromJobsByWiki( array $jobsByWiki ) {
61 $deduplicate = true;
62
63 $jobMapsByWiki = [];
64 foreach ( $jobsByWiki as $wiki => $jobs ) {
65 $jobMapsByWiki[$wiki] = [];
66 foreach ( $jobs as $job ) {
67 if ( $job instanceof JobSpecification ) {
68 $jobMapsByWiki[$wiki][] = $job->toSerializableArray();
69 } else {
70 throw new InvalidArgumentException( "Jobs must be of type JobSpecification." );
71 }
72 $deduplicate = $deduplicate && $job->ignoreDuplicates();
73 }
74 }
75
76 $eJob = new self(
77 Title::makeTitle( NS_SPECIAL, 'Badtitle/' . __CLASS__ ),
78 [ 'jobsByWiki' => $jobMapsByWiki ]
79 );
80 // If *all* jobs to be pushed are to be de-duplicated (a common case), then
81 // de-duplicate this whole job itself to avoid build up in high traffic cases
82 $eJob->removeDuplicates = $deduplicate;
83
84 return $eJob;
85 }
86
87 public function run() {
88 foreach ( $this->params['jobsByWiki'] as $wiki => $jobMaps ) {
89 $jobSpecs = [];
90 foreach ( $jobMaps as $jobMap ) {
91 $jobSpecs[] = JobSpecification::newFromArray( $jobMap );
92 }
93 JobQueueGroup::singleton( $wiki )->push( $jobSpecs );
94 }
95
96 return true;
97 }
98 }