1<?php
2/*
3description : Dokuwiki PubMed2020 plugin: PubMed2020 Helper for Bureaucracy
4author      : Eric Maeker
5email       : eric.maeker[at]gmail.com
6lastupdate  : 2020-12-27
7license     : Public-Domain
8*/
9
10/**
11 * Class helper_plugin_pubmed2020_pmid
12 *
13 * A PubMed2020 form field for Bureaucracy plugin
14 *
15 * Usage:
16 * <form>
17 * ...
18 * pubmed2020_pmid "label"
19 * </form>
20 *
21 * Notes:
22 * You MUST only use ONE pubmed2020_pmid field by form.
23 *
24 * Template usage:
25 * @@yourlabel@@  -> replaced with PMID
26 * @@yourlabel.dataname@@  -> replaced with paper 'dataname' value where 'dataname' in
27   * Ids:
28   *   "pmid"          -> PMID
29   *   "pmcid"         -> if available PMCID
30   *   "doi"           -> DOI references when available
31   *   "pii"           -> PII references when available
32   *   "bookaccession"
33   *
34   * Authors:
35   *   "authors"       -> Array of authors
36   *   "first_author"  -> First author + "et al." if other authors are listed
37   *   "authorsLimit3" -> Three first authors + "et al." if other authors are listed
38   *   "authorsVancouver" -> according to the configuration of the plugin
39   *   "corporate_author" -> If author is corporate
40   *                        (in this case also included in authors and first_author)
41   *   "collectif"     -> If author is a collective
42   *
43   * Titles:
44   *   "title"         -> Full title
45   *   "title_low"     -> Lowered full title
46   *   "translated_title" -> Translated title (TODO: improve this)
47   *   "translated_title_low" -> Lowered translated title (TODO: improve this)
48   *   "book_title"
49   *
50   * Journal:
51   *   "journal_iso"   -> Journal ISO Abbreviation
52   *   "journal_title" -> Journal full title
53   *   "journal_id"
54   *
55   * Other references:
56   *   "lang"          -> language of the article
57   *   "iso"
58   *   "vol"           -> Journal Volume
59   *   "issue"         -> Journal Issue
60   *   "year"          -> Journal Year of publication
61   *   "month"         -> Journal Month of publication
62   *   "pages"         -> Journal pagination
63   *   "abstract"      -> Complete abstract
64   *   "type"          -> Type of paper
65   *   "country"
66   *   "copyright"
67   *   "collection_title"
68   *   "publisher"
69   *
70   * Keywords, Mesh and Hastags:
71   *   "keywords"     -> Non-mesh keywords of the paper
72   *   "mesh"         -> Mesh terms associated with the paper
73   *   "hashtags"     -> Added hastag with command 'addhash'
74   *
75   * Hard coded citations:
76   *   "iso"           -> ISO citation of the article
77   *   "npg_full"      -> Citation for: Neurologie Psychiatrie Geriatrie journal
78   *   "npg_iso"       -> Same with authors and title
79   *
80   * Links:
81   *   "url"           -> URL to PubMed site
82   *   "pmcurl"        -> if available URL to PMC site
83   *   "googletranslate_abstract"   -> Link to google translate prepopulated with abstract
84 */
85class helper_plugin_pubmed2020_pmid extends helper_plugin_bureaucracy_fieldtextbox {
86
87    var $paper_refs = array();
88
89    /**
90     * Arguments:
91     *  - cmd
92     *  - label
93     *  - ^ (optional)
94     *
95     * @param array $args The tokenized definition, only split at spaces
96     */
97    public function initialize($args) {
98        parent::initialize($args);
99        $this->tpl['class'] .= ' pubmed';
100    }
101
102    /**
103     * Allow receiving paper attributes by ".".
104     *    Ex. yourlabel.attribute
105     * TODO: You can pass an optional argument enclosed in brackets, used as a delimiter
106     *    Ex. yourlabel.attributewithdelimiter(, )
107     *
108     * @return string
109     */
110    public function getReplacementPattern() {
111        $label = $this->opt['label'];
112        return '/(@@|##)' . preg_quote($label, '/') .
113            '(?:\.(.*?))?' .    //match attribute after "."
114            '(?:\((.*?)\))?' .  //match parameter enclosed in "()". Used for grps separator
115            '\1/si';
116    }
117
118    /**
119     * Used as an callback for preg_replace_callback
120     *
121     * Access PMID references
122     * When using a pubmed2020_pmid field:
123     *   additional data of the selected paper (PMID) can be used in the template
124     *   '@@fieldlabel.EXTRAFIELD@@' replace with the EXTRAFIELD of the publication
125     *
126     * \see PubMed2020::readMedlineContent() documentation for the EXTRAFIELD list
127     *
128     * @param $matches
129     * @return string
130     */
131    public function replacementValueCallback($matches) {
132//         echo PHP_EOL."<!-------- replacementMultiValueCallback ".PHP_EOL;
133//         echo print_r($matches);
134//         echo "------>".PHP_EOL;
135
136        // Get PMID references
137
138        $value = $this->opt['value'];
139        // attr doesn't exists
140        if (!isset($matches[2])) {
141            return is_null($value) || $value === false ? '' : $value;
142        }
143        $attr = $matches[2];
144
145        // Check and use delimiter
146        // $delimiter = ', ';
147        // if (isset($matches[3])) {
148        //     $delimiter = $matches[3];
149        //  }
150        //  return implode($delimiter, array());
151
152        if (!empty($this->paper_refs[$attr]))
153          return $this->paper_refs[$attr];
154
155        return $matches[0];
156    }
157
158    /**
159     * Return the callback for user replacement
160     *
161     * @return array
162     */
163    public function getReplacementValue() {
164        return array($this, 'replacementValueCallback');
165    }
166
167    /**
168     * Validate value of field
169     * Download from PubMed and
170     * Get the PMID references
171     * Using the PubMed2020 syntax plugin
172     *
173     * @throws Exception when user not exists
174     */
175    protected function _validate() {
176        parent::_validate();
177
178        $pmid = $this->opt['value'];
179
180        // Get the PMID using syntax_plugin_pubmed2020
181        // And store the references array in $this->paper_refs var
182        if(!plugin_isdisabled('pubmed2020')) {
183          $plugin =& plugin_load('syntax', 'pubmed2020');
184          if($plugin) {
185            // With the PubMed2020 plugin: get raw medline content from pubmed website
186            $raw = $plugin->getMedlineContent("pmid", $pmid);
187
188            // With the PubMed2020 plugin: process raw to get the reference array
189            if (empty($raw))
190              $this->paper_refs = array();
191            else
192              $this->paper_refs = $plugin->pubmed2020->readMedlineContent($raw, $plugin);
193          }
194        }
195    }
196}
197