1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Wikibase\Api\Service; |
4
|
|
|
|
5
|
|
|
use Mediawiki\DataModel\EditInfo; |
6
|
|
|
use UnexpectedValueException; |
7
|
|
|
use Wikibase\Api\WikibaseApi; |
8
|
|
|
use Wikibase\DataModel\Reference; |
9
|
|
|
use Wikibase\DataModel\Statement\Statement; |
10
|
|
|
use Wikibase\DataModel\Statement\StatementGuid; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @access private |
14
|
|
|
* |
15
|
|
|
* @author Addshore |
16
|
|
|
*/ |
17
|
|
|
class ReferenceRemover { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var WikibaseApi |
21
|
|
|
*/ |
22
|
|
|
private $api; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param WikibaseApi $api |
26
|
|
|
*/ |
27
|
|
|
public function __construct( WikibaseApi $api ) { |
28
|
|
|
$this->api = $api; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @since 0.2 |
33
|
|
|
* |
34
|
|
|
* @param Reference|string $reference Reference object or hash |
35
|
|
|
* @param Statement|StatementGuid|string $target Statement object or GUID |
36
|
|
|
* @param EditInfo|null $editInfo |
37
|
|
|
* |
38
|
|
|
* @throws UnexpectedValueException |
39
|
|
|
* @return bool |
40
|
|
|
*/ |
41
|
|
|
public function set( $reference, $target, EditInfo $editInfo = null ) { |
42
|
|
|
if ( $reference instanceof Reference ) { |
43
|
|
|
$reference = $reference->getHash(); |
44
|
|
|
} |
45
|
|
|
if ( !is_string( $reference ) ) { |
46
|
|
|
throw new UnexpectedValueException( 'Could not get reference hash from $reference' ); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
View Code Duplication |
if ( is_string( $target ) ) { |
|
|
|
|
50
|
|
|
$guid = $target; |
51
|
|
|
} elseif ( $target instanceof StatementGuid ) { |
52
|
|
|
$guid = $target->getSerialization(); |
|
|
|
|
53
|
|
|
} elseif ( $target instanceof Statement ) { |
54
|
|
|
$guid = $target->getGuid(); |
55
|
|
|
} else { |
56
|
|
|
throw new UnexpectedValueException( 'Could not get statement guid from $target' ); |
57
|
|
|
} |
58
|
|
|
if ( !is_string( $guid ) ) { |
59
|
|
|
throw new UnexpectedValueException( 'Unexpected statement guid got from $target' ); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
$params = [ |
63
|
|
|
'statement' => $guid, |
64
|
|
|
'references' => $reference, |
65
|
|
|
]; |
66
|
|
|
|
67
|
|
|
$this->api->postRequest( 'wbremovereferences', $params, $editInfo ); |
68
|
|
|
return true; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
} |
72
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.