1<?php 2 3 4namespace ComboStrap; 5 6 7class CacheLog 8{ 9 10 11 const TIMESTAMP_ATT = "timestamp"; 12 const EVENT_ATT = "event"; 13 const PATH_ATT = "path"; 14 const EXTENSION_ATT = "extension"; 15 const OPERATION_ATT = "operation"; 16 const MESSAGE_ATT = "message"; 17 const CACHE_LOG_TABLE = 'cache_log'; 18 const CACHE_LOG_ATTRIBUTES = [ 19 self::TIMESTAMP_ATT, 20 self::EVENT_ATT, 21 self::PATH_ATT, 22 self::EXTENSION_ATT, 23 self::OPERATION_ATT, 24 self::MESSAGE_ATT 25 ]; 26 const CANONICAL = "support"; 27 28 public static function deleteCacheIfExistsAndLog(IFetcher $fetcher, string $event, string $message) 29 { 30 31 try { 32 $contentCachePath = $fetcher->getContentCachePath(); 33 } catch (ExceptionNotSupported $e) { 34 return; 35 } 36 37 if (!FileSystems::exists($contentCachePath)) { 38 return; 39 } 40 41 FileSystems::delete($contentCachePath); 42 try { 43 CacheLog::logCacheEvent( 44 $event, 45 $contentCachePath->toAbsoluteId(), 46 $fetcher->getMime()->getExtension(), 47 CacheManager::CACHE_DELETION, 48 $message 49 ); 50 } catch (ExceptionCompile $e) { 51 // should not fired 52 LogUtility::log2file("Error while logging cache event. Error: {$e->getMessage()}"); 53 } 54 55 56 } 57 58 public static function renderCacheAndLog(IFetcherSource $fetcher, string $event, string $message) 59 { 60 try { 61 $fetcher->process(); 62 } catch (ExceptionNotSupported $e) { 63 return; 64 } 65 try { 66 CacheLog::logCacheEvent( 67 $event, 68 $fetcher->getSourcePath()->toAbsoluteId(), 69 $fetcher->getMime()->getExtension(), 70 CacheManager::CACHE_CREATION, 71 $message 72 ); 73 } catch (ExceptionCompile $e) { 74 // should not fired 75 LogUtility::log2file("Error while logging cache event. Error: {$e->getMessage()}"); 76 } 77 } 78 79 /** 80 * @throws ExceptionCompile 81 */ 82 public static function logCacheEvent(string $event, string $path, string $format, string $operation, string $message) 83 { 84 85 86 $row = array( 87 self::TIMESTAMP_ATT => date("c"), 88 self::EVENT_ATT => $event, 89 self::PATH_ATT => $path, 90 self::EXTENSION_ATT => $format, 91 self::OPERATION_ATT => $operation, 92 self::MESSAGE_ATT => $message 93 ); 94 $request = Sqlite::createOrGetBackendSqlite() 95 ->createRequest() 96 ->setTableRow(self::CACHE_LOG_TABLE, $row); 97 try { 98 $request 99 ->execute(); 100 } finally { 101 $request->close(); 102 } 103 104 105 } 106 107 /** 108 * @throws ExceptionCompile 109 */ 110 public static function getCacheLog(): array 111 { 112 $sqlite = Sqlite::createOrGetBackendSqlite(); 113 if ($sqlite === null) { 114 throw new ExceptionCompile("Sqlite is not available"); 115 } 116 117 118 /** 119 * Execute 120 */ 121 $attributes[] = DatabasePageRow::ROWID; 122 $attributes = array_merge($attributes, self::CACHE_LOG_ATTRIBUTES); 123 $select = Sqlite::createSelectFromTableAndColumns(self::CACHE_LOG_TABLE, $attributes); 124 $request = $sqlite->createRequest() 125 ->setQuery($select); 126 try { 127 return $request->execute() 128 ->getRows(); 129 } catch (ExceptionCompile $e) { 130 throw new ExceptionCompile("Unable to get the cache log. Error:" . $e->getMessage(), self::CANONICAL, 0, $e); 131 } finally { 132 $request->close(); 133 } 134 135 } 136 137} 138