Completed
Push — master ( ec2f81...11966d )
by adam
03:28
created

PagePurger   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 113
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Test Coverage

Coverage 85.71%

Importance

Changes 7
Bugs 0 Features 1
Metric Value
wmc 9
c 7
b 0
f 1
lcom 1
cbo 5
dl 0
loc 113
ccs 30
cts 35
cp 0.8571
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A purge() 0 10 1
B purgePages() 0 43 6
A purgeGenerator() 0 7 1
1
<?php
2
3
namespace Mediawiki\Api\Service;
4
5
use Mediawiki\Api\Generator\ApiGenerator;
6
use Mediawiki\Api\MediawikiApi;
7
use Mediawiki\Api\SimpleRequest;
8
use Mediawiki\DataModel\Pages;
9
use Mediawiki\DataModel\Page;
10
11
/**
12
 * @access private
13
 *
14
 * @author Addshore
15
 * @author Thomas Arrow
16
 */
17
class PagePurger {
18
19
	/**
20
	 * @var MediawikiApi
21
	 */
22
	private $api;
23
24
	/**
25
	 * @param MediawikiApi $api
26
	 */
27 5
	public function __construct( MediawikiApi $api ) {
28 5
		$this->api = $api;
29 5
	}
30
31
	/**
32
	 * @since 0.3
33
	 *
34
	 * @brief Purge a single page
35
	 *
36
	 * Purges a single page by submitting a
37
	 * 'purge' action to the mediawiki api
38
	 * with the parameter 'pageids' set to
39
	 * the singe page id
40
	 *
41
	 * @param Page $page the page that is going to be purged
42
	 *
43
	 * @return bool return true if the purge was successful
44
	 */
45 2
	public function purge( Page $page ) {
46 2
		$responseArray = $this->api->postRequest(
47 2
			new SimpleRequest( 'purge', [ 'pageids' => $page->getId() ] )
0 ignored issues
show
Deprecated Code introduced by
The method Mediawiki\DataModel\Page::getId() has been deprecated with message: since 0.5

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
48 2
		);
49
50
		// the purge response for the page
51 2
		$purgeResponse = $responseArray['purge'][0];
52
53 2
		return array_key_exists( 'purged', $purgeResponse );
54
	}
55
56
	/**
57
	 * @since 0.7
58
	 *
59
	 * @brief Purge multiple pages
60
	 *
61
	 * Purges all the pages of the Pages object
62
	 * by submitting a 'purge' action to the mediawiki
63
	 * api with the parameter 'pageids' set to be the
64
	 * pages ids in multiple-value seperation.
65
	 *
66
	 * @param Pages $pages the pages that are going to be purged
67
	 *
68
	 * @return Pages the pages that have been purged successfully
69
	 */
70 2
	public function purgePages( Pages $pages ) {
71 2
		$pagesArray = $pages->toArray();
72 2
		$pagesIds = [];
73
74 2
		foreach ( $pagesArray as $page ) {
75 2
			array_push( $pagesIds, $page->getId() );
0 ignored issues
show
Deprecated Code introduced by
The method Mediawiki\DataModel\Page::getId() has been deprecated with message: since 0.5

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
76 2
		}
77
78
		// convert an array to multiple-value format
79
		// because the mediawiki api require multiple
80
		// values to be seperated like the example
81
		// ex: [111, 222, 333] => "111|222|333"
82 2
		$pageIdsMultiple = implode( '|', $pagesIds );
83
84 2
		$responseArray = $this->api->postRequest(
85 2
			new SimpleRequest( 'purge', [ 'pageids' => $pageIdsMultiple ] )
86 2
		);
87
88
		// array that will hold the successfully purged pages
89 2
		$purgedPages = new Pages();
90
91
		// for every purge result
92 2
		foreach ( $responseArray['purge'] as $purgeResponse ) {
93
			// if the purge for the page was successful
94 2
			if ( array_key_exists( 'purged', $purgeResponse ) ) {
95
				// we iterate all the input pages
96 2
				foreach ( $pagesArray as $page ) {
97
					// and if the page from the input was successfully purged
98 2
					if ( $purgeResponse['title'] === $page->getTitle()->getText() ) {
0 ignored issues
show
Deprecated Code introduced by
The method Mediawiki\DataModel\Page::getTitle() has been deprecated with message: since 0.5

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
99
						// add it in the purgedPages object
100 2
						$purgedPages->addPage( $page );
101
102 2
						break;
103
					}
104
105 2
				}
106
107 2
			}
108
109 2
		}
110
111 2
		return $purgedPages;
112
	}
113
114
	/**
115
	 * @since 0.6
116
	 *
117
	 * @param ApiGenerator $generator
118
	 *
119
	 * @return bool
120
	 */
121
	public function purgeGenerator( ApiGenerator $generator ) {
122
		$this->api->postRequest(
123
			new SimpleRequest( 'purge', $generator->getParams() )
124
		);
125
126
		return true;
127
	}
128
129
}
130