1<?php
2if(!defined('DOKU_INC')) die();
3if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
4require_once(DOKU_PLUGIN.'syntax.php');
5
6class syntax_plugin_pubmed extends DokuWiki_Syntax_Plugin {
7  var $ncbi;
8  var $xmlCache;
9  // Constructor
10  function __construct(){
11    if (!class_exists('plugin_cache'))
12      @require_once(DOKU_PLUGIN.'pubmed/classes/cache.php');
13    if (!class_exists('rcsb')||!class_exists('ncbi')||!class_exists('xml'))
14      @require_once(DOKU_PLUGIN.'pubmed/classes/sciencedb.php');
15    $this->ncbi     = new ncbi();
16    $this->xmlCache = new plugin_cache("ncbi_esummary","pubmed","xml.gz");
17
18    if($this->ncbi == Null) print("NCBI is null !");
19    if($this->xmlCache == Null) print("this->xmlCache is null !");
20  }
21  function getType(){ return 'substition'; }
22  function getSort(){ return 158; }
23  function connectTo($mode){$this->Lexer->addSpecialPattern('\{\{pubmed>[^}]*\}\}',$mode,'plugin_pubmed');}
24 /**
25  * Handle the match
26  */
27  function handle($match, $state, $pos, Doku_Handler $handler){
28    $match = substr($match,9,-2);
29    if(str_contains($match,':')){
30      list($cmd,$pmid) = explode(':',$match);
31    }else{
32        $cmd = $match;
33        $pmid = '';
34    }
35    return array($state,array($cmd,$pmid));
36  }
37 /**
38  * Create output
39  */
40  function render($mode, Doku_Renderer $renderer, $data) {
41    if ($mode!='xhtml')
42      return false;
43    list($state, $query) = $data;
44    list($cmd, $pmid) = $query;
45    $cmd = strtolower($cmd);
46    if ($cmd=='long' || $cmd=='short'){
47      if (!is_numeric($pmid)){
48        $renderer->doc.=sprintf($this->getLang('pubmed_wrong_format'));
49        return false;
50      }
51      $xml = $this->getSummaryXML($pmid);
52      if(empty($xml)){
53        $renderer->doc.=sprintf($this->getLang('pubmed_not_found'),$pmid);
54        return false;
55      }
56      $href_url = sprintf($this->ncbi->pubmedURL,$pmid);
57      $journal = $this->ncbi->GetSummaryItem ("Source",$xml);
58      $date    = $this->ncbi->GetSummaryItem ("PubDate",$xml);
59      $volume  = $this->ncbi->GetSummaryItem ("Volume",$xml);
60      $pages   = $this->ncbi->GetSummaryItem ("Pages",$xml);
61      $authors = $this->ncbi->GetSummaryItems("Author",$xml);
62      $title   = $this->ncbi->GetSummaryItem ("Title",$xml);
63
64      if ($cmd=='long'||$cmd=='short'){
65        $renderer->doc.='<div class="pubmed">';
66        if ($cmd=='long'){
67          $renderer->doc.= '<a href="'.$href_url.'">'.$title.'</a><br/>';
68          $renderer->doc.= implode(', ',$authors).'<br/>';
69          $renderer->doc.= '<span class="jrnl">'.$journal.'</span>';
70        }elseif($cmd=='short'){
71          if (count($authors)>1) $etal = '<span class="etal">et al.</span>';
72          $renderer->doc.= '<a href="'.$href_url.'">'.$authors[0].$etal;
73          $renderer->doc.= '<span class="jrnl">'.$journal.'</span></a>';
74        }
75        $renderer->doc.= '<span class="volume">'.$volume.'</span>';
76        $renderer->doc.= '<span class="pages">p'.$pages.'</span>';
77        $renderer->doc.= '<span class="date">('.$date.')</span></div>'.NL;
78      }
79    }else{
80      switch($cmd){
81        case 'summaryxml':
82          if (!is_numeric($pmid)){
83            $renderer->doc.=sprintf($this->getLang('pubmed_wrong_format'));
84            return false;
85          }
86          $xml = $this->getSummaryXML($pmid);
87          if(empty($xml)){
88            $renderer->doc.=sprintf($this->getLang('pubmed_not_found'),$pmid);
89            return false;
90          }
91          $renderer->doc .= "<pre>".htmlspecialchars($xml)."</pre>";
92          return true;
93
94        case 'clear_summary':
95          $this->xmlCache->ClearCache();
96          $renderer->doc .= 'Cleared.';
97          return true;
98
99        case 'remove_dir':
100          $this->xmlCache->RemoveDir();
101          $renderer->doc .= 'Directory cleared.';
102          return true;
103
104        default:
105          // Command was not found..
106          $renderer->doc.='<div class="pdb_plugin">'.sprintf($this->getLang('plugin_cmd_not_found'),$cmd).'</div>';
107          $renderer->doc.='<div class="pdb_plugin_text">'.$this->getLang('pubmed_available_cmd').'</div>';
108          return true;
109          $renderer->doc.=sprintf($this->getLang('pubmed_wrong_format'));
110          return true;
111      }
112    }
113  }
114
115 /**
116  * Get summary XML from cache or NCBI
117  */
118    function getSummaryXml($pmid){
119    global $conf;
120    if($this->xmlCache == Null){
121        print("Null !!");}
122
123    $cachedXml = $this->xmlCache->GetMediaText($pmid);
124    if ($cachedXml!==false){ return $cachedXml; }
125
126    // Get summary XML
127    $summary = $this->ncbi->SummaryXml('pubmed',$pmid);
128    $cachePath = $this->xmlCache->GetMediaPath($pmid);
129    if (!empty($summary)){
130      if(io_saveFile($cachePath,$summary)){
131        chmod($cachePath,$conf['fmode']);
132      }
133    }
134    return $summary;
135  }
136}
137// TODO: Implement search results !
138
139?>
140