taxonomy.php
9.85 KB
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
<?php
/**
* @package Joomla.Administrator
* @subpackage com_finder
*
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;
/**
* Stemmer base class for the Finder indexer package.
*
* @since 2.5
*/
class FinderIndexerTaxonomy
{
/**
* An internal cache of taxonomy branch data.
*
* @var array
* @since 2.5
*/
public static $branches = array();
/**
* An internal cache of taxonomy node data.
*
* @var array
* @since 2.5
*/
public static $nodes = array();
/**
* Method to add a branch to the taxonomy tree.
*
* @param string $title The title of the branch.
* @param integer $state The published state of the branch. [optional]
* @param integer $access The access state of the branch. [optional]
*
* @return integer The id of the branch.
*
* @since 2.5
* @throws Exception on database error.
*/
public static function addBranch($title, $state = 1, $access = 1)
{
// Check to see if the branch is in the cache.
if (isset(static::$branches[$title]))
{
return static::$branches[$title]->id;
}
// Check to see if the branch is in the table.
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('*')
->from($db->quoteName('#__finder_taxonomy'))
->where($db->quoteName('parent_id') . ' = 1')
->where($db->quoteName('title') . ' = ' . $db->quote($title));
$db->setQuery($query);
// Get the result.
$result = $db->loadObject();
// Check if the database matches the input data.
if ((bool) $result && $result->state == $state && $result->access == $access)
{
// The data matches, add the item to the cache.
static::$branches[$title] = $result;
return static::$branches[$title]->id;
}
/*
* The database did not match the input. This could be because the
* state has changed or because the branch does not exist. Let's figure
* out which case is true and deal with it.
*/
$branch = new JObject;
if (empty($result))
{
// Prepare the branch object.
$branch->parent_id = 1;
$branch->title = $title;
$branch->state = (int) $state;
$branch->access = (int) $access;
}
else
{
// Prepare the branch object.
$branch->id = (int) $result->id;
$branch->parent_id = (int) $result->parent_id;
$branch->title = $result->title;
$branch->state = (int) $result->title;
$branch->access = (int) $result->access;
$branch->ordering = (int) $result->ordering;
}
// Store the branch.
static::storeNode($branch);
// Add the branch to the cache.
static::$branches[$title] = $branch;
return static::$branches[$title]->id;
}
/**
* Method to add a node to the taxonomy tree.
*
* @param string $branch The title of the branch to store the node in.
* @param string $title The title of the node.
* @param integer $state The published state of the node. [optional]
* @param integer $access The access state of the node. [optional]
*
* @return integer The id of the node.
*
* @since 2.5
* @throws Exception on database error.
*/
public static function addNode($branch, $title, $state = 1, $access = 1)
{
// Check to see if the node is in the cache.
if (isset(static::$nodes[$branch][$title]))
{
return static::$nodes[$branch][$title]->id;
}
// Get the branch id, insert it if it does not exist.
$branchId = static::addBranch($branch);
// Check to see if the node is in the table.
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select('*')
->from($db->quoteName('#__finder_taxonomy'))
->where($db->quoteName('parent_id') . ' = ' . $db->quote($branchId))
->where($db->quoteName('title') . ' = ' . $db->quote($title));
$db->setQuery($query);
// Get the result.
$result = $db->loadObject();
// Check if the database matches the input data.
if ((bool) $result && $result->state == $state && $result->access == $access)
{
// The data matches, add the item to the cache.
static::$nodes[$branch][$title] = $result;
return static::$nodes[$branch][$title]->id;
}
/*
* The database did not match the input. This could be because the
* state has changed or because the node does not exist. Let's figure
* out which case is true and deal with it.
*/
$node = new JObject;
if (empty($result))
{
// Prepare the node object.
$node->parent_id = (int) $branchId;
$node->title = $title;
$node->state = (int) $state;
$node->access = (int) $access;
}
else
{
// Prepare the node object.
$node->id = (int) $result->id;
$node->parent_id = (int) $result->parent_id;
$node->title = $result->title;
$node->state = (int) $result->title;
$node->access = (int) $result->access;
$node->ordering = (int) $result->ordering;
}
// Store the node.
static::storeNode($node);
// Add the node to the cache.
static::$nodes[$branch][$title] = $node;
return static::$nodes[$branch][$title]->id;
}
/**
* Method to add a map entry between a link and a taxonomy node.
*
* @param integer $linkId The link to map to.
* @param integer $nodeId The node to map to.
*
* @return boolean True on success.
*
* @since 2.5
* @throws Exception on database error.
*/
public static function addMap($linkId, $nodeId)
{
// Insert the map.
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->select($db->quoteName('link_id'))
->from($db->quoteName('#__finder_taxonomy_map'))
->where($db->quoteName('link_id') . ' = ' . (int) $linkId)
->where($db->quoteName('node_id') . ' = ' . (int) $nodeId);
$db->setQuery($query);
$db->execute();
$id = (int) $db->loadResult();
$map = new JObject;
$map->link_id = (int) $linkId;
$map->node_id = (int) $nodeId;
if ($id)
{
$db->updateObject('#__finder_taxonomy_map', $map, array('link_id', 'node_id'));
}
else
{
$db->insertObject('#__finder_taxonomy_map', $map);
}
return true;
}
/**
* Method to get the title of all taxonomy branches.
*
* @return array An array of branch titles.
*
* @since 2.5
* @throws Exception on database error.
*/
public static function getBranchTitles()
{
$db = JFactory::getDbo();
// Set user variables
$groups = implode(',', JFactory::getUser()->getAuthorisedViewLevels());
// Create a query to get the taxonomy branch titles.
$query = $db->getQuery(true)
->select($db->quoteName('title'))
->from($db->quoteName('#__finder_taxonomy'))
->where($db->quoteName('parent_id') . ' = 1')
->where($db->quoteName('state') . ' = 1')
->where($db->quoteName('access') . ' IN (' . $groups . ')');
// Get the branch titles.
$db->setQuery($query);
return $db->loadColumn();
}
/**
* Method to find a taxonomy node in a branch.
*
* @param string $branch The branch to search.
* @param string $title The title of the node.
*
* @return mixed Integer id on success, null on no match.
*
* @since 2.5
* @throws Exception on database error.
*/
public static function getNodeByTitle($branch, $title)
{
$db = JFactory::getDbo();
// Set user variables
$groups = implode(',', JFactory::getUser()->getAuthorisedViewLevels());
// Create a query to get the node.
$query = $db->getQuery(true)
->select('t1.*')
->from($db->quoteName('#__finder_taxonomy') . ' AS t1')
->join('INNER', $db->quoteName('#__finder_taxonomy') . ' AS t2 ON t2.id = t1.parent_id')
->where('t1.access IN (' . $groups . ')')
->where('t1.state = 1')
->where('t1.title LIKE ' . $db->quote($db->escape($title) . '%'))
->where('t2.access IN (' . $groups . ')')
->where('t2.state = 1')
->where('t2.title = ' . $db->quote($branch));
// Get the node.
$db->setQuery($query, 0, 1);
return $db->loadObject();
}
/**
* Method to remove map entries for a link.
*
* @param integer $linkId The link to remove.
*
* @return boolean True on success.
*
* @since 2.5
* @throws Exception on database error.
*/
public static function removeMaps($linkId)
{
// Delete the maps.
$db = JFactory::getDbo();
$query = $db->getQuery(true)
->delete($db->quoteName('#__finder_taxonomy_map'))
->where($db->quoteName('link_id') . ' = ' . (int) $linkId);
$db->setQuery($query);
$db->execute();
return true;
}
/**
* Method to remove orphaned taxonomy nodes and branches.
*
* @return integer The number of deleted rows.
*
* @since 2.5
* @throws Exception on database error.
*/
public static function removeOrphanNodes()
{
// Delete all orphaned nodes.
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$subquery = $db->getQuery(true);
$subquery1 = $db->getQuery(true);
$subquery1->select($db->quoteName('t.id'))
->from($db->quoteName('#__finder_taxonomy', 't'))
->join('LEFT', $db->quoteName('#__finder_taxonomy_map', 'm') . ' ON ' . $db->quoteName('m.node_id') . '=' . $db->quoteName('t.id'))
->where($db->quoteName('t.parent_id') . ' > 1 ')
->where($db->quoteName('m.link_id') . ' IS NULL');
$subquery->select($db->quoteName('id'))
->from('(' . $subquery1 . ') temp');
$query->delete($db->quoteName('#__finder_taxonomy'))
->where($db->quoteName('id') . ' IN (' . $subquery . ')');
$db->setQuery($query);
$db->execute();
return $db->getAffectedRows();
}
/**
* Method to store a node to the database. This method will accept either a branch or a node.
*
* @param object $item The item to store.
*
* @return boolean True on success.
*
* @since 2.5
* @throws Exception on database error.
*/
protected static function storeNode($item)
{
$db = JFactory::getDbo();
// Check if we are updating or inserting the item.
if (empty($item->id))
{
// Insert the item.
$db->insertObject('#__finder_taxonomy', $item, 'id');
}
else
{
// Update the item.
$db->updateObject('#__finder_taxonomy', $item, 'id');
}
return true;
}
}