1<?php
2/**
3 * DokuWiki plugin PageTitle; Helper component
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Satoshi Sahara <sahara.satoshi@gmail.com>
7 */
8
9if (!defined('DOKU_INC')) die();
10
11class helper_plugin_pagetitle extends DokuWiki_Plugin
12{
13    /**
14     * Hierarchical breadcrumbs for PageTitle plugin
15     *
16     * @param int    $start_depth of hierarchical breadcrumbs
17     * @param bool   $print if false return content
18     * @return bool|string html, or false if no data, true if printed
19     */
20    public function tpl_youarehere($start_depth = 0, $print = true)
21    {
22        global $lang;
23
24        $out = '<span class="bchead">'.$lang['youarehere'].'</span>';
25        $out.= $this->html_youarehere($start_depth);
26        if ($print) {
27            echo $out; return (bool) $out;
28        }
29        return $out;
30    }
31
32    public function html_youarehere($start_depth = 0, $page = null, &$traces = [])
33    {
34        global $conf, $ID;
35
36        if (is_null($page)) {
37            $page = $ID;
38        }
39
40        //prepend virtual root namespace to $ID that is not start page
41        $nodes = ($page == $conf['start']) ? array('') : explode(':', ':'.$page);
42        $depth = count($nodes);
43        $items = array();
44
45        for ($i = $start_depth; $i < $depth; $i++) {
46            $id = $id0 = implode(':', array_slice($nodes, 0, $i+1));
47
48            if (empty($id)) {                       // root start page
49                $id = $conf['start'];
50            } elseif ($id == ':'.$conf['start']) {  // start namespace
51                $id = $conf['start'].':'.$conf['start'];
52                resolve_pageid('', $id, $exists);
53                $name = $conf['start'];
54            } else {
55                resolve_pageid('', $id, $exists);
56                if (!$exists) {
57                    $id = $id.':';
58                    resolve_pageid('', $id, $exists);
59                }
60                $name = p_get_metadata($id, 'shorttitle', METADATA_DONT_RENDER) ?: noNS($id0);
61            }
62            $traces[$i] = $id;
63
64            if ($i < $depth-1 OR ($i == $depth-1 AND !preg_match('/.*:'.$conf['start'].'$/', $id))) {
65                $items[] = '<bdi>'.$this->html_pagelink($id, $name, $exists).'</bdi>';
66            }
67        }
68        // join items with a separator
69        $out = implode(' ›&#x00A0;', $items);
70        return $out;
71    }
72
73
74    /**
75     * Prints a link to a WikiPage
76     * a customised function based on
77     *   tpl_pagelink() defined in inc/template.php,
78     *   html_wikilink() defined in inc/html.php,
79     *   internallink() defined in inc/parser/xhtml.php
80     *
81     * @param string $id    page id
82     * @param string $name  the name of the link
83     * @param bool   $exists
84     * @param bool   $print if false return content
85     * @return bool|string html, or false if no data, true if printed
86     */
87    public function tpl_pagelink($id = null, $name = null, $exists = null, $print = true)
88    {
89        global $conf;
90
91        $out = $this->html_pagelink($id, $name, $exists);
92        if ($print) {
93            echo $out; return (bool) $out;
94        }
95        return $out;
96    }
97
98    private function html_pagelink($id = null, $name = null, $exists = null)
99    {
100        global $conf, $ID;
101
102        if (is_null($id)) $id = $ID;
103
104        $title = p_get_metadata($id, 'title');
105        if (empty($title)) $title = $id;
106
107        if (is_null($exists)) {
108            $class = (page_exists($id)) ? 'wikilink1' : 'wikilink2';
109        } else {
110            $class = ($exists) ? 'wikilink1' : 'wikilink2';
111        }
112
113        $short_title = $name;
114        if (empty($name)) {
115            $short_title = p_get_metadata($id, 'shorttitle') ?: noNS($id);
116        }
117
118        $out = '<a href="'.$this->wl($id).'" class="'.$class.'" title="'.hsc($title).'">';
119        $out.= hsc($short_title).'</a>';
120        return $out;
121    }
122
123
124    /**
125     * builds url of a wikipage
126     * a simplified function of DokuWiki wl() defined inc/common.php
127     *
128     * @param string   $id  page id
129     * @return string
130     */
131    private function wl($id = null)
132    {
133        global $conf;
134
135        if (noNS($id) == $conf['start']) $id = ltrim(getNS($id).':', ':');
136        idfilter($id);
137
138        $xlink = DOKU_BASE;
139
140        switch ($conf['userewrite']) {
141            case 2: // eg. DOKU_BASE/doku.php/wiki:syntax
142                $xlink .= DOKU_SCRIPT.'/'.$id;
143            case 1: // eg. DOKU_BASE/wiki:syntax
144                $xlink .= $id;
145                $xlink = ($xlink == '/') ? '/' : rtrim($xlink,'/');
146                break;
147            default:
148                $xlink .= DOKU_SCRIPT;
149                $xlink .= ($id) ? '?id='.$id : '';
150        }
151        return $xlink;
152    }
153
154
155    /**
156     * Prints or returns the title of the given page (current one if none given)
157     * modified from DokuWiki original function tpl_pagetitle()
158     * defined in inc/template.php
159     * This variant function returns title from metadata, ignoring $conf['useheading']
160     *
161     * @param string $id    page id
162     * @param bool   $print if false return content
163     * @return bool|string html, or false if no data, true if printed
164     */
165    public function tpl_pagetitle($id = null, $print = true)
166    {
167        $out = $this->pagetitle($id);
168        if ($print) {
169            echo $out; return (bool) $out;
170        }
171        return $out;
172    }
173
174    private function pagetitle($id = null)
175    {
176        global $ACT, $ID, $conf, $lang;
177
178        if (is_null($id)) {
179            $title = (p_get_metadata($ID, 'title')) ?: $ID;
180        } else {
181            $title = (p_get_metadata($id, 'title')) ?: $id;
182        }
183
184        // default page title is the page name, modify with the current action
185        switch ($ACT) {
186            // admin functions
187            case 'adminhomepage' :
188            case 'admin' :
189                $page_title = $lang['btn_admin'];
190                // try to get the plugin name
191                /** @var $plugin DokuWiki_Admin_Plugin */
192                if (function_exists('plugin_getRequestAdminPlugin') &&
193                    ($plugin = plugin_getRequestAdminPlugin()) ) {
194                    $plugin_title = $plugin->getMenuText($conf['lang']);
195                    $page_title = $plugin_title ? $plugin_title : $plugin->getPluginName();
196                }
197                break;
198
199            // user functions
200            case 'login' :
201            case 'profile' :
202            case 'register' :
203            case 'resendpwd' :
204                $page_title = $lang['btn_'.$ACT];
205                break;
206
207            // wiki functions
208            case 'search' :
209            case 'index' :
210                $page_title = $lang['btn_'.$ACT];
211                break;
212
213            // page functions
214            case 'edit' :
215                $page_title = "✎ ".$title;
216                break;
217
218            case 'revisions' :
219                $page_title = $title . ' - ' . $lang['btn_revs'];
220                break;
221
222            case 'backlink' :
223            case 'recent' :
224            case 'subscribe' :
225                $page_title = $title . ' - ' . $lang['btn_'.$ACT];
226                break;
227
228            default : // SHOW and anything else not included
229                $page_title = $title;
230        }
231        return hsc($page_title);
232    }
233}
234