summaryrefslogtreecommitdiff
path: root/src/SemanticScuttle/Service/TagStat.php
blob: af8d1107758c36e003d138c78dde63904f51ded5 (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
<?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 statistics service.
 *
 * @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_TagStat 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'] .'tagsstats';
    }

    function getNbChildren($tag1, $relationType, $uId) {
        $tts =SemanticScuttle_Service_Factory::get('Tag2Tag');
        $query = "SELECT tag1, relationType, uId FROM `". $tts->getTableName() ."`";
        $query.= " WHERE tag1 = '" .$tag1 ."'";
        $query.= " AND relationType = '". $relationType ."'";
        $query.= " AND uId = '".$uId."'";

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

    function getNbDescendants($tag1, $relationType, $uId) {
        $query = "SELECT nb FROM `". $this->getTableName() ."`";
        $query.= " WHERE tag1 = '" .$tag1 ."'";
        $query.= " AND relationType = '". $relationType ."'";
        $query.= " AND uId = '".$uId."'";

        $dbresults = $this->db->sql_query($query);
        $row = $this->db->sql_fetchrow($dbresults);
        $this->db->sql_freeresult($dbresults);
        if($row['nb'] == null) {
            return 0;
        } else {
            return (int) $row['nb'];
        }
    }

    function getMaxDepth($tag1, $relationType, $uId) {
        $query = "SELECT depth FROM `". $this->getTableName() ."`";
        $query.= " WHERE tag1 = '" .$tag1 ."'";
        $query.= " AND relationType = '". $relationType ."'";
        $query.= " AND uId = '".$uId."'";

        $dbresults = $this->db->sql_query($query);
        $row = $this->db->sql_fetchrow($dbresults);
        $this->db->sql_freeresult($dbresults);
        if($row['depth'] == null) {
            return 0;
        } else {
            return (int) $row['depth'];
        };
    }

    function getNbUpdates($tag1, $relationType, $uId) {
        $query = "SELECT nbupdate FROM `". $this->getTableName() ."`";
        $query.= " WHERE tag1 = '" .$tag1 ."'";
        $query.= " AND relationType = '". $relationType ."'";
        $query.= " AND uId = '".$uId."'";

        $dbresults = $this->db->sql_query($query);
        $row = $this->db->sql_fetchrow($dbresults);
        $this->db->sql_freeresult($dbresults);
        if($row['nbupdate'] == null) {
            return 0;
        } else {
            return (int) $row['nbupdate'];
        }
    }

    function existStat($tag1, $relationType, $uId) {
        $query = "SELECT tag1, relationType, uId FROM `". $this->getTableName() ."`";
        $query.= " WHERE tag1 = '" .$tag1 ."'";
        $query.= " AND relationType = '". $relationType ."'";
        $query.= " AND uId = '".$uId."'";

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

    function createStat($tag1, $relationType, $uId) {
        $query = "INSERT INTO `". $this->getTableName() ."`";
        $query.= "(tag1, relationType, uId)";
        $query.= " VALUES ('".$tag1."','".$relationType."','".$uId."')";
        $this->db->sql_query($query);
    }

    function updateStat($tag1, $relationType, $uId=null, $stoplist=array()) {
        if(in_array($tag1, $stoplist)) {
            return false;
        }

        $tts =SemanticScuttle_Service_Factory::get('Tag2Tag');
        $linkedTags = $tts->getLinkedTags($tag1, $relationType, $uId);
        $nbDescendants = 0;
        $maxDepth = 0;
        foreach($linkedTags as $linkedTag) {
            $nbDescendants+= 1 + $this->getNbDescendants($linkedTag, $relationType, $uId);
            $maxDepth = max($maxDepth, 1 + $this->getMaxDepth($linkedTag, $relationType, $uId));
        }
        $this->setNbDescendants($tag1, $relationType, $uId, $nbDescendants);
        $this->setMaxDepth($tag1, $relationType, $uId, $maxDepth);
        $this->increaseNbUpdate($tag1, $relationType, $uId);

        // propagation to the precedent tags
        $linkedTags = $tts->getLinkedTags($tag1, $relationType, $uId, true);
        $stoplist[] = $tag1;
        foreach($linkedTags as $linkedTag) {
            $this->updateStat($linkedTag, $relationType, $uId, $stoplist);
        }
    }

    function updateAllStat() {
        $tts =SemanticScuttle_Service_Factory::get('Tag2Tag');

        $query = "SELECT tag1, uId FROM `". $tts->getTableName() ."`";
        $query.= " WHERE relationType = '>'";

        //die($query);

        if (! ($dbresult = $this->db->sql_query($query)) ){
            message_die(GENERAL_ERROR, 'Could not update stats', '', __LINE__, __FILE__, $query, $this->db);
            return false;
        }

        $rowset = $this->db->sql_fetchrowset($dbresult);
        foreach($rowset as $row) {
            $this->updateStat($row['tag1'], '>', $row['uId']);
        }
        $this->db->sql_freeresult($dbresult);
    }

    function setNbDescendants($tag1, $relationType, $uId, $nb) {
        if(!$this->existStat($tag1, $relationType, $uId)) {
            $this->createStat($tag1, $relationType, $uId);
        }
        $query = "UPDATE `". $this->getTableName() ."`";
        $query.= " SET nb = ". $nb;
        $query.= " WHERE tag1 = '" .$tag1 ."'";
        $query.= " AND relationType = '". $relationType ."'";
        $query.= " AND uId = '".$uId."'";
        $this->db->sql_freeresult($this->db->sql_query($query));
    }

    function setMaxDepth($tag1, $relationType, $uId, $depth) {
        if(!$this->existStat($tag1, $relationType, $uId)) {
            $this->createStat($tag1, $relationType, $uId);
        }
        $query = "UPDATE `". $this->getTableName() ."`";
        $query.= " SET depth = ". $depth;
        $query.= " WHERE tag1 = '" .$tag1 ."'";
        $query.= " AND relationType = '". $relationType ."'";
        $query.= " AND uId = '".$uId."'";
        $this->db->sql_query($query);
    }

    function increaseNbUpdate($tag1, $relationType, $uId) {
        if(!$this->existStat($tag1, $relationType, $uId)) {
            $this->createStat($tag1, $relationType, $uId);
        }
        $query = "UPDATE `". $this->getTableName() ."`";
        $query.= " SET nbupdate = nbupdate + 1";
        $query.= " WHERE tag1 = '" .$tag1 ."'";
        $query.= " AND relationType = '". $relationType ."'";
        $query.= " AND uId = '".$uId."'";

        //die($query);

        $this->db->sql_query($query);
    }

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

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

        return true;
    }

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

}
?>