Extension:Calendar (Shane) extended

From MediaWiki.org

Jump to: navigation, search
Calendar extended.png

With this script I added functionality to the calendar similar to the phpwiki calendar:

  • every day is a link to a page. When you click that link, you automatically start editing that page. (current day is marked in bold, days that have an event are underlined)
  • on bottom of the calendar, all events (days that are edited) are summed up + their content is shown
  • the calendar defaults to the "current month"
  • You can follow links to next or previous months

I changed first day of the week from sunday -> monday, this is easily changed:

  • change order in $wday_names
  • add/change this code in function dayOfWeek($month,$year) :
                $weekday_number_b = (($day - 7 * floor($day / 7))); // 
                if ($weekday_number_b == 0) {
                                          $weekday_number = 6;
                }
                else
                {
                                          $weekday_number = ($weekday_number_b - 1);
                }

No doubt this is code is quite ugly, and not very portable(language,html,css..?) but it works mighty fine here, maybe someone can use it too...


This script now works on MediaWiki 1.9.x and should work on all previous versions from 1.6 upwards. Micrology 19:37, 6 May 2007 (UTC)

[edit] Install

Create a file called 'calendar_extension.php' in your extensions/ directory, and add code below. Then simply add the following line to the end of your LocalSettings.php file:

include("extensions/calendar_extension.php");

and you are ready to go! Go to the sandbox and see if it works!

[edit] calendar_extension.php

<?php
# Calendar Extension
# Creates a calendar of the month and year.
#
# Example Code
#
# <calendar></calendar>
# this defaults to "current month"
#
# (I guess you can still use <calendar>2005-05</calendar> as well, in that case you won't be able to jump to next mont)
#
# (extended by petervanmechelen at gmail dot com)
#
# patched for working with wikimedia 1.7.0 by Dirk Schneider <info _at_ dischneider _dot_ de>
#
# 2006-07-12
# patched to show "today" marked only in current month and current year
# patched to work with mw 1.7.0
# patched to let links to previous and next month work
#
# 2007-05-12
# patched to call the Parser in a way that works on 1.9.2+ by Nigel Gilbert <n.gilbert_at_surrey.ac.uk>
# also a number of minor changes and corrections
#
# 2007-10-23
# Added functionality to show upcoming events from the next $no_upcoming_days, even if those events happen in the next month.
# By Michal Bryc mediawiki_calendar_extension _at_ skipatrolguy.com

$wgExtensionFunctions[] = "wfCalendarExtension";
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'Calendar',
        'author' => 'Shane',
        'description' => 'adds <calender> tag, for calender creation',
        'url' => 'http://meta.wikimedia.org/wiki/Calendar_extension'
);
 
/* DO NOT EDIT BEYOND THIS LINE */
 
function wfCalendarExtension() {
    global $wgParser;
    $wgParser->setHook( "calendar", "createmwCalendar" );
}
 
# The callback function for converting the input text to HTML output
function createmwCalendar($input, $argv, &$parser)
{
		$parser->disableCache();
 
    /**
    * check if date in $_GET-parameter
    * fallback on default this month
    **/
        if($input=="")
        {
            if(isset($_GET['month'])&&(isset($_GET['year'])))
            {
                $input = ($_GET['month']<10?"0":"").date($_GET['month']." ".$_GET['year']);
            }
            else
            {
                $input = date("m Y");
            }
        } 
 
	$ret = $parser->parse($input, $parser->getTitle(), $parser->getOptions(), false, false); 
        $array = explode(' ', $ret->getText());
 
        $month = $array[0];
        $year = $array[1];
 
        $mwCalendar = new mwCalendar();
 
        $mwCalendar->dateNow($month, $year);
        return $mwCalendar->showThisMonth($parser);
}
 
 
class mwCalendar
{
        var $cal = "CAL_GREGORIAN";
        var $format = "%Y%m%d";
        var $today;
        var $day;
        var $month;
        var $year;
        var $pmonth;
        var $pyear;
        var $nmonth;
        var $nyear;
// German weekday names
//        var $wday_names = array("Mo","Di","Mi","Do","Fr","Sa","So"); // put sunday first to change order of the days
// English weekday names
        var $wday_names = array("Mo","Tu","We","Th","Fr","Sa","So"); // put sunday first to change order of the days
// German month names
//        var $wmonth_names = array("Januar", "Februar", "M&auml;rz", "April", "Mai", "Juni","Juli", "August","September", "Oktober", "November", "Dezember");
//English month names
        var $wmonth_names = array("January", "February", "March", "April", "May", "June", "July", "August", "September"," October", "November", "December");
 
