summaryrefslogtreecommitdiff
path: root/src/SemanticScuttle/Service/TagCache.php
blob: b0ddfa6f31a9708d5979e20b7576f197966a939a (plain)
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
<?php
/**
 * SemanticScuttle - your social bookmark manager.
 *
 * PHP version 5.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @author   Eric Dane <ericdane@users.sourceforge.net>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */

/**
 * SemanticScuttle tag caching service.
 *
 * This class infers on relation between tags by storing all
 * the including tags or synonymous tag.
 * For example, if the user creates: tag1>tag2>tag3, the system
 * can infer that tag is included into tag1.
 * Instead of computing this relation several times, it is saved
 * into this current table.
 * For synonymy, this table stores also the group of synonymous tags.
 * The table must be updated for each modification of
 * the relations between tags.
 *
 * @category Bookmarking
 * @package  SemanticScuttle
 * @author   Benjamin Huynh-Kim-Bang <mensonge@users.sourceforge.net>
 * @author   Christian Weiske <cweiske@cweiske.de>
 * @author   Eric Dane <ericdane@users.sourceforge.net>
 * @license  GPL http://www.gnu.org/licenses/gpl.html
 * @link     http://sourceforge.net/projects/semanticscuttle
 */
class SemanticScuttle_Service_TagCache extends SemanticScuttle_DbService
{

    /**
     * Returns the single service instance
     *
     * @param DB $db Database object
     *
     * @return SemanticScuttle_Service
     */
    public static function getInstance($db)
    {
        static $instance;
        if (!isset($instance)) {
            $instance = new self($db);
        }
        return $instance;
    }

    protected function __construct($db)
    {
        $this->db =$db;
        $this->tablename = $GLOBALS['tableprefix'] .'tagscache';
    }

    function getChildren($tag1, $uId) {
        $tagservice =SemanticScuttle_Service_Factory::get('Tag');
        $tag1 = $tagservice->normalize($tag1);

        if($tag1 == '') return false;

        $query = "SELECT DISTINCT tag2 as 'tag'";
        $query.= " FROM `". $this->getTableName() ."`";
        $query.= " WHERE relationType = '>'";
        $query.= " AND tag1 = '" . $this->db->sql_escape($tag1) . "'";
        $query.= " AND uId = " . intval($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) {
            $output[] = $row['tag'];
        }

        $this->db->sql_freeresult($dbresult);
        return $output;
    }

