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