Skip to content

Commit ee38d76

Browse files
committed
Hook up express to mediawiki.
1 parent 8fbe5cd commit ee38d76

File tree

5 files changed

+100
-4
lines changed

5 files changed

+100
-4
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# mediawiki-express x.x.x (not yet released)
2+
* Mediawiki version 1.26 (REL1_26 branch)
3+
* First working version.

bin/mediawiki

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env node
2+
var server = require('../');
3+
4+
var argv = require('yargs')
5+
.usage('Usage: $0 -p [port]')
6+
.demand('p')
7+
.alias('p', 'port')
8+
.nargs('p', 1)
9+
.describe('p', 'Server port')
10+
.help('h')
11+
.alias('h', 'help')
12+
.argv;
13+
14+
server.makeServer(+argv.port);

lib/index.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,62 @@
11
// Main entry point.
2+
var express = require('express');
3+
var path = require('path');
4+
var php = require('php-embed');
5+
6+
var IP = path.join(__dirname, '..', 'ip');
7+
process.env.MW_INSTALL_PATH = IP;
8+
process.env.PATH =
9+
'/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin';
10+
11+
var makeServer = function(port) {
12+
var app = express();
13+
app.get('/', function(req, res) {
14+
res.redirect('/wiki/');
15+
});
16+
var handlePhp = function(phpfile, pathinfo, req, res) {
17+
php.request({
18+
file: path.join(IP, phpfile + '.php'),
19+
request: req,
20+
stream: res,
21+
serverInitFunc: function(server) {
22+
server.SERVER_NAME = 'localhost'; // XXX
23+
server.SCRIPT_NAME = '/w/' + phpfile + '.php';
24+
server.PHP_SELF = server.REQUEST_URI;
25+
server.PATH_INFO = '/' + pathinfo;
26+
// According to PHP's cgi_main.c:
27+
// PATH_TRANSLATED = DOCUMENT_ROOT + PATH_INFO
28+
server.DOCUMENT_ROOT = '/var/www'; // Lie
29+
server.PATH_TRANSLATED = server.DOCUMENT_ROOT + '/' + pathinfo;
30+
// Console.log("PHP request:", server.SCRIPT_NAME, server.QUERY_STRING);
31+
},
32+
context: {
33+
mwHook: function(globals) {
34+
// XXX We can't actually set any globals, because
35+
// node-php-embed doesn't yet wrap PHP arrays.
36+
},
37+
},
38+
}).catch(function(e) {
39+
console.error('PHP error:', e);
40+
}).then(function() {
41+
res.end();
42+
});
43+
};
44+
app.all('/wiki/?*', function(req, res) {
45+
handlePhp('index', req.params[0], req, res);
46+
});
47+
app.all('/w/:file.php/?*', function(req, res) {
48+
handlePhp(req.params.file, req.params[0], req, res);
49+
});
50+
app.get('/wiki/?*', function(req, res) {
51+
res.send('Hello, world!');
52+
});
53+
app.use('/w', express.static(IP));
54+
var server = app.listen(port, function() {
55+
var host = server.address().address;
56+
var port = server.address().port;
57+
console.log('Listening at http://%s:%s', host, port);
58+
});
59+
return server;
60+
};
61+
62+
module.exports.makeServer = makeServer;

package.json

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
"version": "1.26.0",
44
"description": "An installation of mediawiki invoked from node.js/express.",
55
"main": "lib/index.js",
6+
"bin": {
7+
"mediawiki": "./bin/mediawiki"
8+
},
69
"scripts": {
710
"jscs-fix": "jscs --fix .",
811
"lint": "jshint . && jscs .",
@@ -29,9 +32,10 @@
2932
"cpr": "^0.4.3",
3033
"express": "^4.13.3",
3134
"password-generator": "^2.0.2",
32-
"php-embed": "^0.0.3",
35+
"php-embed": "^0.5.0",
3336
"pn": "^1.0.0",
34-
"prfun": "^2.1.1"
37+
"prfun": "^2.1.1",
38+
"yargs": "^3.29.0"
3539
},
3640
"devDependencies": {
3741
"jscs": "^2.4.0",

scripts/postinstall.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,15 @@ Promise.resolve().then(function() {
3434
}).then(function() {
3535
if (SKIP_COPY) { return; }
3636
console.log('Creating symlinks...');
37+
var ASSETS = path.join(IP, 'resources', 'assets');
3738
return Promise.join(
3839
fs.symlink('../vendor', path.join(IP, 'vendor'), 'dir'),
3940
fs.symlink('../skins', path.join(IP, 'skins'), 'dir'),
40-
fs.mkdir(path.join(IP, 'data'))
41+
fs.mkdir(path.join(IP, 'data')),
42+
// Poor man's copy:
43+
fs.readFile(path.join(ASSETS, 'mediawiki.png')).then(function(data) {
44+
return fs.writeFile(path.join(ASSETS, 'wiki.png'), data);
45+
})
4146
);
4247
}).then(function() {
4348
console.log('Running PHP installer...');
@@ -61,10 +66,19 @@ Promise.resolve().then(function() {
6166
console.log('Adding hook to LocalSettings.php');
6267
return fs.appendFile(path.join(IP, 'LocalSettings.php'), [
6368
'',
64-
'// Hook for additional configuration by ' + packageJson.name,
69+
'# https://www.mediawiki.org/wiki/Manual:Short_URL',
70+
'$wgScriptPath = "/w";',
71+
'$wgArticlePath = "/wiki/$1";',
72+
'$wgUsePathInfo = true;',
73+
'# Default logo',
74+
'$wgLogo = "$wgScriptPath/resources/assets/wiki.png";',
75+
'# Hook for additional configuration by ' + packageJson.name,
6576
'if (isset($_SERVER["CONTEXT"]) && isset($_SERVER["CONTEXT"]->mwHook)) {',
6677
' $_SERVER["CONTEXT"]->mwHook($GLOBALS);',
6778
'}',
79+
// For debugging, the following might be useful:
80+
// '$wgDebugLogFile = "/tmp/mediawiki.log";',
81+
// 'wfDebugLog("error", "This will show up in the log.");',
6882
'',].join('\n'), 'utf8');
6983
}).then(function() {
7084
console.log('Done!');

0 commit comments

Comments
 (0)