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

Commit

Permalink
Create wikis in a background process
Browse files Browse the repository at this point in the history
* The form now points to start.php, which converts
  allowed POST data to environment variables and
  sends them to new.php
* The output is written to log files in log/*.html
  which are then polled by the JS for new content
  every second
* Logs are only deleted when the wiki is deleted,
  allowing for debugging and inspection by other
  users.

Fixes #399
  • Loading branch information
edg2s committed Dec 14, 2021
1 parent b133f56 commit 1730425
Show file tree
Hide file tree
Showing 8 changed files with 228 additions and 150 deletions.
2 changes: 2 additions & 0 deletions deletewiki.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@ set -ex

rm -rf $PATCHDEMO/wikis/$WIKI

rm $PATCHDEMO/logs/$WIKI.html

# delete database
mysql -u patchdemo --password=patchdemo -e "DROP DATABASE IF EXISTS patchdemo_$WIKI";
2 changes: 1 addition & 1 deletion index.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
echo new OOUI\FormLayout( [
'infusable' => true,
'method' => 'POST',
'action' => 'new.php',
'action' => 'start.php',
'id' => 'new-form',
'items' => [
new OOUI\FieldsetLayout( [
Expand Down
28 changes: 0 additions & 28 deletions js/new.js

This file was deleted.

55 changes: 55 additions & 0 deletions js/start.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
/* global OO, pd */
( function () {
window.pd = window.pd || {};

pd.installProgressField = OO.ui.infuse(
document.getElementsByClassName( 'installProgressField' )[ 0 ]
);

pd.installProgressField.fieldWidget.pushPending();

pd.openWiki = OO.ui.infuse(
document.getElementsByClassName( 'openWiki' )[ 0 ]
);

pd.notify = function ( message, body ) {
if ( 'Notification' in window && +localStorage.getItem( 'patchdemo-notifications' ) ) {
// eslint-disable-next-line no-new
new Notification(
message,
{
icon: './images/favicon-32x32.png',
body: body
}
);
}
};

$( function () {
var log = '';
var offset = 0;
// eslint-disable-next-line no-jquery/no-global-selector
var $log = $( '.newWikiLog' );
function poll() {
$.get( 'log.php', {
wiki: pd.wiki,
offset: offset
} ).then( function ( result ) {
// result can be unbalanced HTML, so store it in
// a string and rewrite the whole thing each time
log += result;
$log.html( log );
offset += result.length;
if ( !pd.finished ) {
setTimeout( poll, 1000 );
}
} );
}

poll();

// Add wiki to URL so that page can be shared/reloaded
history.replaceState( null, '', 'start.php?wiki=' + pd.wiki );
} );

}() );
33 changes: 33 additions & 0 deletions log.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

$offset = $_GET['offset'];
$wiki = $_GET['wiki'];

if ( !preg_match( '/^[0-9a-f]{10,32}$/', $wiki ) ) {
die( 'Invalid wiki name.' );
}

$file = 'logs/' . $wiki . '.html';

if ( file_exists( $file ) ) {
echo file_get_contents(
$file,
false,
null,
$offset
);
} else {
// TODO: De-duplicate with new.php
$err = 'Failed to open log for wiki.';
echo $err;
$errJson = json_encode( $err );
echo <<<EOT
<script>
pd.installProgressField.fieldWidget.setDisabled( true );
pd.installProgressField.fieldWidget.popPending();
pd.installProgressField.setErrors( [ new OO.ui.HtmlSnippet( $errJson ) ] );
pd.notify( 'Your PatchDemo wiki failed to build', $errJson );
pd.finished = true;
</script>
EOT;
}
Loading

0 comments on commit 1730425

Please sign in to comment.