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\Helpers; |
||
11 | |||
12 | use Waca\DataObjects\Domain; |
||
13 | use Waca\Exceptions\ApplicationLogicException; |
||
14 | use Waca\Exceptions\CurlException; |
||
15 | use Waca\Exceptions\MediaWikiApiException; |
||
16 | use Waca\Helpers\Interfaces\IMediaWikiClient; |
||
17 | use Waca\SiteConfiguration; |
||
18 | |||
19 | class BotMediaWikiClient implements IMediaWikiClient |
||
20 | { |
||
21 | /** |
||
22 | * @var HttpHelper |
||
23 | */ |
||
24 | private $httpHelper; |
||
25 | /** @var string */ |
||
26 | private $mediawikiWebServiceEndpoint; |
||
27 | /** @var string */ |
||
28 | private $creationBotUsername; |
||
29 | /** @var string */ |
||
30 | private $creationBotPassword; |
||
31 | /** @var bool */ |
||
32 | private $knownLoggedIn = false; |
||
33 | |||
34 | /** |
||
35 | * BotMediaWikiClient constructor. |
||
36 | * |
||
37 | * @param SiteConfiguration $siteConfiguration |
||
38 | * @param Domain $domain |
||
39 | */ |
||
40 | public function __construct(SiteConfiguration $siteConfiguration, Domain $domain) |
||
41 | { |
||
42 | $this->mediawikiWebServiceEndpoint = $domain->getWikiApiPath(); |
||
43 | |||
44 | $this->creationBotUsername = $siteConfiguration->getCreationBotUsername(); |
||
45 | $this->creationBotPassword = $siteConfiguration->getCreationBotPassword(); |
||
46 | |||
47 | $this->httpHelper = new HttpHelper( |
||
48 | $siteConfiguration, |
||
49 | $siteConfiguration->getCurlCookieJar() |
||
50 | ); |
||
51 | } |
||
52 | |||
53 | public function doApiCall($apiParams, $method = 'GET') |
||
54 | { |
||
55 | $this->ensureLoggedIn(); |
||
56 | $apiParams['assert'] = 'user'; |
||
57 | |||
58 | return $this->callApi($apiParams, $method); |
||
59 | } |
||
60 | |||
61 | private function ensureLoggedIn() |
||
62 | { |
||
63 | if ($this->knownLoggedIn) { |
||
64 | return; |
||
65 | } |
||
66 | |||
67 | $userinfoResult = $this->callApi(array('action' => 'query', 'meta' => 'userinfo'), 'GET'); |
||
68 | if (isset($userinfoResult->query->userinfo->anon)) { |
||
69 | // not logged in. |
||
70 | $this->logIn(); |
||
71 | |||
72 | // retest |
||
73 | $userinfoResult = $this->callApi(array('action' => 'query', 'meta' => 'userinfo'), 'GET'); |
||
74 | if (isset($userinfoResult->query->userinfo->anon)) { |
||
75 | throw new MediaWikiApiException('Unable to log in.'); |
||
76 | } |
||
77 | else { |
||
78 | $this->knownLoggedIn = true; |
||
79 | } |
||
80 | } |
||
81 | else { |
||
82 | $this->knownLoggedIn = true; |
||
83 | } |
||
84 | } |
||
85 | |||
86 | /** |
||
87 | * @param $apiParams |
||
88 | * @param $method |
||
89 | * |
||
90 | * @return mixed |
||
91 | * @throws ApplicationLogicException |
||
92 | * @throws CurlException |
||
93 | */ |
||
94 | private function callApi($apiParams, $method) |
||
95 | { |
||
96 | $apiParams['format'] = 'json'; |
||
97 | |||
98 | if ($method == 'GET') { |
||
99 | $data = $this->httpHelper->get($this->mediawikiWebServiceEndpoint, $apiParams); |
||
100 | } |
||
101 | elseif ($method == 'POST') { |
||
102 | $data = $this->httpHelper->post($this->mediawikiWebServiceEndpoint, $apiParams); |
||
103 | } |
||
104 | else { |
||
105 | throw new ApplicationLogicException('Unsupported HTTP Method'); |
||
106 | } |
||
107 | |||
108 | if ($data === false) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
109 | throw new CurlException('Curl error: ' . $this->httpHelper->getError()); |
||
110 | } |
||
111 | |||
112 | $result = json_decode($data); |
||
113 | |||
114 | return $result; |
||
115 | } |
||
116 | |||
117 | private function logIn() |
||
118 | { |
||
119 | // get token |
||
120 | $tokenParams = array( |
||
121 | 'action' => 'query', |
||
122 | 'meta' => 'tokens', |
||
123 | 'type' => 'login', |
||
124 | ); |
||
125 | |||
126 | $response = $this->callApi($tokenParams, 'POST'); |
||
127 | |||
128 | if (isset($response->error)) { |
||
129 | throw new MediaWikiApiException($response->error->code . ': ' . $response->error->info); |
||
130 | } |
||
131 | |||
132 | $token = $response->query->tokens->logintoken; |
||
133 | |||
134 | if ($token === null) { |
||
135 | throw new MediaWikiApiException('Edit token could not be acquired'); |
||
136 | } |
||
137 | |||
138 | $params = array( |
||
139 | 'action' => 'login', |
||
140 | 'lgname' => $this->creationBotUsername, |
||
141 | 'lgpassword' => $this->creationBotPassword, |
||
142 | 'lgtoken' => $token, |
||
143 | ); |
||
144 | |||
145 | $loginResponse = $this->callApi($params, 'POST'); |
||
146 | |||
147 | if ($loginResponse->login->result == 'Success') { |
||
148 | return; |
||
149 | } |
||
150 | |||
151 | throw new ApplicationLogicException(json_encode($loginResponse)); |
||
152 | } |
||
153 | } |
||
154 |