FS-Mod/FSMod/FSMod.php

126 lines
3.9 KiB
PHP

<?php
/**
* Setup for FSMod extension, a collection of parser hooks and other wiki
* modifications.
*
* @file
* @ingroup Extensions
* @author Björn Kinscher
* @copyright © 2009 Björn Kinscher
* @modified 2014 Christian Zirkelbach
* @modified 2018,2019 Einhard Leichtfuß
* @modified 2019 Bennet Bleßmann
* @license GNU General Public Licence 2.0 or later
*/
if( !defined( 'MEDIAWIKI' ) ) {
echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" );
die( 1 );
}
use MediaWiki\Revision\{RevisionRecord, SlotRecord};
class FSMod {
/*
* Get the FS-Status of the given user
*/
static function fsStatus(string $user_name) {
global $wgLdapBase, $wgLdapServer, $wgLdapPwd, $wgLdapUser;
$data = FSModLdap::perform_search("(uid=$user_name)");
if (!$data) {
return 0;
}
if (array_key_exists('description', $data[0])) {
$status = intval($data[0]['description'][0]);
return $status;
}
return 0;
}
/**
* Helper function to determin if an IP-Address should be considered CAU intern
*/
static function isCAUIP() {
$ip = explode('.', $_SERVER['REMOTE_ADDR']);
return (($ip[0] == '10') && ($ip[1] == '0'))
|| (($ip[0] == '134') && ($ip[1] == '245'))
|| (($ip[0] == '192') && ($ip[1] == '168'));
}
static function deprecatedTagCallback(string $tag_name): callable {
return fn($input, $args, $parser, $frame) => FSMod::deprecatedTag($input, $args, $parser, $frame, $tag_name);
}
private static function deprecatedTag(
?string $input,
array $args,
Parser $parser,
PPFrame $frame,
string $tag_name
) {
$request = RequestContext::getMain()->getRequest();
$oldid = $request->getVal('oldid');
if ($oldid === null) {
error_log("$tag_name on path " . $_SERVER['REQUEST_URI']);
return "$tag_name tag has been deprecated!<br /> If you see this, please inform us, where this should be removed!";
} else {
// potentially looking at an old version of a page
// don't log an error and don't ask user to inform us
return "$tag_name tag has been deprecated!";
}
}
static function handle_xml_errors($errors, $input, $parser) {
foreach(libxml_get_errors() as $error) {
error_log(print_r($error, true));
}
$result = "Ungültiges Format!<br />\n";
$user = $parser->getUserIdentity();
if ($user->isRegistered()) {
foreach(libxml_get_errors() as $error) {
$line = htmlspecialchars(explode("\n", $input)[$error->line]);
$result .= "<br>\n";
$result .= "\tError Level: {$error->level}\tError Code: {$error->code}";
$result .= "\tLine: {$error->line}\tColumn: {$error->column}<br>\n";
$result .= "\tLine Content:\t{$line}<br>\n";
$result .= "\t{$error->message}<br>\n";
}
}
return $result;
}
static function parseTagRecursive(SimpleXMLElement $xml, Parser $parser, PPFrame $frame) : string {
$xmlString = trim($xml->asXML());
$xmlString = substr($xmlString,strlen("<{$xml->getName()}>"),-strlen("</{$xml->getName()}>"));
$inner = $parser->recursiveTagParse($xmlString, $frame);
return $inner;
}
static function loadArticle($name, Parser $parser) {
$title = Title::newFromText($name);
$wikiPage = WikiPage::factory($title);
$revision = $wikiPage->getRevisionRecord();
$content = $revision->getContent(SlotRecord::MAIN, RevisionRecord::RAW );
$text = ContentHandler::getContentText( $content );
$parser->getOutput()->addTemplate($title, $wikiPage->getId(), $revision->getId());
return $text;
}
}