| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\JobQueue; |
| 4 |
|
| 5 |
use Closure; |
| 6 |
use GenericParameterJob; |
| 7 |
use InvalidArgumentException; |
| 8 |
use Job; |
| 9 |
use MediaWiki\Page\PageReference; |
| 10 |
use MediaWiki\Title\Title; |
| 11 |
use Wikimedia\ObjectFactory\ObjectFactory; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
class JobFactory { |
| 17 |
|
| 18 |
private ObjectFactory $objectFactory; |
| 19 |
|
| 20 |
|
| 21 |
private array $jobObjectSpecs; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
public function __construct( ObjectFactory $objectFactory, array $jobObjectSpecs ) { |
| 28 |
$this->objectFactory = $objectFactory; |
| 29 |
$this->jobObjectSpecs = $jobObjectSpecs; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
public function newJob( string $command, $params = [] ): Job { |
| 52 |
if ( !isset( $this->jobObjectSpecs[ $command ] ) ) { |
| 53 |
throw new InvalidArgumentException( "Invalid job command '{$command}'" ); |
| 54 |
} |
| 55 |
|
| 56 |
$spec = $this->jobObjectSpecs[ $command ]; |
| 57 |
$needsTitle = $this->needsTitle( $command, $spec ); |
| 58 |
|
| 59 |
|
| 60 |
if ( $params instanceof PageReference ) { |
| 61 |
|
| 62 |
$title = Title::newFromPageReference( $params ); |
| 63 |
$params = func_num_args() >= 3 ? func_get_arg( 2 ) : []; |
| 64 |
} elseif ( isset( $params['namespace'] ) && isset( $params['title'] ) ) { |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
$title = Title::makeTitle( |
| 69 |
$params['namespace'], |
| 70 |
$params['title'] |
| 71 |
); |
| 72 |
} else { |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
$title = Title::makeTitle( |
| 78 |
NS_SPECIAL, |
| 79 |
'Blankpage' |
| 80 |
); |
| 81 |
} |
| 82 |
|
| 83 |
if ( $needsTitle ) { |
| 84 |
$args = [ $title, $params ]; |
| 85 |
} else { |
| 86 |
$args = [ $params ]; |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
$job = $this->objectFactory->createObject( |
| 91 |
$spec, |
| 92 |
[ |
| 93 |
'allowClassName' => true, |
| 94 |
'allowCallable' => true, |
| 95 |
'extraArgs' => $args, |
| 96 |
'assertClass' => Job::class |
| 97 |
] |
| 98 |
); |
| 99 |
|
| 100 |
|
| 101 |
$job->command = $command; |
| 102 |
return $job; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
private function needsTitle( string $command, $spec ): bool { |
| 115 |
if ( is_callable( $spec ) ) { |
| 116 |
$needsTitle = true; |
| 117 |
} elseif ( is_array( $spec ) ) { |
| 118 |
if ( isset( $spec['needsPage'] ) ) { |
| 119 |
$needsTitle = $spec['needsPage']; |
| 120 |
} elseif ( isset( $spec['class'] ) ) { |
| 121 |
$needsTitle = !is_subclass_of( $spec['class'], |
| 122 |
GenericParameterJob::class ); |
| 123 |
} elseif ( isset( $spec['factory'] ) ) { |
| 124 |
$needsTitle = true; |
| 125 |
} else { |
| 126 |
throw new InvalidArgumentException( |
| 127 |
"Invalid job specification for '{$command}': " . |
| 128 |
"must contain the 'class' or 'factory' key." |
| 129 |
); |
| 130 |
} |
| 131 |
} elseif ( is_string( $spec ) ) { |
| 132 |
$needsTitle = !is_subclass_of( $spec, |
| 133 |
GenericParameterJob::class ); |
| 134 |
} else { |
| 135 |
throw new InvalidArgumentException( |
| 136 |
"Invalid job specification for '{$command}': " . |
| 137 |
"must be a callable, an object spec array, or a class name" |
| 138 |
); |
| 139 |
} |
| 140 |
|
| 141 |
return $needsTitle; |
| 142 |
} |
| 143 |
} |