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 $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'metaCanonicalProcessing', array()); 32 } 33 34 /** 35 * Dokuwiki has already a canonical methodology 36 * https://www.dokuwiki.org/canonical 37 * 38 * @param $event 39 */ 40 function metaCanonicalProcessing($event) 41 { 42 43 global $ID; 44 if (empty($ID)){ 45 // $_SERVER['SCRIPT_NAME']== "/lib/exe/mediamanager.php" 46 // $ID is null 47 return; 48 } 49 50 $page = new Page($ID); 51 52 /** 53 * No canonical for slot page 54 */ 55 if ($page->isSlot()) { 56 return; 57 } 58 59 /** 60 * Where do we pick the canonical URL 61 */ 62 /** 63 * Canonical from meta 64 * 65 * FYI: The creation of the link was extracted from 66 * {@link wl()} that call {@link idfilter()} that performs just a replacement 67 * Calling the wl function will not work because 68 * {@link wl()} use the constant DOKU_URL that is set before any test via getBaseURL(true) 69 */ 70 71 72 $canonicalUrl = $page->getCanonicalUrlOrDefault(); 73 74 /** 75 * Replace the meta entry 76 * 77 * First search the key of the meta array 78 */ 79 $canonicalKey = ""; 80 $canonicalRelArray = array("rel" => "canonical", "href" => $canonicalUrl); 81 foreach ($event->data['link'] as $key => $link) { 82 if ($link["rel"] == "canonical") { 83 $canonicalKey = $key; 84 } 85 } 86 if ($canonicalKey != "") { 87 // Update 88 $event->data['link'][$canonicalKey] = $canonicalRelArray; 89 } else { 90 // Add 91 $event->data['link'][] = $canonicalRelArray; 92 } 93 94 /** 95 * Add the Og canonical meta 96 * https://developers.facebook.com/docs/sharing/webmasters/getting-started/versioned-link/ 97 */ 98 $canonicalOgKeyKey = ""; 99 $canonicalPropertyKey = "og:url"; 100 $canonicalOgArray = array("property" => $canonicalPropertyKey, "content" => $canonicalUrl); 101 // Search if the canonical property is already present 102 foreach ($event->data['meta'] as $key => $meta) { 103 if (array_key_exists("property",$meta)) { 104 /** 105 * We may have several properties 106 */ 107 if ($meta["property"] == $canonicalPropertyKey) { 108 $canonicalOgKeyKey = $key; 109 } 110 } 111 } 112 if ($canonicalOgKeyKey != "") { 113 // Update 114 $event->data['meta'][$canonicalOgKeyKey] = $canonicalOgArray; 115 } else { 116 // Add 117 $event->data['meta'][] = $canonicalOgArray; 118 } 119 120 } 121 122} 123