1<?php 2 3namespace ComboStrap; 4 5/** 6 * The manager that handles the redirection metadata 7 * 8 */ 9class PageRules 10{ 11 12 // Name of the column 13 // Used also in the HTML form as name 14 const ID_NAME = 'ID'; 15 const PRIORITY_NAME = 'PRIORITY'; 16 const MATCHER_NAME = 'MATCHER'; 17 const TARGET_NAME = 'TARGET'; 18 const TIMESTAMP_NAME = 'TIMESTAMP'; 19 20 const CANONICAL = "page:rules"; 21 22 23 /** 24 * Delete Redirection 25 * @param string $ruleId 26 */ 27 function deleteRule(string $ruleId) 28 { 29 30 $request = Sqlite::createOrGetSqlite() 31 ->createRequest() 32 ->setQueryParametrized('delete from PAGE_RULES where id = ?', [$ruleId]); 33 try { 34 $request->execute(); 35 } catch (ExceptionCompile $e) { 36 LogUtility::msg("Something went wrong when deleting the redirections. {$e->getMessage()}"); 37 } finally { 38 $request->close(); 39 } 40 41 } 42 43 44 /** 45 * Is Redirection of a page Id Present 46 * @param integer $id 47 * @return boolean 48 */ 49 function ruleExists($id): bool 50 { 51 $id = strtolower($id); 52 53 54 $request = Sqlite::createOrGetSqlite() 55 ->createRequest() 56 ->setQueryParametrized("SELECT count(*) FROM PAGE_RULES where ID = ?", [$id]); 57 $count = 0; 58 try { 59 $count = $request 60 ->execute() 61 ->getFirstCellValueAsInt(); 62 } catch (ExceptionCompile $e) { 63 LogUtility::msg("Error during pattern exist statement. {$e->getMessage()}"); 64 return false; 65 } finally { 66 $request->close(); 67 } 68 69 return $count === 1; 70 71 72 } 73 74 /** 75 * Is Redirection of a page Id Present 76 * @param string $pattern 77 * @return boolean 78 */ 79 function patternExists(string $pattern): bool 80 { 81 82 83 $request = Sqlite::createOrGetSqlite() 84 ->createRequest() 85 ->setQueryParametrized("SELECT count(*) FROM PAGE_RULES where MATCHER = ?", [$pattern]); 86 $count = 0; 87 try { 88 $count = $request->execute() 89 ->getFirstCellValueAsInt(); 90 } catch (ExceptionCompile $e) { 91 LogUtility::msg("Error during pattern exists query: {$e->getMessage()}"); 92 return false; 93 } finally { 94 $request->close(); 95 } 96 97 return $count === 1; 98 99 100 } 101 102 103 /** 104 * @param $sourcePageId 105 * @param $targetPageId 106 * @param $priority 107 * @return int - the rule id 108 */ 109 function addRule($sourcePageId, $targetPageId, $priority): ?int 110 { 111 $currentDate = date("c"); 112 return $this->addRuleWithDate($sourcePageId, $targetPageId, $priority, $currentDate); 113 } 114 115 /** 116 * Add Redirection 117 * This function was needed to migrate the date of the file conf store 118 * You would use normally the function addRedirection 119 * @param string $matcher 120 * @param string $target 121 * @param $priority 122 * @param $creationDate 123 * @return int - the last id 124 */ 125 function addRuleWithDate($matcher, $target, $priority, $creationDate): ?int 126 { 127 128 $entry = array( 129 'target' => $target, 130 'timestamp' => $creationDate, 131 'matcher' => $matcher, 132 'priority' => $priority 133 ); 134 135 $request = Sqlite::createOrGetSqlite() 136 ->createRequest() 137 ->setTableRow('PAGE_RULES', $entry); 138 $lastInsertId = null; 139 try { 140 $lastInsertId = $request->execute() 141 ->getInsertId(); 142 } catch (ExceptionCompile $e) { 143 LogUtility::msg("There was a problem during Pages Rule insertion. " . $e->getMessage()); 144 return null; 145 } finally { 146 $request->close(); 147 } 148 149 return $lastInsertId; 150 151 } 152 153 function updateRule($id, $matcher, $target, $priority) 154 { 155 $updateDate = date("c"); 156 157 $entry = array( 158 $matcher, 159 $target, 160 $priority, 161 $updateDate, 162 $id 163 ); 164 165 $statement = 'update PAGE_RULES set matcher = ?, target = ?, priority = ?, timestamp = ? where id = ?'; 166 try { 167 $request = Sqlite::createOrGetSqlite() 168 ->createRequest() 169 ->setQueryParametrized($statement, $entry); 170 } catch (ExceptionSqliteNotAvailable $e) { 171 return; 172 } 173 try { 174 $request->execute(); 175 } catch (ExceptionCompile $e) { 176 LogUtility::error("There was a problem during the page rules update. Error: {$e->getMessage()}", self::CANONICAL); 177 } finally { 178 $request->close(); 179 } 180 181 } 182 183 184 /** 185 * Delete all rules 186 * Use with caution 187 */ 188 public function deleteAll() 189 { 190 191 /** @noinspection SqlWithoutWhere */ 192 $request = Sqlite::createOrGetSqlite() 193 ->createRequest() 194 ->setQuery("delete from PAGE_RULES"); 195 try { 196 $request->execute(); 197 } catch (ExceptionCompile $e) { 198 LogUtility::msg('Errors during delete of all redirections. ' . $e->getMessage()); 199 } finally { 200 $request->close(); 201 } 202 203 204 } 205 206 /** 207 * Return the number of page rules 208 * @return integer 209 */ 210 public function count() 211 { 212 213 $request = Sqlite::createOrGetSqlite() 214 ->createRequest() 215 ->setQuery("select count(1) from PAGE_RULES"); 216 217 $count = 0; 218 try { 219 $count = $request->execute() 220 ->getFirstCellValueAsInt(); 221 } catch (ExceptionCompile $e) { 222 LogUtility::msg("Page Rules Count. {$e->getMessage()}"); 223 return 0; 224 } finally { 225 $request->close(); 226 } 227 228 return $count; 229 230 } 231 232 233 /** 234 * @return array 235 */ 236 public function getRules() 237 { 238 239 $request = Sqlite::createOrGetSqlite() 240 ->createRequest() 241 ->setQuery("select * from PAGE_RULES order by PRIORITY"); 242 243 try { 244 return $request->execute() 245 ->getRows(); 246 } catch (ExceptionCompile $e) { 247 LogUtility::msg("Errors during select of all Page rules. {$e->getMessage()}"); 248 return []; 249 } finally { 250 $request->close(); 251 } 252 253 254 } 255 256 public function getRule($id): array 257 { 258 $request = Sqlite::createOrGetSqlite() 259 ->createRequest() 260 ->setQueryParametrized("SELECT * FROM PAGE_RULES where ID = ?", [$id]); 261 try { 262 return $request->execute() 263 ->getFirstRow(); 264 } catch (ExceptionCompile $e) { 265 LogUtility::msg("getRule Error {$e->getMessage()}"); 266 return []; 267 } finally { 268 $request->close(); 269 } 270 271 } 272 273 274} 275