Completed
Push — master ( 97b5b5...7e7f32 )
by Sam
10s
created

PagePurger   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 101
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 6
dl 0
loc 101
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
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\SimpleRequest;
7
use Mediawiki\DataModel\Pages;
8
use Mediawiki\DataModel\Page;
9
10
/**
11
 * @access private
12
 *
13
 * @author Addshore
14
 * @author Thomas Arrow
15
 */
16
class PagePurger extends Service {
17
18
	/**
19
	 * @since 0.3
20
	 *
21
	 * @brief Purge a single page
22
	 *
23
	 * Purges a single page by submitting a
24
	 * 'purge' action to the mediawiki api
25
	 * with the parameter 'pageids' set to
26
	 * the singe page id
27
	 *
28
	 * @param Page $page the page that is going to be purged
29
	 *
30
	 * @return bool return true if the purge was successful
31
	 */
32
	public function purge( Page $page ) {
33
		$responseArray = $this->api->postRequest(
34
			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...
35
		);
36
37
		// the purge response for the page
38
		$purgeResponse = $responseArray['purge'][0];
39
40
		return array_key_exists( 'purged', $purgeResponse );
41
	}
42
43
	/**
44
	 * @since 0.7
45
	 *
46
	 * @brief Purge multiple pages
47
	 *
48
	 * Purges all the pages of the Pages object
49
	 * by submitting a 'purge' action to the mediawiki
50
	 * api with the parameter 'pageids' set to be the
51
	 * pages ids in multiple-value seperation.
52
	 *
53
	 * @param Pages $pages the pages that are going to be purged
54
	 *
55
	 * @return Pages the pages that have been purged successfully
56
	 */
57
	public function purgePages( Pages $pages ) {
58
		$pagesArray = $pages->toArray();
59
		$pagesIds = [];
60
61
		foreach ( $pagesArray as $page ) {
62
			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...
63
		}
64
65
		// convert an array to multiple-value format
66
		// because the mediawiki api require multiple
67
		// values to be seperated like the example
68
		// ex: [111, 222, 333] => "111|222|333"
69
		$pageIdsMultiple = implode( '|', $pagesIds );
70
71
		$responseArray = $this->api->postRequest(
72
			new SimpleRequest( 'purge', [ 'pageids' => $pageIdsMultiple ] )
73
		);
74
75
		// array that will hold the successfully purged pages
76
		$purgedPages = new Pages();
77
78
		// for every purge result
79
		foreach ( $responseArray['purge'] as $purgeResponse ) {
80
			// if the purge for the page was successful
81
			if ( array_key_exists( 'purged', $purgeResponse ) ) {
82
				// we iterate all the input pages
83
				foreach ( $pagesArray as $page ) {
84
					// and if the page from the input was successfully purged
85
					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...
86
						// add it in the purgedPages object
87
						$purgedPages->addPage( $page );
88
89
						break;
90
					}
91
92
				}
93
94
			}
95
96
		}
97
98
		return $purgedPages;
99
	}
100
101
	/**
102
	 * @since 0.6
103
	 *
104
	 * @param ApiGenerator $generator
105
	 *
106
	 * @return bool
107
	 */
108
	public function purgeGenerator( ApiGenerator $generator ) {
109
		$this->api->postRequest(
110
			new SimpleRequest( 'purge', $generator->getParams() )
111
		);
112
113
		return true;
114
	}
115
116
}
117