1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mediawiki\Api\Service; |
4
|
|
|
|
5
|
|
|
use Mediawiki\Api\MediawikiApi; |
6
|
|
|
use Mediawiki\Api\SimpleRequest; |
7
|
|
|
use Mediawiki\DataModel\Page; |
8
|
|
|
use Mediawiki\DataModel\PageIdentifier; |
9
|
|
|
use Mediawiki\DataModel\Revision; |
10
|
|
|
use Mediawiki\DataModel\Title; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @access private |
14
|
|
|
* |
15
|
|
|
* @author Addshore |
16
|
|
|
*/ |
17
|
|
|
class PageDeleter { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var MediawikiApi |
21
|
|
|
*/ |
22
|
|
|
private $api; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param MediawikiApi $api |
26
|
|
|
*/ |
27
|
|
|
public function __construct( MediawikiApi $api ) { |
28
|
|
|
$this->api = $api; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @since 0.2 |
33
|
|
|
* |
34
|
|
|
* @param Page $page |
35
|
|
|
* @param array $extraParams |
36
|
|
|
* |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
|
|
public function delete( Page $page, array $extraParams = [] ) { |
40
|
|
|
$this->api->postRequest( new SimpleRequest( |
41
|
|
|
'delete', |
42
|
|
|
$this->getDeleteParams( $page->getPageIdentifier(), $extraParams ) |
43
|
|
|
) ); |
44
|
|
|
return true; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @since 0.2 |
49
|
|
|
* |
50
|
|
|
* @param Revision $revision |
51
|
|
|
* @param array $extraParams |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
|
|
public function deleteFromRevision( Revision $revision, array $extraParams = [] ) { |
56
|
|
|
$this->api->postRequest( new SimpleRequest( |
57
|
|
|
'delete', |
58
|
|
|
$this->getDeleteParams( $revision->getPageIdentifier(), $extraParams ) |
|
|
|
|
59
|
|
|
) ); |
60
|
|
|
return true; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @since 0.2 |
65
|
|
|
* |
66
|
|
|
* @param int $pageid |
67
|
|
|
* @param array $extraParams |
68
|
|
|
* |
69
|
|
|
* @return bool |
70
|
|
|
*/ |
71
|
|
|
public function deleteFromPageId( $pageid, array $extraParams = [] ) { |
72
|
|
|
$this->api->postRequest( new SimpleRequest( |
73
|
|
|
'delete', |
74
|
|
|
$this->getDeleteParams( new PageIdentifier( null, $pageid ), $extraParams ) |
75
|
|
|
) ); |
76
|
|
|
return true; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @since 0.5 |
81
|
|
|
* |
82
|
|
|
* @param Title|string $title |
83
|
|
|
* @param array $extraParams |
84
|
|
|
* |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
|
|
public function deleteFromPageTitle( $title, array $extraParams = [] ) { |
88
|
|
|
if ( is_string( $title ) ) { |
89
|
|
|
$title = new Title( $title ); |
90
|
|
|
} |
91
|
|
|
$this->api->postRequest( new SimpleRequest( |
92
|
|
|
'delete', |
93
|
|
|
$this->getDeleteParams( new PageIdentifier( $title ), $extraParams ) |
94
|
|
|
) ); |
95
|
|
|
return true; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @param PageIdentifier $identifier |
100
|
|
|
* @param array $extraParams |
101
|
|
|
* |
102
|
|
|
* @return array |
103
|
|
|
*/ |
104
|
|
|
private function getDeleteParams( PageIdentifier $identifier, $extraParams ) { |
105
|
|
|
$params = []; |
106
|
|
|
|
107
|
|
|
if ( !is_null( $identifier->getId() ) ) { |
108
|
|
|
$params['pageid'] = $identifier->getId(); |
109
|
|
|
} else { |
110
|
|
|
$params['title'] = $identifier->getTitle()->getTitle(); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$params['token'] = $this->api->getToken( 'delete' ); |
114
|
|
|
|
115
|
|
|
return array_merge( $extraParams, $params ); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
} |
119
|
|
|
|
Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code: