1<?php 2 3use ComboStrap\CacheDependencies; 4use ComboStrap\Event; 5use ComboStrap\FileSystems; 6use ComboStrap\LocalPath; 7use ComboStrap\Page; 8use ComboStrap\PagePath; 9use ComboStrap\Site; 10 11 12require_once(__DIR__ . '/../ComboStrap/PluginUtility.php'); 13 14/** 15 * When a page is added or deleted from the file system 16 */ 17class action_plugin_combo_pagesystemmutation extends DokuWiki_Action_Plugin 18{ 19 20 21 public const PAGE_SYSTEM_MUTATION_EVENT_NAME = 'page_system_mutation'; 22 23 const TYPE_ATTRIBUTE = "type"; 24 const TYPE_CREATION = "creation"; 25 const TYPE_DELETION = "deletion"; 26 27 28 public function register(Doku_Event_Handler $controller) 29 { 30 31 32 /** 33 * To delete sidebar (cache) cache when a page was modified in a namespace 34 * https://combostrap.com/sideslots 35 */ 36 $controller->register_hook('IO_WIKIPAGE_WRITE', 'BEFORE', $this, 'createFileSystemMutation', array()); 37 38 /** 39 * process the Async event 40 */ 41 $controller->register_hook(self::PAGE_SYSTEM_MUTATION_EVENT_NAME, 'AFTER', $this, 'handleFileSystemMutation'); 42 43 } 44 45 /** 46 * @param $event 47 * @throws Exception 48 * @link https://www.dokuwiki.org/devel:event:io_wikipage_write 49 */ 50 function createFileSystemMutation($event) 51 { 52 53 $data = $event->data; 54 $pageName = $data[2]; 55 56 /** 57 * Modification to the secondary slot are not processed 58 */ 59 if (in_array($pageName, Site::getSecondarySlotNames())) return; 60 61 62 /** 63 * File creation 64 * 65 * ``` 66 * Page creation may be detected by checking if the file already exists and the revision is false. 67 * ``` 68 * From https://www.dokuwiki.org/devel:event:io_wikipage_write 69 * 70 */ 71 $rev = $data[3]; 72 $filePath = $data[0][0]; 73 $file = LocalPath::createFromPath($filePath); 74 if (!FileSystems::exists($file) && $rev === false) { 75 Event::createEvent( 76 action_plugin_combo_pagesystemmutation::PAGE_SYSTEM_MUTATION_EVENT_NAME, 77 [ 78 self::TYPE_ATTRIBUTE => self::TYPE_CREATION, 79 PagePath::getPersistentName() => $file->toDokuPath()->toString() 80 ] 81 ); 82 return; 83 } 84 85 /** 86 * File deletion 87 * (No content) 88 * 89 * ``` 90 * Page deletion may be detected by checking for empty page content. 91 * On update to an existing page this event is called twice, once for the transfer of the old version to the attic (rev will have a value) 92 * and once to write the new version of the page into the wiki (rev is false) 93 * ``` 94 * From https://www.dokuwiki.org/devel:event:io_wikipage_write 95 */ 96 $append = $data[0][2]; 97 if (!$append) { 98 99 $content = $data[0][1]; 100 if (empty($content) && $rev === false) { 101 // Deletion 102 Event::createEvent( 103 action_plugin_combo_pagesystemmutation::PAGE_SYSTEM_MUTATION_EVENT_NAME, 104 [ 105 self::TYPE_ATTRIBUTE => self::TYPE_DELETION, 106 PagePath::getPersistentName() => $file->toDokuPath()->toString() 107 ] 108 ); 109 } 110 } 111 112 } 113 114 public function handleFileSystemMutation($event) 115 { 116 117 /** 118 * We need to re-render the slot 119 * that are {@link \ComboStrap\CacheDependencies::PAGE_SYSTEM_DEPENDENCY} 120 * dependent 121 */ 122 $data = $event->data; 123 124 /** 125 * Re-render 126 */ 127 $path = $data[PagePath::getPersistentName()]; 128 CacheDependencies::reRenderSecondarySlotsIfNeeded( 129 $path, 130 CacheDependencies::PAGE_SYSTEM_DEPENDENCY, 131 self::PAGE_SYSTEM_MUTATION_EVENT_NAME 132 ); 133 134 } 135 136 137} 138 139 140 141