        function mwCalendar()
        {
                $this->day = "1";
                $today = "";
                $month = "";
                $year = "";
                $pmonth = "";
                $pyear = "";
                $nmonth = "";
                $nyear = "";
        }
 
 
        function dateNow($month,$year)
        {
                $this->month = $month;
                $this->year = $year;
                $this->today = strftime("%d",time());
                $this->pmonth = $this->month - 1;
                $this->pyear = $this->year - 1;
                $this->nmonth = $this->month + 1;
                $this->nyear = $this->year + 1;
        }
 
        function daysInMonth($month,$year)
        {
                if (empty($year))
                {
                        $year = mwCalendar::dateNow("%Y");
                }
                if (empty($month))
                {
                        $month = mwCalendar::dateNow("%m");
                }
                if($month == "2")
                {
                        if(mwCalendar::isLeapYear($year))
                        {
                                return 29;
                        }
                        else
                        {
                                return 28;
                        }
                }
                else if ($month == "4" || $month == "6" || $month == "9" || $month == "11")
                {
                        return 30;
                }
                else
                {
                        return 31;
                }
        }
 
        function isLeapYear($year)
        {
            return (($year % 4 == "0" && $year % 100 != "0") || $year % 400 == "0");
        }
 
        function dayOfWeek($month,$year)
        { 
        $weekday_no = date("w", strtotime("1 ".$this->wmonth_names[$month - 1]." $year"));
        if ($weekday_no == 0) return 6;
        else return $weekday_no - 1;
	      }
 
