1<?php
2
3use ComboStrap\LogUtility;
4use ComboStrap\Metadata;
5use ComboStrap\Page;
6
7
8/**
9 * Take the metadata description
10 *
11 * To known more about description and [[docs:seo:seo|search engine optimization]], see:
12 * [[https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/The_head_metadata_in_HTML#Active_learning_The_descriptions_use_in_search_engines|Active learning: The description's use in search engines]].
13 * [[https://developers.google.com/search/docs/beginner/seo-starter-guide#use-the-description-meta-tag|Description section of the Google SEO Starter guide]]
14 */
15class action_plugin_combo_metadescription extends DokuWiki_Action_Plugin
16{
17
18    const DESCRIPTION_META_KEY = 'description';
19    const FACEBOOK_DESCRIPTION_PROPERTY = 'og:description';
20
21    public function register(Doku_Event_Handler $controller)
22    {
23        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'description_modification', array());
24    }
25
26    /**
27     * Add a meta-data description
28     * @param $event
29     * @param $param
30     */
31    function description_modification(&$event, $param)
32    {
33
34        global $ID;
35        if (empty($ID)) {
36            return;  // Admin call for instance
37        }
38
39        /**
40         * Description
41         * https://www.dokuwiki.org/devel:metadata
42         */
43        $page = Page::createPageFromId($ID);
44
45        $description = $page->getDescriptionOrElseDokuWiki();
46        if (empty($description)) {
47            $this->sendDestInfo($ID);
48            return;
49        }
50
51        // Add it to the meta
52        Metadata::upsertMetaOnUniqueAttribute(
53            $event->data['meta'],
54            "name",
55            [
56                "name" => self::DESCRIPTION_META_KEY,
57                "content" => $description
58            ]
59        );
60        Metadata::upsertMetaOnUniqueAttribute(
61            $event->data['meta'],
62            "property",
63            [
64                "property" => self::FACEBOOK_DESCRIPTION_PROPERTY,
65                "content" => $description
66            ]
67        );
68
69
70
71    }
72
73    /**
74     * Just send a test info
75     * @param $ID
76     */
77    public function sendDestInfo($ID)
78    {
79        if (defined('DOKU_UNITTEST')) {
80            // When you make a admin test call, the page ID = start and there is no meta
81            // When there is only an icon, there is also no meta
82            global $INPUT;
83            $showActions = ["show", ""]; // Empty for the test
84            if (in_array($INPUT->str("do"), $showActions)) {
85                LogUtility::msg("Page ($ID): The description should never be null when rendering the page", LogUtility::LVL_MSG_INFO);
86            }
87        }
88    }
89
90
91}
92