1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Terence J. Grant <tjgrant@tatewake.com>
5 */
6
7include_once(DOKU_PLUGIN . 'cite/code.php'); //this ensures the functions in code.php are always available
8
9class action_plugin_cite extends DokuWiki_Action_Plugin
10{
11    /**
12     * register the eventhandlers
13     *
14     * @param Doku_Event_Handler $controller
15     */
16    public function register(Doku_Event_Handler $controller)
17    {
18        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, '_handle_act', array());
19        $controller->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, '_handle_tpl_act', array());
20        $controller->register_hook('MENU_ITEMS_ASSEMBLY', 'AFTER', $this, 'addsvgbutton', array());
21    }
22
23    public function _handle_act($event, $param)
24    {
25        if ($event->data != 'cite') {
26            return;
27        }
28        $event->preventDefault();
29    }
30
31    public function _handle_tpl_act($event, $param)
32    {
33        if ($event->data != 'cite') {
34            return;
35        }
36        $event->preventDefault();
37
38        global $conf, $ID, $REV, $INFO;
39
40        //get always a revision id
41        $revisionId = $REV; //$REV includes converted DATE_AT as well
42        if (!$revisionId) {
43            $revisionId = $INFO['lastmod'];
44        }
45
46        $pagename = tpl_pagetitle($ID, true);
47        $permanentUrl = wl($ID, ['rev' => $revisionId], true);
48        $author = $this->getConf('cite_author');
49        if ($author == '') {
50            $author = 'Anonymous Contributors';
51        }
52        $publisher = $this->getConf('cite_publisher');
53        if ($publisher == '') {
54            $publisher = hsc($conf['title']);
55        }
56
57        $revisionDateSummary = date('j F Y H:i T', $revisionId);
58        $revisionDateYear = date('Y', $revisionId);
59        $revisionDateMLA = date('j M Y, H:i T', $revisionId);
60        $revisionDateMHRA = date('j F Y, H:i T', $revisionId);
61        $revisionDateCBECSE = date('Y M j, H:i T', $revisionId);
62        $revisionDateAMA = date('F j, Y, H:i T', $revisionId);
63
64        $retrieveDateSummary = date('j F Y H:i T');
65        $retrieveDateAPA = date('H:i, j F, Y');
66        $retrieveDateMLA = date('j M Y, H:i');
67        $retrieveDateMHRA = date('j F Y');
68        $retrieveDateChicago = date('F j, Y');
69        $retrieveDateCBECSE = date('Y M j');
70        $retrieveDateBluebook = date('F j, Y');
71        $retrieveDataAMA = date('F j, Y');
72        $retrieveDataBibTeX = date('j-F-Y');
73
74
75        echo <<< EOT
76<h1><a id="bibliographic_details">Bibliographic details for "$pagename"</a></h2>
77<div class="level2">
78
79    <ul>
80        <li class="level1"><div class="li"> Page name: $pagename</div></li>
81        <li class="level1"><div class="li"> Author: $author</div></li>
82        <li class="level1"><div class="li"> Publisher: $publisher.</div></li>
83        <li class="level1"><div class="li"> Date of this revision: $revisionDateSummary</div></li>
84        <li class="level1"><div class="li"> Date retrieved: $retrieveDateSummary</div></li>
85        <li class="level1"><div class="li"> Permanent link: <a href="$permanentUrl" title="$permanentUrl">$permanentUrl</a></div></li>
86        <li class="level1"><div class="li"> Page Version ID: $revisionId</div></li>
87    </ul>
88
89    <p>
90     Please remember to check with your standards guide or professor&rsquo;s guidelines for the exact syntax to suit your needs.
91    </p>
92
93</div>
94<h2><a id="citation_styles_for">Citation styles for "$pagename"</a></h2>
95<div class="level2">
96
97</div>
98
99<h3><a id="apa_style">APA style</a></h3>
100<div class="level3">
101
102    <p>
103    $author ($revisionDateYear). $pagename. $publisher. Retrieved $retrieveDateAPA from <a href="$permanentUrl" title="$permanentUrl">$permanentUrl</a>.
104    </p>
105
106</div>
107<h3><a id="mla_style">MLA style</a></h3>
108
109<div class="level3">
110
111    <p>
112     &ldquo;$pagename.&rdquo; <u>$publisher</u>. $revisionDateMLA. $retrieveDateMLA &lt;<a href="$permanentUrl" title="$permanentUrl">$permanentUrl</a>&gt;.
113    </p>
114
115</div>
116<h3><a id="mhra_style">MHRA style</a></h3>
117<div class="level3">
118
119    <p>
120     $author, &lsquo;$pagename&rsquo;, $publisher, $revisionDateMHRA, &lt;<a href="$permanentUrl" title="$permanentUrl">$permanentUrl</a>&gt;
121     [accessed $retrieveDateMHRA]
122    </p>
123
124</div>
125<h3><a id="chicago_style">Chicago style</a></h3>
126<div class="level3">
127
128    <p>
129     $author, &ldquo;$pagename,&rdquo; $publisher, <a href="$permanentUrl" title="$permanentUrl">$permanentUrl</a>
130     (accessed $retrieveDateChicago).
131    </p>
132
133</div>
134<h3><a id="cbe_cse_style">CBE/CSE style</a></h3>
135
136<div class="level3">
137
138    <p>
139     $author. $pagename [Internet]. $publisher; $revisionDateCBECSE [cited $retrieveDateCBECSE].
140     Available from: <a href="$permanentUrl" title="$permanentUrl">$permanentUrl</a>.
141    </p>
142
143</div>
144<h3><a id="bluebook_style">Bluebook style</a></h3>
145<div class="level3">
146
147    <p>
148
149     $pagename, <a href="$permanentUrl" title="$permanentUrl">$permanentUrl</a> (last visited $retrieveDateBluebook).
150    </p>
151
152</div>
153<h3><a id="ama_style">AMA style</a></h3>
154<div class="level3">
155
156    <p>
157     $author. $pagename. $publisher. $revisionDateAMA. Available at:
158     <a href="$permanentUrl" title="$permanentUrl">$permanentUrl</a>. Accessed $retrieveDataAMA.
159    </p>
160
161</div>
162<h3><a id="bibtex_entry">BibTeX entry</a></h3>
163<div class="level3">
164<pre>
165 @misc{ wiki:xxx,
166   author = "$author",
167   title = "$pagename --- $publisher",
168   year = "$revisionDateYear",
169   url = "$permanentUrl",
170   note = "[Online; accessed $retrieveDataBibTeX]"
171 }
172</pre>
173
174    <p>
175    When using the LaTeX package url (<code>\usepackage{url}</code> somewhere in the preamble), which tends to give much more nicely
176    formatted web addresses, the following may be preferred:
177    </p>
178<pre>
179 @misc{ wiki:xxx,
180   author = "$author",
181   title = "$pagename --- $publisher",
182   year = "$revisionDateYear",
183   url = "\url{{$permanentUrl}}",
184   note = "[Online; accessed $retrieveDataBibTeX]"
185 }
186</pre>
187
188</div>
189EOT;
190    }
191
192    /**
193     * Add 'cite' button to page tools, new SVG based mechanism
194     *
195     * @param Doku_Event $event
196     */
197    public function addsvgbutton(Doku_Event $event)
198    {
199        global $INFO;
200        if ($event->data['view'] != 'page' || !$this->getConf('showcitebutton')) {
201            return;
202        }
203
204        if (!$INFO['exists']) {
205            return;
206        }
207
208        array_splice($event->data['items'], -1, 0, [new \dokuwiki\plugin\cite\MenuItem()]);
209    }
210}
211