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