Extension:Events

From MediaWiki.org

Jump to: navigation, search
Manual on MediaWiki Extensions
List of MediaWiki Extensions
Events

Release status: unknown

Implementation Special page
Description provides the ability to create events on any page of your MediaWiki
Download see below
Hooks used

ArticleSave
ArticleSaveComplete

This extension provides the ability to create events on any page of your MediaWiki. Also included is a highly configurable Special page for aggregating events on your site by various criteria. All events are stored in MySQL, so the Special page is extremely fast.

Original author is Aran Deltac but he decided to no longer maintain it. Thanks to Google cache I've found the instructions, I copy them here, because Aran's wiki has changed. Iubito 16:51, 10 February 2006 (UTC)

Contents

[edit] News

  • 2007-03-06: Added a version for mediaWiki 1.9
  • 2006-03-12: Added code snipped to make it compatible with Christof Damian's Calendar extension.
  • 2006-02-10: Released version 0.2. Aran no longer maintain code, instructions copied here by Iubito with slight changes that make this extension work on MediaWiki 1.4 but no tested.
  • 2005-11-02: Released version 0.1.
  • 2005-11-02: First version of MediaWiki Events released.

[edit] Compatibility

Aran have only tested this on MediaWiki 1.5.1. Iubito applyed changed to be compatible with MediaWiki 1.4, but no tested it. Use at your own risk.

Note I tested this on my MediaWiki installation version 1.9.1 and it didn't work. - Random user

I had the same problem on MediaWiki version 1.9.3. Will there probably be an update? Events are shown on the pages but there are no entries on the special page.

Cstehly 06:47, 24 August 2007 (UTC) Tested with 1.7, it works with following update on Events.php (<1.9) : Replace

Parser::extractTagsAndParams( 'events', $text, $content, $tags, $params );
        foreach($tags as $marker => $markerVal)
                eventsHook( $content[$marker], $params[$marker] );

with

Parser::extractTagsAndParams( array('events'), $text, $matches );
        foreach($matches as  $marker => $data) {
                list( $element, $content, $params, $tag ) = $data;
                eventsHook( $content);
                 }

[edit] Usage

Each event must be on its own line and start with a date and colon. All the tasks must be grouped with a <events>..</events>

<events>
2062-02-14: When I buy my first hover car.
1453-07-12: Joe was born.
</events>

[edit] Events Special Page

This page finds all events on all pages of your site and displays them all on one page: Special:Events. You can provide an argument to limit the number of events that are returned:

  • limit - Maximum number of events to list.

[edit] Installation

Copy/paste codes below in files.


[edit] Version < 1.9

[edit] extensions/events/Events.php

<?php
# MediaWiki Events version 0.2
# Copyright (c) 2005 Aran Clary Deltac
# http://arandeltac.com/MediaWiki_Events
# Modified by Sylvain Machefert
# http://meta.wikimedia.org/w/index.php?title=Events_Extension
# Distributed under that same terms as MediaWiki itself.
 
$wgExtensionFunctions[] = "wfEventsExtension";
$wgHooks['ArticleSave'][] = 'clearEvents' ;
$wgHooks['ArticleSaveComplete'][] = 'saveEvents' ;
global $events_buffer;
 
#-----------------------------------------------#
# Purpose   : Declare parser extensions.
function wfEventsExtension() {
        global $wgParser;
        $wgParser->setHook( "events", "eventsHook" );
 
        global $wgMessageCache;
        $wgMessageCache->addMessages(array('events' => 'Events'));        
}
 
#-----------------------------------------------#
# Purpose   : Display a list of events.
# Parameters: content, A list of events, one per line.
# Returns   : Events HTML.
function eventsHook( $content, $args = null ) {
        global $events_buffer;
        $events = preg_split("/[\n\r]+/", $content);
        $output = '';
        foreach ($events as $event) {
                if (preg_match('/^\s*([0-9]{4}-[0-9]{1,2}-[0-9]{1,2}):\s*(.+)$/',$event,$matches)) {
                        $date = $matches[1];
                        $description = $matches[2];
 
                        $output .= formatEvent( $date, $description );
 
                        $events_buffer[] = array(
                                'date' => $date,
                                'description' => $description
                        );
                }
        }
        return $output;
}
 
