forked from miraheze/CreateWiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpecialCreateWiki.php
288 lines (231 loc) · 8.29 KB
/
SpecialCreateWiki.php
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
<?php
class SpecialCreateWiki extends FormSpecialPage {
function __construct() {
parent::__construct( 'CreateWiki', 'createwiki' );
}
protected function getFormFields() {
global $wgCreateWikiUseCategories, $wgCreateWikiCategories;
$par = $this->par;
$request = $this->getRequest();
$formDescriptor = array();
$formDescriptor['dbname'] = array(
'label-message' => 'createwiki-label-dbname',
'type' => 'text',
'default' => $request->getVal( 'cwDBname' ) ? $request->getVal( 'cwDBname' ) : $par,
'size' => 20,
'required' => true,
'validation-callback' => array( __CLASS__, 'validateDBname' ),
'name' => 'cwDBname',
);
$formDescriptor['requester'] = array(
'label-message' => 'createwiki-label-requester',
'type' => 'text',
'default' => $request->getVal( 'cwRequester' ),
'size' => 20,
'required' => true,
'validation-callback' => array( __CLASS__, 'validateRequester' ),
'name' => 'cwRequester',
);
$formDescriptor['sitename'] = array(
'label-message' => 'createwiki-label-sitename',
'type' => 'text',
'default' => $request->getVal( 'cwSitename' ),
'size' => 20,
'name' => 'cwSitename',
);
// Building a language selector (attribution:
// includes/specials/SpecialPageLanguage.php L68)
$languages = Language::fetchLanguageNames( null, 'wmfile' );
ksort( $languages );
$options = array();
foreach ( $languages as $code => $name ) {
$options["$code - $name"] = $code;
}
$formDescriptor['language'] = array(
'type' => 'select',
'options' => $options,
'label-message' => 'createwiki-label-language',
'default' => $request->getVal( 'cwLanguage' ) ? $request->getVal( 'cwLanguage' ) : 'en',
'name' => 'cwLanguage',
);
$formDescriptor['private'] = array(
'type' => 'check',
'label-message' => 'createwiki-label-private',
'name' => 'cwPrivate',
);
if ( $wgCreateWikiUseCategories && $wgCreateWikiCategories ) {
$formDescriptor['category'] = array(
'type' => 'select',
'label-message' => 'createwiki-label-category',
'options' => $wgCreateWikiCategories,
'name' => 'cwCategory',
'default' => 'uncategorised',
);
}
$formDescriptor['reason'] = array(
'label-message' => 'createwiki-label-reason',
'type' => 'text',
'default' => $request->getVal( 'wpreason' ),
'size' => 45,
'required' => true,
);
return $formDescriptor;
}
public function onSubmit( array $formData ) {
global $IP, $wgCreateWikiSQLfiles, $wgDBname, $wgCreateWikiUseCategories;
$DBname = $formData['dbname'];
$requesterName = $formData['requester'];
$siteName = $formData['sitename'];
$language = $formData['language'];
$private = $formData['private'];
$reason = $formData['reason'];
if ( $wgCreateWikiUseCategories ) {
$category = $formData['category'];
} else {
$category = 'uncategorised';
}
$farmerLogEntry = new ManualLogEntry( 'farmer', 'createwiki' );
$farmerLogEntry->setPerformer( $this->getUser() );
$farmerLogEntry->setTarget( $this->getTitle() );
$farmerLogEntry->setComment( $reason );
$farmerLogEntry->setParameters(
array(
'4::wiki' => $DBname
)
);
$farmerLogID = $farmerLogEntry->insert();
$farmerLogEntry->publish( $farmerLogID );
$dbw = wfGetDB( DB_MASTER );
$dbw->query( 'SET storage_engine=InnoDB;' );
$dbw->query( 'CREATE DATABASE ' . $dbw->addIdentifierQuotes( $DBname ) . ';' );
$this->addWikiToDatabase( $DBname, $siteName, $language, $private, $category );
// Let's ensure our wiki is in the DBlist on the server
// we run the maintenance scripts on.
exec( "/usr/bin/php /srv/mediawiki/w/extensions/CreateWiki/DBListGenerator.php --wiki metawiki" );
$dbw->selectDB( $DBname );
foreach ( $wgCreateWikiSQLfiles as $sqlfile ) {
$dbw->sourceFile( $sqlfile );
}
$this->createMainPage( $language );
$dbw->selectDB( $wgDBname ); // revert back to main wiki
$shcreateaccount = exec( "/usr/bin/php " .
"$IP/extensions/CentralAuth/maintenance/createLocalAccount.php " . wfEscapeShellArg( $requesterName ) . " --wiki " . wfEscapeShellArg( $DBname ) );
if ( !strpos( $shcreateaccount, 'created' ) ) {
wfDebugLog( 'CreateWiki', 'Failed to create local account for requester. - error: ' . $shcreateaccount );
return wfMessage( 'createwiki-error-usernotcreated' )->escaped();
}
$shpromoteaccount = exec( "/usr/bin/php " .
"$IP/maintenance/createAndPromote.php " . wfEscapeShellArg( $requesterName ) . " --bureaucrat --sysop --force --wiki " . wfEscapeShellArg( $DBname ) );
if( $this->getUser()->getName() != $requesterName ) {
$notifyEmail = MailAddress::newFromUser( User::newFromName( $requesterName ) );
$this->sendCreationEmail( $notifyEmail, $siteName );
}
$this->getOutput()->addHTML( '<div class="successbox">' . wfMessage( 'createwiki-success' )->escaped() . '</div>' );
return true;
}
public function validateDBname( $DBname, $allData ) {
global $wgConf;
# HTMLForm's validation-callback somehow gets called, even
# while the form was not submitted yet. This should prevent
# the validation from failing because the submitted value is
# NULL, but it is a hack, and instead the validation just
# shouldn't be called unless the form actually has been
# submitted..
if ( is_null( $DBname ) ) {
return true;
}
$suffixed = false;
foreach ( $wgConf->suffixes as $suffix ) {
if ( substr( $DBname, -strlen( $suffix ) ) === $suffix ) {
$suffixed = true;
break;
}
}
$dbw = wfGetDB( DB_MASTER );
$res = $dbw->query( 'SHOW DATABASES LIKE ' . $dbw->addQuotes( $DBname ) . ';' );
if ( $res->numRows() !== 0 ) {
return wfMessage( 'createwiki-error-dbexists' )->escaped();
}
if ( !$suffixed ) {
return wfMessage( 'createwiki-error-notsuffixed' )->escaped();
}
if ( !ctype_alnum( $DBname ) ) {
return wfMessage( 'createwiki-error-notalnum' )->escaped();
}
if ( strtolower( $DBname ) !== $DBname ) {
return wfMessage( 'createwiki-error-notlowercase' )->escaped();
}
return true;
}
public function validateRequester( $requesterName, $allData ) {
# HTMLForm's validation-callback somehow gets called, even
# while the form was not submitted yet. This should prevent
# the validation from failing because the submitted value is
# NULL, but it is a hack, and instead the validation just
# shouldn't be called unless the form actually has been
# submitted..
if ( is_null( $requesterName ) ) {
return true;
}
$user = User::newFromName( $requesterName );
if ( !$user->getId() ) {
return wfMessage( 'createwiki-error-requesternonexistent' )->escaped();
}
return true;
}
public function addWikiToDatabase( $DBname, $siteName, $language, $private, $category ) {
$dbw = wfGetDB( DB_MASTER );
if ( $private ) {
$private = 1;
} else {
$private = 0;
}
$dbw->insert(
'cw_wikis',
array(
'wiki_dbname' => $DBname,
'wiki_sitename' => $siteName,
'wiki_language' => $language,
'wiki_private' => $private,
'wiki_closed' => 0,
'wiki_inactive' => 0,
'wiki_category' => $category,
),
__METHOD__
);
return true;
}
public function createMainPage( $language ) {
// Don't use Meta's mainpage message!
if ( $language !== 'en' ) {
$page = wfMessage( 'mainpage' )->inLanguage( $language )->plain();
} else {
$page = 'Main_Page';
}
$title = Title::newFromText( $page );
$article = WikiPage::factory( $title );
$article->doEditContent(
new WikitextContent(
wfMessage( 'createwiki-defaultmainpage' )->inLanguage( $language )->plain()
), // Text
'Create main page', // Edit summary
EDIT_SUPPRESS_RC, // Flags
false, // I have no idea what this is
User::newFromName( 'MediaWiki default' ) // We don't want to have incorrect user_id - user_name entries
);
return true;
}
public function getDisplayFormat() {
return 'ooui';
}
protected function getGroupName() {
return 'wikimanage';
}
protected function sendCreationEmail( $notifyEmail, $siteName ) {
global $wgPasswordSender;
$from = new MailAddress( $wgPasswordSender, 'Miraheze' );
$subject = wfMessage( 'createwiki-email-subject', $siteName )->inContentLanguage()->text();
$body = wfMessage( 'createwiki-email-body' )->inContentLanguage()->text();
return UserMailer::send( $notifyEmail, $from, $subject, $body );
}
}