Extension talk:Calendar (Shane) extended

Latest comment: 16 years ago by Dieudo in topic Simpler "dayOfWeek"
The following discussion has been transferred from Meta-Wiki.
Any user names refer to users of that site, who are not necessarily users of MediaWiki.org (even if they share the same username).

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 <tom@lemuria.org>

<?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)

$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)
{
    /**
    * 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);

        $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 = '<table cellpadding="4" cellspacing="0" class="calendar">';
                $output .= '<tr><td><a href="'.$a_lastmonth.'"><</a></td><td colspan="5" class="cal-header"><div style="text-align: 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;"';

                        // contents of event goes under the calendar...
                        $article = new Article ( $title );
                        $articleContent = $article->getPreloadedText($articleName);

                        $viewEvents .= "<div class=\"calendarEventTitle\"><a href=\"http://".$_SERVER['SERVER_NAME'].$wgScript."?title='.$articleName.'&action=edit\">".$articleName."</a></div>";
                        $viewEvents .= '<div class="calendarEventContent">'.$articleContent.'</div><br>';
                    }
                    else
                    {
                        $thisDayExists = false;
                        $alinkeditstyle = '';
                    }
                    if($count > 6)
                    {
                                        if($i == $this->today)
                                        {
                                                $output .= '<td><b><a href="'.$alinkedit.'" '.$alinkeditstyle.'>' . $i . '</a></b></td></tr>';
                                        }
                                        else
                                        {
                                                $output .= '<td class="cal-weekend"><a href="'.$alinkedit.'" '.$alinkeditstyle.'>' . $i . '</a></td></tr>';
                                        }
                                        $count = 0;
                    }
                    else
                    {
                                        if($i == $this->today)
                                        {
                                                $output .= '<td class="cal-today"><b><a href="'.$alinkedit.'" '.$alinkeditstyle.'>' . $i . '</a></b></td>';
                                        }
                                        else

                                        {
                                                $output .= '<td><a href="'.$alinkedit.'" '.$alinkeditstyle.'>' . $i . '</a></td>';
                                        }
                    }
                    $count++;
                }
                for($i=$count;$i<=7;$i++)
                {
                    $output .= "<td> </td>";
                }
                $output .= '</tr></table><br>';

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


                return $output;
        }
}
?>


Bugs

edit

Not sure what this bug is

edit

I use the modified code, because my wiki is in a subfolder of my domain,

but <anything></anything>, nowiki, task etc give me something like that

UNIQ1278c00d130ff9b6-nowiki63155acb276802d00000002

Same problem with the original code.

Documentation Bug

edit

The comments at the top of the file are incorrect. The correct usage is:

 <calendar>7 2006</calendar>

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

Next/Previous buttons

edit

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

edit

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

edit

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

edit

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

Patch to work with mediawiki 1.7.0

edit

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

[http://www.externallink.tld this is an external link]

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

Dischneider 18:06, 11 July 2006 (UTC)Reply

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)Reply

Patched again

edit

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)Reply

<?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
#
# todo -> HowTo disable cache for only one site?

$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)
{
    /**
    * 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);

        $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 = '<table cellpadding="4" cellspacing="0" class="calendar">';
                $output .= '<tr><td><a href="'.$a_lastmonth.'"><</a></td><td colspan="5" class="cal-header"><div style="text-align: 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;"';

                        // contents of event goes under the calendar...
                        $article = new Article ( $wl_title );
                        $articleContent = $article->getContent();
//                      $revision = Revfision::newFromTitle($wl_title);

                        $viewEvents .= "<div style=\"padding:10px\"><a href=\"http://".$_SERVER['SERVER_NAME'].$wgScript."/$articleName\">".$articleName."</a><br />".$articleContent.'</div><br>';
                    }
                    else
                    {
                        $thisDayExists = false;
                        $alinkeditstyle = '';
                    }
                    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;"><a href="'.$alinkedit.'" '.$alinkeditstyle.'>' . $i . '</a></td></tr>';
                                        }
                                        else
                                        {
                                                $output .= '<td style="text-align:right;"><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;"><a href="'.$alinkedit.'" '.$alinkeditstyle.'>' . $i . '</a></td>';
                                        }
                                        else

                                        {
                                                $output .= '<td style="text-align:right;"><a href="'.$alinkedit.'" '.$alinkeditstyle.'>' . $i . '</a></td>';
                                        }
                    }
                    $count++;
                }
                for($i=$count;$i<=7;$i++)
                {
                    $output .= "<td> </td>";
                }
                $output .= '</tr></table><br>';

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


                return $output;
        }
}
?>

One more patch

edit

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.

edit

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<br>Like this ==

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


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

                        $viewEvents .= "<p class=\"calendarEventContent\"><a href=\"http://".$_SERVER['SERVER_NAME'].$wgScript."?title=$articleName\">".$articleName."</a><br />".$articleContentOut[2].'</p>';

next - previous month doesn't works

edit

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

Solution

edit

Day of week - monday first?

edit

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:

        var $wday_names = array("Ma","Ti","On","To","Fr","Lø","Sø"); // put sunday first to change order of the days

and

                $weekday_number_b = (($day - 7 * floor($day / 7))); // 
                if ($weekday_number_b == 0) {
                                          $weekday_number = 6;
                }
                else
                {
                                          $weekday_number = ($weekday_number_b - 1);
                }
                return $weekday_number;
        }

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

edit

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

to

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

Adding an Entry Error

edit

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

edit

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 <calendar></calendar>, not <calendar>11 2006</calendar>) it works as it should.

Much thanks.

Updates that I need

edit

I use 1.8.2 with variables in the URL. Therefor I need to update the Script: Line 234 to:

$viewEvents .= "<div style=\"padding:10px\"><a href=\"http://".$_SERVER['SERVER_NAME'].$wgScript."?title=$articleName\">".$articleName."</a><br />".$articleContent.'</div><br>';

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&auml;rz
-- de:the0bone

Patch

edit
 
    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 .= "<div> ".$wl_title."</div><br>";
            $viewEvents .= "<div style=\"padding:10px\"><a href=\"http://".$_SERV
ER['SERVER_NAME'].$wgScript."/$articleName\">".$articleName."</a><br />".$articleContent->getText().'
</div><br>';
    }

Mediawiki 1.9.2

edit

I am using the patched for 1.70 version above on MediaWiki 1.9.2 and have tried using <calendar></calendar> as well as <calendar>02 2007</calendar> 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)Reply

Simpler "dayOfWeek"

edit

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)Reply


Is there a simple code like this one to start on Monday ?

--Dieudo 19:53, 19 October 2008 (UTC)Reply

The Next/Previous Button is not working.

edit

My calendar is showing but the next month/previous month button is not working. I'm logged in as the admin. please help me out on this.

my code is:

<?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

$wgExtensionFunctions[] = "wfCalendarExtension";
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'Calendar',
        'author' => 'Shane',
        'description' => 'adds <nowiki><calender></nowiki> 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("Mon","Tue","Wed","Thu","Fri ","Sat","Sun"); // put sunday first to change order of the days
// English weekday names
        var $wday_names = array("Mon","Tue","Wed","Thu","Fri ","Sat","Sun"); // put sunday first to change order of the days
// German month names
//        var $wmonth_names = array("January", "February", "March", "April", "May", "June","July", "August","September", "October", "November", "December");
//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"><div style="text-align: 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;
        }
}
?>
Return to "Calendar (Shane) extended" page.