custom-mediawiki-extensions/LocalMotd/LocalMotd.php

126 lines
4.5 KiB
PHP

<?php
#--------------------------------------------------
# Step 1: choose a magic word id
#--------------------------------------------------
# storing the chosen id in a constant is not required
# but still good programming practice - it makes
# searching for all occurrences of the magic word id a
# bit easier - note that the name of the constant
# and the value it is assigned don't have to have anthing
# to do with each other.
define('MAG_LOCALMOTD', 'localmotd');
#---------------------------------------------------
# Step 2: define some words to use in wiki markup
#---------------------------------------------------
$wgHooks['LanguageGetMagic'][] = 'wfLocalMotdWikiWords';
function wfLocalMotdWikiWords(&$aWikiWords, $langID) {
#tell MediaWiki that all {{NiftyVar}}, {{NIFTYVAR}},
#{{CoolVar}}, {{COOLVAR}} and all case variants found
#in wiki text should be mapped to magic id 'mycustomvar1'
# (0 means case-insensitive)
$aWikiWords[MAG_LOCALMOTD] = array(0, 'LocalMotd');
#must do this or you will silence every LanguageGetMagic
#hook after this!
return true;
}
#---------------------------------------------------
# Step 3: assign a value to our variable
#---------------------------------------------------
function toHtml($data) {
return sprintf('<h3>%s</h3><p><em>Skrevet %s av %s</em></p><p>%s</p>',
$data["title"], $data["date"], $data["user"], preg_replace('/@pvv\./', ' AT pvv.', $data["body"]));
}
function tolower($str) {
return str_replace(
array("\xc3\x86", "\xc3\x98", "\xc3\x85"), // ÆØÅ
array("\xc3\xa6", "\xc3\xb8", "\xc3\xa5"), // æøå
strtolower($str));
}
$wgHooks['ParserGetVariableValueSwitch'][] = 'wfLocalMotdAssignAValue';
function wfLocalMotdAssignAValue(&$parser, &$cache, &$magicWordId, &$ret) {
if (MAG_LOCALMOTD == $magicWordId) {
// We found a value
//$ret='This is a really silly value';
//$ret = file_get_contents( '/local/etc/motd' );
#$lines = file('/local/etc/motd');
$lines = file('/usr/local/etc/motd');
$data = array();
$ret = "";
foreach ($lines as $line) {
if (preg_match('/^(\d{4})(\d\d)(\d\d) (.*)/', $line, $matches)) {
if (count($data) > 0) {
$ret .= toHtml($data);
$data = array();
}
$data["date"] = $matches[1]."-".$matches[2]."-".$matches[3];
$data["title"] = substr($matches[4], 0, 1).tolower(substr($matches[4], 1));
$data["body"] = "";
} elseif (preg_match('/^([a-z]+)\s+(.*)/', $line, $matches)) {
$data["user"] = $matches[1];
$data["body"] .= rtrim($matches[2]);
} elseif (preg_match('/^\s*$/', $line)) {
$data["body"] .= "\n\n";
} else {
$data["body"] .= rtrim($line);
}
}
$ret .= toHtml($data);
}
// We must return true for two separate reasons:
// 1. To permit further callbacks to run for this hook.
// They might override our value but that's life.
// Returning false would prevent these future callbacks from running.
// 2. At the same time, "true" indicates we found a value.
// Returning false would the set variable value to null.
//
// In other words, true means "we found a value AND other
// callbacks will run," and false means "we didn't find a value
// AND abort future callbacks." It's a shame these two meanings
// are mixed in the same return value. So as a rule, return
// true whether we found a value or not.
return true;
}
#---------------------------------------------------
# Step 4: register the custom variables so that it
# shows up in Special:Version under the
# listing of custom variables
#---------------------------------------------------
$wgExtensionCredits[MAG_LOCALMOTD][] = array(
'name' => 'Local MOTD',
'author' => 'Magnus Kristiansen',
'url' => 'http://www.mediawiki.org/wiki/Extension:LocalMotd',
'description' => 'Includes the motd from the filesystem'
);
#---------------------------------------------------
# Step 5: register wiki markup words associated with
# MAG_NIFTYVAR as a variable and not some
# other type of magic word
#---------------------------------------------------
$wgHooks['MagicWordwgVariableIDs'][] = 'wfLocalMotdDeclareVarIds';
function wfLocalMotdDeclareVarIds(&$aCustomVariableIds) {
#aCustomVariableIds is where MediaWiki wants to store its
#list of custom variable ids. We oblige by adding ours:
$aCustomVariableIds[] = MAG_LOCALMOTD;
#must do this or you will silence every MagicWordwgVariableIds
#registered after this!
return true;
}