BibliographicExtension/bibliographyformat.php
From PeacockWiki
(Difference between revisions)
Revision as of 10:53, 25 August 2006 (edit) Trevorp (Talk | contribs) ← Previous diff |
Current revision (15:24, 16 October 2006) (edit) 137.166.127.86 (Talk) |
||
Line 18: | Line 18: | ||
foreach($names as $key=>$item) | foreach($names as $key=>$item) | ||
{ | { | ||
- | preg_match('/(.*)\s+(.*)/', trim($item), $match); | + | $item=trim($item); |
+ | $name=split(' ', $item); | ||
+ | $lastname=array_pop($name); | ||
+ | $givennames=BibliographyFormat::makeList($name, ' ', ' '); | ||
$initials=array(); | $initials=array(); | ||
- | $name=array(); | + | if(count($name)>0) |
- | foreach(split(' ', $match[1]) as $initial) | + | foreach(split('[ -]', $givennames) as $initial) |
- | { | + | { |
- | $initials[]=$initial[0]; | + | if($initial==strtoupper($initial)) |
- | $name[]=$initial; | + | for($letter=0; $letter<strlen($initials); $letter++) |
- | } | + | $initials[]=$initial[$letter]; |
- | $names[$key]=array($match[1], $match[2], $initials, $name); | + | else |
+ | $initials[]=$initial[0]; | ||
+ | } | ||
+ | $names[$key]=array($givennames, $lastname, $initials, $name); | ||
} | } | ||
return $names; | return $names; | ||
Line 36: | Line 42: | ||
var $mFields=array(); | var $mFields=array(); | ||
+ | var $displayYear=null; | ||
static $styleClass = ''; | static $styleClass = ''; | ||
Line 52: | Line 59: | ||
} | } | ||
- | static function getArticleText($name) | + | static function getArticleName($name) |
- | { | + | { |
- | if($name=='') | + | if($name=='') |
- | return null; | + | return null; |
- | $title=Title::newFromText($name); | + | $title=Title::newFromText($name); |
- | $title=Revision::newFromTitle( $title ); | + | return $title->mDbkeyform; |
- | if($title==null) | + | } |
- | return null; | + | |
- | return $title->getText(); | + | static function getArticleText($name) |
- | } | + | { |
+ | if($name=='') | ||
+ | return null; | ||
+ | $title=Title::newFromText($name); | ||
+ | $title=Revision::newFromTitle( $title ); | ||
+ | if($title==null) | ||
+ | return null; | ||
+ | return $title->getText(); | ||
+ | } | ||
static function setStyle($style, $file) | static function setStyle($style, $file) | ||
Line 87: | Line 102: | ||
{ | { | ||
return call_user_func(array(BibliographyFormat::$styleClass, 'referenceList'), $argv); | return call_user_func(array(BibliographyFormat::$styleClass, 'referenceList'), $argv); | ||
+ | } | ||
+ | |||
+ | function orderReferences($articles) | ||
+ | { | ||
+ | return call_user_func(array(BibliographyFormat::$styleClass, 'orderReferences'), $articles); | ||
} | } | ||
Line 99: | Line 119: | ||
} | } | ||
- | function names() | + | function authors() |
{ | { | ||
- | return BibliographicPageParser::names($this->mFields['author']); | + | if($this->field('author')) |
+ | return BibliographicPageParser::names($this->mFields['author']); | ||
+ | if($this->field('editor')) | ||
+ | return $this->editors(); | ||
+ | return ""; | ||
+ | } | ||
+ | |||
+ | function editors() | ||
+ | { | ||
+ | return BibliographicPageParser::names($this->mFields['editor']); | ||
} | } | ||
function year() | function year() | ||
{ | { | ||
- | return $this->mFields['year']; | + | if($this->displayYear) return $this->displayYear; |
+ | return $this->rawYear(); | ||
+ | } | ||
+ | |||
+ | function rawYear() | ||
+ | { | ||
+ | if(key_exists('year', $this->mFields)) | ||
+ | return $this->mFields['year']; | ||
+ | if(key_exists('date', $this->mFields)) | ||
+ | { | ||
+ | $date=split('/', $this->mFields['date']); | ||
+ | return date('Y', strtotime('1/1/'.$date[0])); | ||
+ | } | ||
+ | return '?'; | ||
} | } | ||
Line 117: | Line 159: | ||
{ | { | ||
return $this->mFields['type']; | return $this->mFields['type']; | ||
+ | } | ||
+ | |||
+ | function field($name) | ||
+ | { | ||
+ | if(!key_exists($name, $this->mFields)) | ||
+ | return NULL; | ||
+ | return $this->mFields[$name]; | ||
} | } | ||
Line 122: | Line 171: | ||
{ | { | ||
return $this->mFields['reference']; | return $this->mFields['reference']; | ||
+ | } | ||
+ | |||
+ | function formatCount($num) | ||
+ | { | ||
+ | if(!is_numeric($num)) | ||
+ | return $num; | ||
+ | if($num>3 && $num<21) | ||
+ | return $num.'th'; | ||
+ | switch($num % 10) | ||
+ | { | ||
+ | case 1: | ||
+ | return $num.'st'; | ||
+ | case 1: | ||
+ | return $num.'nd'; | ||
+ | case 1: | ||
+ | return $num.'rd'; | ||
+ | } | ||
+ | return $num.'th'; | ||
} | } | ||
Current revision
<?php class BibliographicPageParser { static function parse($wikiText) { preg_match('/\{\{.*?\/(.*?)(\|.*)\}\}/ms', $wikiText, $match); $fields=array('type'=>$match[1]); preg_match_all('/\|(.*?)=(.*?)(\||$)/ms', $match[2], $match); foreach($match[1] as $key => $value) $fields[$value]=$match[2][$key]; return $fields; } static function names($names) { $names=split(',', $names); foreach($names as $key=>$item) { $item=trim($item); $name=split(' ', $item); $lastname=array_pop($name); $givennames=BibliographyFormat::makeList($name, ' ', ' '); $initials=array(); if(count($name)>0) foreach(split('[ -]', $givennames) as $initial) { if($initial==strtoupper($initial)) for($letter=0; $letter<strlen($initials); $letter++) $initials[]=$initial[$letter]; else $initials[]=$initial[0]; } $names[$key]=array($givennames, $lastname, $initials, $name); } return $names; } } class BibliographyFormat { var $mFields=array(); var $displayYear=null; static $styleClass = ''; static function renderWikiText($input) { global $wgOut; return $wgOut->parse($input, false); } function BibliographyFormat($reference) { $this->mFields=BibliographicPageParser::parse($this->getArticleText($reference)); $this->mFields['reference']=$reference; if(BibliographyFormat::$styleClass=='') BibliographyFormat::setStyle('CSUHarvard_Style', 'extensions/bibliographystyle-csu.php'); } static function getArticleName($name) { if($name=='') return null; $title=Title::newFromText($name); return $title->mDbkeyform; } static function getArticleText($name) { if($name=='') return null; $title=Title::newFromText($name); $title=Revision::newFromTitle( $title ); if($title==null) return null; return $title->getText(); } static function setStyle($style, $file) { require_once($file); BibliographyFormat::$styleClass=$style; } function intext($argv) { return call_user_func(array(BibliographyFormat::$styleClass, 'intext'), $argv, $this); } static function intextcite($articles) { return call_user_func(array(BibliographyFormat::$styleClass, 'intextcite'), $articles); } function reference($argv) { return call_user_func(array(BibliographyFormat::$styleClass, 'reference'), $this); } function referenceList($argv) { return call_user_func(array(BibliographyFormat::$styleClass, 'referenceList'), $argv); } function orderReferences($articles) { return call_user_func(array(BibliographyFormat::$styleClass, 'orderReferences'), $articles); } function bibliography($refrenceList) { $bibliography="bibliography:\n"; foreach ($refrenceList as $item) { $bibliography.="#".$item->reference($str, $argv)."\n"; } return $bibliography; } function authors() { if($this->field('author')) return BibliographicPageParser::names($this->mFields['author']); if($this->field('editor')) return $this->editors(); return ""; } function editors() { return BibliographicPageParser::names($this->mFields['editor']); } function year() { if($this->displayYear) return $this->displayYear; return $this->rawYear(); } function rawYear() { if(key_exists('year', $this->mFields)) return $this->mFields['year']; if(key_exists('date', $this->mFields)) { $date=split('/', $this->mFields['date']); return date('Y', strtotime('1/1/'.$date[0])); } return '?'; } function title() { return $this->mFields['name']; } function type() { return $this->mFields['type']; } function field($name) { if(!key_exists($name, $this->mFields)) return NULL; return $this->mFields[$name]; } function referencename() { return $this->mFields['reference']; } function formatCount($num) { if(!is_numeric($num)) return $num; if($num>3 && $num<21) return $num.'th'; switch($num % 10) { case 1: return $num.'st'; case 1: return $num.'nd'; case 1: return $num.'rd'; } return $num.'th'; } static function makeList($items, $delim=', ', $finalDelim=' & ') { $list=''; if(count($items)==1) return array_shift($items); foreach($items as $key=>$item) if($key+1==count($items)) $list.=$finalDelim.$item; else $list.=$delim.$item; return substr($list, strlen($delim)+0); } } ?>