    function addChild($tag1, $tag2, $uId) {
        $tagservice =SemanticScuttle_Service_Factory::get('Tag');
        $tag1 = $tagservice->normalize($tag1);
        $tag2 = $tagservice->normalize($tag2);

        if($tag1 == $tag2 || strlen($tag1) == 0 || strlen($tag2) == 0
        || ($this->existsChild($tag1, $tag2, $uId))) {
            return false;
        }

        $values = array('tag1' => $tag1, 'tag2' => $tag2, '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 add tag cache inference', '', __LINE__, __FILE__, $query, $this->db);
            return false;
        }
        $this->db->sql_transaction('commit');
    }

    function removeChild($tag1, $tag2, $uId) {
        if(($tag1 != '' && $tag1 == $tag2) ||
        ($tag1 == '' && $tag2 == '' && $uId == '')) {
            return false;
        }

        $query = 'DELETE FROM '. $this->getTableName();
        $query.= ' WHERE 1=1';
        $query.= strlen($tag1)>0 ? ' AND tag1 = \''. $this->db->sql_escape($tag1) . "'" : '';
        $query.= strlen($tag2)>0 ? ' AND tag2 = \''. $this->db->sql_escape($tag2) . "'" : '';
        $query.= ' AND relationType = ">"';
        $query.= strlen($uId)>0 ? ' AND uId = ' . intval($uId) : '';

        if (!($dbresult =& $this->db->sql_query($query))) {
            message_die(GENERAL_ERROR, 'Could not remove tag cache inference', '', __LINE__, __FILE__, $query, $this->db);
            return false;
        }
    }

    function removeChildren($tag1, $uId) {
        $this->removeChild($tag1, NULL, $uId);
    }

    function existsChild($tag1, $tag2, $uId) {
        $tagservice =SemanticScuttle_Service_Factory::get('Tag');
        $tag1 = $tagservice->normalize($tag1);
        $tag2 = $tagservice->normalize($tag2);

        $query = "SELECT tag1, tag2, relationType, uId FROM `". $this->getTableName() ."`";
        $query.= " WHERE tag1 = '" . $this->db->sql_escape($tag1) . "'";
        $query.= " AND tag2 = '" . $this->db->sql_escape($tag2) . "'";
        $query.= " AND relationType = '>'";
        $query.= " AND uId = " . intval($uId);

        //echo($query."<br>\n");

        $dbres = $this->db->sql_query($query);
        $rows = $this->db->sql_numrows($dbres);
        $this->db->sql_freeresult($dbres);
        return $rows > 0;
    }

    /*
     * Synonyms of a same concept are a group. A group has one main synonym called key
     * and a list of synonyms called values.
     */
    function addSynonym($tag1, $tag2, $uId) {

        if($tag1 == $tag2 || strlen($tag1) == 0 || strlen($tag2) == 0
        || ($this->existsSynonym($tag1, $tag2, $uId))) {
            return false;
        }

        $case1 = '0'; // not in DB
        if($this->_isSynonymKey($tag1, $uId)) {
            $case1 = 'key';
        } elseif($this->_isSynonymValue($tag1, $uId)) {
            $case1 = 'value';
        }

        $case2 = '0'; // not in DB
        if($this->_isSynonymKey($tag2, $uId)) {
            $case2 = 'key';
        } elseif($this->_isSynonymValue($tag2, $uId)) {
            $case2 = 'value';
        }
        $case = $case1.$case2;

        // all the possible cases
        switch ($case) {
            case 'keykey':
                $values = $this->_getSynonymValues($tag2, $uId);
                $this->removeSynonymGroup($tag2, $uId);
                foreach($values as $value) {
                    $this->addSynonym($tag1, $value['tag'], $uId);
                }
                $this->addSynonym($tag1, $tag2, $uId);
                break;

            case 'valuekey':
                $key = $this->_getSynonymKey($tag1, $uId);
                $this->addSynonym($key, $tag2, $uId);
                break;

            case 'keyvalue':
                $this->addSynonym($tag2, $tag1, $uId);
                break;
            case 'valuevalue':
                $key1 =  $this->_getSynonymKey($tag1, $uId);
                $key2 =  $this->_getSynonymKey($tag2, $uId);
                $this->addSynonym($key1, $key2, $uId);
                break;
            case '0value':
                $key = $this->_getSynonymKey($tag2, $uId);
                $this->addSynonym($key, $tag1, $uId);
                break;
            case 'value0':
                $this->addSynonym($tag2, $tag1, $uId);
                break;
            case '0key':
                $this->addSynonym($tag2, $tag1, $uId);
                break;
            default:
                $values = array('tag1' => $tag1, 'tag2' => $tag2, '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 add tag cache synonymy', '', __LINE__, __FILE__, $query, $this->db);
                    return false;
                }
                $this->db->sql_transaction('commit');
                break;
        }
    }

    function removeSynonymGroup($tag1, $uId) {
        $query = 'DELETE FROM '. $this->getTableName();
        $query.= ' WHERE 1=1';
        $query.= ' AND tag1 = \''. $this->db->sql_escape($tag1) . "'";
        $query.= ' AND relationType = "="';
        $query.= ' AND uId = ' . intval($uId);

        if (!($dbresult =& $this->db->sql_query($query))) {
            message_die(GENERAL_ERROR, 'Could not remove tag cache inference', '', __LINE__, __FILE__, $query, $this->db);
            return false;
        }
    }

    function _isSynonymKey($tag1, $uId) {
        $tagservice =SemanticScuttle_Service_Factory::get('Tag');
        $tag1 = $tagservice->normalize($tag1);

        $query = "SELECT tag1 FROM `". $this->getTableName() ."`";
        $query.= " WHERE tag1 = '" . $this->db->sql_escape($tag1) ."'";
        $query.= " AND relationType = '='";
        $query.= " AND uId = " . intval($uId);

        $dbres = $this->db->sql_query($query);
        $rows = $this->db->sql_numrows($dbres);
        $this->db->sql_freeresult($dbres);
        return $rows > 0;
    }

    function _isSynonymValue($tag2, $uId) {
        $tagservice =SemanticScuttle_Service_Factory::get('Tag');
        $tag2 = $tagservice->normalize($tag2);

        $query = "SELECT tag2 FROM `". $this->getTableName() ."`";
        $query.= " WHERE tag2 = '" . $this->db->sql_escape($tag2) . "'";
        $query.= " AND relationType = '='";
        $query.= " AND uId = " . intval($uId);

        $dbres = $this->db->sql_query($query);
        $rows = $this->db->sql_numrows($dbres);
        $this->db->sql_freeresult($dbres);
        return $rows > 0;
    }

    function getSynonyms($tag1, $uId) {
        $values = array();
        if($this->_isSynonymKey($tag1, $uId)) {
            $values = $this->_getSynonymValues($tag1, $uId);
        } elseif($this->_isSynonymValue($tag1, $uId)) {
            $key = $this->_getSynonymKey($tag1, $uId);
            $values = $this->_getSynonymValues($key, $uId, $tag1);
            $values[] = $key;
        }
        return $values;
    }

    function _getSynonymKey($tag2, $uId) {
        $tagservice =SemanticScuttle_Service_Factory::get('Tag');
        $tag2 = $tagservice->normalize($tag2);

        if($this->_isSynonymKey($tag2, $uId)) return $tag2;

        if($tag2 == '') return false;

        $query = "SELECT DISTINCT tag1 as 'tag'";
        $query.= " FROM `". $this->getTableName() ."`";
        $query.= " WHERE relationType = '='";
        $query.= " AND tag2 = '" . $this->db->sql_escape($tag2) . "'";
        $query.= " AND uId = " . intval($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;
        }

        $row = $this->db->sql_fetchrow($dbresult);
        $this->db->sql_freeresult($dbresult);
        return $row['tag'];
    }

    /*
     * Return values associated with a key.
     * $tagExcepted allows to hide a value.
     */
    function _getSynonymValues($tag1, $uId, $tagExcepted = NULL) {
        $tagservice =SemanticScuttle_Service_Factory::get('Tag');
        $tag1 = $tagservice->normalize($tag1);
        $tagExcepted = $tagservice->normalize($tagExcepted);

        if($tag1 == '') return false;

        $query = "SELECT DISTINCT tag2 as 'tag'";
        $query.= " FROM `". $this->getTableName() ."`";
        $query.= " WHERE relationType = '='";
        $query.= " AND tag1 = '" . $this->db->sql_escape($tag1) . "'";
        $query.= " AND uId = " . intval($uId);
        $query.= $tagExcepted!=''?" AND tag2!='" . $this->db->sql_escape($tagExcepted) . "'" : '';

        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) {
            $output[] = $row['tag'];
        }

        $this->db->sql_freeresult($dbresult);
        return $output;
    }

    function existsSynonym($tag1, $tag2, $uId) {
        if($this->_getSynonymKey($tag1, $uId) == $tag2 || $this->_getSynonymKey($tag2, $uId) == $tag1) {
            return true;
        } else {
            return false;
        }
    }


    function updateTag($tag1, $relationType, $otherTags, $uId) {
        if($relationType == '=') {
            if($this->getSynonyms($tag1, $uId)) {  // remove previous data avoiding unconstistency
                $this->removeSynonymGroup($tag1, $uId);
            }

            foreach($otherTags as $tag2) {
                $this->addSynonym($tag1, $tag2, $uId);
            }
        } elseif($relationType == '>') {
            if(count($this->getChildren($tag1, $uId))>0) { // remove previous data avoiding unconstistency
                $this->removeChildren($tag1);
            }

            foreach($otherTags as $tag2) {
                $this->addChild($tag1, $tag2, $uId);
            }
        }
    }

    function deleteByUser($uId) {
        $query = 'DELETE FROM '. $this->getTableName() .' WHERE uId = '. intval($uId);

        if (!($dbresult = & $this->db->sql_query($query))) {
            message_die(GENERAL_ERROR, 'Could not delete user tags cache', '', __LINE__, __FILE__, $query, $this->db);
            return false;
        }

        return true;

    }

    function deleteAll() {
        $query = 'TRUNCATE TABLE `'. $this->getTableName() .'`';
        $this->db->sql_query($query);
    }

}
?>