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