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