Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F5973210
JobFactory.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
3 KB
Referenced Files
None
Subscribers
None
JobFactory.php
View Options
<?php
namespace
MediaWiki\JobQueue
;
use
Closure
;
use
GenericParameterJob
;
use
InvalidArgumentException
;
use
Job
;
use
MediaWiki\Page\PageReference
;
use
MediaWiki\Title\Title
;
use
Wikimedia\ObjectFactory\ObjectFactory
;
/**
* @since 1.40
*/
class
JobFactory
{
private
ObjectFactory
$objectFactory
;
/** @var array<array|callable|string> Object specs, see ObjectFactory */
private
array
$jobObjectSpecs
;
/**
* @param ObjectFactory $objectFactory
* @param array<array|callable|string> $jobObjectSpecs Object specs, see ObjectFactory
*/
public
function
__construct
(
ObjectFactory
$objectFactory
,
array
$jobObjectSpecs
)
{
$this
->
objectFactory
=
$objectFactory
;
$this
->
jobObjectSpecs
=
$jobObjectSpecs
;
}
/**
* Create the appropriate object to handle a specific job.
*
* @note For backwards compatibility with Job::factory,
* this method also supports an alternative signature:
* @code
* newJob(
* string $command,
* PageReference $page,
* array $params
* )
* @endcode
*
* @param string $command Job command
* @param array $params Job parameters
*
* @return Job
* @throws InvalidArgumentException
*/
public
function
newJob
(
string
$command
,
$params
=
[]
):
Job
{
if
(
!
isset
(
$this
->
jobObjectSpecs
[
$command
]
)
)
{
throw
new
InvalidArgumentException
(
"Invalid job command '{$command}'"
);
}
$spec
=
$this
->
jobObjectSpecs
[
$command
];
$needsTitle
=
$this
->
needsTitle
(
$command
,
$spec
);
// TODO: revisit support for old method signature
if
(
$params
instanceof
PageReference
)
{
// Backwards compatibility for old signature ($command, $title, $params)
$title
=
Title
::
newFromPageReference
(
$params
);
$params
=
func_num_args
()
>=
3
?
func_get_arg
(
2
)
:
[];
}
elseif
(
isset
(
$params
[
'namespace'
]
)
&&
isset
(
$params
[
'title'
]
)
)
{
// Handle job classes that take title as constructor parameter.
// If a newer classes like GenericParameterJob uses these parameters,
// then this happens in Job::__construct instead.
$title
=
Title
::
makeTitle
(
$params
[
'namespace'
],
$params
[
'title'
]
);
}
else
{
// Default title for job classes not implementing GenericParameterJob.
// This must be a valid title because it not directly passed to
// our Job constructor, but rather its subclasses which may expect
// to be able to use it.
$title
=
Title
::
makeTitle
(
NS_SPECIAL
,
'Blankpage'
);
}
if
(
$needsTitle
)
{
$args
=
[
$title
,
$params
];
}
else
{
$args
=
[
$params
];
}
/** @var Job $job */
$job
=
$this
->
objectFactory
->
createObject
(
$spec
,
[
'allowClassName'
=>
true
,
'allowCallable'
=>
true
,
'extraArgs'
=>
$args
,
'assertClass'
=>
Job
::
class
]
);
// TODO: create a setter, marked @internal
$job
->
command
=
$command
;
return
$job
;
}
/**
* Determines whether the job class needs a Title to be passed
* as the first parameter to the constructor.
*
* @param string $command
* @param string|array|Closure $spec
*
* @return bool
*/
private
function
needsTitle
(
string
$command
,
$spec
):
bool
{
if
(
is_callable
(
$spec
)
)
{
$needsTitle
=
true
;
}
elseif
(
is_array
(
$spec
)
)
{
if
(
isset
(
$spec
[
'needsPage'
]
)
)
{
$needsTitle
=
$spec
[
'needsPage'
];
}
elseif
(
isset
(
$spec
[
'class'
]
)
)
{
$needsTitle
=
!
is_subclass_of
(
$spec
[
'class'
],
GenericParameterJob
::
class
);
}
elseif
(
isset
(
$spec
[
'factory'
]
)
)
{
$needsTitle
=
true
;
}
else
{
throw
new
InvalidArgumentException
(
"Invalid job specification for '{$command}': "
.
"must contain the 'class' or 'factory' key."
);
}
}
elseif
(
is_string
(
$spec
)
)
{
$needsTitle
=
!
is_subclass_of
(
$spec
,
GenericParameterJob
::
class
);
}
else
{
throw
new
InvalidArgumentException
(
"Invalid job specification for '{$command}': "
.
"must be a callable, an object spec array, or a class name"
);
}
return
$needsTitle
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, Oct 3, 16:27 (12 h, 49 m ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
44/69/430fc490d0a18239549de6f541e9
Default Alt Text
JobFactory.php (3 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment