forked from symphonycms/symphonycms
/
class.lookup.php
394 lines (371 loc) · 8.75 KB
/
class.lookup.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
<?php
/**
* Created by JetBrains PhpStorm.
* User: Giel
* Date: 29-03-12
* Time: 22:39
* File: class.managerlookup.php
*/
class Lookup
{
const LOOKUP_PAGES = 'pages';
const LOOKUP_SECTIONS = 'sections';
const LOOKUP_FIELDS = 'fields';
// Instance is used for singleton:
private static $_instances;
// Index is used for index management:
/* @var $_index SimpleXMLElement */
private $_index;
// Keep track of the type
private $_type;
// The path to the XML-files (workspace/pages and workspace/sections)
private $_path;
// The element name to use as root-tag
private $_element_name;
// Internal cache (for this class)
private $_cache;
/**
* Get the index
*
* @param $type
* The type of the index
* @return Lookup
*/
public static function index($type)
{
if(!self::$_instances)
{
self::$_instances = array();
}
if(!self::$_instances[$type])
{
self::$_instances[$type] = new Lookup($type);
}
return self::$_instances[$type];
}
/**
* Private constructor, so this class can only be used as a singleton.
*/
private function __construct($type)
{
$this->_type = $type;
// Create an index:
switch($this->_type)
{
case self::LOOKUP_PAGES :
{
$this->_path = PAGES.'/*.xml';
$this->_element_name = 'pages';
$this->reIndex();
break;
}
}
}
/**
* Save a lookup
*
* @param $hash
* The unique hash of the item that is saved
* @return int
* The ID internal used in the database.
*/
public function save($hash)
{
switch($this->_type)
{
case self::LOOKUP_PAGES :
{
Symphony::Database()->insert(array('hash'=>$hash), 'tbl_lookup_pages');
return Symphony::Database()->getInsertID();
break;
}
}
return false;
}
/**
* Delete a lookup
*
* @param $idOrHash
* The ID of the item, or the hash value
* @return void
*/
public function delete($idOrHash)
{
switch($this->_type)
{
case self::LOOKUP_PAGES :
{
if(is_numeric($idOrHash) && strlen($idOrHash) != 32)
{
// Assume it's an ID
Symphony::Database()->delete('tbl_lookup_pages', '`id` = '.$idOrHash);
} else {
// Assume it's a hash
Symphony::Database()->delete('tbl_lookup_pages', '`hash` = \''.$idOrHash.'\'');
}
break;
}
}
}
/**
* Run an XPath expression on the index
*
* @param $path
* The XPath expression
* @param bool $singleValue
* Does this function return a single value?
* @return bool|SimpleXMLElement|SimpleXMLElement[]
*/
public function xpath($path, $singleValue = false)
{
if(!$singleValue)
{
return $this->_index->xpath('/'.$this->_type.'/'.$path);
} else {
$_result = $this->_index->xpath('/'.$this->_type.'/'.$path);
if(count($_result) > 0)
{
return $_result[0];
} else {
return false;
}
}
}
/**
* Return the ID according to the hash
*
* @param $hash
* The hash
* @return int
*/
public function getId($hash)
{
if(!isset($this->_cache['hash'][$hash]))
{
$this->_cache['hash'][$hash] = Symphony::Database()->fetchVar('id', 0,
sprintf('SELECT `id` FROM `tbl_lookup_pages` WHERE `hash` = \'%s\';', $hash));
}
return $this->_cache['hash'][$hash];
}
/**
* Return the hash according to the ID
*
* @param $id
* The id
* @return string
*/
public function getHash($id)
{
if(!isset($this->_cache['id'][$id]))
{
$this->_cache['id'][$id] = Symphony::Database()->fetchVar('hash', 0,
sprintf('SELECT `hash` FROM `tbl_lookup_pages` WHERE `id` = %d;', $id));
}
return $this->_cache['id'][$id];
}
/**
* Get all used hashes
*
* @return array
*/
public function getAllHashes()
{
return Symphony::Database()->fetchCol('hash', 'SELECT `hash` FROM `tbl_lookup_pages`;');
}
/**
* Check if there are duplicate hashes. Returns false if not, otherwise the hash in question
*
* @return bool|string
*/
public function hasDuplicateHashes()
{
$_hashes = array();
foreach($this->_index->children() as $_child)
{
if(!in_array((string)$_child->unique_hash, $_hashes))
{
$_hashes[] = (string)$_child->unique_hash;
} else {
return (string)$_child->unique_hash;
}
}
return false;
}
/**
* Fetch the index
*
* @param $orderBy
* The key to order by
* @param string $orderDirection
* The direction (asc or desc)
* @param bool $sortNumeric
* Should sorting be numeric (default) or as a string?
* @param string $where
* An xpath expression to filter on
* @return array
* An array with SimpleXMLElements
*/
public function fetch($where = null, $orderBy = null, $orderDirection = 'asc', $sortNumeric = true)
{
// Build the new array:
$array = array();
// @todo: one day, this whole fetch-function is going to use a nice simple xpath expression to get them pages
if($where != null && isset($where['xpath']))
{
$array = $this->_index->xpath($where['xpath']);
} else {
foreach($this->_index->children() as $_item)
{
if(!empty($where))
{
/* See if this item passes the filter:
* array(
* 'id' => array('eq', 12),
* 'name' => array('neq', 'tom'),
* 'nr' => array('gt', 10),
* 'nr' => array('lt', 20),
* 'nr' => array('gte', 10),
* 'nr' => array('lte', 20)
* );
*/
$passes = false;
foreach($where as $key => $expression)
{
switch($expression[0])
{
case 'eq' :
{
$passes = (string)$_item->$key == (string)$expression[1];
break;
}
case 'neq' :
{
$passes = (string)$_item->$key != (string)$expression[1];
break;
}
case 'gt' :
{
$passes = (float)$_item->$key > (float)$expression[1];
break;
}
case 'lt' :
{
$passes = (float)$_item->$key < (float)$expression[1];
break;
}
case 'gte' :
{
$passes = (float)$_item->$key >= (float)$expression[1];
break;
}
case 'lte' :
{
$passes = (float)$_item->$key <= (float)$expression[1];
break;
}
}
}
if($passes)
{
$array[] = $_item;
}
} else {
// Just add it:
$array[] = $_item;
}
}
}
// Order the array:
if($orderBy != null)
{
$sorter = array();
// Create an indexed array of items:
foreach($array as $_item)
{
// Prevent duplicate keys from being overwritten:
$ok = true;
$i = 1;
while($ok)
{
$key = (string)$_item->$orderBy.'_'.$i;
if(!isset($sorter[$key]))
{
$sorter[$key] = $_item;
$ok = false;
}
$i++;
}
}
// Sort the array:
if($sortNumeric) {
ksort($sorter, SORT_NUMERIC);
} else {
ksort($sorter, SORT_STRING);
}
if($orderDirection == 'desc')
{
$sorter = array_reverse($sorter);
}
// Build the new array:
$array = array();
foreach($sorter as $_item)
{
$array[] = $_item;
}
}
return $array;
}
/**
* Get the maximum value of an item
*
* @param $name
* The name of the element
* @return int
*/
public function getMax($name)
{
$_max = 0;
foreach($this->_index->children() as $_item)
{
if((int)$_item->$name > $_max) { $_max = (int)$_item->$name; }
}
return $_max;
}
/**
* Setup the index. The index is a SimpleXMLElement which stores all information about all
* items. Therefore, the setup only needs to be loaded once, and not for each request.
*/
public function reIndex()
{
// Clear the cache:
$this->_cache = array('id' => array(), 'hash' => array());
// Build the index:
$this->_index = new SimpleXMLElement('<'.$this->_element_name.'/>');
$_pages = glob($this->_path);
foreach($_pages as $_page)
{
$this->mergeXML($this->_index, simplexml_load_file($_page));
}
}
/**
* @param $xml_element SimpleXMLElement
* @param $append SimpleXMLElement
* @return void
*/
private function mergeXML($xml_element, $append)
{
if ($append) {
if (strlen(trim((string) $append))==0) {
$xml = $xml_element->addChild($append->getName());
foreach($append->children() as $child) {
$this->mergeXML($xml, $child);
}
} else {
$xml = $xml_element->addChild($append->getName(), (string) $append);
}
foreach($append->attributes() as $n => $v) {
$xml->addAttribute($n, $v);
}
}
}
}