#-----------------------------------------------#
# Purpose   : HTML format an event.
function formatEvent( $date, $description, $page='' ) {
        global $wgOut;
 
        $date_ary = explode( '-', $date );
        $date = sprintf( '%04d-%02d-%02d', $date_ary[0], $date_ary[1], $date_ary[2] );
        $description = "'''".$date.":'''&nbsp; ".$description;
 
        if ($page) { $description .= ' ([['. $page .']])'; }
        $output .= $wgOut->parse($description,false) .'<br />';
 
        return $output;
}
 
#-----------------------------------------------#
# Purpose   : Used before saveing a page to clear the events buffer.
function clearEvents() {    
        global $events_buffer;
        $events_buffer = array();
        return 1;
}
 
#-----------------------------------------------#
# Purpose   : Used after a page is saved to first delete 
#             all events and then save the new ones created 
#             in the events buffer.
# Parameters: article, The article object.
function saveEvents( $article, $user, $text ) {
        global $events_buffer;
        $page_id = $article->getID();
 
        $dbr =& wfGetDB( DB_MASTER );
        $dbr->delete(
                'events',
                array( 'page_id' => $page_id )
        );
 
        # Rebuild the $events_buffer array (in case we're on a <calendar> page)
        clearEvents();
        $content = array(); $tags = array(); $params = array();
 
        Parser::extractTagsAndParams( 'events', $text, $content, $tags, $params );
        foreach($tags as $marker => $markerVal)
                eventsHook( $content[$marker], $params[$marker] );
 
        foreach ($events_buffer as $event) {
                $event['page_id'] = $page_id;
                $dbr->insert(
                        'events',
                        $event
                );
        }
        $events_buffer = array();
        return 1;
}
?>

[edit] includes/SpecialEvents.php

<?php
# MediaWiki Events version 0.2
# Copyright (c) 2005 Aran Clary Deltac
# http://arandeltac.com/MediaWiki_Events
# Modified by Sylvain Machefert
# http://meta.wikimedia.org/w/index.php?title=Events_Extension
# Distributed under that same terms as MediaWiki itself.
 
require_once('SpecialPage.php');
$wgSpecialPages['Events'] = new SpecialPage ( 'Events' );
 
function wfSpecialEvents( $args_string = '' ) {
        global $wgOut;
        $page_titles = array();
        $dbr =& wfGetDB( DB_MASTER );
        # Parse arguments.
        $args = array();
        foreach (explode('&',$args_string) as $pair) {
                $pair = explode('=',$pair);
                $args[$pair[0]] = $pair[1];
        }
        # Define options for the SQL.
        $options = array( 'ORDER BY'=>'date DESC' );
        if ($args['limit']) {
                $options['LIMIT'] = $args['limit'];
        }
        # Run the SQL.
        $res = $dbr->select(
                'events', 
                array('page_id','date','description'), 
                '', 'Database::select',
                $options
        );
        if (!$res) { return; }
 
        # Set the title for this page.
        $wgOut->setPageTitle( 'Events' );
 
        # Generate HTML list of events.
        $count = 0;
        $last_year = '0000';
        $last_month = '00';
        while ($event = $dbr->fetchRow( $res )) {
                $page_title = $page_titles[$event['page_id']];
                if (!$page_title) {
                        $page_titles[$event['page_id']] = Title::nameOf($event['page_id']);
                        $page_title = $page_titles[$event['page_id']];
                }
                $date_ary = explode( '-', $event['date'] );
                if ( $last_year!=$date_ary[0] or $last_month!=$date_ary[1] ) {
                        $last_year = $date_ary[0];
                        $last_month = $date_ary[1];
                        $wgOut->addWikiText('== '.$last_year.'-'.$last_month.' ==');
                }
        $wgOut->addHTML(
                        formatEvent( $event['date'], $event['description'], $page_title )
                );
                $count++;
        }
        $dbr->freeResult( $res );
        # Display a message if there are no events.
        if (!$count) {
                $wgOut->addWikiText('No events found!');
        }
}
?>

[edit] LocalSettings.php

