Extension talk:Calendar (Shane) extended

I've made a few changes here:
 * using $wgScript instead of $_SERVER values - this works if the wiki is installed in a subdir
 * a few other cleanups I found necessary to get it to work on 1.6.3
 * using "action=purge" in the last/next month links. This takes care of caching, though it isn't perfect.

Tom 

<?php
 * 1) Calendar Extension
 * 2) Creates a calendar of the month and year.
 * 3) Example Code
 * 4) this defaults to "current month"
 * 5) (I guess you can still use 2005-05 as well, in that case you won't be able to jump to next mont)
 * 6) (extended by petervanmechelen at gmail dot com)
 * 1) this defaults to "current month"
 * 2) (I guess you can still use 2005-05 as well, in that case you won't be able to jump to next mont)
 * 3) (extended by petervanmechelen at gmail dot com)
 * 1) (extended by petervanmechelen at gmail dot com)
 * 1) (extended by petervanmechelen at gmail dot com)

$wgExtensionFunctions[] = "wfCalendarExtension"; $wgExtensionCredits['parserhook'][] = array(       'name' => 'Calendar',        'author' => 'Shane',        'description' => 'adds 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" ); }

function createmwCalendar($input) {   /**    * 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"); }       }        global $wgOut; $input = $wgOut->parse($input, false); $array = explode(' ', $input);
 * 1) The callback function for converting the input text to HTML output

$month = $array[0]; $year = $array[1];

$mwCalendar = new mwCalendar;

$mwCalendar->dateNow($month, $year); return $mwCalendar->showThisMonth; }

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; var $wday_names = array("Mo","Di","Mi","Do","Fr","Sa","So"); // put sunday first to change order of the days var $wmonth_names = array("Januar","Februar","M&auml;rz","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember");

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) {               if ($month > 2) { $month -= 2; }               else {                               $month += 10; $year--; }

$day = ( floor((13 * $month - 1) / 5) +                                                $this->day + ($year % 100) +                                                floor(($year % 100) / 4) +                                                floor(($year / 100) / 4) - 2 *                                                floor($year / 100) + 77);

$weekday_number = (($day - 7 * floor($day / 7)))-1; // remove -1 to make sunday first day of the week

return $weekday_number; }       function getWeekDay {               $week_day = mwCalendar::dayOfWeek($this->month,$this->year); //return $this->wday_names[$week_day]; return $week_Day; }

function showThisMonth {               global $wgScript; $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="&amp;"; } else { $purge="action=purge&amp;"; }               $a_lastmonth = $currentpage.$purge."month=".$lastmonth."&amp;year=".$lastyear; $a_nextmonth = $currentpage.$purge."month=".$nextmonth."&amp;year=".$nextyear;

$output = ' ';

/**               * Show events for this month **/               $output .= $viewEvents;

return $output; } } ?>

Not sure what this bug is
I use the modified code, because my wiki is in a subfolder of my domain,

but, nowiki, task etc give me something like that

UNIQ1278c00d130ff9b6-nowiki63155acb276802d00000002

Same problem with the original code.

Documentation Bug
The comments at the top of the file are incorrect. The correct usage is: 7 2006

Note the ' ' in the explode call and the '-' in the comments, need to change one or the other.

Next/Previous buttons
I had to modify line 177 like this to make those work:

$currentpage .= $params[$i]."&";

Otherwise the URL looked like this :

index.php?title=Accueilaction=purgemonth=7&year=2006

also needed to modify line 181 $purge="";

Prev/Next month buttons work only when logged
Prev/next month buttons work fine when the user is logged (no need of sysop status). But when the user is not logged, they lead to a page asking for a confirmation for purging the cache then go back to the main page with an unchanged month.

Comment: Thats not true, im logged, and even they dont work.

