1<?php 2 3use ComboStrap\Event; 4use ComboStrap\ExceptionCombo; 5use ComboStrap\FileSystems; 6use ComboStrap\MetadataDokuWikiStore; 7use ComboStrap\Page; 8use ComboStrap\PagePath; 9use ComboStrap\Reference; 10use ComboStrap\References; 11 12 13require_once(__DIR__ . '/../ComboStrap/PluginUtility.php'); 14 15/** 16 * Refresh the analytics when a backlink mutation occurs for a page 17 */ 18class action_plugin_combo_backlinkmutation extends DokuWiki_Action_Plugin 19{ 20 21 22 public const BACKLINK_MUTATION_EVENT_NAME = 'BACKLINK_MUTATION'; 23 24 25 public function register(Doku_Event_Handler $controller) 26 { 27 28 29 /** 30 * create the async event 31 */ 32 $controller->register_hook(MetadataDokuWikiStore::PAGE_METADATA_MUTATION_EVENT, 'AFTER', $this, 'create_backlink_mutation', array()); 33 34 /** 35 * process the async event 36 */ 37 $controller->register_hook(self::BACKLINK_MUTATION_EVENT_NAME, 'AFTER', $this, 'handle_backlink_mutation'); 38 39 40 } 41 42 43 public function handle_backlink_mutation(Doku_Event $event, $param) 44 { 45 46 47 $data = $event->data; 48 $pagePath = $data[PagePath::getPersistentName()]; 49 $page = Page::createPageFromQualifiedPath($pagePath); 50 51 // delete analytics 52 FileSystems::deleteIfExists($page->getAnalyticsDocument()->getCachePath()); 53 $page->getDatabasePage()->replicateAnalytics(); 54 55 56 } 57 58 /** 59 */ 60 function create_backlink_mutation(Doku_Event $event, $params) 61 { 62 63 64 $data = $event->data; 65 66 /** 67 * If this is not a mutation on references we return. 68 */ 69 if ($data["name"] !== References::getPersistentName()) { 70 return; 71 }; 72 73 $newRows = $data["new_value"]; 74 $oldRows = $data["old_value"]; 75 76 $afterReferences = []; 77 if ($newRows !== null) { 78 foreach ($newRows as $rowNewValue) { 79 $reference = $rowNewValue[Reference::getPersistentName()]; 80 $afterReferences[$reference] = $reference; 81 } 82 } 83 84 if ($oldRows !== null) { 85 foreach ($oldRows as $oldRow) { 86 $beforeReference = $oldRow[Reference::getPersistentName()]; 87 if (isset($afterReferences[$beforeReference])) { 88 unset($afterReferences[$beforeReference]); 89 } else { 90 Event::createEvent(action_plugin_combo_backlinkmutation::BACKLINK_MUTATION_EVENT_NAME, [PagePath::getPersistentName() => $beforeReference]); 91 /** 92 * Delete the analytics 93 */ 94 FileSystems::deleteIfExists(Page::createPageFromQualifiedPath($beforeReference)->getAnalyticsDocument()->getCachePath()); 95 } 96 } 97 } 98 foreach ($afterReferences as $newReference) { 99 Page::createPageFromQualifiedPath($newReference) 100 ->getAnalyticsDocument() 101 ->deleteIfExists(); 102 Event::createEvent(action_plugin_combo_backlinkmutation::BACKLINK_MUTATION_EVENT_NAME, [PagePath::getPersistentName() => $newReference]); 103 /** 104 * Delete the analytics 105 */ 106 FileSystems::deleteIfExists(Page::createPageFromQualifiedPath($newReference)->getAnalyticsDocument()->getCachePath()); 107 } 108 109 110 } 111 112 113} 114 115 116 117