1<?php 2 3use ComboStrap\CacheDependencies; 4use ComboStrap\CacheLog; 5use ComboStrap\CacheManager; 6use ComboStrap\Event; 7use ComboStrap\ExceptionCombo; 8use ComboStrap\FileSystems; 9use ComboStrap\LogUtility; 10use ComboStrap\MetadataDokuWikiStore; 11use ComboStrap\Page; 12use ComboStrap\PagePath; 13use ComboStrap\Reference; 14use ComboStrap\References; 15 16 17require_once(__DIR__ . '/../ComboStrap/PluginUtility.php'); 18 19/** 20 * Refresh the analytics when a backlink mutation occurs for a page 21 */ 22class action_plugin_combo_backlinkmutation extends DokuWiki_Action_Plugin 23{ 24 25 26 public const BACKLINK_MUTATION_EVENT_NAME = 'backlink_mutation'; 27 28 29 public function register(Doku_Event_Handler $controller) 30 { 31 32 33 /** 34 * create the async event 35 */ 36 $controller->register_hook(MetadataDokuWikiStore::PAGE_METADATA_MUTATION_EVENT, 'AFTER', $this, 'create_backlink_mutation', array()); 37 38 /** 39 * process the Async event 40 */ 41 $controller->register_hook(self::BACKLINK_MUTATION_EVENT_NAME, 'AFTER', $this, 'handle_backlink_mutation'); 42 43 44 } 45 46 47 public function handle_backlink_mutation(Doku_Event $event, $param) 48 { 49 50 51 $data = $event->data; 52 $pagePath = $data[PagePath::getPersistentName()]; 53 $reference = Page::createPageFromQualifiedPath($pagePath); 54 55 if ($reference->isSecondarySlot()) { 56 return; 57 } 58 59 /** 60 * Delete and recompute analytics 61 */ 62 CacheLog::deleteCacheIfExistsAndLog( 63 $reference->getAnalyticsDocument(), 64 self::BACKLINK_MUTATION_EVENT_NAME, 65 "Backlink mutation" 66 ); 67 try { 68 $reference->getDatabasePage()->replicateAnalytics(); 69 } catch (ExceptionCombo $e) { 70 LogUtility::msg("Backlink Mutation: Error while trying to replicate the analytics. Error: {$e->getMessage()}"); 71 } 72 73 /** 74 * Render the (footer slot) if it has a backlink dependency 75 */ 76 CacheDependencies::reRenderSecondarySlotsIfNeeded( 77 $pagePath, 78 CacheDependencies::BACKLINKS_DEPENDENCY, 79 self::BACKLINK_MUTATION_EVENT_NAME 80 ); 81 82 83 84 } 85 86 /** 87 */ 88 function create_backlink_mutation(Doku_Event $event, $params) 89 { 90 91 92 $data = $event->data; 93 94 /** 95 * If this is not a mutation on references we return. 96 */ 97 if ($data["name"] !== References::getPersistentName()) { 98 return; 99 }; 100 101 $newRows = $data["new_value"]; 102 $oldRows = $data["old_value"]; 103 104 $afterReferences = []; 105 if ($newRows !== null) { 106 foreach ($newRows as $rowNewValue) { 107 $reference = $rowNewValue[Reference::getPersistentName()]; 108 $afterReferences[$reference] = $reference; 109 } 110 } 111 112 if ($oldRows !== null) { 113 foreach ($oldRows as $oldRow) { 114 $beforeReference = $oldRow[Reference::getPersistentName()]; 115 if (isset($afterReferences[$beforeReference])) { 116 unset($afterReferences[$beforeReference]); 117 } else { 118 Event::createEvent( 119 action_plugin_combo_backlinkmutation::BACKLINK_MUTATION_EVENT_NAME, 120 [ 121 PagePath::getPersistentName() => $beforeReference 122 ] 123 ); 124 } 125 } 126 } 127 foreach ($afterReferences as $newReference) { 128 Event::createEvent( 129 action_plugin_combo_backlinkmutation::BACKLINK_MUTATION_EVENT_NAME, 130 [PagePath::getPersistentName() => $newReference]); 131 } 132 133 134 } 135 136 137} 138 139 140 141