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

Commit dbe9d3e

Browse files
committed
Allow site config to be overridden by JSON
Fixes #19
1 parent 3aa5120 commit dbe9d3e

File tree

5 files changed

+46
-3
lines changed

5 files changed

+46
-3
lines changed

LocalSettings.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,15 @@ $wgLogos = [
4646
]
4747
];
4848
$wgFavicon = "$wgResourceBasePath/favicon.ico";
49+
50+
// Apply config.json
51+
$config = json_decode(
52+
file_get_contents( 'config.json' ),
53+
true
54+
);
55+
if ( $config ) {
56+
foreach ( $config as $key => $value ) {
57+
$name = "wg$key";
58+
$$name = $value;
59+
}
60+
}

createwiki.sh

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ date +%s > $PATCHDEMO/wikis/$NAME/created.txt
4545
# apply our default settings
4646
cat $PATCHDEMO/LocalSettings.txt >> $PATCHDEMO/wikis/$NAME/w/LocalSettings.php
4747

48+
# add site config
49+
echo "$SITECONFIG" >> $PATCHDEMO/wikis/$NAME/w/config.json
50+
4851
# update Main_Page
4952
sleep 1 # Ensure edit appears after creation in history
5053
echo "$MAINPAGE" | php $PATCHDEMO/wikis/$NAME/w/maintenance/edit.php "Main_Page"

index.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
( function () {
22
var myWikis, wikisTable,
3-
form = document.getElementById( 'new-form' );
3+
form = document.getElementById( 'new-form' ),
4+
siteConfig = form.querySelector( '[name=siteConfig]' );
45

5-
form.addEventListener( 'submit', function () {
6+
form.addEventListener( 'submit', function ( e ) {
7+
if ( siteConfig.value.trim() ) {
8+
try {
9+
JSON.parse( siteConfig.value );
10+
} catch ( err ) {
11+
e.preventDefault();
12+
alert( 'Invalid JSON: ' + err.message )
13+
return;
14+
}
15+
}
616
form.querySelector( 'button[type=submit]' ).disabled = true;
717
} );
818

index.php

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,12 @@
2727
<div>Then, apply patches:</div>
2828
<textarea name="patches" placeholder="Gerrit changeset number or Change-Id, one per line" rows="4" cols="50"></textarea>
2929
</label>
30+
<label>
31+
<div>Site config:<br/>All keys will be given a <code>$wg</code> prefix.<br/>This file will be <strong>public</strong>.</div>
32+
<textarea name="siteConfig" placeholder="{
33+
&quot;Sitename&quot;: &quot;Test wiki&quot;
34+
}" rows="4" cols="50"></textarea>
35+
</label>
3036
<button type="submit">Create demo</button>
3137
</form>
3238
<br/>
@@ -82,6 +88,8 @@
8288
}
8389
$creator = get_creator( $dir );
8490
$created = get_created( $dir );
91+
$siteConfig = get_if_file_exists( 'wikis/' . $dir . '/w/config.json' );
92+
$hasConfig = $siteConfig && strlen( trim( $siteConfig ) );
8593

8694
if ( !$created ) {
8795
// Add created.txt to old wikis
@@ -94,7 +102,8 @@
94102
$wikis[ $dir ] = [
95103
'mtime' => $created,
96104
'title' => $title,
97-
'creator' => $creator
105+
'creator' => $creator,
106+
'hasConfig' => $hasConfig,
98107
];
99108
}
100109
}
@@ -115,6 +124,12 @@
115124
$anyCanDelete = $anyCanDelete || $canDelete;
116125
$rows .= '<tr' . ( $creator !== $username ? ' class="other"' : '' ) . '>' .
117126
'<td class="title">' . $title . '</td>' .
127+
'<td>' .
128+
( $data[ 'hasConfig' ] ?
129+
'<a href="wikis/' . $wiki . '/w/config.json">JSON</a>' :
130+
''
131+
) .
132+
'</td>' .
118133
'<td><a href="wikis/' . $wiki . '/w">' . $wiki . '</a></td>' .
119134
'<td>' . date( 'c', $data[ 'mtime' ] ) . '</td>' .
120135
( $useOAuth ? '<td>' . ( $creator ? user_link( $creator ) : '?' ) . '</td>' : '' ) .
@@ -127,6 +142,7 @@
127142

128143
echo '<tr>' .
129144
'<th>Patches</th>' .
145+
'<th>Config</th>' .
130146
'<th>Link</th>' .
131147
'<th>Time</th>' .
132148
( $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 = $_POST['patches'];
9+
$siteConfig = $_POST['siteConfig'];
910

1011
$namePath = md5( $branch . $patches . time() );
1112
$server = ( isset( $_SERVER['HTTPS'] ) ? 'https://' : 'http://' ) . $_SERVER['HTTP_HOST'];
@@ -147,6 +148,7 @@
147148
'WIKINAME' => $wikiName,
148149
'CREATOR' => $user ? $user->username : '',
149150
'MAINPAGE' => $mainPage,
151+
'SITECONFIG' => $siteConfig,
150152
'SERVER' => $server,
151153
'SERVERPATH' => $serverPath,
152154
'COMPOSER_HOME' => __DIR__ . '/composer',

0 commit comments

Comments
 (0)