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