1<?php 2 3use ComboStrap\CacheLog; 4use ComboStrap\CacheManager; 5use ComboStrap\Event; 6use ComboStrap\ExceptionBadArgument; 7use ComboStrap\ExceptionCompile; 8use ComboStrap\ExceptionNotExists; 9use ComboStrap\FileSystems; 10use ComboStrap\LogUtility; 11use ComboStrap\LowQualityCalculatedIndicator; 12use ComboStrap\LowQualityPageOverwrite; 13use ComboStrap\Meta\Store\MetadataDokuWikiStore; 14use ComboStrap\MarkupPath; 15use ComboStrap\MetadataMutation; 16use ComboStrap\PagePath; 17use ComboStrap\Site; 18 19 20 21/** 22 * Delete the backlinks when there is a page quality mutation 23 */ 24class action_plugin_combo_qualitymutation extends DokuWiki_Action_Plugin 25{ 26 27 28 public const QUALITY_MUTATION_EVENT_NAME = 'quality_mutation'; 29 const CANONICAL = "low_quality"; 30 const DESC = "desc"; 31 32 public static function getQualityMetas(): array 33 { 34 return [ 35 LowQualityCalculatedIndicator::getPersistentName(), 36 LowQualityPageOverwrite::getPersistentName() 37 ]; 38 } 39 40 41 public function register(Doku_Event_Handler $controller) 42 { 43 44 45 /** 46 * create the async event 47 */ 48 $controller->register_hook(MetadataMutation::PAGE_METADATA_MUTATION_EVENT, 'AFTER', $this, 'create_quality_mutation', array()); 49 50 /** 51 * process the Async event 52 */ 53 $controller->register_hook(self::QUALITY_MUTATION_EVENT_NAME, 'AFTER', $this, 'handle_quality_mutation'); 54 55 56 } 57 58 59 public function handle_quality_mutation(Doku_Event $event, $param) 60 { 61 62 63 $data = $event->data; 64 $path = $data[PagePath::getPersistentName()]; 65 $page = MarkupPath::createPageFromAbsoluteId($path); 66 67 if (!$page->getCanBeOfLowQuality()) { 68 return; 69 } 70 71 /** 72 * Delete the html document cache to rewrite the links 73 * 74 */ 75 foreach ($page->getBacklinks() as $backlink) { 76 try { 77 $htmlDocument = $backlink->createHtmlFetcherWithItselfAsContextPath(); 78 } catch (ExceptionNotExists $e) { 79 continue; 80 } 81 try { 82 $wikiId = $backlink->getWikiId(); 83 } catch (ExceptionBadArgument $e) { 84 LogUtility::internalError("Backlink should be only for wiki path"); 85 continue; 86 } 87 $desc = $data[self::DESC]; 88 CacheLog::deleteCacheIfExistsAndLog( 89 $htmlDocument, 90 self::QUALITY_MUTATION_EVENT_NAME, 91 "The {$wikiId} of {$path} had its HTML cache deleted ($desc)." 92 ); 93 94 } 95 } 96 97 98 /** 99 */ 100 function create_quality_mutation(Doku_Event $event, $params): void 101 { 102 103 if (!Site::isLowQualityProtectionEnable()) { 104 return; 105 } 106 107 /** 108 * If this is not a mutation on references we return. 109 */ 110 $data = $event->data; 111 $variableName = $data["name"]; 112 if (!(in_array($variableName, self::getQualityMetas()))) { 113 return; 114 } 115 116 $newValue = $data[MetadataMutation::NEW_VALUE_ATTRIBUTE]; 117 $path = $data[PagePath::getPersistentName()]; 118 Event::createEvent( 119 self::QUALITY_MUTATION_EVENT_NAME, 120 [ 121 PagePath::getPersistentName() => $path, 122 self::DESC => "The variable ($variableName) has the new value ($newValue)" 123 ] 124 ); 125 126 127 } 128 129} 130 131 132 133