1
|
|
|
<?php |
2
|
|
|
/****************************************************************************** |
3
|
|
|
* Wikipedia Account Creation Assistance tool * |
4
|
|
|
* * |
5
|
|
|
* All code in this file is released into the public domain by the ACC * |
6
|
|
|
* Development Team. Please see team.json for a list of contributors. * |
7
|
|
|
******************************************************************************/ |
8
|
|
|
|
9
|
|
|
namespace Waca\Helpers; |
10
|
|
|
|
11
|
|
|
use Waca\Exceptions\CurlException; |
12
|
|
|
use Waca\Helpers\Interfaces\IBlacklistHelper; |
13
|
|
|
|
14
|
|
|
class BlacklistHelper implements IBlacklistHelper |
15
|
|
|
{ |
16
|
|
|
/** @var HttpHelper */ |
17
|
|
|
private $httpHelper; |
18
|
|
|
/** |
19
|
|
|
* Cache of previously requested usernames |
20
|
|
|
* @var array |
21
|
|
|
*/ |
22
|
|
|
private $cache = array(); |
23
|
|
|
/** @var string */ |
24
|
|
|
private $mediawikiWebServiceEndpoint; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* BlacklistHelper constructor. |
28
|
|
|
* |
29
|
|
|
* @param HttpHelper $httpHelper |
30
|
|
|
* @param string $mediawikiWebServiceEndpoint |
31
|
|
|
*/ |
32
|
|
|
public function __construct(HttpHelper $httpHelper, $mediawikiWebServiceEndpoint) |
33
|
|
|
{ |
34
|
|
|
$this->httpHelper = $httpHelper; |
35
|
|
|
$this->mediawikiWebServiceEndpoint = $mediawikiWebServiceEndpoint; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Returns a value indicating whether the provided username is blacklisted by the on-wiki title blacklist |
40
|
|
|
* |
41
|
|
|
* @param string $username |
42
|
|
|
* |
43
|
|
|
* @return false|string False if the username is not blacklisted, else the blacklist entry. |
44
|
|
|
*/ |
45
|
|
|
public function isBlacklisted($username) |
46
|
|
|
{ |
47
|
|
View Code Duplication |
if (isset($this->cache[$username])) { |
|
|
|
|
48
|
|
|
$result = $this->cache[$username]; |
49
|
|
|
if ($result === false) { |
50
|
|
|
return false; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
return $result['line']; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
try { |
57
|
|
|
$result = $this->performWikiLookup($username); |
58
|
|
|
} |
59
|
|
|
catch (CurlException $ex) { |
60
|
|
|
// LOGME log this, but fail gracefully. |
61
|
|
|
return false; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
View Code Duplication |
if ($result['result'] === 'ok') { |
|
|
|
|
65
|
|
|
// not blacklisted |
66
|
|
|
$this->cache[$username] = false; |
67
|
|
|
|
68
|
|
|
return false; |
69
|
|
|
} |
70
|
|
|
else { |
71
|
|
|
$this->cache[$username] = $result; |
72
|
|
|
|
73
|
|
|
return $result['line']; |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Performs a fetch to MediaWiki for the relevant title blacklist entry |
79
|
|
|
* |
80
|
|
|
* @param string $username The username to look up |
81
|
|
|
* |
82
|
|
|
* @return array |
83
|
|
|
* @throws CurlException |
84
|
|
|
*/ |
85
|
|
|
private function performWikiLookup($username) |
86
|
|
|
{ |
87
|
|
|
$endpoint = $this->mediawikiWebServiceEndpoint; |
88
|
|
|
|
89
|
|
|
$parameters = array( |
90
|
|
|
'action' => 'titleblacklist', |
91
|
|
|
'format' => 'php', |
92
|
|
|
'tbtitle' => $username, |
93
|
|
|
'tbaction' => 'new-account', |
94
|
|
|
'tbnooverride' => true, |
95
|
|
|
); |
96
|
|
|
|
97
|
|
|
$apiResult = $this->httpHelper->get($endpoint, $parameters); |
98
|
|
|
|
99
|
|
|
$data = unserialize($apiResult); |
100
|
|
|
|
101
|
|
|
return $data['titleblacklist']; |
102
|
|
|
} |
103
|
|
|
} |
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.