xref: /template/strap/action/metadescription.php (revision 007225e5fb2d3f64edaccd3bd447ca26effb9d68)
1<?php
2
3use ComboStrap\LogUtility;
4
5
6
7/**
8 * Take the metadata description
9 *
10 */
11
12
13class action_plugin_combo_metadescription extends DokuWiki_Action_Plugin
14{
15
16    const DESCRIPTION_META_KEY = 'description';
17    const DESCRIPTION_PROPERTY = 'og:description';
18
19    public function register(Doku_Event_Handler $controller)
20    {
21        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'description_modification', array());
22    }
23
24    /**
25     * Add a meta-data description
26     */
27    function description_modification(&$event, $param)
28    {
29
30        // if (empty($event->data) || empty($event->data['meta'])) return;
31
32        global $ID;
33
34        /**
35         * Description
36         * https://www.dokuwiki.org/devel:metadata
37         */
38        if (defined('DOKU_UNITTEST')) {
39            if ($ID == null || $ID == "") {
40                return;  // Admin call for instance in test
41            }
42            $dokuWikiDescription = p_get_metadata($ID, self::DESCRIPTION_META_KEY);
43        } else {
44            $render = METADATA_RENDER_USING_CACHE;
45            $dokuWikiDescription = p_get_metadata($ID, self::DESCRIPTION_META_KEY, $render);
46        }
47
48        if (empty($dokuWikiDescription) || $dokuWikiDescription == "") {
49            $this->sendDestInfo($ID);
50            return;
51        }
52
53        // Get the abstract and suppress the carriage return
54        $description = str_replace("\n", " ", $dokuWikiDescription['abstract']);
55        if (empty($description)) {
56            $this->sendDestInfo($ID);
57            return;
58        }
59
60        // Suppress the title
61        $title = p_get_metadata($ID, 'title');
62        $description = str_replace($title, "", $description);
63        // Suppress the star, the tab, About
64        $description = preg_replace('/(\*|\t|About)/im', "", $description);
65        // Suppress all double space and trim
66        $description = trim(preg_replace('/  /m', " ", $description));
67
68        // Add it to the meta
69        $event->data['meta'][] = array("name" => self::DESCRIPTION_META_KEY, "content" => $description);
70        $event->data['meta'][] = array("property" => self::DESCRIPTION_PROPERTY, "content" => $description);
71
72
73    }
74
75    /**
76     * Just send a test info
77     * @param $ID
78     */
79    public function sendDestInfo($ID)
80    {
81        if (defined('DOKU_UNITTEST')) {
82            // When you make a admin test call, the page ID = start and there is no meta
83            // When there is only an icon, there is also no meta
84            global $INPUT;
85            $showActions = ["show", ""]; // Empty for the test
86            if (in_array($INPUT->str("do"), $showActions)) {
87                LogUtility::msg("Page ($ID): The description should never be null when rendering the page", LogUtility::LVL_MSG_INFO);
88            }
89        }
90    }
91
92
93}
94