MediaWiki master
JobFactory.php
Go to the documentation of this file.
1<?php
2
3namespace MediaWiki\JobQueue;
4
5use Closure;
6use InvalidArgumentException;
9use Wikimedia\ObjectFactory\ObjectFactory;
10
15
16 private ObjectFactory $objectFactory;
17
19 private array $jobObjectSpecs;
20
25 public function __construct( ObjectFactory $objectFactory, array $jobObjectSpecs ) {
26 $this->objectFactory = $objectFactory;
27 $this->jobObjectSpecs = $jobObjectSpecs;
28 }
29
49 public function newJob( string $command, $params = [] ): Job {
50 if ( !isset( $this->jobObjectSpecs[ $command ] ) ) {
51 throw new InvalidArgumentException( "Invalid job command '{$command}'" );
52 }
53
54 $spec = $this->jobObjectSpecs[ $command ];
55 $needsTitle = $this->needsTitle( $command, $spec );
56
57 // TODO: revisit support for old method signature
58 if ( $params instanceof PageReference ) {
59 // Backwards compatibility for old signature ($command, $title, $params)
60 $title = Title::newFromPageReference( $params );
61 $params = func_num_args() >= 3 ? func_get_arg( 2 ) : [];
62 } elseif ( isset( $params['namespace'] ) && isset( $params['title'] ) ) {
63 // Handle job classes that take title as constructor parameter.
64 // If a newer classes like GenericParameterJob uses these parameters,
65 // then this happens in Job::__construct instead.
66 $title = Title::makeTitle(
67 $params['namespace'],
68 $params['title']
69 );
70 } else {
71 // Default title for job classes not implementing GenericParameterJob.
72 // This must be a valid title because it not directly passed to
73 // our Job constructor, but rather its subclasses which may expect
74 // to be able to use it.
75 $title = Title::makeTitle(
77 'Blankpage'
78 );
79 }
80
81 if ( $needsTitle ) {
82 $args = [ $title, $params ];
83 } else {
84 $args = [ $params ];
85 }
86
88 $job = $this->objectFactory->createObject(
89 $spec,
90 [
91 'allowClassName' => true,
92 'allowCallable' => true,
93 'extraArgs' => $args,
94 'assertClass' => Job::class
95 ]
96 );
97
98 // TODO: create a setter, marked @internal
99 $job->command = $command;
100 return $job;
101 }
102
112 private function needsTitle( string $command, $spec ): bool {
113 if ( is_callable( $spec ) ) {
114 $needsTitle = true;
115 } elseif ( is_array( $spec ) ) {
116 if ( isset( $spec['needsPage'] ) ) {
117 $needsTitle = $spec['needsPage'];
118 } elseif ( isset( $spec['class'] ) ) {
119 $needsTitle = !is_subclass_of( $spec['class'],
120 GenericParameterJob::class );
121 } elseif ( isset( $spec['factory'] ) ) {
122 $needsTitle = true;
123 } else {
124 throw new InvalidArgumentException(
125 "Invalid job specification for '{$command}': " .
126 "must contain the 'class' or 'factory' key."
127 );
128 }
129 } elseif ( is_string( $spec ) ) {
130 $needsTitle = !is_subclass_of( $spec,
131 GenericParameterJob::class );
132 } else {
133 throw new InvalidArgumentException(
134 "Invalid job specification for '{$command}': " .
135 "must be a callable, an object spec array, or a class name"
136 );
137 }
138
139 return $needsTitle;
140 }
141}
const NS_SPECIAL
Definition Defines.php:54
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:82
newJob(string $command, $params=[])
Create the appropriate object to handle a specific job.
__construct(ObjectFactory $objectFactory, array $jobObjectSpecs)
Describe and execute a background job.
Definition Job.php:41
Represents a title within MediaWiki.
Definition Title.php:78
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
array $params
The job parameters.
if(count( $args)< 1) $job