Add these lines at the end of LocalSettings.php :

include("extensions/events/Events.php");
include("includes/SpecialEvents.php");

[edit] Version >= 1.9.0

[edit] extensions/events/Events.php

--Mchansy 19:44, 6 March 2007 (UTC)

<?php
# MediaWiki Events version 0.2
# Copyright (c) 2005 Aran Clary Deltac
# http://arandeltac.com/MediaWiki_Events
# Modified by Sylvain Machefert
# http://meta.wikimedia.org/w/index.php?title=Events_Extension
# Distributed under that same terms as MediaWiki itself.
 
$wgExtensionFunctions[] = "wfEventsExtension";
$wgHooks['ArticleSave'][] = 'clearEvents' ;
$wgHooks['ArticleSaveComplete'][] = 'saveEvents' ;
global $events_buffer;
 
#-----------------------------------------------#
# Purpose   : Declare parser extensions.
function wfEventsExtension() {
        global $wgParser;
        $wgParser->setHook( "events", "eventsHook" );
}
 
#-----------------------------------------------#
# Purpose   : Display a list of events.
# Parameters: content, A list of events, one per line.
# Returns   : Events HTML.
function eventsHook( $content, $args = null, &$parser ) {
        global $events_buffer;
        $output = '';
        $local_parser = new Parser;
 
        clearEvents();
              addToEventBuffer($content);
 
        foreach ($events_buffer as $event) {
                $output .= formatEvent( 
                             $event['date'],
                             $event['description'],
                             '',
                             $parser
                             );
 
        }
        return $output;
}
 
#-----------------------------------------------#
# Purpose   : Add event to Buffer.
function addToEventBuffer( $content) {
 
        global $events_buffer;
 
        $events = preg_split("/[\n\r]+/", $content);
        foreach ($events as $event) {
                if (preg_match('/^\s*([0-9]{4}-[0-9]{1,2}-[0-9]{1,2}):\s*(.+)$/',$event,$matches)) {
                        $date = $matches[1];
                        $description = $matches[2];
 
                        $events_buffer[] = array(
                                'date' => $date,
                                'description' => $description,
                        );
                }
        }
}
 
#-----------------------------------------------#
# Purpose   : HTML format an event.
function formatEvent( $date, $description, $page='', &$parser) {
        global $wgOut;
        global $wgLang;
 
        $date_ary = explode( '-', $date );
        $date_out = $wgLang->date($date_ary[0] . $date_ary[1] . $date_ary[2], true);
        $date_daynum = date("w", mktime(0, 0, 0, $date_ary[1], $date_ary[2], $date_ary[0]));
        $date_day = $wgLang->getWeekdayName( $date_daynum + 1);
        $description = $date_day." '''".$date_out."'''  ".$description;
 
        if ($page) { 
                $description .= ' ([['. $page .']])'; 
        }
 
        $parserOutput = $parser->parse($description, $parser->mTitle, $parser->mOptions, true, false);
        $output = $parserOutput->getText();
 
        return $output;
}
 
#-----------------------------------------------#
# Purpose   : Used before saveing a page to clear the events buffer.
function clearEvents() {    
        global $events_buffer;
        $events_buffer = array();
        return 1;
}
 
#-----------------------------------------------#
# Purpose   : Used after a page is saved to first delete 
#             all events and then save the new ones created 
#             in the events buffer.
# Parameters: article, The article object.
function saveEvents( &$article, &$user, &$text, &$summary, &$minoredit, &$watchthis, &$sectionanchor, &$flags) {
        global $events_buffer, $wgOut;
 
        $page_id = $article->getID();
        $wgOut->addHTML($output_item);
 
        $dbr =& wfGetDB( DB_SLAVE );
        $dbr->delete(
                'events',
                array( 'page_id' => $page_id )
        );
 
        # Rebuild the $events_buffer array (in case we're on a <calendar> page)
        clearEvents();
        $content = array(); $tags = array(); $params = array();
 
 
        $matches = array();
        $elements[] = 'Events';
 
        $text = Parser::extractTagsAndParams( $elements, $text, $matches );
        foreach( $matches as $marker => $data ) {
                list( $element, $content, $params, $tag ) = $data;
                addToEventBuffer($content);
        }
 
        foreach ($events_buffer as $event) {
                $event['page_id'] = $page_id;
                $dbr->insert(
                        'mw_events',
                        $event
                );
        }
        $events_buffer = array();
        return 1;
}
?>

