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