1<?php 2 3use ComboStrap\ExceptionNotFound; 4use ComboStrap\ExecutionContext; 5use ComboStrap\LogUtility; 6use ComboStrap\MarkupPath; 7use ComboStrap\Meta\Api\Metadata; 8 9 10/** 11 * Take the metadata description 12 * 13 * To known more about description and [[docs:seo:seo|search engine optimization]], see: 14 * [[https://developer.mozilla.org/en-US/docs/Learn/HTML/Introduction_to_HTML/The_head_metadata_in_HTML#Active_learning_The_descriptions_use_in_search_engines|Active learning: The description's use in search engines]]. 15 * [[https://developers.google.com/search/docs/beginner/seo-starter-guide#use-the-description-meta-tag|Description section of the Google SEO Starter guide]] 16 */ 17class action_plugin_combo_metadescription extends DokuWiki_Action_Plugin 18{ 19 20 const DESCRIPTION_META_KEY = 'description'; 21 const FACEBOOK_DESCRIPTION_PROPERTY = 'og:description'; 22 23 public function register(Doku_Event_Handler $controller) 24 { 25 $controller->register_hook('TPL_METAHEADER_OUTPUT', 'BEFORE', $this, 'description_modification', array()); 26 } 27 28 /** 29 * Add a meta-data description 30 * @param $event 31 * @param $param 32 */ 33 function description_modification(&$event, $param) 34 { 35 36 try { 37 $pageTemplate = ExecutionContext::getActualOrCreateFromEnv() 38 ->getExecutingPageTemplate(); 39 } catch (ExceptionNotFound $e) { 40 return; 41 } 42 43 if(!$pageTemplate->isSocial()){ 44 return; 45 } 46 47 /** 48 * Description 49 * https://www.dokuwiki.org/devel:metadata 50 */ 51 try { 52 $wikiPath = $pageTemplate->getRequestedContextPath(); 53 } catch (ExceptionNotFound $e) { 54 LogUtility::internalError("A social template should have a path"); 55 return; 56 } 57 58 $page = MarkupPath::createPageFromPathObject($wikiPath); 59 60 $description = $page->getDescriptionOrElseDokuWiki(); 61 if (empty($description)) { 62 $this->sendDestInfo($wikiPath->getWikiId()); 63 return; 64 } 65 66 // Add it to the meta 67 Metadata::upsertMetaOnUniqueAttribute( 68 $event->data['meta'], 69 "name", 70 [ 71 "name" => self::DESCRIPTION_META_KEY, 72 "content" => $description 73 ] 74 ); 75 Metadata::upsertMetaOnUniqueAttribute( 76 $event->data['meta'], 77 "property", 78 [ 79 "property" => self::FACEBOOK_DESCRIPTION_PROPERTY, 80 "content" => $description 81 ] 82 ); 83 84 85 86 } 87 88 /** 89 * Just send a test info 90 * @param $ID 91 */ 92 public function sendDestInfo($ID) 93 { 94 if (defined('DOKU_UNITTEST')) { 95 // When you make a admin test call, the page ID = start and there is no meta 96 // When there is only an icon, there is also no meta 97 global $INPUT; 98 $showActions = ["show", ""]; // Empty for the test 99 if (in_array($INPUT->str("do"), $showActions)) { 100 LogUtility::msg("Page ($ID): The description should never be null when rendering the page", LogUtility::LVL_MSG_INFO); 101 } 102 } 103 } 104 105 106} 107