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

Commit 08ab0b8

Browse files
committed
Allow site config to be overridden by trusted users
Fixes #19
1 parent fad4d87 commit 08ab0b8

File tree

6 files changed

+72
-5
lines changed

6 files changed

+72
-5
lines changed

LocalSettings.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,6 @@ $wgGENewcomerTasksRemoteApiUrl = 'https://en.wikipedia.org/w/api.php';
6363
$wgGENewcomerTasksTopicType = 'ores';
6464
$wgWelcomeSurveyExperimentalGroups['exp2_target_specialpage']['range'] = '0-9';
6565
$wgGEHomepageMentorsList = 'Project:GrowthExperiments_mentors';
66+
67+
// Apply config.php
68+
include( 'config.php' );

config.default.php

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

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
@@ -11,6 +11,7 @@
1111
include 'config.default.php';
1212
if ( file_exists( 'config.php' ) ) {
1313
include 'config.php';
14+
// TODO: Make this recursive
1415
$config = array_merge( $config, $localConfig );
1516
}
1617

@@ -114,10 +115,31 @@ function can_delete( $creator = null ) {
114115
global $config, $user;
115116
$username = $user ? $user->username : null;
116117
$admins = $config[ 'oauth' ] ? $config[ 'oauth' ][ 'admins' ] : [];
117-
return $config[ 'allowDelete' ] || ( $username && $username === $creator ) ||
118+
return $config[ 'allowDelete' ] ||
119+
( $username && $username === $creator ) ||
118120
( $username && in_array( $username, $admins, true ) );
119121
}
120122

123+
function can_configure() {
124+
global $config, $user;
125+
$username = $user ? $user->username : null;
126+
$admins = $config[ 'oauth' ] ? $config[ 'oauth' ][ 'admins' ] : [];
127+
$configurers = $config[ 'oauth' ] ? $config[ 'oauth' ][ 'configurers' ] : [];
128+
if (
129+
$config[ 'allowSiteConfig' ] ||
130+
( $username && in_array( $username, $admins, true ) )
131+
) {
132+
return true;
133+
}
134+
$configurersMatch = $config[ 'oauth' ] ? $config[ 'oauth' ][ 'configurersMatch' ] : [];
135+
foreach ( $configurersMatch as $pattern ) {
136+
if ( preg_match( $pattern, $username ) ) {
137+
return true;
138+
}
139+
}
140+
return false;
141+
}
142+
121143
function user_link( $username ) {
122144
global $config;
123145
$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
@@ -68,14 +68,33 @@
6868
'align' => 'left',
6969
]
7070
),
71+
new OOUI\FieldLayout(
72+
can_configure() ?
73+
new OOUI\MultilineTextInputWidget( [
74+
'name' => 'siteConfig',
75+
'placeholder' => "\$wgSitename = 'Test wiki';",
76+
'rows' => 4,
77+
] ) :
78+
new OOUI\MessageWidget( [
79+
'label' => 'Only trusted users can modify site config.',
80+
] ),
81+
[
82+
'label' => 'Site config:',
83+
'help' => new OOUI\HtmlSnippet( 'This file will be <strong>public</strong>.' ),
84+
'helpInline' => true,
85+
'align' => 'left',
86+
]
87+
),
7188
new DetailsFieldLayout(
7289
new OOUI\CheckboxMultiselectInputWidget( [
7390
'name' => 'repos[]',
7491
'options' => $repoOptions,
7592
'value' => array_keys( $repoData ),
7693
] ),
7794
[
78-
'label' => 'Choose extensions to enable (default: all):',
95+
'label' => 'Choose extensions to enable:',
96+
'help' => new OOUI\HtmlSnippet( '<br/>Defaults to all' ),
97+
'helpInline' => true,
7998
'align' => 'left',
8099
]
81100
),
@@ -157,6 +176,8 @@
157176
}
158177
$creator = get_creator( $dir );
159178
$created = get_created( $dir );
179+
$siteConfig = get_if_file_exists( 'wikis/' . $dir . '/w/config.txt' );
180+
$hasConfig = $siteConfig && strlen( trim( $siteConfig ) );
160181

161182
if ( !$created ) {
162183
// Add created.txt to old wikis
@@ -169,7 +190,8 @@
169190
$wikis[ $dir ] = [
170191
'mtime' => $created,
171192
'title' => $title,
172-
'creator' => $creator
193+
'creator' => $creator,
194+
'hasConfig' => $hasConfig,
173195
];
174196
}
175197
}
@@ -190,7 +212,13 @@
190212
$anyCanDelete = $anyCanDelete || $canDelete;
191213
$rows .= '<tr' . ( $creator !== $username ? ' class="other"' : '' ) . '>' .
192214
'<td class="title">' . ( $title ?: '<em>No patches</em>' ) . '</td>' .
193-
'<td><a href="wikis/' . $wiki . '/w">' . $wiki . '</a></td>' .
215+
'<td>' .
216+
( !empty( $data[ 'hasConfig' ] ) ?
217+
'<a href="wikis/' . $wiki . '/w/config.txt">Config</a>' :
218+
''
219+
) .
220+
'</td>' .
221+
'<td><a href="wikis/' . $wiki . '/w">' . substr( $wiki, 0, 20 ) . '&hellip;</a></td>' .
194222
'<td class="date">' . date( 'c', $data[ 'mtime' ] ) . '</td>' .
195223
( $useOAuth ? '<td>' . ( $creator ? user_link( $creator ) : '?' ) . '</td>' : '' ) .
196224
( $canDelete ?
@@ -202,6 +230,7 @@
202230

203231
echo '<tr>' .
204232
'<th>Patches</th>' .
233+
'<th>Config</th>' .
205234
'<th>Link</th>' .
206235
'<th>Time</th>' .
207236
( $useOAuth ? '<th>Creator</th>' : '' ) .

new.php

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

77
$branch = trim( $_POST['branch'] );
88
$patches = trim( $_POST['patches'] );
9+
$siteConfig = can_configure() ? trim( $_POST['siteConfig'] ) : '';
910

1011
$namePath = md5( $branch . $patches . time() );
1112
$server = ( isset( $_SERVER['HTTPS'] ) ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'];
@@ -180,6 +181,7 @@
180181
'WIKINAME' => $wikiName,
181182
'CREATOR' => $user ? $user->username : '',
182183
'MAINPAGE' => $mainPage,
184+
'SITECONFIG' => $siteConfig,
183185
'SERVER' => $server,
184186
'SERVERPATH' => $serverPath,
185187
'COMPOSER_HOME' => __DIR__ . '/composer',

0 commit comments

Comments
 (0)