1 | <?php |
||
2 | /****************************************************************************** |
||
3 | * Wikipedia Account Creation Assistance tool * |
||
4 | * ACC Development Team. Please see team.json for a list of contributors. * |
||
5 | * * |
||
6 | * This is free and unencumbered software released into the public domain. * |
||
7 | * Please see LICENSE.md for the full licencing statement. * |
||
8 | ******************************************************************************/ |
||
9 | |||
10 | namespace Waca\Providers; |
||
11 | |||
12 | use Exception; |
||
13 | use Waca\DataObjects\AntiSpoofCache; |
||
14 | use Waca\DataObjects\Domain; |
||
15 | use Waca\Helpers\HttpHelper; |
||
16 | use Waca\PdoDatabase; |
||
17 | use Waca\Providers\Interfaces\IAntiSpoofProvider; |
||
18 | |||
19 | /** |
||
20 | * Cached API Antispoof Provider |
||
21 | * |
||
22 | * Provides a list of similar usernames from a MediaWiki API module, and caches |
||
23 | * it in the database. |
||
24 | */ |
||
25 | class CachedApiAntispoofProvider implements IAntiSpoofProvider |
||
26 | { |
||
27 | /** |
||
28 | * @var PdoDatabase |
||
29 | */ |
||
30 | private $database; |
||
31 | |||
32 | /** |
||
33 | * @var HttpHelper |
||
34 | */ |
||
35 | private $httpHelper; |
||
36 | |||
37 | public function __construct(PdoDatabase $database, HttpHelper $httpHelper) |
||
38 | { |
||
39 | $this->database = $database; |
||
40 | $this->httpHelper = $httpHelper; |
||
41 | } |
||
42 | |||
43 | public function getSpoofs($username) |
||
44 | { |
||
45 | // FIXME: domains! |
||
46 | /** @var Domain $domain */ |
||
47 | $domain = Domain::getById(1, $this->database); |
||
48 | |||
49 | /** @var AntiSpoofCache $cacheResult */ |
||
50 | $cacheResult = AntiSpoofCache::getByUsername($username, $this->database); |
||
51 | if ($cacheResult == false) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
52 | // get the data from the API |
||
53 | $data = $this->httpHelper->get($domain->getWikiApiPath(), array( |
||
54 | 'action' => 'antispoof', |
||
55 | 'format' => 'php', |
||
56 | 'username' => $username, |
||
57 | )); |
||
58 | |||
59 | $cacheEntry = new AntiSpoofCache(); |
||
60 | $cacheEntry->setDatabase($this->database); |
||
61 | $cacheEntry->setUsername($username); |
||
62 | $cacheEntry->setData($data); |
||
63 | $cacheEntry->save(); |
||
64 | |||
65 | $cacheResult = $cacheEntry; |
||
66 | } |
||
67 | else { |
||
68 | $data = $cacheResult->getData(); |
||
69 | } |
||
70 | |||
71 | $result = unserialize($data); |
||
72 | |||
73 | if (!isset($result['antispoof']) || !isset($result['antispoof']['result'])) { |
||
74 | $cacheResult->delete(); |
||
75 | |||
76 | if (isset($result['error']['info'])) { |
||
77 | throw new Exception("Unrecognised API response to query: " . $result['error']['info']); |
||
78 | } |
||
79 | |||
80 | throw new Exception("Unrecognised API response to query."); |
||
81 | } |
||
82 | |||
83 | if ($result['antispoof']['result'] == "pass") { |
||
84 | // All good here! |
||
85 | return array(); |
||
86 | } |
||
87 | |||
88 | if ($result['antispoof']['result'] == "conflict") { |
||
89 | // we've got conflicts, let's do something with them. |
||
90 | return $result['antispoof']['users']; |
||
91 | } |
||
92 | |||
93 | if ($result['antispoof']['result'] == "error") { |
||
94 | // we've got conflicts, let's do something with them. |
||
95 | throw new Exception("Encountered error while getting result: " . $result['antispoof']['error']); |
||
96 | } |
||
97 | |||
98 | throw new Exception("Unrecognised API response to query."); |
||
99 | } |
||
100 | } |
||
101 |