BibliographicExtension/bibliographyformat.php

From PeacockWiki

Revision as of 03:40, 26 August 2006; Trevorp (Talk | contribs)
(diff) ←Older revision | Current revision | Newer revision→ (diff)
Jump to: navigation, search
<?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)
    {
      preg_match('/(.*)\s+(.*)/', trim($item), $match);
      $initials=array();
      $name=array();
      foreach(split(' ', $match[1]) as $initial)
      {
        $initials[]=$initial[0];
        $name[]=$initial;
      }
      $names[$key]=array($match[1], $match[2], $initials, $name);
    }
    return $names;
  }

}

class BibliographyFormat {

  var $mFields=array();
  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 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 bibliography($refrenceList)
  {
    $bibliography="bibliography:\n";
    foreach ($refrenceList as $item)
    {
      $bibliography.="#".$item->reference($str, $argv)."\n";
    }
    return $bibliography;
  }

  function names()
  {
    return BibliographicPageParser::names($this->mFields['author']);
  }

  function year()
  {
    return $this->mFields['year'];
  }

  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);
  }
  
}

?>
Personal tools