1<?php 2/** 3 * Copyright (c) 2021. ComboStrap, Inc. and its affiliates. All Rights Reserved. 4 * 5 * This source code is licensed under the GPL license found in the 6 * COPYING file in the root directory of this source tree. 7 * 8 * @license GPL 3 (https://www.gnu.org/licenses/gpl-3.0.en.html) 9 * @author ComboStrap <support@combostrap.com> 10 * 11 * 12 */ 13 14use ComboStrap\Console; 15use ComboStrap\Event; 16use ComboStrap\ExceptionCompile; 17use ComboStrap\ExceptionNotFound; 18use ComboStrap\ExceptionRuntimeInternal; 19use ComboStrap\ExecutionContext; 20use ComboStrap\FileSystems; 21use ComboStrap\LogUtility; 22use ComboStrap\MarkupPath; 23use ComboStrap\Meta\Store\MetadataDbStore; 24use ComboStrap\Meta\Store\MetadataDokuWikiStore; 25use ComboStrap\PluginUtility; 26use ComboStrap\Reference; 27use ComboStrap\References; 28 29 30/** 31 * Process metadata put them in the sqlite database (ie create derived index) 32 * 33 * 34 * For the replication, this is the equivalent of the dokuwiki {@link \dokuwiki\Search\Indexer} 35 * (textual search engine, plus metadata index) 36 * 37 * Note that you can disable a page to go into the index 38 * via the `internal index` metadata. See {@link idx_addPage()} 39 * ``` 40 * $indexenabled = p_get_metadata($page, 'internal index', METADATA_RENDER_UNLIMITED); 41 * ``` 42 */ 43class action_plugin_combo_indexer extends DokuWiki_Action_Plugin 44{ 45 46 /** 47 * Bad canonical for now as we will add the 48 * {@link \ComboStrap\ComboFs} system 49 * but it's still a valid page 50 */ 51 const CANONICAL = "replication"; 52 53 public function register(Doku_Event_Handler $controller) 54 { 55 56 /** 57 * We do it after because if there is an error 58 * We will not stop the Dokuwiki Processing 59 * 60 * We could do it after 61 * https://www.dokuwiki.org/devel:event:parser_metadata_render 62 * but it would then not be async 63 * 64 * Note: We support other extension for markup 65 * but dokuwiki does not index other extension 66 * in {@link idx_addPage} (page is the id) 67 */ 68 $controller->register_hook('INDEXER_PAGE_ADD', 'AFTER', $this, 'indexViaIndexerAdd', array()); 69 70 /** 71 * 72 * https://www.dokuwiki.org/devel:event:parser_metadata_render 73 * 74 */ 75 76 77 78 } 79 80 /** 81 * @throws ExceptionCompile 82 */ 83 public function indexViaIndexerAdd(Doku_Event $event, $param) 84 { 85 86 /** 87 * Check that the actual page has analytics data 88 * (if there is a cache, it's pretty quick) 89 */ 90 $id = $event->data['page']; 91 $page = MarkupPath::createMarkupFromId($id); 92 93 /** 94 * From {@link idx_addPage} 95 * They receive even the deleted page 96 */ 97 $databasePage = $page->getDatabasePage(); 98 if (!FileSystems::exists($page)) { 99 $databasePage->delete(); 100 return; 101 } 102 103 if ($databasePage->shouldReplicate()) { 104 try { 105 $databasePage->replicate(); 106 } catch (ExceptionCompile $e) { 107 if (PluginUtility::isDevOrTest()) { 108 // to get the stack trace 109 throw $e; 110 } 111 $message = "Error with the database replication for the page ($page). " . $e->getMessage(); 112 if (Console::isConsoleRun()) { 113 throw new ExceptionCompile($message); 114 } else { 115 LogUtility::error($message); 116 } 117 } 118 } 119 120 121 } 122 123 124 function indexViaMetadataRendering(Doku_Event $event, $params) 125 { 126 127 try { 128 $wikiPath = ExecutionContext::getActualOrCreateFromEnv() 129 ->getExecutingWikiPath(); 130 } catch (ExceptionNotFound $e) { 131 // markup string run 132 return; 133 } 134 135 $page = MarkupPath::createPageFromPathObject($wikiPath); 136 137 $references = References::createFromResource($page) 138 ->setReadStore(MetadataDokuWikiStore::class); 139 140 $internalIdReferences = $event->data['current']['relation']['references']; 141 foreach ($internalIdReferences as $internalIdReferenceValue => $internalIdReferenceExist) { 142 $ref = Reference::createFromResource($page) 143 ->setReadStore(MetadataDokuWikiStore::class) 144 ->setFromStoreValueWithoutException($internalIdReferenceValue); 145 try { 146 $references->addRow([$ref]); 147 } catch (ExceptionNotFound $e) { 148 LogUtility::internalError("The identifier and the value identifier should be known at this stage", self::CANONICAL, $e); 149 } 150 } 151 152 try { 153 // persist to database 154 $references 155 ->setWriteStore(MetadataDbStore::class) 156 ->persist(); 157 } catch (ExceptionCompile $e) { 158 LogUtility::warning("Reference error when persisting to the file system store: " . $e->getMessage(), self::CANONICAL, $e); 159 } 160 161 } 162 163 164} 165 166 167 168