BibliographicExtension/bibliography.php

From PeacockWiki

Revision as of 23:31, 26 August 2006; Trevorp (Talk | contribs)
(diff) ←Older revision | Current revision | Newer revision→ (diff)
Jump to: navigation, search
<?php

require('extensions/bibliographyformat.php');

if(isset($_GET['bibfile']))
  doFileBib();

$wgExtensionCredits["parserhook"][]=array(
  'name' => 'Bibliographic Extension',
  'version' => '0.1',
  'url' => 'http://wiki.peacocktech.com/wiki/BibliographicExtension',
  'author' => '[http://about.peacocktech.com/trevorp/ Trevor Peacock]',
  'description' => 'Adds a bibliographic engine' );
$wgExtensionFunctions[] = "BibliographicExtension";

function BibliographicExtension()
{
  global $wgParser;

  class Bibliography {

    var $mBibliography = array();

    function Bibliography()
    {
      global $wgParser, $wgHooks;
      $wgParser->setHook("refrence", array( &$this, "renderRefrence"));
      $wgParser->setHook("cite", array( &$this, "renderCite"));
      $wgParser->setHook("bibliography", array( &$this, "renderBibliography"));
      $wgHooks['SkinTemplateSetupPageCss'][] = array( &$this, 'cssBibliography');
      #$wgHooks['OutputPageBeforeHTML'][] = array( &$this, 'PostProcess');
    }

    function renderWikiText($input, &$parser)
    {
      return $parser->parse($input, $parser->mTitle, $parser->mOptions, 
        false, false)->getText();
    }

    function cssBibliography()
    {
      global $wgScriptPath;
      $css = "/*<![CDATA[*/".
      " @import \"$wgScriptPath/?file=bibliography.css\"; ".
      "/*]]>*/";
      return true;
    }

    function renderRefrence($str, $argv, $parser)
    {
      $text=$this->renderWikiText($str, $parser);
#      var_dump($this->mBibliography);
      while(preg_match('/<cite (.*?)\/>/ms', $text, $match, PREG_OFFSET_CAPTURE))
      {
        preg_match_all('/(.*?)=\"(.*?)\"/ms', $match[1][0], $match2);
#        var_dump($match2);
        $params=array();
        foreach($match2[1] as $key=>$item)
        {
          if(trim($item)=='article')
          {
            if(!key_exists('articles', $params))
              $params['articles']=array();
            $params['articles'][$match2[2][$key]]=array('article'=>$this->mBibliography[$match2[2][$key]]);
            $lastarticle=$match2[2][$key];
          }
          else
          {
            if(trim($item)=='note')
              $params['articles'][$lastarticle]['note']=$match2[2][$key];
            else
              $params[trim($item)]=$match2[2][$key];
          }
        }
#        var_dump($params);
        $start=$match[0][1];
        $len=strlen($match[0][0]);
        $refrence=BibliographyFormat::intextcite($params);
        $text=substr_replace($text, $refrence, $start, $len);
      }

      $refrence=null;
      while(preg_match('/<bibliography\/>/ms', $text, $match, PREG_OFFSET_CAPTURE))
      {
        $start=$match[0][1];
        $len=strlen($match[0][0]);
        if(!isset($refrence)) $refrence=BibliographyFormat::referenceList($this->mBibliography);
        $text=substr_replace($text, $refrence, $start, $len);
      }
      return $text;
    }

    var $mCite;
    function renderCite($str, $argv, $parser)
    {
      if(!isset($this->mCite))
      {
        $this->mCite=array();
      }
      else
      {
        if(key_exists('article', $argv))
          $this->mCite[$argv['article']]=key_exists('note', $argv)?$argv['note']:'';
        $this->renderWikiText($str, $parser);
        return;
      }
      if(key_exists('article', $argv))
        $this->mCite[$argv['article']]=key_exists('note', $argv)?$argv['note']:'';
      $this->renderWikiText($str, $parser);
      $text="<cite type=\"".(key_exists('type', $argv)?$argv['type']:'normal')."\"";
      foreach($this->mCite as $item=>$note)
      {
        $text.=" article=\"".$item."\"";
        if ($note!='')
          $text.=" note=\"".$note."\"";
        if(!key_exists($item, $this->mBibliography))
          $this->mBibliography[$item]=new BibliographyFormat($item);
      }
      $text.="/>";
      unset($this->mCite);
      return $text;
    }

    function renderBibliography($str, $argv, $parser)
    {
      return "<bibliography/>";
    }

    function PostProcess($parserOutput, $text)
    {
      $text=$text;
    }

  }

  new Bibliography();

}

function doFileBib()
{
  switch ($_GET['bibfile'])
  {
    case "bibliography.css":
      header("Content-type: text/css");
        ?>
//CSS here
<?php
      die();
  }
}

?>
Personal tools