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 print 'ComboIndexer(): Indexer started'. NL; 97 98 try { 99 /** 100 * Check that the actual page has analytics data 101 * (if there is a cache, it's pretty quick) 102 */ 103 $id = $event->data['page']; 104 $page = MarkupPath::createMarkupFromId($id); 105 106 /** 107 * From {@link idx_addPage} 108 * They receive even the deleted page 109 */ 110 $databasePage = $page->getDatabasePage(); 111 if (!FileSystems::exists($page)) { 112 $databasePage->delete(); 113 return; 114 } 115 116 if ($databasePage->shouldReplicate()) { 117 try { 118 $databasePage->replicate(); 119 } catch (ExceptionCompile $e) { 120 if (PluginUtility::isDevOrTest()) { 121 // to get the stack trace 122 throw $e; 123 } 124 $message = "Error with the database replication for the page ($page). " . $e->getMessage(); 125 if (Console::isConsoleRun()) { 126 throw new ExceptionCompile($message); 127 } else { 128 LogUtility::error($message); 129 } 130 } 131 } 132 133 } finally { 134 print 'ComboIndexer(): Indexer finished'. NL; 135 $lock->release(); 136 } 137 138 } 139 140 141 function indexViaMetadataRendering(Doku_Event $event, $params) 142 { 143 144 try { 145 $wikiPath = ExecutionContext::getActualOrCreateFromEnv() 146 ->getExecutingWikiPath(); 147 } catch (ExceptionNotFound $e) { 148 // markup string run 149 return; 150 } 151 152 $page = MarkupPath::createPageFromPathObject($wikiPath); 153 154 $references = References::createFromResource($page) 155 ->setReadStore(MetadataDokuWikiStore::class); 156 157 $internalIdReferences = $event->data['current']['relation']['references']; 158 foreach ($internalIdReferences as $internalIdReferenceValue => $internalIdReferenceExist) { 159 $ref = Reference::createFromResource($page) 160 ->setReadStore(MetadataDokuWikiStore::class) 161 ->setFromStoreValueWithoutException($internalIdReferenceValue); 162 try { 163 $references->addRow([$ref]); 164 } catch (ExceptionNotFound $e) { 165 LogUtility::internalError("The identifier and the value identifier should be known at this stage", self::CANONICAL, $e); 166 } 167 } 168 169 try { 170 // persist to database 171 $references 172 ->setWriteStore(MetadataDbStore::class) 173 ->persist(); 174 } catch (ExceptionCompile $e) { 175 LogUtility::warning("Reference error when persisting to the file system store: " . $e->getMessage(), self::CANONICAL, $e); 176 } 177 178 } 179 180 181} 182 183 184 185