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