1<?php 2 3require_once(__DIR__ . '/../vendor/autoload.php'); 4 5use ComboStrap\CacheLog; 6use ComboStrap\Event; 7use ComboStrap\ExceptionCompile; 8use ComboStrap\ExceptionNotExists; 9use ComboStrap\LogUtility; 10use ComboStrap\MarkupCacheDependencies; 11use ComboStrap\MarkupPath; 12use ComboStrap\MetadataMutation; 13use ComboStrap\PagePath; 14use ComboStrap\Reference; 15use ComboStrap\References; 16 17 18/** 19 * Refresh the analytics when a backlink mutation occurs for a page 20 */ 21class action_plugin_combo_backlinkmutation extends DokuWiki_Action_Plugin 22{ 23 24 25 public const BACKLINK_MUTATION_EVENT_NAME = 'backlink_mutation'; 26 27 28 public function register(Doku_Event_Handler $controller) 29 { 30 31 32 /** 33 * create the async event 34 */ 35 $controller->register_hook(MetadataMutation::PAGE_METADATA_MUTATION_EVENT, 'AFTER', $this, 'create_backlink_mutation', array()); 36 37 /** 38 * process the Async event 39 */ 40 $controller->register_hook(self::BACKLINK_MUTATION_EVENT_NAME, 'AFTER', $this, 'handle_backlink_mutation'); 41 42 43 } 44 45 /** 46 * @param Doku_Event $event 47 * @param $param 48 * @return void 49 */ 50 public function handle_backlink_mutation(Doku_Event $event, $param) 51 { 52 53 54 $data = $event->data; 55 $pagePath = $data[PagePath::getPersistentName()]; 56 $reference = MarkupPath::createPageFromAbsoluteId($pagePath); 57 58 if ($reference->isSlot()) { 59 return; 60 } 61 62 /** 63 * Delete and recompute analytics 64 */ 65 try { 66 $analyticsDocument = $reference->fetchAnalyticsDocument(); 67 } catch (ExceptionNotExists $e) { 68 return; 69 } 70 CacheLog::deleteCacheIfExistsAndLog( 71 $analyticsDocument, 72 self::BACKLINK_MUTATION_EVENT_NAME, 73 "Backlink mutation" 74 ); 75 76 try { 77 /** 78 * This is only to recompute the {@link \ComboStrap\Meta\Field\BacklinkCount backlinks metric} and 79 * {@link \ComboStrap\LowQualityPage low quality page metrics} 80 * TODO: when the derived meta are in the meta array and not in the {@link renderer_plugin_combo_analytics document}, 81 * we could just compute them there and modify it with a plus 1 82 */ 83 $reference->getDatabasePage()->replicateAnalytics(); 84 } catch (ExceptionCompile $e) { 85 LogUtility::msg("Backlink Mutation: Error while trying to replicate the analytics. Error: {$e->getMessage()}"); 86 } 87 88 /** 89 * Render the (footer slot) if it has a backlink dependency 90 */ 91 MarkupCacheDependencies::reRenderSideSlotIfNeeded( 92 $pagePath, 93 MarkupCacheDependencies::BACKLINKS_DEPENDENCY, 94 self::BACKLINK_MUTATION_EVENT_NAME 95 ); 96 97 98 } 99 100 /** 101 */ 102 function create_backlink_mutation(Doku_Event $event, $params) 103 { 104 105 106 $data = $event->data; 107 108 /** 109 * If this is not a mutation on references we return. 110 */ 111 if ($data[MetadataMutation::NAME_ATTRIBUTE] !== References::getPersistentName()) { 112 return; 113 }; 114 115 $actualReferenceDatas = $data[MetadataMutation::NEW_VALUE_ATTRIBUTE]; 116 $oldReferenceDatas = $data[MetadataMutation::OLD_VALUE_ATTRIBUTE]; 117 118 /** 119 * Create an array of the actual reference with the key as path 120 */ 121 $actualReferences = []; 122 if ($actualReferenceDatas !== null) { 123 foreach ($actualReferenceDatas as $actualReferenceData) { 124 $actualReferenceWikiPathString = $actualReferenceData[Reference::getPersistentName()]; 125 $actualReferences[$actualReferenceWikiPathString] = $actualReferenceWikiPathString; 126 } 127 } 128 129 if ($oldReferenceDatas !== null) { 130 foreach ($oldReferenceDatas as $oldReferenceData) { 131 132 $oldReferenceWikiPathString = $oldReferenceData[Reference::getPersistentName()]; 133 134 if (isset($actualReferences[$oldReferenceWikiPathString])) { 135 unset($actualReferences[$oldReferenceWikiPathString]); 136 continue; 137 } 138 139 /** 140 * Deleted reference 141 */ 142 Event::createEvent( 143 action_plugin_combo_backlinkmutation::BACKLINK_MUTATION_EVENT_NAME, 144 [ 145 PagePath::getPersistentName() => $oldReferenceWikiPathString 146 ] 147 ); 148 149 } 150 } 151 152 /** 153 * The new references 154 */ 155 foreach ($actualReferences as $newReference) { 156 Event::createEvent( 157 action_plugin_combo_backlinkmutation::BACKLINK_MUTATION_EVENT_NAME, 158 [PagePath::getPersistentName() => $newReference]); 159 } 160 161 162 } 163 164 165} 166 167 168 169