BibliographicExtension/bibliographyformat.php
From PeacockWiki
Revision as of 15:17, 16 October 2006; 137.166.127.86 (Talk)
(diff) ←Older revision | Current revision | Newer revision→ (diff)
(diff) ←Older revision | Current revision | Newer revision→ (diff)
<?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); } } ?>