xref: /plugin/combo/action/metacanonical.php (revision 5f891b7e09648e05e78f5882f3fdde1e9df9b0f1)
1<?php
2
3use ComboStrap\MetadataUtility;
4use ComboStrap\PluginUtility;
5use ComboStrap\Page;
6
7if (!defined('DOKU_INC')) die();
8
9/**
10 *
11 *
12 *   * The name of the file should be the last name of the class
13 *   * There should be only one name
14 */
15class action_plugin_combo_metacanonical extends DokuWiki_Action_Plugin
16{
17
18    /**
19     * The conf
20     */
21    const CANONICAL_LAST_NAMES_COUNT_CONF = 'MinimalNamesCountForAutomaticCanonical';
22
23
24    function __construct()
25    {
26        // enable direct access to language strings
27        // ie $this->lang
28        $this->setupLocale();
29    }
30
31    public function register(Doku_Event_Handler $controller)
32    {
33        $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'metaCanonicalProcessing', array());
34    }
35
36    /**
37     * Dokuwiki has already a canonical methodology
38     * https://www.dokuwiki.org/canonical
39     *
40     * @param $event
41     */
42    function metaCanonicalProcessing($event)
43    {
44
45        global $ID;
46        if (empty($ID)){
47            // $_SERVER['SCRIPT_NAME']== "/lib/exe/mediamanager.php"
48            // $ID is null
49            return;
50        }
51
52        $page = new Page($ID);
53
54        /**
55         * No canonical for bars
56         */
57        if ($page->isBar()) {
58            return;
59        }
60
61        /**
62         * Where do we pick the canonical URL
63         */
64        /**
65         * Canonical from meta
66         *
67         * FYI: The creation of the link was extracted from
68         * {@link wl()} that call {@link idfilter()} that performs just a replacement
69         * Calling the wl function will not work because
70         * {@link wl()} use the constant DOKU_URL that is set before any test via getBaseURL(true)
71         */
72
73
74        $canonicalUrl = $page->getCanonicalUrlOrDefault();
75
76        /**
77         * Replace the meta entry
78         *
79         * First search the key of the meta array
80         */
81        $canonicalKey = "";
82        $canonicalRelArray = array("rel" => "canonical", "href" => $canonicalUrl);
83        foreach ($event->data['link'] as $key => $link) {
84            if ($link["rel"] == "canonical") {
85                $canonicalKey = $key;
86            }
87        }
88        if ($canonicalKey != "") {
89            // Update
90            $event->data['link'][$canonicalKey] = $canonicalRelArray;
91        } else {
92            // Add
93            $event->data['link'][] = $canonicalRelArray;
94        }
95
96        /**
97         * Add the Og canonical meta
98         * https://developers.facebook.com/docs/sharing/webmasters/getting-started/versioned-link/
99         */
100        $canonicalOgKeyKey = "";
101        $canonicalPropertyKey = "og:url";
102        $canonicalOgArray = array("property" => $canonicalPropertyKey, "content" => $canonicalUrl);
103        // Search if the canonical property is already present
104        foreach ($event->data['meta'] as $key => $meta) {
105            if (array_key_exists("property",$meta)) {
106                /**
107                 * We may have several properties
108                 */
109                if ($meta["property"] == $canonicalPropertyKey) {
110                    $canonicalOgKeyKey = $key;
111                }
112            }
113        }
114        if ($canonicalOgKeyKey != "") {
115            // Update
116            $event->data['meta'][$canonicalOgKeyKey] = $canonicalOgArray;
117        } else {
118            // Add
119            $event->data['meta'][] = $canonicalOgArray;
120        }
121
122    }
123
124}
125