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
|
<?php
class Tag2TagService {
var $db;
var $tablename;
function &getInstance(&$db) {
static $instance;
if (!isset($instance))
$instance =& new Tag2TagService($db);
return $instance;
}
function Tag2TagService(&$db) {
$this->db =& $db;
$this->tablename = $GLOBALS['tableprefix'] .'tags2tags';
}
function addLinkedTags($tag1, $tag2, $relationType, $uId) {
if($tag1 == $tag2 || strlen($tag1) == 0 || strlen($tag2) == 0
|| ($relationType != ">" && $relationType != "=")
|| ($this->existsLinkedTags($tag1, $tag2, $relationType, $uId))) {
return false;
}
$values = array('tag1' => $tag1, 'tag2' => $tag2, 'relationType'=> $relationType, 'uId'=> $uId);
$query = 'INSERT INTO '. $this->getTableName() .' '. $this->db->sql_build_array('INSERT', $values);
//die($query);
if (!($dbresult =& $this->db->sql_query($query))) {
$this->db->sql_transaction('rollback');
message_die(GENERAL_ERROR, 'Could not attach tag to tag', '', __LINE__, __FILE__, $query, $this->db);
return false;
}
$this->db->sql_transaction('commit');
// Update stats
$tsts =& ServiceFactory::getServiceInstance('TagStatService');
$tsts->updateStat($tag1, $relationType, $uId);
return true;
}
// Return the target linked tags. If inverseRelation is true, return the source linked tags.
function getLinkedTags($tag, $relationType, $uId = null, $inverseRelation = false, $stopList = array()) {
// Set up the SQL query.
if($inverseRelation) {
$queriedTag = "tag1";
$givenTag = "tag2";
} else {
$queriedTag = "tag2";
$givenTag = "tag1";
}
$query = "SELECT DISTINCT ". $queriedTag ." as 'tag'";
$query.= " FROM `". $this->getTableName() ."`";
$query.= " WHERE 1=1";
if($tag !=null) {
$query.= " AND ". $givenTag ." = '". $tag ."'";
}
if($relationType) {
$query.= " AND relationType = '". $relationType ."'";
}
if($uId != null) {
$query.= " AND uId = '".$uId."'";
}
//die($query);
if (! ($dbresult =& $this->db->sql_query($query)) ){
message_die(GENERAL_ERROR, 'Could not get related tags', '', __LINE__, __FILE__, $query, $this->db);
return false;
}
$rowset = $this->db->sql_fetchrowset($dbresult);
$output = array();
foreach($rowset as $row) {
if(!in_array($row['tag'], $stopList)) {
$output[] = $row['tag'];
}
}
//bijective case for '='
if($relationType == '=' && $inverseRelation == false) {
//$stopList[] = $tag;
$bijectiveOutput = $this->getLinkedTags($tag, $relationType, $uId, true, $stopList);
$output = array_merge($output, $bijectiveOutput);
//$output = array_unique($output); // remove duplication
}
return $output;
}
/* TODO: clean the outputs to obtain homogenous ones*/
function getAllLinkedTags($tag1, $relationType, $uId, $asFlatList=true, $stopList=array()) {
$asFlatList = true; //we disable the tree list parameter for the moment
if(in_array($tag1, $stopList)) {
return array();
}
$stopList[] = $tag1;
$linkedTags = $this->getLinkedTags($tag1, $relationType, $uId, false, $stopList);
if($relationType != '=') {
$linkedTags = array_merge($linkedTags, $this->getLinkedTags($tag1, '=', $uId, false, $stopList));
}
if(count($linkedTags) == 0) {
return array();
} else {
$output = array();
if($asFlatList == true) {
//$output[$tag1] = $tag1;
} else {
$output = array('node'=>$tag1);
}
foreach($linkedTags as $linkedTag) {
$allLinkedTags = $this->getAllLinkedTags($linkedTag, $relationType, $uId, $asFlatList, $stopList);
if($asFlatList == true) {
$output[] = $linkedTag;
if(is_array($allLinkedTags)) {
$output = array_merge($output, $allLinkedTags);
} else {
$output[] = $allLinkedTags;
}
} else {
$output[] = $allLinkedTags;
}
}
}
//$output = array_unique($output); // remove duplication
return $output;
}
function getOrphewTags($relationType, $uId = 0, $limit = null, $orderBy = null) {
$query = "SELECT DISTINCT tts.tag1 as tag";
$query.= " FROM `". $this->getTableName() ."` tts";
if($orderBy != null) {
$tsts =& ServiceFactory::getServiceInstance('TagStatService');
$query.= ", ".$tsts->getTableName() ." tsts";
}
$query.= " WHERE tts.tag1 <> ALL";
$query.= " (SELECT DISTINCT tag2 FROM `". $this->getTableName() ."`";
$query.= " WHERE relationType = '".$relationType."'";
if($uId > 0) {
$query.= " AND uId = '".$uId."'";
}
$query.= ")";
if($uId > 0) {
$query.= " AND tts.uId = '".$uId."'";
}
switch($orderBy) {
case "nb":
$query.= " AND tts.tag1 = tsts.tag1";
$query.= " AND tsts.relationType = '".$relationType."'";
if($uId > 0) {
$query.= " AND tsts.uId = ".$uId;
}
$query.= " ORDER BY tsts.nb DESC";
break;
case "depth": // by nb of descendants
$query.= " AND tts.tag1 = tsts.tag1";
$query.= " AND tsts.relationType = '".$relationType."'";
if($uId > 0) {
$query.= " AND tsts.uId = ".$uId;
}
$query.= " ORDER BY tsts.depth DESC";
break;
case "nbupdate":
$query.= " AND tts.tag1 = tsts.tag1";
$query.= " AND tsts.relationType = '".$relationType."'";
if($uId > 0) {
$query.= " AND tsts.uId = ".$uId;
}
$query.= " ORDER BY tsts.nbupdate DESC";
break;
}
if($limit != null) {
$query.= " LIMIT 0,".$limit;
}
if (! ($dbresult =& $this->db->sql_query($query)) ){
message_die(GENERAL_ERROR, 'Could not get linked tags', '', __LINE__, __FILE__, $query, $this->db);
return false;
}
return $this->db->sql_fetchrowset($dbresult);
}
function getMenuTags($uId) {
if(strlen($GLOBALS['menuTag']) < 1) {
return array();
} else {
// we don't use the getAllLinkedTags function in order to improve performance
$query = "SELECT tag2 as 'tag', COUNT(tag2) as 'count'";
$query.= " FROM `". $this->getTableName() ."`";
$query.= " WHERE tag1 = '".$GLOBALS['menuTag']."'";
$query.= " AND relationType = '>'";
if($uId > 0) {
$query.= " AND uId = '".$uId."'";
}
$query.= " GROUP BY tag2";
$query.= " ORDER BY count DESC";
$query.= " LIMIT 0, ".$GLOBALS['maxSizeMenuBlock'];
if (! ($dbresult =& $this->db->sql_query($query)) ){
message_die(GENERAL_ERROR, 'Could not get linked tags', '', __LINE__, __FILE__, $query, $this->db);
return false;
}
return $this->db->sql_fetchrowset($dbresult);
}
}
function existsLinkedTags($tag1, $tag2, $relationType, $uId) {
$query = "SELECT tag1, tag2, relationType, uId FROM `". $this->getTableName() ."`";
$query.= " WHERE tag1 = '" .$tag1 ."'";
$query.= " AND tag2 = '".$tag2."'";
$query.= " AND relationType = '". $relationType ."'";
$query.= " AND uId = '".$uId."'";
return $this->db->sql_numrows($this->db->sql_query($query)) > 0;
}
function getLinks($uId) {
$query = "SELECT tag1, tag2, relationType, uId FROM `". $this->getTableName() ."`";
$query.= " WHERE 1=1";
if($uId > 0) {
$query.= " AND uId = '".$uId."'";
}
return $this->db->sql_fetchrowset($this->db->sql_query($query));
}
function removeLinkedTags($tag1, $tag2, $relationType, $uId) {
if($tag1 == $tag2 || strlen($tag1) == 0 || strlen($tag2) == 0
|| ($relationType != ">" && $relationType != "=")) {
return false;
}
$query = 'DELETE FROM '. $this->getTableName();
$query.= ' WHERE tag1 = "'. $tag1 .'"';
$query.= ' AND tag2 = "'. $tag2 .'"';
$query.= ' AND relationType = "'. $relationType .'"';
$query.= ' AND uId = "'. $uId .'"';
if (!($dbresult =& $this->db->sql_query($query))) {
message_die(GENERAL_ERROR, 'Could not remove tag relation', '', __LINE__, __FILE__, $query, $this->db);
return false;
}
// Update stats
$tsts =& ServiceFactory::getServiceInstance('TagStatService');
$tsts->updateStat($tag1, $relationType, $uId);
return true;
}
function renameTag($uId, $oldName, $newName) {
$query = 'UPDATE `'. $this->getTableName() .'`';
$query.= ' SET tag1="'.$newName.'"';
$query.= ' WHERE tag1="'.$oldName.'"';
$query.= ' AND uId="'.$uId.'"';
$this->db->sql_query($query);
$query = 'UPDATE `'. $this->getTableName() .'`';
$query.= ' SET tag2="'.$newName.'"';
$query.= ' WHERE tag2="'.$oldName.'"';
$query.= ' AND uId="'.$uId.'"';
$this->db->sql_query($query);
// Update stats
$tsts =& ServiceFactory::getServiceInstance('TagStatService');
$tsts->updateStat($oldName, '=', $uId);
$tsts->updateStat($oldName, '>', $uId);
$tsts->updateStat($newName, '=', $uId);
$tsts->updateStat($newName, '>', $uId);
return true;
}
function deleteAll() {
$query = 'TRUNCATE TABLE `'. $this->getTableName() .'`';
$this->db->sql_query($query);
$tsts =& ServiceFactory::getServiceInstance('TagStatService');
$tsts->deleteAll();
}
// Properties
function getTableName() { return $this->tablename; }
function setTableName($value) { $this->tablename = $value; }
}
?>
|