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: 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 = $this->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:
152: public function __construct($mId = false) {
153: global $cfg;
154: cApiPathresolveCacheHelper::setup($cfg['sql']['sqlprefix'] . '_pathresolve_cache');
155: parent::__construct($cfg['sql']['sqlprefix'] . '_pathresolve_cache', 'idpathresolvecache');
156: $this->setFilters(array(), array());
157: if ($mId !== false) {
158: $this->loadByPrimaryKey($mId);
159: }
160: }
161:
162: 163: 164: 165: 166: 167:
168: public function isCacheTimeExpired() {
169: global $cfg;
170: if (!$this->isLoaded()) {
171: throw new cException('Item not loaded!');
172: }
173: $cacheTime = (isset($cfg['pathresolve_heapcache_time'])) ? $cfg['pathresolve_heapcache_time'] : 60 * 60 * 24;
174: return $this->get('lastcached') + $cacheTime < time();
175: }
176:
177: 178: 179: 180: 181: 182: 183: 184: 185: 186:
187: public function setField($name, $value, $bSafe = true) {
188: switch ($name) {
189: case 'idcat':
190: case 'idlang':
191: $value = (int) $value;
192: break;
193: }
194:
195: return parent::setField($name, $value, $bSafe);
196: }
197:
198: }
199: