Added templates
[bibliodb.git] / citeproc.php
blob587cf84d852effb300f037f50e239a688211ea98
1 <?php
2 $cas=microtime();
3 /*Generátor citací, podle specifikace citeproc
4 */
5 define("field_not_exist","field doesn`t exist");
6 define("error_message","Error occured: ");
7 define("bad_data","multiple fields, should be just one");
9 class citeprocData{
11 private $data=array();
12 private $fieldslist=array();
14 public function loadData($data){
15 for($i=0;$i<count($data);$i++){
16 $this->data[$i]=$data[$i];
17 $this->fieldslist[$data[$i]["type"]][]=$i;
20 private function getData($field){
21 if(array_key_exists($field,$this->fieldslist)){
22 $list=array();
23 foreach($this->fieldslist[$field] as $record){
24 $list[]=$this->data[$record];
26 return $list;
27 }else{
28 throw new Exception(field_not_exist);
31 public function getField($field){
32 try{
33 $list=$this->getData($field);
34 if(count($list)==1){
35 return($list[0]["value"]);
36 }else{
37 throw new Exception(bad_data);
39 }catch(Exception $e){
40 $this->printError($e);
43 public function getList($field){
44 return $this->getData($field);
48 class citeprocBase{
49 public $prefix="";
50 public $suffix="";
51 public $macros=array();
52 public $data;
53 function __construct(citeprocData $data=null){
54 $this->data=$data;
56 public function addMacro($name,&$code){
57 $this->macros[$name]=$code;
60 public function get($value){
61 return $this->prefix.$this->data->getField($value).$this->suffix;
64 public function printError(Exception $e){
65 echo "<div style=\"border:1px solid black;color:red;\">".error_message.$e->getMessage()."</div>\n";
69 class citeprocInfo extends citeprocBase{
70 public $id="";
71 public $title="";
72 public $link="";
73 public $author=array(array("name"=>"","email"=>"","uri"=>""));
74 public $citation_format="";
75 public $field="";
76 public $updated="";
77 public $summary="";
78 public $rights="";
81 class citeprocBibliography extends citeprocBase{
82 function printBibliography(array $fields){
83 foreach($fields as $field=>$macro){
84 $exec=$this->macros[$macro];
85 echo $exec->get($field);
89 class citeprocCitation extends citeprocBase{}
91 class citeprocField extends citeprocBase{
92 public $locale;
93 public $data;
94 public $suffix=". ";
95 public function __construct(citeprocData $data,$locale=null){
96 $this->data=$data;
97 $this->locale=$locale;
100 class citeprocText extends citeprocField{}
102 class citeprocDate extends citeprocField{
103 public $form=array("year-month-day","year-month","year");
104 /* "year-month-day" - default, displays year, month and day
105 * "year-month" - displays year and month
106 * "year" - displays year only*/
109 class citeprocList extends citeprocField{}
111 class citeprocAuthor extends citeprocList{
112 public $et_al_min=5;
113 public $et_al_uses_first=3;
114 public $and=" - ";
115 public $delimiter=" - ";
116 public $delimiter_precedes_last=array("");
117 public $form="long";
118 private function formatAuthor($author){
119 return $author["last"].", ".$author["first"];
121 public function get($field){
122 $authors=$this->data->getList($field);
123 $v=array();
124 if(count($authors)>0){
125 $v[]= $this->prefix;
126 count($authors)<$this->et_al_min ? $authorlist=count($authors) : $authorlist=$this->et_al_uses_first;
127 $authorlist==count($authors) ? $etal="" : $etal=$this->locale->get("et-al");
128 $m=array();
129 for($i=0;$i<$authorlist;$i++){
130 $m[]=$this->formatAuthor($authors[$i]["value"]);
132 $v[]=implode($this->delimiter,$m);
133 $v[]=" $etal";
134 $v[]= $this->suffix;
135 }else{
136 $v[]="¾ádný autoøi";
138 return $this->prefix.implode("",$v);
143 class citeprocLocale extends citeprocBase{
144 public $lang;
145 public $locale=array("cs"=>array(
146 "et-al"=>"et al"
148 function get($c){
149 return $this->locale[$this->lang][$c];
153 class Logical{
154 public $op=null;
155 public $np=array();
156 protected $n=array("kk","nn","pp");
157 function nul(){
158 $this->np=array();
159 $this->op=null;
161 function getValue(){
162 if(count($this->np)<1)return null;
163 if($this->op==null){
164 return array_shift($this->np);
165 }else{
166 $n=$this->op;
167 $this->op=null;
168 return $n;
171 function is($c){
172 $this->np[]=in_array($c,$this->n) ? true : false;
173 return $this;
175 function a(){
176 $p=true;
177 $m=$this->getValue();
178 while($m!==null){
179 $p=$p&&$m;
180 $m=$this->getValue();
182 $this->op=$p;
183 return $this;
185 function o(){
186 $p=false;
187 $m=$this->getValue();
188 while($m!==null){
189 $p=$p||$m;
190 $m=$this->getValue();
192 $this->op=$p;
193 return $this;
196 function dump(){
197 echo "<p>$this->op</p>";
198 return $this;
203 $citeData=new citeprocData();
204 $data= array(
205 array("type"=>"author","value"=>array("first"=>"Jan","last"=>"Krka")),
206 array("type"=>"author","value"=>array("first"=>"Martin","last"=>"Bla¾ek")),
207 array("type"=>"author","value"=>array("first"=>"Pepa","last"=>"Nosek")),
208 array("type"=>"author","value"=>array("first"=>"Jirka","last"=>"Cicek")),
209 array("type"=>"author","value"=>array("first"=>"Martin","last"=>"Bla¾ek")),
210 array("type"=>"title","value"=>"Titulek knihy"),
211 array("type"=>"date","value"=>"2009"),
212 array("type"=>"publisher","value"=>"Academia")
214 $citeData->loadData($data);
215 $locale=new citeprocLocale();
216 $locale->lang="cs";
217 //echo $citeData->getField("autnhor");
218 $base=new citeprocField($citeData,$locale);
219 $author=new citeprocAuthor($citeData,$locale);
220 $bibliography=new citeprocBibliography($citeData);
221 $bibliography->addMacro("base",$base);
222 $bibliography->addMacro("author",$author);
223 $bibliography->printBibliography(array(
224 "author"=>"author",
225 "title"=>"base",
226 "date"=>"base",
227 "publisher"=>"base"
229 $c=new Logical;
230 $c->is("nn")->is("kk")->is("kk")->a()->is("nn")->a()->is("nn")->a()->is("nnk")->o()->dump();
231 echo (microtime()-$cas);