1<?php 2 3/** 4 * Bibliography handler is responsible for getting the bibliography info from Zotero portal. 5 * 6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 7 * @author Adam Kučera <adam.kucera@wrent.cz> 8 */ 9 10/** 11 * Class representing the Bibliography handler. 12 */ 13class BibHandler { 14 15 /** 16 * Zotero $type id 17 * @var int 18 */ 19 protected $id; 20 /** 21 * Group or User 22 * @var string 23 */ 24 protected $type; 25 /** 26 * Zotero access key 27 * @var string 28 */ 29 protected $key; 30 /** 31 * Zotero local repository location 32 * @var string 33 */ 34 protected $repository; 35 /** 36 * Bibliography entries itself. 37 * @var array 38 */ 39 protected $bib_entries; 40 /** 41 * An instance of BibHandler 42 * @var BibHandler 43 */ 44 protected static $instance; 45 46 /** 47 * BibHandler is singleton, so this function is used to retrive the link to it. 48 * @return BibHandler 49 */ 50 public static function getInstance() { 51 if(!isset(BibHandler::$instance)) { 52 BibHandler::$instance = new BibHandler(); 53 } 54 return BibHandler::$instance; 55 } 56 57 /** 58 * Private construktor, only static method can construct it 59 * @global array $conf Global DokuWiki configuration 60 */ 61 protected function __construct() { 62 global $conf; 63 $this->bib_entries = array(); 64 65 //get the zotero configuration file 66 $zotero_config = file_get_contents(DOKU_INC . 'lib/plugins/zotero/config.ini'); 67 //parse ID and its type 68 preg_match('#([usergop]*)id =([ \d]*)#', $zotero_config, $match); 69 $this->type = trim($match[1]) . "s"; 70 $this->id = trim($match[2]); 71 //parse access key 72 preg_match('#key =(.*)$#m', $zotero_config, $match); 73 $this->key = trim($match[1]); 74 //parse local repository location 75 preg_match('#cachePage =(.*)$#m', $zotero_config, $match); 76 $namespace = explode(':', trim($match[1])); 77 $this->repository = $conf['datadir']; 78 foreach ($namespace as $name) { 79 $this->repository .= '/'; 80 $this->repository .= $name; 81 } 82 $this->repository .= '.txt'; 83 } 84 85 /** 86 * Load an entry from the external repository using REST api and the 87 * information from local repository and insert it to bibliography items. 88 * @param string $entry Short title of an cited bibliography. 89 */ 90 public function insert($entry) { 91 //parse ID of the given $entry 92 $rep = file_get_contents($this->repository); 93 $regex = '#\|(.{8})\]\]\|' . $entry . '\|#'; 94 preg_match($regex, $rep, $match); 95 $id = $match[1]; 96 97 //load the bibtex file using REST api 98 $url = "https://api.zotero.org/". 99 $this->type 100 ."/" . 101 $this->id 102 . "/items/" . 103 $id 104 . "?key=" . 105 $this->key 106 . "&format=atom&content=bibtex"; 107 $item = simplexml_load_string(file_get_contents($url)); 108 $bib_item = (string) $item->content; 109 //make the short title as the title of the entry 110 preg_match('#^[@].*\{(.*),$#m', $bib_item, $match); 111 $bib_item = str_replace($match[1], $entry, $bib_item); 112 $this->bib_entries[$entry] = $bib_item; 113 } 114 115 /** 116 * Get the bibtex file from all the entries. 117 * @return string 118 */ 119 public function getBibtex() { 120 $bibtex = ''; 121 foreach ($this->bib_entries as $bib) { 122 $bibtex .= $bib . "\n\n"; 123 } 124 return $bibtex; 125 } 126 127 /** 128 * Returns true, if there is no bibliography entries. 129 * @return boolean 130 */ 131 public function isEmpty() { 132 if(empty($this->bib_entries)) { 133 return true; 134 } else { 135 return false; 136 } 137 } 138} 139