-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathMediawikiInit.php
More file actions
66 lines (53 loc) · 2.03 KB
/
MediawikiInit.php
File metadata and controls
66 lines (53 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
<?php
namespace App\Jobs;
use App\Http\Curl\HttpRequest;
use App\Services\MediaWikiHostResolver;
class MediawikiInit extends Job {
private $wikiDomain;
private $username;
private $email;
/**
* @return void
*/
public function __construct($wikiDomain, $username, $email) {
$this->wikiDomain = $wikiDomain;
$this->username = $username;
$this->email = $email;
}
/**
* @return void
*/
public function handle(HttpRequest $request, MediaWikiHostResolver $mwHostResolver) {
$data = [
'username' => $this->username,
'email' => $this->email,
];
$request->setOptions([
CURLOPT_URL => $mwHostResolver->getBackendHostForDomain($this->wikiDomain) . '/w/api.php?action=wbstackInit&format=json',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_TIMEOUT => 60,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => http_build_query($data),
CURLOPT_HTTPHEADER => [
'content-type: application/x-www-form-urlencoded',
'host: ' . $this->wikiDomain,
],
]);
$rawResponse = $request->execute();
$err = $request->error();
$request->close();
if ($err) {
throw new \RuntimeException('curl error for ' . $this->wikiDomain . ': ' . $err);
}
$response = json_decode($rawResponse, true);
if (!is_array($response) || !array_key_exists('wbstackInit', $response)) {
throw new \RuntimeException('wbstackInit call for ' . $this->wikiDomain . '. No wbstackInit key in response: ' . $rawResponse);
}
if ($response['wbstackInit']['success'] == 0) {
throw new \RuntimeException('wbstackInit call for ' . $this->wikiDomain . ' was not successful:' . $rawResponse);
}
// Otherwise there was success (and we could get the userId if we wanted...
}
}