Extension:HarvardRef

From MediaWiki.org
Jump to: navigation, search
MediaWiki extensions manual - list
Crystal Clear action run.png
HarvardRef

Release status: beta

Implementation Link markup
Description Replaces internal links with Harvard-style references.
Author(s) JonathanWillifordtalk
Last version 0.0.0
MediaWiki Tested with 1.17, may work with as low as 1.10.0
License GPL
Download #Code
Hooks used
InternalParseBeforeLinks
Check usage and version matrix

Contents

What can this extension do? [edit]

This extension modifies internal links (ex. [[#refname]]) to be harvardized references, assuming the Harvard text is enclosed in an HTML element with the id refname.

Usage [edit]

Download instructions [edit]

Please cut and paste the code found below and place it in $IP/extensions/ExtensionName/ExtensionName.php. Note: $IP stands for the root directory of your MediaWiki installation, the same directory that holds LocalSettings.php.

Installation [edit]

To install this extension, add the following to LocalSettings.php:

require_once("$IP/extensions/HarvardRef/HarvardRef.php");

Code [edit]

<?php
/*
        The HarvardRef extension is free software: you can redistribute it
        and/or modify it under the terms of the GNU General Public License
        as published by the Free Software Foundation, either version 3 of
        the License, or (at your option) any later version.
 
        This program is distributed WITHOUT ANY WARRANTY. See
        http://www.gnu.org/licenses/#GPL for more details.
*/
 
if ( !defined( 'MEDIAWIKI' ) ) {
        echo <<<EOT
To install my extension, put the following line in LocalSettings.php:
require_once( "\$IP/extensions/HarvardRef/HarvardRef.php" );
EOT;
        exit( 1 );
}
 
$wgExtensionCredits['specialpage'][] = array(
        'path' => __FILE__,
        'name' => 'HarvardRef',
        'author' => 'Jonathan Williford',
        'url' => 'http://www.mediawiki.org/wiki/Extension:HarvardRef',
        'version' => '0.0.0',
);
 
// user configurable parameters
 
global $wgHooks;
 
$dir = dirname( __FILE__ ) . '/';
 
$wgHooks['InternalParseBeforeLinks'][] = 'harvardRefParserHook';
 
function harvardRefParserHook( &$parser, &$text ) {
        $pattern1 = '/\[\[#([^| ]*)\]\]/';
        if( !preg_match($pattern1, $text, $matches) ) {
                return false;
        }
        $id = $matches[1];
 
        // need to find the Harvard reference (ex. Smith et al 2011)
        $pattern2a = "/\Wid=\"$id\"[^>]*>([^<]*)<\//";
        $pattern2b = "/\Wid='$id'[^>]*>([^<]*)<\//";
        if( !preg_match($pattern2a, $text, $matches) && 
            !preg_match($pattern2b, $text, $matches) ) 
        {
                return false;
        }
        $harvardref = $matches[1];
 
        $replace = "($harvardref)";
        $text = preg_replace($pattern1,$replace,$text);
        return true;
}

See also [edit]

Extension:HarvardReferences