1: <?php
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
16:
17: 18: 19: 20: 21: 22:
23: class cApiPathresolveCacheHelper {
24:
25: 26: 27: 28: 29:
30: protected static $_tableCreated = false;
31:
32: 33: 34: 35: 36: 37: 38: 39: 40:
41: public static function setup($cfg) {
42: if (isset($cfg['pathresolve_heapcache']) && true === $cfg['pathresolve_heapcache'] && false === self::$_tableCreated) {
43: $db = cRegistry::getDb();
44: $tableName = $cfg['sql']['sqlprefix'] . '_pathresolve_cache';
45:
46: $sql = "SHOW TABLES LIKE '" . $db->escape($tableName) . "'";
47: $db->query($sql);
48:
49: if (!$db->nextRecord()) {
50:
51:
52:
53:
54: $sql = 'CREATE TABLE `' . $db->escape($tableName) . '` (
55: `idpathresolvecache` INT(10) NOT NULL AUTO_INCREMENT,
56: `path` VARCHAR(255) NOT NULL,
57: `idcat` INT(10) NOT NULL,
58: `idlang` INT(10) NOT NULL,
59: `lastcached` INT(10) NOT NULL,
60: PRIMARY KEY (`idpathresolvecache`)
61: ) ENGINE = HEAP;';
62: $db->query($sql);
63: }
64: self::$_tableCreated = true;
65: }
66: }
67:
68: }
69:
70: 71: 72: 73: 74: 75:
76: class cApiPathresolveCacheCollection extends ItemCollection {
77: 78: 79: 80: 81: 82:
83: public function __construct() {
84: global $cfg;
85: cApiPathresolveCacheHelper::setup($cfg);
86: parent::__construct($cfg['sql']['sqlprefix'] . '_pathresolve_cache', 'idpathresolvecache');
87: $this->_setItemClass('cApiPathresolveCache');
88: }
89:
90: 91: 92: 93: 94: 95: 96: 97: 98: 99: 100: 101:
102: public function create($path, $idcat, $idlang, $lastcached = '') {
103: $oItem = $this->createNewItem();
104:
105: if (empty($lastcached)) {
106: $lastcached = time();
107: }
108:
109: $oItem->set('path', $path, false);
110: $oItem->set('idcat', $idcat, false);
111: $oItem->set('idlang', $idlang, false);
112: $oItem->set('lastcached', $lastcached, false);
113: $oItem->store();
114:
115: return $oItem;
116: }
117:
118: 119: 120: 121: 122: 123: 124: 125: 126:
127: public function fetchLatestByPathAndLanguage($path, $idlang) {
128: $this->select("path LIKE '" . $this->db->escape($path) . "' AND idlang=" . (int) $idlang, '', 'lastcached DESC', '1');
129: return $this->next();
130: }
131:
132: 133: 134: 135: 136: 137: 138: 139: 140: 141:
142: public function deleteByCategoryAndLanguage($idcat, $idlang) {
143: $this->select('idcat=' . (int) $idcat . ' AND idlang=' . (int) $idlang);
144: while (($oCode = $this->next()) !== false) {
145: $this->delete($oCode->get('idpathresolvecache'));
146: }
147: }
148:
149: }
150:
151: 152: 153: 154: 155: 156:
157: class cApiPathresolveCache extends Item
158: {
159: 160: 161: 162: 163: 164: 165: 166: 167:
168: public function __construct($mId = false) {
169: global $cfg;
170: cApiPathresolveCacheHelper::setup($cfg);
171: parent::__construct($cfg['sql']['sqlprefix'] . '_pathresolve_cache', 'idpathresolvecache');
172: $this->setFilters(array(), array());
173: if ($mId !== false) {
174: $this->loadByPrimaryKey($mId);
175: }
176: }
177:
178: 179: 180: 181: 182: 183:
184: public function isCacheTimeExpired() {
185: global $cfg;
186: if (!$this->isLoaded()) {
187: throw new cException('Item not loaded!');
188: }
189: $cacheTime = (isset($cfg['pathresolve_heapcache_time'])) ? $cfg['pathresolve_heapcache_time'] : 60 * 60 * 24;
190: return $this->get('lastcached') + $cacheTime < time();
191: }
192:
193: 194: 195: 196: 197: 198: 199: 200: 201: 202:
203: public function setField($name, $value, $bSafe = true) {
204: switch ($name) {
205: case 'idcat':
206: case 'idlang':
207: $value = (int) $value;
208: break;
209: }
210:
211: return parent::setField($name, $value, $bSafe);
212: }
213:
214: }
215: