FS-Mod/FSMod/SpecialConvertProtocol.php

284 lines
9.3 KiB
PHP

<?php
/**
* Special:StuBU, a special page that implements a StuBu generator form.
* Based on the pdfforms typo3 plugin of Felix B. Holzke.
*
* @file
* @ingroup Extensions
* @author Björn Kinscher
* @copyright © 2009 Björn Kinscher
* @license GNU General Public Licence 2.0 or later
*/
if( !defined( 'MEDIAWIKI' ) ) {
echo( "not a valid entry point.\n" );
die( 1 );
}
/**
* Provides the stubu form
* @ingroup SpecialPage
*/
class SpecialConvertProtocol extends SpecialPage {
/**
* Constructor
*/
public function __construct() {
parent::__construct( 'ConvertProtocol', 'fsmod-convert-protocol');
}
/**
* Main execution function
*
* @param $par Mixed: Parameters passed to the page
*/
public function execute( $par ) {
$this->checkPermissions();
$out = $this->getOutput();
$request = $this->getRequest();
$submit = $request->getVal('submit');
if ($version = self::get_converter_version()){
$out->addHTML("Version: <pre>$version</pre>");
}
if ($submit !== null) {
# content of from field for wiki url
$wiki_url = $request->getVal( 'wiki' );
# protocol format
$protocol_type = $request->getVal('type');
# content of form field for pad url
$protocol = $request->getVal( 'content' );
$draft = $request->getVal('draft');
if ($wiki_url !== null
&& $protocol_type !== null
&& $protocol !== null) {
$ret_result = self::convert_protocol($protocol, $protocol_type, $draft === "draft",$result ,$err_result);
if ($ret_result === 0) {
$success = $this->create_wiki_page($result,$wiki_url);
if ($success){
$out->addWikiTextAsContent("Protocol \"converted\" and saved at [[$wiki_url]]<br />");
}else{
$out->addHTML("Protocol \"converted\", but failed to save!<br />");
$out->addHTML("You can find the output below to manually save it!<br />");
}
}else {
$out->addHTML("Protocol conversion failed!<br /> See output below!");
}
$esc_protocol = htmlspecialchars($protocol);
$esc_result = htmlspecialchars($result);
$esc_err_result = htmlspecialchars($err_result);
//input
$out->addHTML(<<<"HTML"
<div class="mw-collapsible mw-collapsed">
<div style="font-weight:bold;line-height:1.6;">Input</div>
<div class="mw-collapsible-content"><pre>
{$esc_protocol}
</pre></div>
</div><br/>
HTML);
//output
$out->addHTML(<<<"HTML"
<div class="mw-collapsible mw-collapsed">
<div style="font-weight:bold;line-height:1.6;">Output</div>
<div class="mw-collapsible-content"><pre>
{$esc_result}
</pre></div>
</div><br/>
HTML);
//stderr
$out->addHTML(<<<"HTML"
<div class="mw-collapsible">
<div style="font-weight:bold;line-height:1.6;">Stderr</div>
<div class="mw-collapsible-content"><pre>
{$esc_err_result}
</pre></div>
</div><br/>
HTML);
$out->addHtml("Return Value: {$ret_result}<br />");
}else {
$out->addHTML("Not all required fields where filled!");
}
}
self::print_form($out);
}
/**
* crate or override a wikipage with the given content
*
* @param $page_content String: The desired page content
* @param $page_title String: The Path of the Page
* @return boolean: Whether this was successful
*/
function create_wiki_page($page_content, $page_title) {
$user = $this->getUser();
# get title from url
$wiki_title = Title::newFromText($page_title);
# page object for title
$wikiPage = WikiPage::factory($wiki_title);
# updater for page
$updater = $wikiPage->newPageUpdater($user);
# create Content
$content = new WikitextContent($page_content);
# overwrite whole page content
$updater->setContent("main",$content);
# no clue
$updater->setRcPatrolStatus(RecentChange::PRC_PATROLLED);
$comment = CommentStoreComment::newUnsavedComment("Imported Protokoll");
# save changes
$newRev = $updater->saveRevision($comment);
return $updater->wasSuccessful();
}
/**
* determine and return the version of the protocol converter
*
* @return String|false: The Version if successful or false otherwise
*/
static function get_converter_version() {
global $wgProtocolConverter;
$version_descspec = array(
1 => array("pipe", "w"), //STDOUT
);
$version_process = proc_open($wgProtocolConverter." --version",$version_descspec, $version_pipes);
if (is_resource($version_process)){
$version = stream_get_contents($version_pipes[1]);
fclose($version_pipes[1]);
proc_close($version_process);
return $version;
}else{
return false;
}
}
/**
* crate or override a wikipage with the given content
*
* @param $protocol String: The protocol content
* @param $protocol_type String: The protocol format either "md" or "txt"
* @param $draft bool: wheter the <entwurf /> tag should be generated
* @param &$stdout String: The content of STDOUT of the protocol converter
* @param &$stderr String: The content of STDERR of the protocol converter
* @return int|false: The return value of the protocol converted or false if something failed
*/
static function convert_protocol($protocol, $protocol_type, $draft, &$stdout , &$stderr) {
global $wgProtocolConverter;
switch ($protocol_type) {
case 'md':
$format = ' --format=md';
break;
case 'txt':
$format = ' --format=txt';
break;
default:
$format = '';
break;
}
if (!$draft) {
$publish = ' --publish';
} else {
$publish = '';
}
$descspec = array(
0 => array("pipe", "r"), //STDIN
1 => array("pipe", "w"), //STDOUT
2 => array("pipe", "w"), //STDERR
);
$convert_process = proc_open($wgProtocolConverter . $format . $publish ,$descspec, $pipes);
//make sure stdout and stderr are initialized in case of an error
$stdout = "";
$stderr = "";
if (is_resource($convert_process)){
//pipe protocol into conversion process
fwrite($pipes[0], $protocol);
fclose($pipes[0]);
$stdout = stream_get_contents($pipes[1]);
$stderr = stream_get_contents($pipes[2]);
fclose($pipes[1]);
fclose($pipes[2]);
//make sure pipes are closed first otherwise risk deadlock
return proc_close($convert_process);
}else {
return false;
}
}
static function print_form($out) {
$out->addHTML("
<h2>Convert Protokoll to Wiki Entry</h2>
<form method='POST'>
<label for='wikiUrl'>
Wiki Page
<input id='wikiUrl' type='url' name='wiki' required='required' placeholder='Protokoll:YYYY-MM-DD'/>
</label><br />
<label for='protocolType'>
Protocol Type
<select id='protocolType' name='type' required='required'>
<option value='md'> Markdown (CodiMD) </option>
<option value='txt'> Textfile (EtherPad)</option>
</select>
</label><br />
<label for='protocolContent'>
Protocol Content
</label><br />
<textarea id='protocolContent'
name='content'
required='required'
style='width: 100%; height: 40vh;'
placeholder='Hier Markdown bzw. Plain Text einfügen'></textarea><br />
<label for='draft'>
Entwurf
</label><input id='draft' type='checkbox' name='draft' value='draft' checked='checked'><br />
<input type='submit' name='submit' />
</form>
");
}
}