Extension:MoodleGlossary

From MediaWiki.org

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

Release status: beta

Implementation Extended syntax
Description Display description of a Moodle Glossary word on mouseover, if that word appears in a wiki article.
Author(s) Jack Eapen based on code by Benjamin Kahn
Version 1.1 (25-09-07)
MediaWiki Tested with 1.6,1.9.3,1.10.2,1.11.0
Download no link

Contents

[edit] Problem Statement

We have a Moodle site and a Mediawiki-powered wiki. People create glossary entries in Moodle and articles in wiki. If the words in those articles are present in Moodle Glossary, it would be nice to highlight them and provide the definition on mouseover.

This extension does that and it's based on the Glossary Extension created by Benjamin Kahn.

[edit] Major Update for MW 1.11

A minor change is done in the extension to make it compatible with MW 1.11 (which necessiates the functions to retun a value). I'm replacing the old code with the new one. This is tested with MW 1.9.3, 1.10.2 and 1.11.0

[edit] Installation

1. Create the file:

[edit] extensions/MoodleGlossary.php

<?php
# MoodleGlossary MediaWiki Extention
# Created by Jack Eapen based on Benjamin Kahn's Glossary extyension.
#
# Based on the Emoticon MediaWiki Extension written by Alex Wollangk (alex@wollangk.com)
 
if ( !defined( 'MEDIAWIKI' ) ) {
        die( 'This file is a MediaWiki extension, it is not a valid entry point' );
}
 
global $wgHooks;
global $wgExtensionCredits;
 
$wgExtensionFunctions[] = "wfTooltip";
 
$wgExtensionCredits['parserhook'][] = array(
        'name' => 'MoodleGlossary',
        'status' => 'beta',
        'type' => 'hook',
        'author' => 'Jack Eapen based on code by Benjamin Kahn (xkahn@zoned.net)',
        'version' => '1.1',
        'update' => '25-09-2007',
        'description' => 'Display description of a Moodle Glossary word on mouseover, if that word appears in a wiki article',
);
 
function wfTooltip() {
    global $wgParser;
    $wgParser->setHook( "tooltip", "tooltip" );
}
 
$wgHooks['ParserBeforeStrip'][] = 'fnTooltips';
 
function tooltip ( $input ) {
  $input = preg_replace ('/XQX/', '', $input);
  $split = explode("|", $input, 2);
  return "<b><font color=\"brown\"><span class=\"glossary\" title=\"tip- " . $split[1] . "\">" . $split[0] . "</span></font></b>";
}
 
// The callback function for replacing acronyms with tooltip tags
function fnTooltips( &$parser, &$text, &$strip_state ) {
  global $action; // Access the global "action" variable
  global $wgMoodleTablePrefix;
  global $wgMoodleDBServer;
  global $wgMoodleDBName;
  global $wgMoodleDBUser;
  global $wgMoodleDBPassword;
        // Only do the replacement if the action is not edit or history
 
        if(
                $action !== 'edit'
                && $action !== 'history'
                && $action !== 'delete'
                && $action !== 'watch'
                && $parser->mTitle->getNamespace() != NS_SPECIAL
                && $parser->mTitle->mNamespace !== 8
        )
        {
 
                $acro = array ();
                $repl = array ();
 
               $conn= mysql_connect($wgMoodleDBServer,$wgMoodleDBUser,$wgMoodleDBPassword) ;
               mysql_select_db($wgMoodleDBName);
                $sql='select concept,definition from '.$wgMoodleTablePrefix.'glossary_entries where approved=1';
                $result=mysql_query($sql);
                if($result) {
                       while ($currEmoticon=mysql_fetch_row($result))
                        {
                                         // start by trimming the search value
                                        $currEmoticon[ 0 ] = trim( $currEmoticon[ 0 ] );
                                        //Jack-to replace the TRUSTTEXT inserted in Moodle
                                       $currEmoticon[ 1 ]= str_replace('#####TRUSTTEXT#####','',$currEmoticon[ 1 ]);
                                        // trim the replacement value
                                         $currEmoticon[ 1 ] = strip_tags( $currEmoticon[ 1 ] );
                                        $currEmoticon[ 1 ] = trim( $currEmoticon[ 1 ] );
 
                                        array_push ($acro, '/(\b)' . $currEmoticon[ 0 ] . '(\b)/');
                                         array_push ($repl, 'XQX' . $currEmoticon[ 0 ] . "XQX|XQX" . $currEmoticon[ 1 ] . 'XQX');
 
                      // $text=mysql_error();
                        }
 
               $text = preg_replace ($acro, $repl, $text);
             }
             mysql_close($conn);
  return true;      //Jack-this line for MW 1.11 compatibility
}
return false;  //Jack-this line for MW 1.11 compatibility
}
 
?>

2. Add the following to the end of: (right above the "?>" at the end)

[edit] LocalSettings.php

require_once("$IP/extensions/MoodleGlossary.php");
$wgMoodleTablePrefix='mdl_'; //Your Moodle table prefix.
$wgMoodleDBServer='localhost'; //YOUR_DB_SERVER
$wgMoodleDBName='YOUR_MOODLE_DATABASE_NAME';
$wgMoodleDBUser='YOUR_DB_USER_NAME';
$wgMoodleDBPassword='YOUR_DB_PASSWORD';

[edit] Known issues

  1. The code is now not respecting the wiki links. i.e. if you have a wiki word in the Glossary, that will be highlighted and displayed with the brackets as [[wiki word]].
  1. This extension defines (unintentionally?) a <tooltip> tag and may conflict with Extension:Tooltip.
Resolved now.
--Jackeapen 12:27, 25 September 2007 (UTC)

[edit] Disclaimer

I'm not an expert programmer. So i'm unaware of the security holes in this. So please try at your own risk. I'm using this in a non-threatening intranet environment. So not much bothered about SQL injection vulnerabilities or similar stuff :) . Feel free to modify the code in the spirit of open source.

Personal tools