Difference between revisions of "Extension:SimpleCalendar"

From Organic Design wiki
m (sortkey)
(disable cache for calendars)
Line 1: Line 1:
 
<?php
 
<?php
# MediaWiki SimpleCalendar Extension{{#Security:*|dev}}{{#Security:view|*}}{{php}}{{Category:Extensions|SimpleCalendar}}
+
# MediaWiki SimpleCalendar Extension{{php}}{{Category:Extensions|SimpleCalendar}}
 
# - See http://www.mediawiki.org/wiki/Extension:Simple_Calendar for installation and usage details
 
# - See http://www.mediawiki.org/wiki/Extension:Simple_Calendar for installation and usage details
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 
# - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
 
# - Author: http://www.organicdesign.co.nz/nad
 
# - Author: http://www.organicdesign.co.nz/nad
  
define('SIMPLECALENDAR_VERSION','1.2.1, 2007-05-07');
+
define('SIMPLECALENDAR_VERSION','1.2.2, 2007-06-11');
  
$wgExtensionFunctions[]       = 'wfSetupSimpleCalendar';
+
$wgExtensionFunctions[] = 'wfSetupSimpleCalendar';
 
$wgHooks['LanguageGetMagic'][] = 'wfCalendarLanguageGetMagic';
 
$wgHooks['LanguageGetMagic'][] = 'wfCalendarLanguageGetMagic';
  
Line 19: Line 19:
  
 
function wfCalendarLanguageGetMagic(&$magicWords,$langCode = 0) {
 
function wfCalendarLanguageGetMagic(&$magicWords,$langCode = 0) {
        $magicWords['calendar'] = array(0,'calendar');
+
$magicWords['calendar'] = array(0,'calendar');
        return true;
+
return true;
        }
+
}
  
 
function wfSetupSimpleCalendar() {
 
function wfSetupSimpleCalendar() {
        global $wgParser;
+
global $wgParser;
        $wgParser->setFunctionHook('calendar','wfRenderCalendar');
+
$wgParser->setFunctionHook('calendar','wfRenderCalendar');
        return true;
+
return true;
        }
+
}
  
 
# Renders a table of all the individual month tables
 
# Renders a table of all the individual month tables
 
function wfRenderCalendar(&$parser) {
 
function wfRenderCalendar(&$parser) {
        $argv = array();
+
$parser->mOutput->mCacheTime = -1;
        foreach (func_get_args() as $arg) if (!is_object($arg)) {
+
$argv = array();
                if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]]=$match[2];
+
foreach (func_get_args() as $arg) if (!is_object($arg)) {
                }
+
if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]]=$match[2];
        if (isset($argv['format'])) $f = $argv['format']; else $f = '%e %B %Y';
+
}
        if (isset($argv['title']))  $p = $argv['title'].'/'; else $p = '';
+
if (isset($argv['format'])) $f = $argv['format']; else $f = '%e %B %Y';
        if (isset($argv['query']))  $q = $argv['query'].'&action=edit'; else $q = 'action=edit';
+
if (isset($argv['title']))  $p = $argv['title'].'/'; else $p = '';
        if (isset($argv['year']))  $y = $argv['year']; else $y = date('Y');
+
if (isset($argv['query']))  $q = $argv['query'].'&action=edit'; else $q = 'action=edit';
        if (isset($argv['month'])) {
+
if (isset($argv['year']))  $y = $argv['year']; else $y = date('Y');
                $m = $argv['month'];
+
if (isset($argv['month'])) {
                return wfRenderMonth(strftime('%m',strtotime("$y-$m-01")),$y,$p,$q,$f);
+
$m = $argv['month'];
                } else $m = 1;
+
return wfRenderMonth(strftime('%m',strtotime("$y-$m-01")),$y,$p,$q,$f);
        $table = "{| class=\"calendar\"\n";
+
} else $m = 1;
        for ($rows = 3; $rows--; $table .= "|-\n")
+
$table = "{| class=\"calendar\"\n";
                for ($cols = 0; $cols < 4; $cols++)
+
for ($rows = 3; $rows--; $table .= "|-\n")
                        $table .= '|'.wfRenderMonth($m++,$y,$p,$q,$f)."\n";
+
for ($cols = 0; $cols < 4; $cols++)
        return "$table\n|}\n";
+
$table .= '|'.wfRenderMonth($m++,$y,$p,$q,$f)."\n";
        }
+
return "$table\n|}\n";
 +
}
  
 
# Return a calendar table of the passed month and year
 
# Return a calendar table of the passed month and year
 