Pretty looking
Hi, how did you manage this pretty looking calendar (your png image, so nicely framed? Can I ask how you achived all this? Thanks! Guy

Hi,

I use the default theme monobook and didn't change much at all: in skins/monobook/main.css I added .calendarEventTitle a{ font-weight: bold; }

div.calendarEventContent{ padding-left: 12px; }

table.calendar, table.calendar td{ border-style: solid; border-width: thin; border-color: #AAAAAA; text-align: center; } that should do it? Peter

Yes this did it! Now the calendar looks good, thanks so much! Guy

Windows Adjustment
REQUEST_URI not defined on Windows systems, change to PHP_SELF. --Julie 18:12, 6 July 2006 (UTC)

Patch to work with mediawiki 1.7.0
To do calendar_extension_extended working with mediawiki 1.7.0 I had to patch calendar. I used the version taken from this discussion.

At line 220/221 I had to change

$article       = new Article( $title ); $articleContext = getPreloadedText( $articleName );

to

$article       = new Article( $wl_title ); $articleContext = getContext( $article );

First line will define the article, $title isn't declared. Second line gets the context of the article, getPreloadedText is not available in 1.7.0 (deprcated?).

Works fine, but if the content contains links like

this is an external link

it will be shown like using nowiki-Tags. Anybody any idea?

Dischneider 18:06, 11 July 2006 (UTC)

After applying all the patches this script only works halfway for me with 1.7.0. In particular:
 * cannot change month (url pattern called on click is like ".../index.php?title=Sandboxaction=submitaction=purge&month=8&year=2006")
 * list of events does not show up below calendar

--Eric Pichon 19:56, 11 July 2006 (UTC)

Patched again
Due to some errors I patched calendar_extension_extended again. Now, the described function next and previous month is working well. As addon I changed the function to show the calendar to mark the actually date only in current month and year as today. With the old extension every "today" in every month and every year were be marked. You can see it working at http://wiki.wemelug.de - please don't put any event without relevance to wemelug into, it's not an testing system!

Additional minor changes: link to existing date-pages are no longer in edit mode.

Here comes my complete script, thanks a lot

Dischneider 08:32, 12 July 2006 (UTC)


 * 8) 2006-07-12
 * 9) patched to show "today" marked only in current month and current year
 * 10) patched to work with mw 1.7.0
 * 11) patched to let links to previous and next month work
 * 12) todo -> HowTo disable cache for only one site?
 * 1) patched for working with wikimedia 1.7.0 by Dirk Schneider 
 * 2) 2006-07-12
 * 3) patched to show "today" marked only in current month and current year
 * 4) patched to work with mw 1.7.0
 * 5) patched to let links to previous and next month work
 * 6) todo -> HowTo disable cache for only one site?
 * 1) todo -> HowTo disable cache for only one site?
 * 1) todo -> HowTo disable cache for only one site?

$wgExtensionFunctions[] = "wfCalendarExtension"; $wgExtensionCredits['parserhook'][] = array(       'name' => 'Calendar',        'author' => 'Shane',        'description' => 'adds 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" ); }

function createmwCalendar($input) {   /**    * 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"); }       }        global $wgOut; $input = $wgOut->parse($input, false); $array = explode(' ', $input);
 * 1) The callback function for converting the input text to HTML output

$month = $array[0]; $year = $array[1];

$mwCalendar = new mwCalendar;

$mwCalendar->dateNow($month, $year); return $mwCalendar->showThisMonth; }

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; var $wday_names = array("Mo","Di","Mi","Do","Fr","Sa","So"); // put sunday first to change order of the days var $wmonth_names = array("Januar","Februar","März","April","Mai","Juni","Juli","August","September","Oktober","November","Dezember");

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) {               if ($month > 2) { $month -= 2; }               else {                               $month += 10; $year--; }

$day = ( floor((13 * $month - 1) / 5) +                                                $this->day + ($year % 100) +                                                floor(($year % 100) / 4) +                                                floor(($year / 100) / 4) - 2 *                                                floor($year / 100) + 77);

$weekday_number = (($day - 7 * floor($day / 7)))-1; // remove -1 to make sunday first day of the week

return $weekday_number; }       function getWeekDay {               $week_day = mwCalendar::dayOfWeek($this->month,$this->year); //return $this->wday_names[$week_day]; return $week_Day; }

function showThisMonth {               global $wgScript; $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 = ' ';

/**               * Show events for this month **/               $output .= $viewEvents;

return $output; } } ?>

One more patch
Even the above code contains a small bug.

