BibliographicExtension/bibliography.php

From PeacockWiki

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();
    var $citeTag="bcite";

    function Bibliography()
    {
      global $wgParser, $wgHooks;
      $wgParser->setHook("reference", array( &$this, "renderReference"));
      $wgParser->setHook($this->citeTag, 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(&$css)
    {
      global $wgScriptPath;
      $css.= "/*<![CDATA[*/".
        " @import \"$wgScriptPath/?bibfile=bibliography.css\"; ";
      if(isset($_GET['printable']) && $_GET['printable']=='yes')
        $css.= " @import \"$wgScriptPath/?bibfile=bibliographyPrint.css\"; ";
      if(isset($_GET['final']) && $_GET['printable']=='yes')
        $css.= " @import \"$wgScriptPath/?bibfile=bibliographyPrintFinal.css\"; ";
      $css.= "/*]]>*/";
      return true;
    }

    function renderReference($str, $argv, $parser)
    {
      $text=$this->renderWikiText($str, $parser);
      $this->mBibliography=BibliographyFormat::orderReferences($this->mBibliography);
#      var_dump($this->mBibliography);
      while(preg_match('/<'.$this->citeTag.' (.*?)\/>/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]);
        $reference=BibliographyFormat::intextcite($params);
        $text=substr_replace($text, $reference, $start, $len);
      }

      $reference=null;
      while(preg_match('/<bibliography\/>/ms', $text, $match, PREG_OFFSET_CAPTURE))
      {
        $start=$match[0][1];
        $len=strlen($match[0][0]);
        if(!isset($reference)) $reference=BibliographyFormat::referenceList($this->mBibliography);
        $text=substr_replace($text, $reference, $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="<".$this->citeTag." type=\"".(key_exists('type', $argv)?$argv['type']:'normal')."\"";
      foreach($this->mCite as $item=>$note)
      {
        $item=BibliographyFormat::getArticleName($item);
        $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");
        ?>
p {
  text-indent: 10px;
}

h1 {
  page-break-before: always;
}

h1.firstHeading {
  page-break-before: avoid;
}

p.bibliographicReference,
.bibliographicReference p {
  padding-left: 20px;
  text-indent: -20px;
  line-height: 2em;
}

.bibliographicReferences{
}
<?php
      die();
    case "bibliographyPrint.css":
      header("Content-type: text/css");
        ?>
.firstHeading,
.printfooter,
#footer,
#catlinks {
  display: none;
}
<?php
      die();
    case "bibliographyPrintFinal.css":
      header("Content-type: text/css");
        ?>
p {
  margin: .5em 0 .5em 0;
  line-height: 2em;
}
<?php
      die();
  }
}

?>
Personal tools