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 global $ID; 45 global $conf; 46 47 48 /** 49 * No canonical for bars 50 */ 51 $page = new Page($ID); 52 53 if ($page->isBar()) { 54 return; 55 } 56 57 /** 58 * Where do we pick the canonical URL 59 */ 60 /** 61 * Canonical from meta 62 * 63 * FYI: The creation of the link was extracted from 64 * {@link wl()} that call {@link idfilter()} that performs just a replacement 65 * Calling the wl function will not work because 66 * {@link wl()} use the constant DOKU_URL that is set before any test via getBaseURL(true) 67 */ 68 69 $canonical = MetadataUtility::getMeta(Page::CANONICAL_PROPERTY); 70 71 /** 72 * The last part of the id as canonical 73 */ 74 // How many last parts are taken into account in the canonical processing (2 by default) 75 $canonicalLastNamesCount = $this->getConf(self::CANONICAL_LAST_NAMES_COUNT_CONF, 0); 76 if ($canonical == null && $canonicalLastNamesCount > 0) { 77 /** 78 * Takes the last names part 79 */ 80 $names = $page->getNames(); 81 $namesLength = sizeof($names); 82 if ($namesLength > $canonicalLastNamesCount) { 83 $names = array_slice($names, $namesLength - $canonicalLastNamesCount); 84 } 85 /** 86 * If this is a start page, delete the name 87 * ie javascript:start will become javascript 88 */ 89 if ($page->isStartPage()) { 90 $names = array_slice($names, 0, $namesLength - 1); 91 } 92 $canonical = implode(":", $names); 93 p_set_metadata($ID, array(Page::CANONICAL_PROPERTY => $canonical)); 94 } 95 96 $canonicalUrl = Page::getUrl($canonical); 97 98 /** 99 * Replace the meta entry 100 * 101 * First search the key of the meta array 102 */ 103 $canonicalKey = ""; 104 $canonicalRelArray = array("rel" => "canonical", "href" => $canonicalUrl); 105 foreach ($event->data['link'] as $key => $link) { 106 if ($link["rel"] == "canonical") { 107 $canonicalKey = $key; 108 } 109 } 110 if ($canonicalKey != "") { 111 // Update 112 $event->data['link'][$canonicalKey] = $canonicalRelArray; 113 } else { 114 // Add 115 $event->data['link'][] = $canonicalRelArray; 116 } 117 118 /** 119 * Add the Og canonical meta 120 * https://developers.facebook.com/docs/sharing/webmasters/getting-started/versioned-link/ 121 */ 122 $canonicalOgKeyKey = ""; 123 $canonicalPropertyKey = "og:url"; 124 $canonicalOgArray = array("property" => $canonicalPropertyKey, "content" => $canonicalUrl); 125 foreach ($event->data['meta'] as $key => $meta) { 126 if ($meta["property"] == $canonicalPropertyKey) { 127 $canonicalOgKeyKey = $key; 128 } 129 } 130 if ($canonicalOgKeyKey != "") { 131 // Update 132 $event->data['meta'][$canonicalOgKeyKey] = $canonicalOgArray; 133 } else { 134 // Add 135 $event->data['meta'][] = $canonicalOgArray; 136 } 137 138 } 139 140} 141