-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathWikiController.php
More file actions
228 lines (187 loc) · 8.23 KB
/
WikiController.php
File metadata and controls
228 lines (187 loc) · 8.23 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
<?php
namespace App\Http\Controllers;
use App\Helper\DomainHelper;
use App\Helper\DomainValidator;
use App\Helper\ProfileValidator;
use App\Jobs\ElasticSearchAliasInit;
use App\Jobs\KubernetesIngressCreate;
use App\Jobs\MediawikiInit;
use App\Jobs\ProvisionQueryserviceNamespaceJob;
use App\Jobs\ProvisionWikiDbJob;
use App\QueryserviceNamespace;
use App\Wiki;
use App\WikiDb;
use App\WikiDomain;
use App\WikiManager;
use App\WikiProfile;
use App\WikiSetting;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
class WikiController extends Controller {
private $domainValidator;
private $profileValidator;
public function __construct(DomainValidator $domainValidator, ProfileValidator $profileValidator) {
$this->profileValidator = $profileValidator;
$this->domainValidator = $domainValidator;
}
public function create(Request $request): \Illuminate\Http\Response {
$sharedIndexPrefix = Config::get('wbstack.elasticsearch_shared_index_prefix');
$esHosts = Config::get('wbstack.elasticsearch_hosts');
$isSearchConfigValid = $esHosts && $sharedIndexPrefix;
if (Config::get('wbstack.elasticsearch_enabled_by_default') && !$isSearchConfigValid) {
abort(503, 'Search enabled, but its configuration is invalid');
}
$user = $request->user();
$submittedDomain = strtolower($request->input('domain'));
$submittedDomain = DomainHelper::encode($submittedDomain);
$domainValidator = $this->domainValidator->validate($submittedDomain);
$isSubdomain = $this->isSubDomain($submittedDomain);
$domainValidator->validateWithBag('post');
// TODO extra validation that username is correct?
$request->validate([
'sitename' => 'required|min:3',
'username' => 'required',
'profile' => 'nullable|json',
]);
$rawProfile = false;
if ($request->filled('profile')) {
$rawProfile = json_decode($request->input('profile'), true);
$profileValidator = $this->profileValidator->validate($rawProfile);
$profileValidator->validateWithBag('post');
}
$wiki = null;
$dbAssignment = null;
// TODO create with some sort of owner etc?
DB::transaction(function () use ($user, $request, &$wiki, &$dbAssignment, $submittedDomain, $rawProfile) {
$dbVersion = Config::get('wbstack.wiki_db_use_version');
$wikiDbCondition = ['wiki_id' => null, 'version' => $dbVersion];
// Fail if there is not enough storage ready
if (WikiDb::where($wikiDbCondition)->count() == 0) {
abort(503, 'No databases ready');
}
if (QueryserviceNamespace::where('wiki_id', null)->count() == 0) {
abort(503, 'No query namespaces ready');
}
$numWikis = $user->managesWikis()->count() + 1;
$maxWikis = config('wbstack.wiki_max_per_user');
if (config('wbstack.wiki_max_per_user') !== false && $numWikis > config('wbstack.wiki_max_per_user')) {
abort(403, "Too many wikis. Your new total of {$numWikis} would exceed the limit of {$maxWikis} per user.");
}
$wiki = Wiki::create([
'sitename' => $request->input('sitename'),
'domain' => $submittedDomain,
]);
// Assign storage
$dbAssignment = DB::table('wiki_dbs')->where($wikiDbCondition)->limit(1)->update(['wiki_id' => $wiki->id]);
if (!$dbAssignment) {
abort(503, 'Database ready, but failed to assign');
}
$nsAssignment = DB::table('queryservice_namespaces')->where(['wiki_id' => null])->limit(1)->update(['wiki_id' => $wiki->id]);
if (!$nsAssignment) {
abort(503, 'QS Namespace ready, but failed to assign');
}
// Create keys for OAuth2
// T336937
$keyPair = openssl_pkey_new([
'private_key_bits' => 2048,
'private_key_type' => OPENSSL_KEYTYPE_RSA,
]);
// Extract private key
openssl_pkey_export($keyPair, $wgOAuth2PrivateKey);
// Extract pub key
$keyDetails = openssl_pkey_get_details($keyPair);
$wgOAuth2PublicKey = $keyDetails['key'];
WikiSetting::create([
'wiki_id' => $wiki->id,
'name' => WikiSetting::wgOAuth2PrivateKey,
'value' => $wgOAuth2PrivateKey,
]);
WikiSetting::create([
'wiki_id' => $wiki->id,
'name' => WikiSetting::wgOAuth2PublicKey,
'value' => $wgOAuth2PublicKey,
]);
// Create initial needed non default settings
// Docs: https://www.mediawiki.org/wiki/Manual:$wgSecretKey
WikiSetting::create([
'wiki_id' => $wiki->id,
'name' => WikiSetting::wgSecretKey,
'value' => Str::random(64),
]);
// Create the elasticsearch setting
// T314937
WikiSetting::create([
'wiki_id' => $wiki->id,
'name' => WikiSetting::wwExtEnableElasticSearch,
'value' => Config::get('wbstack.elasticsearch_enabled_by_default'),
]);
// Also track the domain forever in your domains table
$wikiDomain = WikiDomain::create([
'domain' => $wiki->domain,
'wiki_id' => $wiki->id,
]);
$ownerAssignment = WikiManager::create([
'user_id' => $user->id,
'wiki_id' => $wiki->id,
]);
// Create WikiProfile
if ($rawProfile) {
WikiProfile::create(['wiki_id' => $wiki->id, ...$rawProfile]);
}
});
// TODO maybe always make these run in a certain order..?
dispatch(new MediawikiInit($wiki->domain, $request->input('username'), $user->email));
// Only dispatch a job to add a k8s ingress IF we are using a custom domain...
if (!$isSubdomain) {
dispatch(new KubernetesIngressCreate($wiki->id, $wiki->domain));
}
// dispatch elasticsearch init job to enable the feature
if (Config::get('wbstack.elasticsearch_enabled_by_default') && $isSearchConfigValid) {
foreach ($esHosts as $esHost) {
dispatch(new ElasticSearchAliasInit($wiki->id, $esHost));
}
}
// opportunistic dispatching of jobs to make sure storage pools are topped up
dispatch(new ProvisionWikiDbJob(null, null, 10));
dispatch(new ProvisionQueryserviceNamespaceJob(null, 10));
$res['success'] = true;
$res['message'] = 'Success!';
$res['data'] = [
'id' => $wiki->id,
'name' => $wiki->name,
'domain' => $wiki->domain,
];
return response($res);
}
public function delete(Request $request): \Illuminate\Http\JsonResponse {
$wikiDeletionReason = $request->input('deletionReasons');
$wiki = $request->attributes->get('wiki');
if (isset($wikiDeletionReason)) {
// Save the wiki deletion reason
$wiki->update(['wiki_deletion_reason' => $wikiDeletionReason]);
}
// Delete the wiki
$wiki->delete();
// Return a success
return response()->json('Success', 200);
}
// TODO should this just be get wiki?
public function getWikiDetailsForIdForOwner(Request $request): \Illuminate\Http\Response {
$wiki = $request->attributes->get('wiki')
->load('wikiManagers')
->load('wikiDbVersion')
->load('wikiLatestProfile')
->load('publicSettings');
$res = [
'success' => true,
'data' => $wiki,
];
return response($res);
}
public static function isSubDomain(string $domain, ?string $subDomainSuffix = null): bool {
$subDomainSuffix = $subDomainSuffix ?? Config::get('wbstack.subdomain_suffix');
return preg_match('/' . preg_quote($subDomainSuffix) . '$/', $domain) === 1;
}
}