        function showThisMonth(&$parser)
        {
                global $wgScript;
                $viewEvents = "";
                $lastyear = ($this->month==1?$this->year - 1:$this->year);
                $nextyear = ($this->month==12?$this->year + 1:$this->year);
                $lastmonth = ($this->month==1? 12 : $this->month - 1);
                $nextmonth = ($this->month==12? 1 : $this->month + 1);
                $currentpage = "http://".$_SERVER['SERVER_NAME'].(strpos($_SERVER['REQUEST_URI'],"?")?substr($_SERVER['REQUEST_URI'],0,strpos($_SERVER['REQUEST_URI'],"?")+1):$_SERVER['REQUEST_URI']."?");
                $params = explode("&",$_SERVER['QUERY_STRING']);
                for($i=0;$i<count($params);$i++)
                {
                    if((substr($params[$i],0,5)!="month") && (substr($params[$i],0,4)!="year"))
                    {
                        $currentpage .= $params[$i]."&";
                    }
                }
                if (strpos($currentpage,'action=purge')) {
                        $purge="";
                } else {
                        $purge="action=purge&";
                }
                $a_lastmonth = $currentpage.$purge."month=".$lastmonth."&year=".$lastyear;
                $a_nextmonth = $currentpage.$purge."month=".$nextmonth."&year=".$nextyear;
 
                $output = '<table cellpadding="4" cellspacing="0" class="calendar">';
                $output .= '<tr><td><a href="'.$a_lastmonth.'"><</a></td><td colspan="5" class="cal-header"><center>'. $this->wmonth_names[$this->pmonth] . " " .$this->year .'</cen
ter></td><td><a href="'.$a_nextmonth.'">></a></td></tr>';
                $output .= '<tr style="background:#EEEEEE;">';
                for($i=0;$i<7;$i++)
                        $output .= '<td>'. $this->wday_names[$i]. '</td>';
                $output .= '</tr>';
                $wday = mwCalendar::dayOfWeek($this->month,$this->year);
                $no_days = mwCalendar::daysInMonth($this->month,$this->year);
                $count = 1;
                $output .= '<tr>';
                for($i=1;$i<=$wday;$i++)
                {
                    $output .= '<td> </td>';
                    $count++;
                }
                /**
                * every day is edit link to that day
                **/
                for($i=1;$i<=$no_days;$i++)
                {
                    $dayNr = ($i<10?"0".$i:$i);
                    $articleName= $this->year."-".$this->month."-".$dayNr;
                    $alinkedit = "http://".$_SERVER['SERVER_NAME'].$wgScript."?title=".$articleName /*."&action=edit"*/;
 
                    $wl_title = Title::newFromText ( $articleName );
                    if ($wl_title->exists())
                    {
                        $thisDayExists = true;
                        $alinkeditstyle = 'style="text-decoration:underline;"';
                        $existsStyle =  "background-color:#FFCC66;";
 
                        // contents of event goes under the calendar...
                        $article = new Article ( $wl_title );
                        $ret = $parser->parse($article->getContent(), $parser->getTitle(), $parser->getOptions(), true, false);
                        $articleContent = $ret->getText();
                        $viewEvents .= "<div style=\"padding:10px\"><a href=\"http://".$_SERVER['SERVER_NAME'].$wgScript."/$articleName\">".$articleName."</a><br />".$articleContent.'</div><br>';
                    }
                    else
                    {
                        $thisDayExists = false;
                        $alinkeditstyle = '';
                        $existsStyle =  "";
                    }
                    if($count > 6)
                    {
                        if(($i == $this->today) && ($this->month == strftime("%m",time())) && ($this->year == strftime("%Y",time())))
                        {
                            $output .= '<td style="text-align:right; font-weight:bold; background-color:#CCFFCC;"><a href="'.$alinkedit.'" '.$alinkeditstyle.'>' . $i . '</a></td></tr>';
                        }
                        else
                        {
                        	$output .= '<td style="text-align:right;' . $existsStyle .'"><a href="'.$alinkedit.'" '.$alinkeditstyle.'>' . $i . '</a></td></tr>';
                        }
                          $count = 0;
                    }
                    else
                    {
                    	if(($i == $this->today) && ($this->month == strftime("%m",time())) && ($this->year == strftime("%Y",time())))
                      {
                      	$output .= '<td style="text-align=right; font-weight:bold; background-color:#CCFFCC;"><a href="'.$alinkedit.'" '.$alinkeditstyle.'>' . $i . '</a></td>';
                      }
                      else
                      {
                         $output .= '<td style="text-align:right;' . $existsStyle . '"><a href="'.$alinkedit.'" '.$alinkeditstyle.'>' . $i . '</a></td>';
                      }
                    }
                    $count++;
                }
                if ($count> 1) for($i=$count;$i<=7;$i++)
                {
                    $output .= "<td> </td>";
                }
                $output .= '</tr></table><br>';
 
                /**
                * Show events for this month
                **/
                $output .= $viewEvents;
 
 
                return $output;
        }
}
?>

[edit] Showing Upcoming Events

If you'd like to have the ability to display a list of upcoming events from your calendar, then find the line

$wgParser->setHook( "calendar", "createmwCalendar" );

and immediately after it, add the line

$wgParser->setHook( "upcomingevents", "createmwUpcoming" );

Then find the function

function createmwCalendar($input, $argv, &$parser)

and after it, add the code

