forked from Orain/CreateWiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SpecialCreateWiki.php
190 lines (165 loc) · 5.98 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
<?php
/**
* Copyright (C) 2013 Orain, Kudu, Southparkfan and contributors
*
* This file is part of CreateWiki.
*
* CreateWiki is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* CreateWiki is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License
* for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with CreateWiki. If not, see <http://www.gnu.org/licenses/>.
*/
class SpecialCreateWiki extends SpecialPage {
public function __construct() {
parent::__construct( 'CreateWiki', 'createwiki' );
}
public function execute( $par ) {
$this->checkPermissions();
$this->setHeaders();
$form = new HTMLForm( array(
'dbname' => array(
'default' => $par, // e.g. Special:CreateWiki/enwiki
'filter-callback' => array( 'SpecialCreateWiki', 'filter' ),
'label-message' => 'createwiki-label-dbname',
'maxlength' => 30,
'required' => true,
'size' => 30,
'type' => 'text',
'validation-callback' => array( 'SpecialCreateWiki', 'validateDBname' ),
),
'founder' => array(
'filter-callback' => array( 'SpecialCreateWiki', 'filter' ),
'label-message' => 'createwiki-label-founder',
'required' => true,
'size' => 30,
'type' => 'text',
'validation-callback' => array( 'SpecialCreateWiki', 'validateFounder' ),
),
'comment' => array(
'label-message' => 'createwiki-label-comment',
'maxlength' => 79,
'size' => 79,
'type' => 'text',
),
)
);
$form->setSubmitTextMsg( 'createwiki-label-create' );
$form->setTitle( $this->getPageTitle() );
$form->setSubmitCallback( array( 'SpecialCreateWiki', 'processInput' ) );
$form->show();
}
public static function filter( $string, $allData ) {
return trim( $string );
}
public static function validateDBname( $DBname, $allData ) {
global $wgConf;
$suffixed = false;
foreach ( $wgConf->suffixes as $suffix ) {
if ( substr( $DBname, -strlen( $suffix ) ) === $suffix ) {
$suffixed = true;
break;
}
}
if ( !$suffixed ) {
return wfMessage( 'createwiki-error-notsuffixed' )->plain();
}
if ( !ctype_alnum( $DBname ) ) {
return wfMessage( 'createwiki-error-notalnum' )->plain();
}
if ( strtolower( $DBname ) != $DBname ) {
return wfMessage( 'createwiki-label-dbnamecontainsuppercase' )->plain();
}
return true;
}
public static function validateFounder( $founderName, $allData ) {
$user = User::newFromName( $founderName );
if ( !$user->getId() ) {
return wfMessage( 'createwiki-error-nonexistentfounder' )->plain();
}
return true;
}
/**
* @param array $formData
* @param HtmlForm $form
*
* @return bool|string
* @throws DBUnexpectedError
* @throws Exception
* @throws MWException
*/
public static function processInput( array $formData, HtmlForm $form ) {
error_reporting( 0 );
global $wgCreateWikiSQLfiles, $IP;
$DBname = $formData['dbname'];
$founderName = $formData['founder'];
wfDebugLog( 'CreateWiki', 'Creating wiki ' . $DBname . ' with a founder of ' . $founderName );
$dbw = wfGetDB( DB_MASTER );
$dbTest = $dbw->query( 'SHOW DATABASES LIKE ' . $dbw->addQuotes( $DBname ) . ';' );
$rows = $dbTest->numRows();
$dbTest->free();
if ( $rows !== 0 ) {
return wfMessage( 'createwiki-error-dbexists' )->plain();
}
$farmerLogEntry = new ManualLogEntry( 'farmer', 'createandpromote' );
$farmerLogEntry->setPerformer( $form->getUser() );
$farmerLogEntry->setTarget( $form->getTitle() );
$farmerLogEntry->setComment( $formData['comment'] );
$farmerLogEntry->setParameters( array(
'4::wiki' => $DBname,
'5::founder' => $founderName,
)
);
$farmerLogID = $farmerLogEntry->insert();
$farmerLogEntry->publish( $farmerLogID );
$dbw->query( 'SET storage_engine=InnoDB;' );
$dbw->query( 'CREATE DATABASE ' . $dbw->addIdentifierQuotes( $DBname ) . ';' );
$dbw->selectDB( $DBname );
foreach ( $wgCreateWikiSQLfiles as $file ) {
$dbw->sourceFile( $file );
}
$dbw->insert( 'site_stats', array( 'ss_row_id' => 1 ) );
$dbw->close();
// Add DNS record to cloudflare
global $wgCreateWikiUseCloudFlare, $wgCloudFlareUser, $wgCloudFlareKey;
if ( $wgCreateWikiUseCloudFlare ) {
$domainPrefix = substr( $DBname, 0, -4 );
$cloudFlare = new cloudflare_api( $wgCloudFlareUser, $wgCloudFlareKey );
$cloudFlareResult = $cloudFlare->rec_new(
'orain.org',
'CNAME',
$domainPrefix,
'lb.orain.org'
);
if( !is_object( $cloudFlareResult ) || $cloudFlareResult->result !== 'success' ) {
wfDebugLog( 'CreateWiki', 'CloudFlare FAILED to add CNAME for ' . $domainPrefix . '.orain.org' );
} else {
wfDebugLog( 'CreateWiki', 'CloudFlare CNAME added for ' . $domainPrefix . '.orain.org' );
}
} else {
wfDebugLog( 'CreateWiki', 'CloudFlare is not enabled.' );
}
// Create local account for founder (hack)
$out = exec( "php5 $IP/extensions/CentralAuth/maintenance/createLocalAccount.php " . escapeshellarg( $founderName ) . ' --wiki ' . escapeshellarg( $DBname ) );
if ( !strpos( $out, 'created' ) ) {
wfDebugLog( 'CreateWiki', 'Failed to create local account for founder.' );
return wfMessage( 'createwiki-error-usernotcreated' )->plain();
}
require_once( "$IP/includes/UserRightsProxy.php" );
// Grant founder sysop and bureaucrat rights
$founderUser = UserRightsProxy::newFromName( $DBname, $founderName );
$newGroups = array( 'sysop', 'bureaucrat' );
array_map( array( $founderUser, 'addGroup' ), $newGroups );
$founderUser->invalidateCache();
$form->getOutput()->addWikiMsg( 'createwiki-success', $DBname );
wfDebugLog( 'CreateWiki', 'Successfully created ' . $DBname . ' with a founder of ' . $founderName );
return true;
}
}