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 46 if ($_SERVER['SCRIPT_NAME']== "/lib/exe/mediamanager.php"){ 47 // $ID is null 48 return; 49 } 50 51 global $ID; 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 $canonical = MetadataUtility::getMeta(Page::CANONICAL_PROPERTY); 74 75 /** 76 * The last part of the id as canonical 77 */ 78 // How many last parts are taken into account in the canonical processing (2 by default) 79 $canonicalLastNamesCount = $this->getConf(self::CANONICAL_LAST_NAMES_COUNT_CONF, 0); 80 if ($canonical == null && $canonicalLastNamesCount > 0) { 81 /** 82 * Takes the last names part 83 */ 84 $names = $page->getNames(); 85 $namesLength = sizeof($names); 86 if ($namesLength > $canonicalLastNamesCount) { 87 $names = array_slice($names, $namesLength - $canonicalLastNamesCount); 88 } 89 /** 90 * If this is a start page, delete the name 91 * ie javascript:start will become javascript 92 */ 93 if ($page->isStartPage()) { 94 $names = array_slice($names, 0, $namesLength - 1); 95 } 96 $canonical = implode(":", $names); 97 p_set_metadata($ID, array(Page::CANONICAL_PROPERTY => $canonical)); 98 } 99 100 $canonicalUrl = Page::getUrl($canonical); 101 102 /** 103 * Replace the meta entry 104 * 105 * First search the key of the meta array 106 */ 107 $canonicalKey = ""; 108 $canonicalRelArray = array("rel" => "canonical", "href" => $canonicalUrl); 109 foreach ($event->data['link'] as $key => $link) { 110 if ($link["rel"] == "canonical") { 111 $canonicalKey = $key; 112 } 113 } 114 if ($canonicalKey != "") { 115 // Update 116 $event->data['link'][$canonicalKey] = $canonicalRelArray; 117 } else { 118 // Add 119 $event->data['link'][] = $canonicalRelArray; 120 } 121 122 /** 123 * Add the Og canonical meta 124 * https://developers.facebook.com/docs/sharing/webmasters/getting-started/versioned-link/ 125 */ 126 $canonicalOgKeyKey = ""; 127 $canonicalPropertyKey = "og:url"; 128 $canonicalOgArray = array("property" => $canonicalPropertyKey, "content" => $canonicalUrl); 129 foreach ($event->data['meta'] as $key => $meta) { 130 if ($meta["property"] == $canonicalPropertyKey) { 131 $canonicalOgKeyKey = $key; 132 } 133 } 134 if ($canonicalOgKeyKey != "") { 135 // Update 136 $event->data['meta'][$canonicalOgKeyKey] = $canonicalOgArray; 137 } else { 138 // Add 139 $event->data['meta'][] = $canonicalOgArray; 140 } 141 142 } 143 144} 145