1<?php
2
3use ComboStrap\ExceptionCompile;
4use ComboStrap\ExceptionNotFound;
5use ComboStrap\Html;
6use ComboStrap\LogUtility;
7use ComboStrap\MarkupPath;
8use ComboStrap\PluginUtility;
9use ComboStrap\Site;
10
11
12/**
13 * Class action_plugin_combo_metatitle
14 * Set and manage the title of an HTML page
15 * The event is triggered in the strap template
16 */
17class action_plugin_combo_metatitle extends DokuWiki_Action_Plugin
18{
19
20
21    const TITLE_SEPARATOR = ' | ';
22
23    public function register(Doku_Event_Handler $controller)
24    {
25        $controller->register_hook('TPL_TITLE_OUTPUT', 'BEFORE', $this, 'handleTitle', array());
26    }
27
28    function handleTitle(&$event, $param)
29    {
30        $event->data = self::getHtmlTitle();
31    }
32
33
34    static function getHtmlTitle(): string
35    {
36
37        // Page Title
38        // Root Home page
39        $currentPage = MarkupPath::createFromRequestedPage();
40
41        $pageTitle = $currentPage->getTitleOrDefault();
42
43        // Namespace name
44        try {
45            $parentPage = $currentPage->getParent();
46            $pageTitle .= self::TITLE_SEPARATOR . $parentPage->getNameOrDefault();
47        } catch (ExceptionNotFound $e) {
48            // no parent
49        }
50
51        // Site name
52        if (!empty(Site::getName())) {
53            $pageTitle .= self::TITLE_SEPARATOR . Site::getName();
54        }
55
56        return Html::encode($pageTitle);
57
58    }
59}
60