[edit] includes/SpecialEvents.php

--Mchansy 19:44, 6 March 2007 (UTC)

<?php
# MediaWiki Events version 0.2
# Copyright (c) 2005 Aran Clary Deltac
# http://arandeltac.com/MediaWiki_Events
# Modified by Sylvain Machefert
# http://meta.wikimedia.org/w/index.php?title=Events_Extension
# Distributed under that same terms as MediaWiki itself.
 
require_once('SpecialPage.php');
$wgSpecialPages['Events'] = new SpecialPage ( 'Events' );
 
function wfSpecialEvents( $args_string = '' ) {
        global $wgOut;
        global $wgParser;
        global $wgLang;
 
        $page_titles = array();
        $dbr =& wfGetDB( DB_SLAVE );
        # Parse arguments.
        $args = array();
        foreach (explode('&',$args_string) as $pair) {
                $pair = explode('=',$pair);
                $args[$pair[0]] = $pair[1];
        }
        # Define options for the SQL.
        $options = array( 'ORDER BY'=>'date ASC' );
        if ($args['limit']) {
                $options['LIMIT'] = $args['limit'];
        }
        # Run the SQL.
        $res = $dbr->select(
                'events', 
                array('page_id','date','description'), 
                '(DATEDIFF(date,CURDATE()) <= 365 ) and (DATEDIFF(date,CURDATE()) >= 0)',
                'Database::select',
                $options
        );
        if (!$res) { return; }
 
        # Set the title for this page.
        $wgOut->setPageTitle( 'Ereignisse' );
 
        # Generate HTML list of events.
        $count = 0;
        $last_year = '0000';
        $last_month = '00';
        while ($event = $dbr->fetchRow( $res )) {
                $page_title = $page_titles[$event['page_id']];
                if (!$page_title) {
                        $page_titles[$event['page_id']] = Title::nameOf($event['page_id']);
                        $page_title = $page_titles[$event['page_id']];
                }
                $date_ary = explode( '-', $event['date'] );
                if ( $last_year!=$date_ary[0] or $last_month!=$date_ary[1] ) {
                        $last_year = $date_ary[0];
                        $last_month = $date_ary[1];
                        $last_month_name = $wgLang->getMonthName($last_month);
                        $wgOut->addWikiText('== '.$last_month_name.' '.$last_year.' ==');
                }
 
                $output_item = formatEvent( $event['date'], $event['description'], $page_title, $wgParser);
 
                $wgOut->addHTML($output_item);
                $count++;
        }
        $dbr->freeResult( $res );
        # Display a message if there are no events.
        if (!$count) {
                $wgOut->addWikiText('No events found!');
        }
}
?>

[edit] LocalSettings.php

--Mchansy 19:44, 6 March 2007 (UTC)

Add these lines at the end of LocalSettings.php :

require_once("extensions/events/Events.php");
require_once("includes/SpecialEvents.php");

[edit] Create the SQL table

Do not forget to add the prefixe to the table name !

Not quite clear what the warning in the previous line means; do you mean:

If you use a prefix for your MediaWiki MySQL tables, such as

mw_

then replace the line

CREATE TABLE events (

with

CREATE TABLE mw_events (

If this is what is meant, do you also need to change the code in the above php snippets? I'd be more accurate, but I'm not sufficiently familiar with PHP/MySQL

No, this isn't necessary because the prefix is specified during installation. so operating on a table in the database via medawiki-db-functions always appends the prefix in front of the tablename. 85.125.245.194 14:45, 12 July 2006 (UTC)


CREATE TABLE events (
    page_id INT(8) UNSIGNED NOT NULL DEFAULT 0,
    date DATE NOT NULL DEFAULT '0000-00-00',
    description TEXT NOT NULL,
    KEY date_idx (DATE)
);


All done !

[edit] License

This code is distributed under the same terms as MediaWiki itself.


Personal tools