PagePurger   A
last analyzed

Complexity

Total Complexity 9

Size/Duplication

Total Lines 107
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 0%

Importance

Changes 0
Metric Value
wmc 9
lcom 1
cbo 7
dl 0
loc 107
ccs 0
cts 36
cp 0
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A purge() 0 16 2
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\Page;
8
use Mediawiki\DataModel\Pages;
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
	 * 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
		if ( $page->getPageIdentifier()->getId() ) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $page->getPageIdentifier()->getId() of type integer|null is loosely compared to true; this is ambiguous if the integer can be zero. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
34
			$params = [ 'pageids' => $page->getPageIdentifier()->getId() ];
35
		} else {
36
			$params = [ 'titles' => $page->getPageIdentifier()->getTitle()->getText() ];
37
		}
38
39
		$responseArray = $this->api->postRequest(
40
			new SimpleRequest( 'purge', $params )
41
		);
42
43
		// the purge response for the page
44
		$purgeResponse = $responseArray['purge'][0];
45
46
		return array_key_exists( 'purged', $purgeResponse );
47
	}
48
49
	/**
50
	 * @since 0.7
51
	 *
52
	 * Purge multiple pages
53
	 *
54
	 * Purges all the pages of the Pages object
55
	 * by submitting a 'purge' action to the mediawiki
56
	 * api with the parameter 'pageids' set to be the
57
	 * pages ids in multiple-value seperation.
58
	 *
59
	 * @param Pages $pages the pages that are going to be purged
60
	 *
61
	 * @return Pages the pages that have been purged successfully
62
	 */
63
	public function purgePages( Pages $pages ) {
64
		$pagesArray = $pages->toArray();
65
		$pagesIds = [];
66
67
		foreach ( $pagesArray as $page ) {
68
			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...
69
		}
70
71
		// convert an array to multiple-value format
72
		// because the mediawiki api require multiple
73
		// values to be seperated like the example
74
		// ex: [111, 222, 333] => "111|222|333"
75
		$pageIdsMultiple = implode( '|', $pagesIds );
76
77
		$responseArray = $this->api->postRequest(
78
			new SimpleRequest( 'purge', [ 'pageids' => $pageIdsMultiple ] )
79
		);
80
81
		// array that will hold the successfully purged pages
82
		$purgedPages = new Pages();
83
84
		// for every purge result
85
		foreach ( $responseArray['purge'] as $purgeResponse ) {
86
			// if the purge for the page was successful
87
			if ( array_key_exists( 'purged', $purgeResponse ) ) {
88
				// we iterate all the input pages
89
				foreach ( $pagesArray as $page ) {
90
					// and if the page from the input was successfully purged
91
					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...
92
						// add it in the purgedPages object
93
						$purgedPages->addPage( $page );
94
95
						break;
96
					}
97
98
				}
99
100
			}
101
102
		}
103
104
		return $purgedPages;
105
	}
106
107
	/**
108
	 * @since 0.6
109
	 *
110
	 * @param ApiGenerator $generator
111
	 *
112
	 * @return bool
113
	 */
114
	public function purgeGenerator( ApiGenerator $generator ) {
115
		$this->api->postRequest(
116
			new SimpleRequest( 'purge', $generator->getParams() )
117
		);
118
119
		return true;
120
	}
121
122
}
123