Skip to content
This repository was archived by the owner on Sep 23, 2023. It is now read-only.

Commit ff997ef

Browse files
committed
Allow site config to be overridden by trusted users
Fixes #19
1 parent dccf3f9 commit ff997ef

6 files changed

Lines changed: 72 additions & 5 deletions

File tree

LocalSettings.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,3 +74,6 @@ $wgGENewcomerTasksRemoteApiUrl = 'https://en.wikipedia.org/w/api.php';
7474
$wgGENewcomerTasksTopicType = 'ores';
7575
$wgWelcomeSurveyExperimentalGroups['exp2_target_specialpage']['range'] = '0-9';
7676
$wgGEHomepageMentorsList = 'Project:GrowthExperiments_mentors';
77+
78+
// Apply config.php
79+
include( 'config.php' );

config.default.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
'banner' => '',
55
// Allow any user to delete wikis, e.g. on a private installation
66
'allowDelete' => false,
7+
// Allow any user to add site config, e.g. on a private installation
8+
'allowSiteConfig' => false,
79
// Require that patches are V+2 before building the wiki
810
'requireVerified' => true,
911
// OAuth config. When enabled only authenticated users can create
@@ -14,6 +16,11 @@
1416
'key' => null,
1517
'secret' => null,
1618
// OAuth admins can delete any wiki
17-
'admins' => []
19+
'admins' => [],
20+
// These users can override site configs. This is the same level of trust as V+2,
21+
// as those users can also execute arbitrary code.
22+
'configurers' => [],
23+
// Same as above, but regexes e.g. / \(WMF\)$/
24+
'configurersMatch' => [],
1825
]
1926
];

createwiki.sh

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,10 @@ date +%s > $PATCHDEMO/wikis/$NAME/created.txt
4949
# apply our default settings
5050
cat $PATCHDEMO/LocalSettings.txt >> $PATCHDEMO/wikis/$NAME/w/LocalSettings.php
5151

52+
# add site config
53+
echo "$SITECONFIG" >> $PATCHDEMO/wikis/$NAME/w/config.txt
54+
echo $'<?php\n'"$SITECONFIG" >> $PATCHDEMO/wikis/$NAME/w/config.php
55+
5256
# update Main_Page
5357
sleep 1 # Ensure edit appears after creation in history
5458
echo "$MAINPAGE" | php $PATCHDEMO/wikis/$NAME/w/maintenance/edit.php "Main_Page"

includes.php

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
include 'config.default.php';
1313
if ( file_exists( 'config.php' ) ) {
1414
include 'config.php';
15+
// TODO: Make this recursive
1516
$config = array_merge( $config, $localConfig );
1617
}
1718

@@ -150,10 +151,31 @@ function can_delete( $creator = null ) {
150151
global $config, $user;
151152
$username = $user ? $user->username : null;
152153
$admins = $config[ 'oauth' ] ? $config[ 'oauth' ][ 'admins' ] : [];
153-
return $config[ 'allowDelete' ] || ( $username && $username === $creator ) ||
154+
return $config[ 'allowDelete' ] ||
155+
( $username && $username === $creator ) ||
154156
( $username && in_array( $username, $admins, true ) );
155157
}
156158