function createmwUpcoming($input, $argv, &$parser)
{
    $parser->disableCache();
 
    /**
    * check if date in $_GET-parameter
    * fallback on default this month
    **/
        if($input=="")
        {
            if(isset($_GET['month'])&&(isset($_GET['year'])))
            {
                $input = ($_GET['month']<10?"0":"").date($_GET['month']." ".$_GET['year']);
            }
            else
            {
                $input = date("m Y");
            }
        } 
 
        $ret = $parser->parse($input, $parser->getTitle(), $parser->getOptions(), false, false); 
        $array = explode(' ', $ret->getText());
 
        $month = $array[0];
        $year = $array[1];
 
        $mwCalendar = new mwCalendar();
 
        $mwCalendar->dateNow($month, $year);
        return $mwCalendar->getUpcomingEvents($parser);
}

Finally, find the function

function showThisMonth(&$parser)

and immediately after it add the function

function getUpcomingEvents(&$parser)
        {
          global $wgScript;
          $viewEvents = "";
          $nextyear = ($this->month==12?$this->year + 1:$this->year);
          $nextmonth = ($this->month==12? 1 : $this->month + 1);
          $currentpage = "http://".$_SERVER['SERVER_NAME'].(strpos($_SERVER['REQUEST_URI'],"?")?substr($_SERVER['REQUEST_URI'],0,strpos($_SERVER['REQUEST_URI'],"?")+1):$_SERVER['REQUEST_URI']."?");
          $params = explode("&",$_SERVER['QUERY_STRING']);
          for($i=0;$i<count($params);$i++)
          {
              if((substr($params[$i],0,5)!="month") && (substr($params[$i],0,4)!="year"))
              {
                  $currentpage .= $params[$i]."&";
              }
          }
 
          $no_days_this_month = mwCalendar::daysInMonth($this->month,$this->year);
          $no_days_next_month = mwCalendar::daysInMonth($nextmonth, $nextyear);
 
          // Number of days of upcoming events to list. Don't set to over 31.
          $no_upcoming_days = 31;
 
          // Limit the number of upcoming events to list.
          $max_num_events = 5;
 
          $num_events_found = 0;
          for($i=$this->today;$i<=$this->today + $no_upcoming_days;$i++)
          {
            $day = $i;
            $month = $this->month;
            if( $i > $no_days_this_month ) 
            {
              $day = $i - $no_days_this_month;
              $month = $nextmonth;
            }
            $dayNr = ($day < 10 ? "0".$day : $day);
            $articleName= $this->year . "-" . $month . "-" . $dayNr;
 
            $wl_title = Title::newFromText ( $articleName );
            if ($wl_title->exists() && $num_events_found < $max_num_events)
            {
              $num_events_found++;
 
              // Get the article contents
              $article = new Article ( $wl_title );
              $ret = $parser->parse($article->getContent(), $parser->getTitle(), $parser->getOptions(), true, false);
              $articleContent = $ret->getText();
              //$viewEvents .= "<div style=\"padding:10px\"><a href=\"http://".$_SERVER['SERVER_NAME'].$wgScript."/$articleName\">".$articleName."</a><br />".$articleContent.'</div><br>';
              $viewEvents .= "<li><a href=\"http://".$_SERVER['SERVER_NAME'].$wgScript."?title=".$articleName . "\">".$articleName."</a><br />".$articleContent.'</li>';
            }
          }
 
          return "<ul>" . $viewEvents . "</ul>";
        }

Note that you can set $max_num_events and $no_upcoming_days to values which will limit the total number of events shown to the next $max_num_events events and show only events occurring in the next $no_upcoming_days days. To display upcoming events, place a <upcomingevents/> tag in the location where you want upcoming events to appear.


Hallo,

Fatal error: Call to a member function exists() on a non-object in C:\xampp\htdocs\mediawiki\extensions\calendar_extension.php on line 217

Diese Meldung erscheit am Bildschirm wenn ich den Kalender installiere. Sie zeigt auf die Zeile "if ($wl_title->exists())".

An was kann das liegen? Gruss, TP