Calendar

<?php
/\*\*

```
* SimpleCalendar Extension
* @package MediaWiki
* @subpackage Extensions
* @author Aran Dunkley User:Nad
* @licence GNU General Public Licence 2.0 or later
*/

```

define( 'SIMPLECALENDAR\_VERSION', '1.2.11, 2015-06-19' );
$wgExtensionCredits['parserhook'][] = array(
 'name' => 'SimpleCalendar',
 'author' => 'Aran Dunkley',
 'description' => 'A simple calendar extension',
 'url' => 'http://www.mediawiki.org/wiki/Extension:SimpleCalendar',
 'version' => SIMPLECALENDAR\_VERSION
);
class SimpleCalendar {
 function \_\_construct() {
 global $wgExtensionFunctions;
 $wgExtensionFunctions[] = array( $this, 'setup' );
 Hooks::register( 'LanguageGetMagic', $this );
 }
 public function onLanguageGetMagic( &$magicWords, $langCode = 0 ) {
 $magicWords['calendar'] = array( 0, 'calendar' );
 return true;
 }
 public function setup() {
 global $wgParser, $wgResourceModules, $wgExtensionAssetsPath;
 // Register the parser-function
 $wgParser->setFunctionHook( 'calendar', array( $this, 'render' ) );
 // Add CSS
 $path = $wgExtensionAssetsPath . '/' . basename( \_\_DIR\_\_ );
 $wgResourceModules['ext.ajaxcomments'] = array(
 'styles' => array( 'simplecalendar.css' ),
 'localBasePath' => \_\_DIR\_\_,
 'remoteBasePath' => $path,
 );
 }
 /\*\*
 \* Expands the "calendar" magic word to a table of all the individual month tables
 \*/
 public function render( $parser ) {
 $parser->disableCache();
 // Retrieve args
 $argv = array();
 foreach ( func\_get\_args() as $arg ) {
 if ( !is\_object( $arg ) ) {
 if ( preg\_match( '/^(.+?)\s\*=\s\*(.+)$/', $arg, $match ) ) {
 $argv[$match[1]] = $match[2];
 }
 }
 }
 // Set options to defaults or specified values
 $f = isset( $argv['format'] )  ? $argv['format']  : ( strtoupper(substr(PHP\_OS, 0, 3)) == 'WIN' ? '%#d %B %Y' : '%e %B %Y' );
 $df = isset( $argv['dayformat'] ) ? $argv['dayformat']  : false;
 $p = isset( $argv['title'] )  ? $argv['title'] . '/'  : *;*
 $q = isset( $argv['query'] )  ? $argv['query'] . '&action=edit' : 'action=edit';
 $y = isset( $argv['year'] )  ? $argv['year']  : date( 'Y' );
 // If a month is specified, return only that month's table
 if ( isset( $argv['month'] ) ) {
 $m = $argv['month'];
 $table = $this->renderMonth( strftime( '%m', strtotime( "$y-$m-01" ) ), $y, $p, $q, $f, $df );
 }
 // Otherwise start month at 1 and build the main container table
 else {
 $m = 1;

 $table = "

";
 for ( $rows = 3; $rows--; $table .= "" ) {
 for ( $cols = 0; $cols < 4; $cols++ ) {
 $table .= "";
 }
 }

 $table .= "

\n";

 }
 return array( $table, 'isHTML' => true, 'noparse' => true );
 }
 /\*\*
 \* Return a calendar table of the passed month and year
 \*/
 private function renderMonth( $m, $y, $prefix, $query, $format, $dayformat ) {
 $thisDay = date( 'd' );
 $thisMonth = date( 'n' );
 $thisYear = date( 'Y' );
 if ( !$d = date( 'w', $ts = mktime( 0, 0, 0, $m, 1, $y ) ) ) {
 $d = 7;
 }
 $month = wfMessage( strtolower( strftime( '%B', $ts ) ) )->text();
 $days = array();
 foreach ( array( 'M', 'T', 'W', 'T', 'F', 'S', 'S' ) as $i => $day ) {
 $days[] = $dayformat ? wfMessage( strftime( $dayformat, mktime( 0, 0, 0, 2, $i, 2000 ) ) )->text() : $day;
 }

 $table = "\n

\n\t\n";
 $table .= "\t\n";
 $table .= "\t\n";
 if ( $d > 1 ) {
 $table .= "\t\t" . str\_repeat( "", $d - 1 ) . "\n";
 }
 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\t\n\t\n";
 }
 $t = ( $day == $thisDay && $m == $thisMonth && $y == $thisYear ) ? ' today' : *;*
 $ttext = $prefix . trim( strftime( $format, mktime( 0, 0, 0, $m, $day, $y ) ) );
 $title = Title::newFromText( $ttext );
 if ( is\_object( $title ) ) {
 $class = $title->exists() ? 'day-active' : 'day-empty';
 $url = $title->getLocalURL( $title->exists() ? *: $query );*
 } else {
 $url = "Bad title: \"$ttext\" (using format \"$format\")";
 }
 $table .= "\t\t\n";
 }
 }

 $table .= "\n\t\n

";

 return $table;
 }
}
new SimpleCalendar();