159+
function can_configure() {
160+
global $config, $user;
161+
$username = $user ? $user->username : null;
162+
$admins = $config[ 'oauth' ] ? $config[ 'oauth' ][ 'admins' ] : [];
163+
$configurers = $config[ 'oauth' ] ? $config[ 'oauth' ][ 'configurers' ] : [];
164+
if (
165+
$config[ 'allowSiteConfig' ] ||
166+
( $username && in_array( $username, $admins, true ) )
167+
) {
168+
return true;
169+
}
170+
$configurersMatch = $config[ 'oauth' ] ? $config[ 'oauth' ][ 'configurersMatch' ] : [];
171+
foreach ( $configurersMatch as $pattern ) {
172+
if ( preg_match( $pattern, $username ) ) {
173+
return true;
174+
}
175+
}
176+
return false;
177+
}
178+
157179
function user_link( $username ) {
158180
global $config;
159181
$base = preg_replace( '/(.*\/index.php).*/i', '$1', $config[ 'oauth' ][ 'url' ] );

index.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,14 +71,33 @@
7171
'align' => 'left',
7272
]
7373
),
74+
new OOUI\FieldLayout(
75+
can_configure() ?
76+
new OOUI\MultilineTextInputWidget( [
77+
'name' => 'siteConfig',
78+
'placeholder' => "\$wgSitename = 'Test wiki';",
79+
'rows' => 4,
80+
] ) :
81+
new OOUI\MessageWidget( [
82+
'label' => 'Only trusted users can modify site config.',
83+
] ),
84+
[
85+
'label' => 'Site config:',
86+
'help' => new OOUI\HtmlSnippet( 'This file will be <strong>public</strong>.' ),
87+
'helpInline' => true,
88+
'align' => 'left',
89+
]
90+
),
7491
new DetailsFieldLayout(
7592
new OOUI\CheckboxMultiselectInputWidget( [
7693
'name' => 'repos[]',
7794
'options' => $repoOptions,
7895
'value' => array_keys( $repoData ),
7996
] ),
8097
[
81-
'label' => 'Choose extensions to enable (default: all):',
98+
'label' => 'Choose extensions to enable:',
99+
'help' => new OOUI\HtmlSnippet( '<br/>Defaults to all' ),
100+
'helpInline' => true,
82101
'align' => 'left',
83102
]
84103
),
@@ -172,6 +191,8 @@
172191
}
173192
$creator = get_creator( $dir );
174193
$created = get_created( $dir );
194+
$siteConfig = get_if_file_exists( 'wikis/' . $dir . '/w/config.txt' );
195+
$hasConfig = $siteConfig && strlen( trim( $siteConfig ) );
175196

176197
if ( !$created ) {
177198
// Add created.txt to old wikis
@@ -184,7 +205,8 @@
184205
$wikis[ $dir ] = [
185206
'mtime' => $created,
186207
'title' => $title,
187-
'creator' => $creator
208+
'creator' => $creator,
209+
'hasConfig' => $hasConfig,
188210
];
189211
}
190212
}
@@ -205,7 +227,13 @@
205227
$anyCanDelete = $anyCanDelete || $canDelete;
206228
$rows .= '<tr' . ( $creator !== $username ? ' class="other"' : '' ) . '>' .
207229
'<td data-label="Patches" class="title">' . ( $title ?: '<em>No patches</em>' ) . '</td>' .
208-
'<td data-label="Link"><a href="wikis/' . $wiki . '/w">' . $wiki . '</a></td>' .
230+
'<td data-label="Config">' .
231+
( !empty( $data[ 'hasConfig' ] ) ?
232+
'<a href="wikis/' . $wiki . '/w/config.txt">Config</a>' :
233+
''
234+
) .
235+
'</td>' .
236+
'<td data-label="Link"><a href="wikis/' . $wiki . '/w">' . substr( $wiki, 0, 20 ) . '&hellip;</a></td>' .
209237
'<td data-label="Time" class="date">' . date( 'c', $data[ 'mtime' ] ) . '</td>' .
210238
( $useOAuth ? '<td data-label="Creator">' . ( $creator ? user_link( $creator ) : '?' ) . '</td>' : '' ) .
211239
( $canDelete ?
@@ -217,6 +245,7 @@
217245

218246
echo '<tr>' .
219247
'<th>Patches</th>' .
248+
'<th>Config</th>' .
220249
'<th>Link</th>' .
221250
'<th>Time</th>' .
222251
( $useOAuth ? '<th>Creator</th>' : '' ) .

new.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
$branch = trim( $_POST['branch'] );
1313
$patches = trim( $_POST['patches'] );
14+
$siteConfig = can_configure() ? trim( $_POST['siteConfig'] ) : '';
1415

1516
$namePath = md5( $branch . $patches . time() );
1617
$server = ( isset( $_SERVER['HTTPS'] ) ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'];
@@ -263,6 +264,7 @@ function set_progress( int $pc, string $label ) {
263264
'WIKINAME' => $wikiName,
264265
'CREATOR' => $user ? $user->username : '',
265266
'MAINPAGE' => $mainPage,
267+
'SITECONFIG' => $siteConfig,
266268
'SERVER' => $server,
267269
'SERVERPATH' => $serverPath,
268270
'COMPOSER_HOME' => __DIR__ . '/composer',

0 commit comments

Comments
 (0)