Extension:HarvardReferences

Description
Extension supports Author-date ("Harvard system") referencing style, e.g. Smith 2008:1


 * Link - in square brackets: [Smith 2008:1]
 * Anchor to link - [*Smith 2008]

Optional page numbers can be set after ":" char in link. Some links with different page numbers can refer to one anchor.

Extension uses JavaScript to highlight references and backlinks, but works also without JavaScript.

MediaWiki version
Extension was tested on 1.15.3 version of MediaWiki.

Setup
1) make HarvardReferences.php (source code is below) in folder extensions/HarvardReferences.

2) Write require_once("$IP/extensions/HarvardReferences/HarvardReferences.php");  into file LocalSettings.php.

Source code
//HarvardReferences.php  __FILE__,	'name'         => 'HarvardReferences',	'version'      => '1.0',	'author'       => 'X-romix', 	'url'          => 'http://www.mediawiki.org/wiki/Extension:HarvardReferences',	'description'  => 'Author-date ("Harvard system") referencing style, e.g. Smith 2008:1' ); class HarvardReferences{ var $SwitchOff = false; var $refs = array; var $refs_count = array;

function HarvardReferences { //Constructor $this->setHooks; }	function setHooks { global $wgParser, $wgHooks; //Hook for tag . Use this tag as  for compatibility reasons. //see http://www.mediawiki.org/wiki/Manual:Tag_extensions for details $wgParser->setHook( 'HarvardReferences', array( &$this, 'fnHarvardReferences' ) ); //function fnHarvardReferences) - is below

//Hook to ParserBeforeTidy event - "Used to process the nearly-rendered html code for the page (but before any html tidying occurs)" //see also http://www.mediawiki.org/wiki/Manual:Hooks/ParserBeforeTidy //http://www.mediawiki.org/wiki/Manual:Hooks $wgHooks['ParserBeforeTidy'][] = array( &$this, 'fnParserBeforeTidy' ); //function fnParserBeforeTidy - is below }

//	function fnHarvardReferences( $str, $argv, $parser ){ $s=$argv['SwitchOff']; $s=trim(strtolower($s)); if($s=='yes' || $s=='true'){ $this->SwitchOff = true; }		return ""; }

function fnParserBeforeTidy(&$parser, &$text){ global $IP; if($this->SwitchOff == true){ return true; }		$this->romix_log("--"); preg_match_all("/			(\[\*)   # [* characters			([^\]]+)  # any text, e.g. Smith 2010			(\])      # ] character			/x", $text, $matches, PREG_SET_ORDER); foreach($matches as $match){ $s=$match[2]; $this->refs[] = $s; //fill global array refs[] with names of labels ( e.g. "Smith 2010") }		//process links $text=preg_replace_callback("/			(\[)			# [ character			([^\]\:\*]+)	# any text, e.g. Smith 2010 without ] * : characters			(\]|\:[^\]]+\])	# ] character or :page number etc. and ] character			/x", 			array( __CLASS__, 'ParseHarvRefCallback' ), 			$text);

//process anchors $text=preg_replace_callback("/			(\[\*)     # [* characters			([^\]]+)	# any text, e.g. Smith 2010 without ] characters			(\])        # ] character 			/x", 			array( __CLASS__, 'ParseHarvAnchorsCallback' ), 			$text);

$text.=' function HrvHighlight_Back(prm_name){ var anchorTags = document.getElementsByTagName("sup"); for (var i = 0; i < anchorTags.length ; i++){ var ob1=anchorTags[i]; if(ob1.id.indexOf("harv_ref-"+prm_name+"-")==0){ ob1.style.backgroundColor="#DDEEFF"; }else{ ob1.style.backgroundColor=""; }				}			}			function HrvHighlight_Ref(prm_name){ var anchorTags = document.getElementsByTagName("sup"); for (var i = 0; i < anchorTags.length ; i++){ var ob1=anchorTags[i]; if(ob1.id=="harv_ref-"+prm_name){ ob1.style.backgroundColor="yellow"; }else if(ob1.id=="harv_note-"+prm_name){ ob1.style.backgroundColor="yellow"; }else{ ob1.style.backgroundColor=""; }				}			}

function HrvHighlight_Note(prm_name){ var anchorTags = document.getElementsByTagName("sup"); for (var i = 0; i < anchorTags.length ; i++){ var ob1=anchorTags[i]; if(ob1.id=="harv_note-"+prm_name){ ob1.style.backgroundColor="yellow"; }else{ ob1.style.backgroundColor=""; }				}			}

';		return true; }	function FormatAsLink($s){ $from=array(" ", ",", "'"); $to="_"; return str_replace($from, $to, $s); }	function ParseHarvRefCallback($matches){ $s=$matches[2]; $pages=$matches[3]; $pages=str_replace(']', '', $pages); $pages=trim($pages); if (!in_array($s, $this->refs)) { return $matches[0]; //not do any changes }		if(!isset($this->refs_count[$s])){ $this->refs_count[$s]=1; }else{ $this->refs_count[$s]++; }		$cnt=$this->refs_count[$s]; $name=$this->FormatAsLink($s); $r=''. "". "[".$s."]"."".$pages." "; return $r; }

function ParseHarvAnchorsCallback($matches){ $s=$matches[2]; if(!isset($this->refs_count[$s])){ $cnt=0; }else{ $cnt=$this->refs_count[$s]; }			$name=$this->FormatAsLink($s); if($cnt==0){ // no links to this anchor $r="[$s]"; }else if($cnt==1){ // 1 link to anchor $r=''. '['.$s.'] ^ '; //&uarr; }else if($cnt>=2){ // more than 1 link to anchor $r=''. '['.$s.'] ^ '; for($i=1; $i<=$cnt; $i++){ $r.=' '.$i.' '; }		}		return $r; }	function romix_log($s){ $path = dirname( __FILE__ ); $fp = fopen ($path."/romix.txt", "a"); fwrite ($fp, $s . "\n"); fclose ($fp); }	}

function wfHarvardReferences { new HarvardReferences; return true; } ?>