Extension:Calendar (Shane)

From MediaWiki.org

Jump to: navigation, search

The Calendar extension extension creates a calendar of the month and year in any MediaWiki page.

Contents

[edit] Install

Create a file called 'calendar_extension.php' in your extensions/ directory, and add the following code...

Works on Mediawiki 1.5.x and 1.6.x.

[edit] Code

<?php
# Calendar Extension
# Creates a calendar of the month and year.
#
# Example Code
#
# <calendar>05 2006</calendar>
#
 
$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)
{
	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("Sun","Mon","Tue","Wed","Thu","Fri","Sat");
	var $wmonth_names = array("January","Febuary","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) 
	{ 
		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))); 
 
		return $weekday_number; 
  }
 
	function getWeekDay()
	{
		$week_day = mwCalendar::dayOfWeek($this->month,$this->year);
		//return $this->wday_names[$week_day];
		return $week_Day;
	}
 
	function showThisMonth()
	{
	  	$output = '';	//	reset var
		$output = '<table cellpadding="2" cellspacing="2">';
		$output .= '<tr><td colspan="7" class="cal-header">'. $this->wmonth_names[$this->pmonth] . " " .$this->year .'</td></tr>';
		$output .= '<tr>';
		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>&nbsp;</td>';
			$count++;
		}
		for($i=1;$i<=$no_days;$i++)
		{
				if($count > 6)
				{
					if($i == $this->today)
					{
						$output .= '<td>' . $i . '</td></tr>';
					}
					else
					{
						$output .= '<td class="cal-weekend">' . $i . '</td></tr>';
					}
					$count = 0;
				}
				else
				{
					if($i == $this->today)
					{
						$output .= '<td class="cal-today">' . $i . '</td>';
					}
					else
					{
						$output .= '<td>' . $i . '</td>';
					}
				}
				$count++;
		}
		$output .= '</tr></table>';
		return $output;
	} 
}
 
?>

== 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] Usage

Basic syntax for adding the calendar is, as detailed in the code:

<calendar>05 2006</calendar>

[edit] Comments

At present this extension appears to do little more than draw you up a calendar. But it does. You can add events to this calendar like this:

<calendar>
## name=mycalendar
## view=threemonths
## format = mycalendar-%year-%m-%d
</calendar>

This didn't work and I cant find this in the code!

Personal tools