function wfRenderMonth($m,$y,$prefix = '',$query = '',$format = '%e %B %Y') {
 
function wfRenderMonth($m,$y,$prefix = '',$query = '',$format = '%e %B %Y') {
        $thisDay  = date('d');
+
$thisDay  = date('d');
        $thisMonth = date('n');
+
$thisMonth = date('n');
        $thisYear  = date('Y');
+
$thisYear  = date('Y');
        if (!$d = date('w',$ts = mktime(0,0,0,$m,1,$y))) $d = 7;
+
if (!$d = date('w',$ts = mktime(0,0,0,$m,1,$y))) $d = 7;
        $month = strftime('%B',$ts);
+
$month = strftime('%B',$ts);
        $table = "\n{| class=\"month\"\n|- class=\"heading\"\n|colspan=\"7\"|$month\n|- class=\"dow\"\n";
+
$table = "\n{| class=\"month\"\n|- class=\"heading\"\n|colspan=\"7\"|$month\n|- class=\"dow\"\n";
        $table .= "|M||T||W||T||F||S||S\n";
+
$table .= "|M||T||W||T||F||S||S\n";
        if ($d > 1) $table .= "|-".str_repeat("\n|&nbsp;\n",$d-1);
+
if ($d > 1) $table .= "|-".str_repeat("\n|&nbsp;\n",$d-1);
        for ($i = $day = $d; $day < 32; $i++) {
+
for ($i = $day = $d; $day < 32; $i++) {
                $day = $i - $d + 1;
+
$day = $i - $d + 1;
                if ($day < 29 or checkdate($m,$day,$y)) {
+
if ($day < 29 or checkdate($m,$day,$y)) {
                        if ($i%7 == 1) $table .= "\n|-\n";
+
if ($i%7 == 1) $table .= "\n|-\n";
                        $t = ($day == $thisDay and $m == $thisMonth and $y == $thisYear) ? ' class="today"' : '';
+
$t = ($day == $thisDay and $m == $thisMonth and $y == $thisYear) ? ' class="today"' : '';
                        $ttext = $prefix.trim(strftime($format,mktime(0,0,0,$m,$day,$y)));
+
$ttext = $prefix.trim(strftime($format,mktime(0,0,0,$m,$day,$y)));
                        $title = Title::newFromText($ttext);
+
$title = Title::newFromText($ttext);
                        $class = $title->exists() ? 'day-active' : 'day-empty';
+
$class = $title->exists() ? 'day-active' : 'day-empty';
                        $url = $title->getFullURL($title->exists() ? '' : $query);
+
$url = $title->getFullURL($title->exists() ? '' : $query);
                        $table .= "|$t|[$url <span class='$class'>$day</span>]\n";
+
$table .= "|$t|[$url <span class='$class'>$day</span>]\n";
                        }
+
}
                }
+
}
        return "$table\n|}";
+
return "$table\n|}";
        }
+
}
 
?>
 
?>

Revision as of 22:22, 11 June 2007

<?php

  1. MediaWiki SimpleCalendar ExtensionTemplate:Php
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.
  1. - See http://www.mediawiki.org/wiki/Extension:Simple_Calendar for installation and usage details
  2. - Licenced under LGPL (http://www.gnu.org/copyleft/lesser.html)
  3. - Author: http://www.organicdesign.co.nz/nad

define('SIMPLECALENDAR_VERSION','1.2.2, 2007-06-11');

$wgExtensionFunctions[] = 'wfSetupSimpleCalendar'; $wgHooks['LanguageGetMagic'][] = 'wfCalendarLanguageGetMagic';

$wgExtensionCredits['parserhook'][] = array( 'name' => 'Simple Calendar', 'author' => 'User:Nad', 'description' => 'A simple calendar extension', 'url' => 'http://www.mediawiki.org/wiki/Extension:Simple_Calendar', 'version' => SIMPLECALENDAR_VERSION );

function wfCalendarLanguageGetMagic(&$magicWords,$langCode = 0) { $magicWords['calendar'] = array(0,'calendar'); return true; }

function wfSetupSimpleCalendar() { global $wgParser; $wgParser->setFunctionHook('calendar','wfRenderCalendar'); return true; }

  1. Renders a table of all the individual month tables

function wfRenderCalendar(&$parser) { $parser->mOutput->mCacheTime = -1; $argv = array(); foreach (func_get_args() as $arg) if (!is_object($arg)) { if (preg_match('/^(.+?)\\s*=\\s*(.+)$/',$arg,$match)) $argv[$match[1]]=$match[2]; } if (isset($argv['format'])) $f = $argv['format']; else $f = '%e %B %Y'; if (isset($argv['title'])) $p = $argv['title'].'/'; else $p = ; if (isset($argv['query'])) $q = $argv['query'].'&action=edit'; else $q = 'action=edit'; if (isset($argv['year'])) $y = $argv['year']; else $y = date('Y'); if (isset($argv['month'])) { $m = $argv['month']; return wfRenderMonth(strftime('%m',strtotime("$y-$m-01")),$y,$p,$q,$f); } else $m = 1; $table = "{| class=\"calendar\"\n"; for ($rows = 3; $rows--; $table .= "|-\n") for ($cols = 0; $cols < 4; $cols++) $table .= '|'.wfRenderMonth($m++,$y,$p,$q,$f)."\n"; return "$table\n|}\n"; }

  1. Return a calendar table of the passed month and year

function wfRenderMonth($m,$y,$prefix = ,$query = ,$format = '%e %B %Y') { $thisDay = date('d'); $thisMonth = date('n'); $thisYear = date('Y'); if (!$d = date('w',$ts = mktime(0,0,0,$m,1,$y))) $d = 7; $month = strftime('%B',$ts); $table = "\n{| class=\"month\"\n|- class=\"heading\"\n|colspan=\"7\"|$month\n|- class=\"dow\"\n"; $table .= "|M||T||W||T||F||S||S\n"; if ($d > 1) $table .= "|-".str_repeat("\n| \n",$d-1); for ($i = $day = $d; $day < 32; $i++) { $day = $i - $d + 1; if ($day < 29 or checkdate($m,$day,$y)) { if ($i%7 == 1) $table .= "\n|-\n"; $t = ($day == $thisDay and $m == $thisMonth and $y == $thisYear) ? ' class="today"' : ; $ttext = $prefix.trim(strftime($format,mktime(0,0,0,$m,$day,$y))); $title = Title::newFromText($ttext); $class = $title->exists() ? 'day-active' : 'day-empty'; $url = $title->getFullURL($title->exists() ?  : $query); $table .= "|$t|[$url $day]\n"; } } return "$table\n|}"; } ?>