In function createmwCalendar the following line (it's line 49) $input = ($_GET['month']<10?"0":"").date($_GET['month']." ".$_GET['year']); should probably read: $input = ($_GET['month']<10?"0":"").$_GET['month']." ".$_GET['year'];

Only display a headline under the calendar.
I used Dischneider's script, but changed a few lines, because I wanted to show just a heading under the calendar. -Trond

$articleContent = $article->getContent; // get title // == The first title like this == // will be the text under the calender // == Can also be breaked Like this ==

ereg("(\== )([^==]*)(.*)", $articleContent, $articleContentOut) ;

//                     $revision = Revfision::newFromTitle($wl_title);

$viewEvents .= "".$articleName." ".$articleContentOut[2].' ';

next - previous month doesn't works
if you're not logged it ask for you to purge or not, same probleme with IE and FF version 1.6.8. And if you have some event recorded in a day, when you click on this day (in the calendar) it display you the edit pahe, and not the daily page (in readable version)

thanks for all

Day of week - monday first?
To have monday instead of sunday first day of week, you need to add some code.

This is what the code looks like when it works:

and

On the first version of this code, months when first day of month was sunday, went wrong.

Change Article Name to German/European Data Format -> dd.mm.yyyy
edit

$articleName = $this->year."-".$this->month."-".$dayNr;

to

$articleName = $dayNr.".".$this->month.".".$this->year;

Adding an Entry Error
I'm running mediawiki 1.6.8. If I click on a date to add an entry, it tries to go to the index.php page but it tells me:

The requested URL /index.php was not found on this server.

Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.


 * see below -tb

Version 1.8.2
Is there a version or calendar that functions correctly with WM 1.8.2?

I tried all of the variations mentioned here in the discussions, and can not get the month change to work, nor the year to change.

Thanks.

edit: apologies, with the initialization of the calendar without any date (eg, not 11 2006 ) it works as it should.

Much thanks.

Updates that I need
I use 1.8.2 with variables in the URL. Therefor I need to update the Script: Line 234 to: $viewEvents .= "".$articleName." ".$articleContent.' '; otherwise the URL is wrong and it links to index.php

additional there is a mistake in line 83 with März (for the german) M&amp;auml;rz -- de:the0bone

Patch
if ($wl_title->exists) {           $thisDayExists = true; $alinkeditstyle = 'style="text-decoration:underline;"';

// contents of event goes under the calendar... $article = new Article ( $wl_title ); #      $articleContent = $article->getContent; $editp = new EditPage($article); $ep = new Parser; $ep->firstCallInit; $articleContent = $ep->parse($article->getContent,$wl_title,ParserO ptions::newFromUser( $temp = NULL ));

//                     $revision = Revfision::newFromTitle($wl_title );

$viewEvents .= "".$articleName." ".$articleContent->getText.' ';   }
 * 1) $viewEvents .= " ".$wl_title." ";

Mediawiki 1.9.2
I am using the patched for 1.70 version above on MediaWiki 1.9.2 and have tried using  as well as 02 2007 as also suggested above.

I get the following error: �UNIQde5ccdf26a49ab0-calendar-00000002-QINU

Does anybody know a fix for this? Many thanks.

COMMENT: Hi, I am using Mediawiki 1.8.3 and I get a similar error when using two calendar-tags on the same article page. I also get this kind of error when using the CategoryTree extension on the same article page. So maybe try using the calendar-tag only once (since nowiki-tags don't seem to work either), or without any other tag-based extensions. Hope this helps (I know it probably doesn't) ;o) -- Tri

COMMENT: Using 1.9.2 I have the same problem. Me for 1.9.3 aswell.

Fixed! The code in the article has now been fixed so that it works with 1.9.2 etc. Micrology 19:41, 6 May 2007 (UTC)

Simpler "dayOfWeek"
Hey - I have replaced the code of "function dayOfWeek" to this one: function dayOfWeek($month,$year) { return date("w",mktime(0,0,0,$month,1,$year)); That's all!

How easy it is... And it starts with Sunday, not Monday.

I'll make some more patches, probably, and post them here. Peleg 11:01, 14 May 2007 (UTC)