Repository navigation
Expand file tree
/
Copy pathclass.Nst.php
More file actions
626 lines (536 loc) · 19.7 KB
/
Copy pathclass.Nst.php
File metadata and controls
626 lines (536 loc) · 19.7 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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
<?php
/**
* Gestion de structure par representation intervallaire (Nested Set Trees)
*
*
*
* <p>Gestion de structure par representation intervallaire</p>
*
* @name intervallaire
* @author Powange
* @link
* @copyright Powange 2009
* @version 1.0.0
* @package class.NST
*
* References:
* http://www.sitepoint.com/article/1105/2
* http://searchdatabase.techtarget.com/tip/1,289483,sid13_gci537290,00.html
* http://dbforums.com/arch/57/2002/6/400142
* http://www.edutech.ch/contribution/nstrees
*
*
*
* Datastructures:
* ---------------
*
* Handle:
* key: 'table': name of the table that contains the tree structure
* key: 'lvalname': name of the attribute (field) that contains the left value
* key: 'rvalname': name of the attribute (field) that contains the right value
*
* Node:
* key 'l': left value
* key 'r': right value
*
*
* Orientation
* -----------
*
* n0
* / | \
* n1 N n3
* / \
* n4 n5
*
* directions from the perspective of the node N:
* n0: up / ancestor
* n1: previous (sibling)
* n3: next (sibling)
* n4: first (child)
* n5: last (child)
*/
class NST
{
/****************************************************************************************************************/
/* Déclaration des attributs /
/**************************************************************************************************************/
private $TableName='';
private $LeftValName='';
private $RightValName='';
/**************************************************************************************************************\
/* Fin de déclaration des attributs \
/****************************************************************************************************************\
/**
* __construct
* Initialise les attributs suivant les informations données par l'utilisateur
*
* @param string $TableName nom de la table
* @param string $LeftValName nom de la colonne des bornes gauches
* @param string $RightValName nom de la colonne des bornes droites
*/
public function __construct($TableName='table', $LeftValName='bg', $RightValName='bd')
{
$this->TableName=$TableName;
$this->LeftValName=$LeftValName;
$this->RightValName=$RightValName;
}
/****************************************************************************************************************/
/* Bloc : méthodes accesseur /
/**************************************************************************************************************/
public function __get ($nom)
{
if (isset ($this->$nom))
return $this->$nom;
}
/****************************************************************************************************************/
/* Constructeur d'Arbre /
/**************************************************************************************************************/
/* Créer une nouvelle entrée racine et retourne le nœud 'l'=1, 'r'=2. */
public function nstNewRoot($othercols=null)
{
$newnode['l'] = 1;
$newnode['r'] = 2;
$this->_insertNew($newnode, $othercols);
return $newnode;
}
/* Créer une nouvelle feuille au debut dans '$node'. */
public function nstNewFirstChild($node, $othercols=null)
{
$newnode['l'] = $node['l']+1;
$newnode['r'] = $node['l']+2;
$this->_shiftRLValues($newnode['l'], 2);
$this->_insertNew($newnode, $othercols);
return $newnode;
}
/* Créer une nouvelle feuille en dernier dans '$node'. */
public function nstNewLastChild($node, $othercols=null)
{
$newnode['l'] = $node['r'];
$newnode['r'] = $node['r']+1;
$this->_shiftRLValues($newnode['l'], 2);
$this->_insertNew($newnode, $othercols);
return $newnode;
}
public function nstNewPrevSibling($node, $othercols=null)
{
$newnode['l'] = $node['l'];
$newnode['r'] = $node['l']+1;
$this->_shiftRLValues($newnode['l'], 2);
$this->_insertNew($newnode, $othercols);
return $newnode;
}
public function nstNewNextSibling($node, $othercols=null)
{
$newnode['l'] = $node['r']+1;
$newnode['r'] = $node['r']+2;
$this->_shiftRLValues($newnode['l'], 2);
$this->_insertNew($newnode, $othercols);
return $newnode;
}
/****************************************************************************************************************/
/* Routines internes /
/**************************************************************************************************************/
/* Ajoute '$delta' à toutes les valeurs L and R qui sont >= '$first'. '$delta' peut aussi être négative. */
private function _shiftRLValues($first, $delta)
{
//print("DECALAGE: ajoute $delta aux bornes >= $first <br/>");
$sql='UPDATE '.$this->TableName.' SET
'.$this->LeftValName.'='.$this->LeftValName.'+'.intval($delta).'
WHERE '.$this->LeftValName.'>='.$first;
mysql_query($sql);
$sql='UPDATE '.$this->TableName.' SET
'.$this->RightValName.'='.$this->RightValName.'+'.intval($delta).'
WHERE '.$this->RightValName.'>='.$first;
mysql_query($sql);
}
/* Ajoute '$delta' à toutes les valeurs L and R qui sont >= '$first' and <= '$last'. '$delta' peut aussi être négative.
* retourne les valeurs first/last décalé dans un array de nœud. */
private function _shiftRLRange($first, $last, $delta)
{
$sql='UPDATE '.$this->TableName.' SET
'.$this->LeftValName.'='.$this->LeftValName."+$delta
WHERE ".$this->LeftValName.">=$first AND ".$this->LeftValName."<=$last";
mysql_query($sql);
$sql='UPDATE '.$this->TableName.' SET
'.$this->RightValName.'='.$this->RightValName."+$delta
WHERE ".$this->RightValName.">=$first AND ".$this->RightValName."<=$last";
mysql_query($sql);
return array('l'=>$first+$delta, 'r'=>$last+$delta);
}
/* Créer une nouvelle entrée. */
private function _insertNew($node, $othercols=null)
{
if(strlen($othercols)>0)
$othercols .= ',';
$sql='INSERT INTO '.$this->TableName.' SET
'.$othercols
.$this->LeftValName.'='.$node['l'].',
'.$this->RightValName.'='.$node['r'];
if(!mysql_query($sql)) $this->_prtError();
}
/****************************************************************************************************************/
/* Réorganisation de l'Arbre /
/**************************************************************************************************************/
/* Toutes les fonction nstMove... retourne la nouvelle position du sous-arbre déplacé. */
/* déplace le nœud '$src' et tous ses éléments (sous-arborescence) afin d'être le prochain frère de '$dst' */
public function nstMoveToNextSibling($src, $dst)
{
return $this->_moveSubtree($src, $dst['r']+1);
}
/* déplace le nœud '$src' et tous ses éléments (sous-arborescence) afin d'être le précédent frère de '$dst'. */
public function nstMoveToPrevSibling($src, $dst)
{
return $this->_moveSubtree($src, $dst['l']);
}
/* déplace le nœud '$src' et tous ses éléments (sous-arborescence) afin d'être le premier élément de '$dst'. */
public function nstMoveToFirstChild($src, $dst)
{
return $this->_moveSubtree($src, $dst['l']+1);
}
/* déplace le nœud '$src' et tous ses éléments (sous-arborescence) afin d'être le dernier élément de '$dst'. */
public function nstMoveToLastChild($src, $dst)
{
return $this->_moveSubtree($src, $dst['r']);
}
/* '$src' est le nœud/sous-arbre, '$to' est sa valeur L de destination */
private function _moveSubtree($src, $to)
{
$treesize = $src['r']-$src['l']+1;
$this->_shiftRLValues($to, $treesize);
// $src a été décalé aussi?
if($src['l'] >= $to){
$src['l'] += $treesize;
$src['r'] += $treesize;
}
/* now there's enough room next to target to move the subtree*/
$newpos =
$this->_shiftRLRange($src['l'], $src['r'], $to-$src['l']);
/* correct values after source */
$this->_shiftRLValues($src['r']+1, -$treesize);
// dest a été décalé aussi?
if($src['l'] <= $to){
$newpos['l'] -= $treesize;
$newpos['r'] -= $treesize;
}
return $newpos;
}
/****************************************************************************************************************/
/* Destructeurs d'arbres /
/**************************************************************************************************************/
/* supprime l'ensemble de l'arborescence, y compris tous les dossiers. */
public function nstDeleteTree ()
{
$sql='DELETE FROM '.$this->TableName;
if(!mysql_query($sql)) {$this->_prtError();}
}
/* supprime le nœud '$node' et de tous ses éléments (sous-arbres). */
public function nstDelete($node)
{
$leftanchor = $node['l'];
$sql='DELETE FROM '.$this->TableName.'
WHERE
'.$this->LeftValName.'>='.$node['l'].'
AND '.$this->RightValName.'<='.$node['r'];
if(!mysql_query($sql)) {$this->_prtError();}
$this->_shiftRLValues($node['r']+1, $node['l'] - $node['r'] -1);
$whereclause=$this->LeftValName.'<'.$leftanchor.'
ORDER BY '.$this->LeftValName.' DESC';
return $this->nstGetNodeWhere($whereclause);
}
/****************************************************************************************************************/
/* Requêtes d'arbres /
/**************************************************************************************************************/
/* Les fonctions suivantes retournent un nœud en cours de validité (valeur L et R),
/* ou L = 0, R = 0 si le résultat n'existe pas.
/* ******************************************************************* */
/* Renvoie le premier nœud qui correspond au '$whereClause'.
La clause 'WHERE' peut éventuellement aussi contenir les clauses 'ORDER BY' ou 'LIMIT'.
'$othercols' peut contenir les autres colonnes a récupérer du nœud */
public function nstGetNodeWhere($whereclause, $othercols=null)
{
$noderes['l'] = 0;
$noderes['r'] = 0;
if(!empty($othercols))
$othercols .= ', ';
$sql='SELECT
'.$othercols.'
'.$this->LeftValName.' AS l,
'.$this->RightValName.' AS r
FROM '.$this->TableName.'
WHERE '.$whereclause;
$res = mysql_query($sql);
if(!$res) {$this->_prtError();}
else {
if($noderes = mysql_fetch_array ($res)) {
return $noderes;
}
}
return false;
}
/* Renvoie le nœud qui a pour LeftValName la valeur '$leftval'. */
public function nstGetNodeWhereLeft($leftval, $othercols=null)
{
return $this->nstGetNodeWhere($this->LeftValName.'='.$leftval, $othercols);
}
/* Renvoie le nœud qui a pour RightValName la valeur '$rightval'. */
public function nstGetNodeWhereRight($rightval, $othercols=null)
{
return $this->nstGetNodeWhere($this->RightValName.'='.$rightval, $othercols);
}
/* Renvoie le nœud racine. */
public function nstRoot($othercols=null)
{
return $this->nstGetNodeWhere($this->LeftValName.'=1', $othercols, $othercols);
}
/* Renvoie le premier element du nœud '$node'. */
public function nstFirstChild($node, $othercols=null)
{
return $this->nstGetNodeWhere($this->LeftValName.'='.($node['l']+1), $othercols);
}
/* Renvoie le dernier élément du nœud '$node'. */
public function nstLastChild($node, $othercols=null)
{
return $this->nstGetNodeWhere($this->RightValName.'='.($node['r']-1), $othercols);
}
/* Renvoie le précèdent frère du nœud '$node'. */
public function nstPrevSibling($node, $othercols=null)
{
return $this->nstGetNodeWhere($this->RightValName.'='.($node['l']-1), $othercols);
}
/* Renvoie le frère suivant du nœud '$node'. */
public function nstNextSibling($node, $othercols=null)
{
return $this->nstGetNodeWhere($this->LeftValName.'='.($node['r']+1), $othercols);
}
/* Renvoie le parent du nœud '$node'. */
public function nstAncestor($node, $othercols=null)
{
return $this->nstGetNodeWhere(
$this->LeftValName.'<'.($node['l'])
.' AND '.$this->RightValName.'>'.($node['r'])
.' ORDER BY '.$this->RightValName
, $othercols);
}
/****************************************************************************************************************/
/* Requêtes d'arbres (UPDATE) /
/**************************************************************************************************************/
/* Les fonctions suivantes
/* ******************************************************************* */
public function nstUpdateWhere($whereclause, $updatecols)
{
if(empty($updatecols))
return false;
$sql='UPDATE '.$this->TableName.' SET
'.$updatecols.'
WHERE '.$whereclause;
$res = mysql_query($sql);
if(!$res) {$this->_prtError();}
else
return true;
return false;
}
public function nstUpdateNode($node, $updatecols)
{
return $this->nstUpdateWhere($this->LeftValName.'='.($node['l']), $updatecols);
}
public function nstUpdateSubtree($node, $updatecols)
{
return $this->nstUpdateWhere($this->LeftValName.'>='.($node['l']).' AND '.$this->RightValName.'<='.($node['r']), $updatecols);
}
/****************************************************************************************************************/
/* Fonctions d'arbres de validité /
/**************************************************************************************************************/
/* Les fonctions suivantes retournent une valeur booléenne
/* ******************************************************************* */
/* Vérifie si le noeud est valide (L-value < R-value), (Aucune base de données n'est necessaire)*/
public function nstValidNode($node)
{
return ($node['l'] < $node['r']);
}
/* Vérifie si le noeud a un parent*/
public function nstHasAncestor($node)
{
return $this->nstValidNode($this->nstAncestor($node));
}
/* Vérifie si le noeud a un frère précèdent*/
public function nstHasPrevSibling($node)
{
return $this->nstValidNode($this->nstPrevSibling($node));
}
/* Vérifie si le noeud a un frère suivant*/
public function nstHasNextSibling($node)
{
return $this->nstValidNode($this->nstNextSibling($node));
}
/* Vérifie si le noeud a un ou plusieurs éléments*/
public function nstHasChildren($node)
{
return (($node['r']-$node['l'])>1);
}
/* Vérifie si le noeud est la racine*/
public function nstIsRoot($node)
{
return ($node['l']==1);
}
/* Vérifie si le noeud est une feuille*/
public function nstIsLeaf($node)
{
return (($node['r']-$node['l'])==1);
}
/* Vérifie si 'node1' est un élément directe ou dans le sous-arbre de 'node2' */
public function nstIsChild ($node1, $node2)
{
return (($node1['l']>$node2['l']) and ($node1['r']<$node2['r']));
}
/* Vérifie si 'node1' est un élément directe ou dans le sous-arbre de 'node2' ou egal à 'node2' */
public function nstIsChildOrEqual ($node1, $node2)
{
return (($node1['l']>=$node2['l']) and ($node1['r']<=$node2['r']));
}
/* Vérifie si 'node1' est égal à 'node2' */
public function nstEqual ($node1, $node2)
{
return (($node1['l']==$node2['l']) and ($node1['r']==$node2['r']));
}
/****************************************************************************************************************/
/* Fonctions d'arbres /
/**************************************************************************************************************/
/* Les fonctions suivantes retournent une valeur entière
/* ******************************************************************* */
/* Retourne le nombre d'élément du nœud '$node' */
public function nstNbChildren($node)
{
return (($node['r']-$node['l']-1)/2);
}
/* retourne le niveau du nœud '$node', soit le nombre de parent. (niveau de la racine = 0)*/
public function nstLevel($node)
{
$sql='SELECT
COUNT(*) AS level
FROM '.$this->TableName.'
WHERE '.$this->LeftValName.'<'.$node['l'].' AND '.$this->RightValName.'>'.$node['r'];
$res = mysql_query($sql);
if(!$res) {$this->_prtError();}
if($row = mysql_fetch_array($res))
{
return $row['level'];
}
else
{
return 0;
}
}
/****************************************************************************************************************/
/* Arbre Walks /
/**************************************************************************************************************/
/* initializes preorder walk and returns a walk handle */
public function nstWalkPreorder($node)
{
$sql='SELECT *
FROM '.$this->TableName.'
WHERE '.$this->LeftValName.'>='.$node['l'].' AND '.$this->RightValName.'<='.$node['r'].'
ORDER BY '.$this->LeftValName;
$res = mysql_query($sql);
if(!$res) {$this->_prtError();}
return array('recset'=>$res,
'prevl'=>$node['l'], 'prevr'=>$node['r'], // needed to efficiently calculate the level
'level'=>-2 );
}
public function nstWalkNext(&$walkhand)
{
if($row = mysql_fetch_array($walkhand['recset'], MYSQL_ASSOC))
{
// calcul du niveau
$walkhand['level']+= $walkhand['prevl'] - $row[$this->LeftValName] +2;
// stock le nœud courant
$walkhand['prevl'] = $row[$this->LeftValName];
$walkhand['prevr'] = $row[$this->RightValName];
$walkhand['row'] = $row;
return array('l'=>$row[$this->LeftValName], 'r'=>$row[$this->RightValName]);
}
else {return FALSE;}
}
public function nstWalkAttribute($walkhand, $attribute)
{
return $walkhand['row'][$attribute];
}
public function nstWalkCurrent($walkhand)
{
return array('l'=>$walkhand['prevl'], 'r'=>$walkhand['prevr']);
}
public function nstWalkLevel($walkhand)
{
return $walkhand['level'];
}
/****************************************************************************************************************/
/* Outils d'affichages /
/**************************************************************************************************************/
/* Retourne l'attribut du noeud spécifié */
public function nstNodeAttribute($node, $attribute)
{
$sql='SELECT '.$attribute.'
FROM '.$this->TableName.'
WHERE '.$this->LeftValName.'='.$node['l'];
$res = mysql_query($sql);
if(!$res) {$this->_prtError();}
if($row = mysql_fetch_array ($res))
{
return $row[$attribute];
}
else
{
return '';
}
}
/* */
public function nstPrintSubtree($node, $attributes)
{
$wlk = $this->nstWalkPreorder($node);
while ($curr = $this->nstWalkNext($wlk))
{
// Affiche l'indentation
print(str_repeat(' ', $this->nstWalkLevel($wlk)*4));
// Affiche les attributs
$att = reset($attributes);
while($att)
{
// La ligne suivante est plus efficace: print ($att.":".nstWalkAttribute($wlk, $att));
print ($wlk['row'][$att]);
$att = next($attributes);
}
print ('<br/>');
}
}
/* Imprime les attributs de l'arbre entier. */
public function nstPrintTree($attributes)
{
$this->nstPrintSubtree($this->nstRoot(), $attributes);
}
/* returns a string representing the breadcrumbs from $node to $root
/* retourne une chaîne représentant la chapelure de $node à $root
Exemple: "root > a-node > another-node > current-node"
Contributed by Nick Luethi
*/
public function nstBreadcrumbsString($node, $TitleValName)
{
// nœuds courant
$ret = nstNodeAttribute($node, $TitleValName);
// traiter les nœuds ancêtres
while($this->nstAncestor($node) != array('l'=>0,'r'=>0))
{
$ret = nstNodeAttribute($this->nstAncestor($node), $TitleValName).' > '.$ret;
$node = $this->nstAncestor($node);
}
return $ret;
//return " breadcrumb: <font size='1'>".$ret."</font>";
}
/****************************************************************************************************************/
/* Fonctions Internes /
/**************************************************************************************************************/
private function _prtError()
{
echo '<p>Error: '.mysql_errno().': '.mysql_error().'</p>';
}
}
?>