]> CyberLeo.Net >> Repos - SourceForge/phpwiki.git/blob - lib/wikilens/RatingsDb.php
Whitespace only
[SourceForge/phpwiki.git] / lib / wikilens / RatingsDb.php
1 <?php
2
3 /*
4  * @author:  Dan Frankowski (wikilens group manager), Reini Urban (as plugin)
5  *
6  * TODO:
7  * - fix RATING_STORAGE = WIKIPAGE (dba, file)
8  * - fix smart caching
9  * - finish mysuggest.c (external engine with data from mysql)
10  * - add the various show modes (esp. TopN queries in PHP)
11  */
12 /*
13  CREATE TABLE rating (
14         dimension INT(4) NOT NULL,
15         raterpage INT(11) NOT NULL,
16         rateepage INT(11) NOT NULL,
17         ratingvalue FLOAT NOT NULL,
18         rateeversion INT(11) NOT NULL,
19         isPrivate ENUM('yes','no'),
20         tstamp TIMESTAMP(14) NOT NULL,
21         PRIMARY KEY (dimension, raterpage, rateepage)
22  );
23 */
24
25 // For other than SQL backends. dba + adodb SQL ratings are allowed but deprecated.
26 // We will probably drop this hack.
27 if (!defined('RATING_STORAGE'))
28     // for DATABASE_TYPE=dba and forced RATING_STORAGE=SQL we must use ADODB,
29     // but this is problematic.
30     define('RATING_STORAGE', $GLOBALS['request']->_dbi->_backend->isSQL() ? 'SQL' : 'WIKIPAGE');
31     //define('RATING_STORAGE','WIKIPAGE');   // not fully supported yet
32
33 // leave undefined for internal, slow php engine.
34 //if (!defined('RATING_EXTERNAL'))
35 //    define('RATING_EXTERNAL',PHPWIKI_DIR . 'suggest.exe');
36
37 // Dimensions
38 if (!defined('EXPLICIT_RATINGS_DIMENSION'))
39     define('EXPLICIT_RATINGS_DIMENSION', 0);
40 if (!defined('LIST_ITEMS_DIMENSION'))
41     define('LIST_ITEMS_DIMENSION', 1);
42 if (!defined('LIST_OWNER_DIMENSION'))
43     define('LIST_OWNER_DIMENSION', 2);
44 if (!defined('LIST_TYPE_DIMENSION'))
45     define('LIST_TYPE_DIMENSION', 3);
46
47 //TODO: split class into SQL and metadata backends
48 class RatingsDb extends WikiDB {
49
50     function RatingsDb() {
51         global $request;
52         $this->_dbi = &$request->_dbi;
53         $this->_backend = &$this->_dbi->_backend;
54         $this->dimension = null;
55         if (RATING_STORAGE == 'SQL') {
56             if (isa($this->_backend, 'WikiDB_backend_PearDB')) {
57                 $this->_sqlbackend = &$this->_backend;
58                 $this->dbtype = "PearDB";
59             } elseif (isa($this->_backend, 'WikiDB_backend_ADODOB')) {
60                 $this->_sqlbackend = &$this->_backend;
61                 $this->dbtype = "ADODB";
62             } else {
63                 include_once 'lib/WikiDB/backend/ADODB.php';
64                 // It is not possible to decouple a ref from the source again. (4.3.11)
65                 // It replaced the main request backend. So we don't initialize _sqlbackend before.
66                 //$this->_sqlbackend = clone($this->_backend);
67                 $this->_sqlbackend = new WikiDB_backend_ADODB($GLOBALS['DBParams']);
68                 $this->dbtype = "ADODB";
69             }
70             $this->iter_class = "WikiDB_backend_".$this->dbtype."_generic_iter";
71
72             extract($this->_sqlbackend->_table_names);
73             if (empty($rating_tbl)) {
74                 $rating_tbl = (!empty($GLOBALS['DBParams']['prefix'])
75                                ? $GLOBALS['DBParams']['prefix'] : '') . 'rating';
76                 $this->_sqlbackend->_table_names['rating_tbl'] = $rating_tbl;
77             }
78         } else {
79             $this->iter_class = "WikiDB_Array_PageIterator";
80         }
81     }
82
83     // this is a singleton.  It ensures there is only 1 ratingsDB.
84     function & getTheRatingsDb(){
85         static $_theRatingsDb;
86
87         if (!isset($_theRatingsDb)){
88             $_theRatingsDb = new RatingsDb();
89         }
90         //echo "rating db is $_theRatingsDb";
91         return $_theRatingsDb;
92     }
93
94
95 /// *************************************************************************************
96 // FIXME
97 // from Reini Urban's RateIt plugin
98     function addRating($rating, $userid, $pagename, $dimension) {
99         if (RATING_STORAGE == 'SQL') {
100             $page = $this->_dbi->getPage($pagename);
101             $current = $page->getCurrentRevision();
102             $rateeversion = $current->getVersion();
103             $this->sql_rate($userid, $pagename, $rateeversion, $dimension, $rating);
104         } else {
105             $this->metadata_set_rating($userid, $pagename, $dimension, $rating);
106         }
107     }
108
109     function deleteRating($userid=null, $pagename=null, $dimension=null) {
110         if (is_null($dimension)) $dimension = $this->dimension;
111         if (is_null($userid))    $userid = $this->userid;
112         if (is_null($pagename))  $pagename = $this->pagename;
113         if (RATING_STORAGE == 'SQL') {
114             $this->sql_delete_rating($userid, $pagename, $dimension);
115         } else {
116             $this->metadata_set_rating($userid, $pagename, $dimension, -1);
117         }
118     }
119
120     function getRating($userid=null, $pagename=null, $dimension=null) {
121         if (RATING_STORAGE == 'SQL') {
122             $ratings_iter = $this->sql_get_rating($dimension, $userid, $pagename);
123             if ($rating = $ratings_iter->next() and isset($rating['ratingvalue'])) {
124                 return $rating['ratingvalue'];
125             } else
126                 return false;
127         } else {
128             return $this->metadata_get_rating($userid, $pagename, $dimension);
129         }
130     }
131
132     function getUsersRated($dimension=null, $orderby = null) {
133         if (is_null($dimension)) $dimension = $this->dimension;
134         //if (is_null($userid))    $userid = $this->userid;
135         //if (is_null($pagename))  $pagename = $this->pagename;
136         if (RATING_STORAGE == 'SQL') {
137             $ratings_iter = $this->sql_get_users_rated($dimension, $orderby);
138             if ($rating = $ratings_iter->next() and isset($rating['ratingvalue'])) {
139                 return $rating['ratingvalue'];
140             } else
141                 return false;
142         } else {
143             return $this->metadata_get_users_rated($dimension, $orderby);
144         }
145     }
146
147     /**
148      * Get ratings.
149      *
150      * @param dimension  The rating dimension id.
151      *                   Example: 0
152      *                   [optional]
153      *                   If this is null (or left off), the search for ratings
154      *                   is not restricted by dimension.
155      *
156      * @param rater  The page id of the rater, i.e. page doing the rating.
157      *               This is a Wiki page id, often of a user page.
158      *               Example: "DanFr"
159      *               [optional]
160      *               If this is null (or left off), the search for ratings
161      *               is not restricted by rater.
162      *               TODO: Support an array
163      *
164      * @param ratee  The page id of the ratee, i.e. page being rated.
165      *               Example: "DudeWheresMyCar"
166      *               [optional]
167      *               If this is null (or left off), the search for ratings
168      *               is not restricted by ratee.
169      *
170      * @param orderby An order-by clause with fields and (optionally) ASC
171      *                or DESC.
172      *               Example: "ratingvalue DESC"
173      *               [optional]
174      *               If this is null (or left off), the search for ratings
175      *               has no guaranteed order
176      *
177      * @param pageinfo The type of page that has its info returned (i.e.,
178      *               'pagename', 'hits', and 'pagedata') in the rows.
179      *               Example: "rater"
180      *               [optional]
181      *               If this is null (or left off), the info returned
182      *               is for the 'ratee' page (i.e., thing being rated).
183      *
184      * @return DB iterator with results
185      */
186     function get_rating($dimension=null, $rater=null, $ratee=null,
187                         $orderby = null, $pageinfo = "ratee") {
188         if (RATING_STORAGE == 'SQL') {
189             $ratings_iter = $this->sql_get_rating($dimension, $rater, $pagename);
190             if ($rating = $ratings_iter->next() and isset($rating['ratingvalue'])) {
191                 return $rating['ratingvalue'];
192             } else
193                 return false;
194         } else {
195             return $this->metadata_get_rating($rater, $pagename, $dimension);
196         }
197     }
198
199     /* UR: What is this for? NOT USED!
200        Maybe the list of users (ratees) who rated on this page.
201      */
202     function get_users_rated($dimension=null, $pagename = null, $orderby = null) {
203         if (RATING_STORAGE == 'SQL') {
204             $ratings_iter = $this->sql_get_users_rated($dimension, $pagename, $orderby);
205         // iter as userid
206         $users = array();
207         while ($rating = $ratings_iter->next()) {
208         $users[] = $rating['userid'];
209         }
210         return $users;
211         } else {
212             return $this->metadata_get_users_rated($dimension, $pagename, $orderby);
213         }
214     }
215
216     /**
217      * Like get_rating(), but return a WikiDB_PageIterator
218      * FIXME!
219      */
220     function get_rating_page($dimension=null, $rater=null, $ratee=null,
221                         $orderby = null, $pageinfo = "ratee") {
222         if (RATING_STORAGE == 'SQL') {
223             return $this->sql_get_rating($dimension, $rater, $ratee, $orderby, $pageinfo);
224         } else {
225         // empty dummy iterator
226         $pages = array();
227         return new WikiDB_Array_PageIterator($pages);
228         }
229     }
230
231     /**
232      * Delete a rating.
233      *
234      * @param rater  The page id of the rater, i.e. page doing the rating.
235      *               This is a Wiki page id, often of a user page.
236      * @param ratee  The page id of the ratee, i.e. page being rated.
237      * @param dimension  The rating dimension id.
238      *
239      * @access public
240      *
241      * @return true upon success
242      */
243     function delete_rating($rater, $ratee, $dimension) {
244         if (RATING_STORAGE == 'SQL') {
245             $this->sql_delete_rating($rater, $ratee, $dimension);
246         } else {
247             $this->metadata_set_rating($rater, $ratee, $dimension, -1);
248         }
249     }
250
251     /**
252      * Rate a page.
253      *
254      * @param rater  The page id of the rater, i.e. page doing the rating.
255      *               This is a Wiki page id, often of a user page.
256      * @param ratee  The page id of the ratee, i.e. page being rated.
257      * @param rateeversion  The version of the ratee page.
258      * @param dimension  The rating dimension id.
259      * @param rating The rating value (a float).
260      *
261      * @access public
262      *
263      * @return true upon success
264      */
265     function rate($rater, $ratee, $rateeversion, $dimension, $rating) {
266         if (RATING_STORAGE == 'SQL') {
267             $page = $this->_dbi->getPage($pagename);
268             $current = $page->getCurrentRevision();
269             $rateeversion = $current->getVersion();
270             $this->sql_rate($userid, $pagename, $rateeversion, $dimension, $rating);
271         } else {
272             $this->metadata_set_rating($userid, $pagename, $dimension, $rating);
273         }
274     }
275
276     //function getUsersRated(){}
277
278 //*******************************************************************************
279     // TODO:
280     // Use wikilens/RatingsUser.php for the php methods.
281     //
282     // Old:
283     // Currently we have to call the "suggest" CGI
284     //   http://www-users.cs.umn.edu/~karypis/suggest/
285     // until we implement a simple recommendation engine.
286     // Note that "suggest" is only free for non-profit organizations.
287     // I am currently writing a binary CGI mysuggest using suggest, which loads
288     // data from mysql.
289     function getPrediction($userid=null, $pagename=null, $dimension=null) {
290         if (is_null($dimension)) $dimension = $this->dimension;
291         if (is_null($userid))    $userid   = $this->userid;
292         if (is_null($pagename))  $pagename = $this->pagename;
293
294         if (RATING_STORAGE == 'SQL') {
295             $dbh = &$this->_sqlbackend;
296             if (isset($pagename))
297                 $page = $dbh->_get_pageid($pagename);
298             else
299                 return 0;
300             if (isset($userid))
301                 $user = $dbh->_get_pageid($userid);
302             else
303                 return 0;
304         }
305         if (defined('RATING_EXTERNAL') and RATING_EXTERNAL) {
306             // how call mysuggest.exe? as CGI or natively
307             //$rating = HTML::Raw("<!--#include virtual=".RATING_ENGINE." -->");
308             $args = "-u$user -p$page -malpha"; // --top 10
309             if (isset($dimension))
310                 $args .= " -d$dimension";
311             $rating = passthru(RATING_EXTERNAL . " $args");
312         } else {
313             $rating = $this->php_prediction($userid, $pagename, $dimension);
314         }
315         return $rating;
316     }
317
318     /**
319      * Slow item-based recommendation engine, similar to suggest RType=2.
320      * Only the SUGGEST_EstimateAlpha part
321      * Take wikilens/RatingsUser.php for the php methods.
322      */
323     function php_prediction($userid=null, $pagename=null, $dimension=null) {
324         if (is_null($dimension)) $dimension = $this->dimension;
325         if (is_null($userid))    $userid   = $this->userid;
326         if (is_null($pagename))  $pagename = $this->pagename;
327         if (empty($this->buddies)) {
328             require_once 'lib/wikilens/RatingsUser.php';
329             require_once 'lib/wikilens/Buddy.php';
330             $user = RatingsUserFactory::getUser($userid);
331             $this->buddies = getBuddies($user, $GLOBALS['request']->_dbi);
332         }
333         return $user->knn_uu_predict($pagename, $this->buddies, $dimension);
334     }
335
336     function getNumUsers($pagename=null, $dimension=null) {
337         if (is_null($dimension)) $dimension = $this->dimension;
338         if (is_null($pagename))  $pagename = $this->pagename;
339         if (RATING_STORAGE == 'SQL') {
340             $ratings_iter = $this->sql_get_rating($dimension, null, $pagename,
341                                                   null, "ratee");
342             return $ratings_iter->count();
343         } else {
344             if (!$pagename) return 0;
345             $page = $this->_dbi->getPage($pagename);
346             $data = $page->get('rating');
347             if (!empty($data[$dimension]))
348                 return count($data[$dimension]);
349             else
350                 return 0;
351         }
352     }
353
354     function getAvg($pagename=null, $dimension=null) {
355         if (is_null($dimension)) $dimension = $this->dimension;
356         if (is_null($pagename))  $pagename = $this->pagename;
357         if (RATING_STORAGE == 'SQL') {
358             $dbi = &$this->_sqlbackend;
359         if (isset($pagename) || isset($dimension)) {
360         $where = "WHERE";
361         }
362             if (isset($pagename)) {
363                 $raterid = $this->_sqlbackend->_get_pageid($pagename, true);
364                 $where .= " raterpage=$raterid";
365             }
366             if (isset($dimension)) {
367         if (isset($pagename)) $where .= " AND";
368                 $where .= " dimension=$dimension";
369             }
370             //$dbh = &$this->_dbi;
371             extract($dbi->_table_names);
372             $query = "SELECT AVG(ratingvalue) as avg"
373            . " FROM $rating_tbl r, $page_tbl p "
374            . $where
375            . " GROUP BY raterpage";
376             $result = $dbi->_dbh->query($query);
377             $iter = new $this->iter_class($this, $result);
378             $row = $iter->next();
379             return $row['avg'];
380         } else {
381             if (!$pagename) return 0;
382             $page = $this->_dbi->getPage($pagename);
383             $data = $page->get('rating');
384             if (!empty($data[$dimension]))
385                 // hash of userid => rating
386                 return array_sum(array_values($data[$dimension])) / count($data[$dimension]);
387         else
388         return 0;
389         }
390     }
391 //*******************************************************************************
392
393     /**
394      * Get ratings.
395      *
396      * @param dimension  The rating dimension id.
397      *                   Example: 0
398      *                   [optional]
399      *                   If this is null (or left off), the search for ratings
400      *                   is not restricted by dimension.
401      *
402      * @param rater  The page id of the rater, i.e. page doing the rating.
403      *               This is a Wiki page id, often of a user page.
404      *               Example: "DanFr"
405      *               [optional]
406      *               If this is null (or left off), the search for ratings
407      *               is not restricted by rater.
408      *               TODO: Support an array
409      *
410      * @param ratee  The page id of the ratee, i.e. page being rated.
411      *               Example: "DudeWheresMyCar"
412      *               [optional]
413      *               If this is null (or left off), the search for ratings
414      *               is not restricted by ratee.
415      *               TODO: Support an array
416      *
417      * @param orderby An order-by clause with fields and (optionally) ASC
418      *                or DESC.
419      *               Example: "ratingvalue DESC"
420      *               [optional]
421      *               If this is null (or left off), the search for ratings
422      *               has no guaranteed order
423      *
424      * @param pageinfo The type of page that has its info returned (i.e.,
425      *               'pagename', 'hits', and 'pagedata') in the rows.
426      *               Example: "rater"
427      *               [optional]
428      *               If this is null (or left off), the info returned
429      *               is for the 'ratee' page (i.e., thing being rated).
430      *
431      * @return DB iterator with results
432      */
433     function sql_get_rating($dimension=null, $rater=null, $ratee=null,
434                             $orderby=null, $pageinfo = "ratee") {
435         if (is_null($dimension)) $dimension = $this->dimension;
436         $result = $this->_sql_get_rating_result($dimension, $rater, $ratee, $orderby, $pageinfo);
437         return new $this->iter_class($this, $result);
438     }
439
440     function sql_get_users_rated($dimension=null, $pagename=null, $orderby=null) {
441         if (is_null($dimension)) $dimension = $this->dimension;
442         $result = $this->_sql_get_rating_result($dimension, null, $pagename, $orderby, "rater");
443         return new $this->iter_class($this, $result);
444     }
445
446     // all users who rated this page resp if null all pages.. needed?
447     function metadata_get_users_rated($dimension=null, $pagename=null, $orderby=null) {
448         if (is_null($dimension)) $dimension = $this->dimension;
449     $users = array();
450     if (!$pagename) {
451         // TODO: all pages?
452         return new WikiDB_Array_PageIterator($users);
453     }
454     $page = $this->_dbi->getPage($pagename);
455     $data = $page->get('rating');
456     if (!empty($data[$dimension])) {
457         //array($userid => (float)$rating);
458         return new WikiDB_Array_PageIterator(array_keys($data[$dimension]));
459         }
460         return new WikiDB_Array_PageIterator($users);
461     }
462
463     /**
464      * @access private
465      * @return result ressource, suitable to the iterator
466      */
467     function _sql_get_rating_result($dimension=null, $rater=null, $ratee=null,
468                                     $orderby=null, $pageinfo = "ratee") {
469         // pageinfo must be 'rater' or 'ratee'
470         if (($pageinfo != "ratee") && ($pageinfo != "rater"))
471             return;
472         $dbi = &$this->_sqlbackend;
473         if (is_null($dbi))
474             return;
475         //$dbh = &$this->_dbi;
476         extract($dbi->_table_names);
477         $where = "WHERE r." . $pageinfo . "page = p.id";
478         if (isset($dimension)) {
479             $where .= " AND dimension=$dimension";
480         }
481         if (isset($rater)) {
482             $raterid = $dbi->_get_pageid($rater, true);
483             $where .= " AND raterpage=$raterid";
484         }
485         if (isset($ratee)) {
486             if(is_array($ratee)){
487                 $where .= " AND (";
488                 for($i = 0; $i < count($ratee); $i++){
489                     $rateeid = $dbi->_get_pageid($ratee[$i], true);
490                     $where .= "rateepage=$rateeid";
491                     if($i != (count($ratee) - 1)){
492                         $where .= " OR ";
493                     }
494                 }
495                 $where .= ")";
496             } else {
497                 $rateeid = $dbi->_get_pageid($ratee, true);
498                 $where .= " AND rateepage=$rateeid";
499             }
500         }
501         $orderbyStr = "";
502         if (isset($orderby)) {
503             $orderbyStr = " ORDER BY " . $orderby;
504         }
505         if (isset($rater) or isset($ratee)) $what = '*';
506         // same as _get_users_rated_result()
507         else {
508         $what = 'DISTINCT p.pagename';
509         if ($pageinfo == 'rater')
510         $what = 'DISTINCT p.pagename as userid';
511     }
512
513         $query = "SELECT $what"
514                . " FROM $rating_tbl r, $page_tbl p "
515                . $where
516                . $orderbyStr;
517         $result = $dbi->_dbh->query($query);
518         return $result;
519     }
520
521     /**
522      * Delete a rating.
523      *
524      * @param rater  The page id of the rater, i.e. page doing the rating.
525      *               This is a Wiki page id, often of a user page.
526      * @param ratee  The page id of the ratee, i.e. page being rated.
527      * @param dimension  The rating dimension id.
528      *
529      * @access public
530      *
531      * @return true upon success
532      */
533     function sql_delete_rating($rater, $ratee, $dimension) {
534         //$dbh = &$this->_dbi;
535         $dbi = &$this->_sqlbackend;
536         extract($dbi->_table_names);
537
538         $dbi->lock();
539         $raterid = $dbi->_get_pageid($rater, true);
540         $rateeid = $dbi->_get_pageid($ratee, true);
541         $where = "WHERE raterpage=$raterid and rateepage=$rateeid";
542         if (isset($dimension)) {
543             $where .= " AND dimension=$dimension";
544         }
545         $dbi->_dbh->query("DELETE FROM $rating_tbl $where");
546         $dbi->unlock();
547         return true;
548     }
549
550     /**
551      * Rate a page.
552      *
553      * @param rater  The page id of the rater, i.e. page doing the rating.
554      *               This is a Wiki page id, often of a user page.
555      * @param ratee  The page id of the ratee, i.e. page being rated.
556      * @param rateeversion  The version of the ratee page.
557      * @param dimension  The rating dimension id.
558      * @param rating The rating value (a float).
559      *
560      * @access public
561      *
562      * @return true upon success
563      */
564     //               ($this->userid, $this->pagename, $page->version, $this->dimension, $rating);
565     function sql_rate($rater, $ratee, $rateeversion, $dimension, $rating) {
566         $dbi = &$this->_sqlbackend;
567         extract($dbi->_table_names);
568         if (empty($rating_tbl))
569             $rating_tbl = $this->_dbi->getParam('prefix') . 'rating';
570
571         $dbi->lock();
572         $raterid = $dbi->_get_pageid($rater, true);
573         $rateeid = $dbi->_get_pageid($ratee, true);
574         assert($raterid);
575         assert($rateeid);
576         //mysql optimize: REPLACE if raterpage and rateepage are keys
577         $dbi->_dbh->query("DELETE from $rating_tbl WHERE dimension=$dimension AND raterpage=$raterid AND rateepage=$rateeid");
578         $where = "WHERE raterpage='$raterid' AND rateepage='$rateeid'";
579         $insert = "INSERT INTO $rating_tbl (dimension, raterpage, rateepage, ratingvalue, rateeversion)"
580             ." VALUES ('$dimension', $raterid, $rateeid, '$rating', '$rateeversion')";
581         $dbi->_dbh->query($insert);
582
583         $dbi->unlock();
584         return true;
585     }
586
587     function metadata_get_rating($userid, $pagename, $dimension) {
588         if (!$pagename) return false;
589         $page = $this->_dbi->getPage($pagename);
590         $data = $page->get('rating');
591         if (!empty($data[$dimension][$userid]))
592             return (float)$data[$dimension][$userid];
593         else
594             return false;
595     }
596
597     function metadata_set_rating($userid, $pagename, $dimension, $rating = -1) {
598         if (!$pagename) return false;
599         $page = $this->_dbi->getPage($pagename);
600         $data = $page->get('rating');
601         if ($rating == -1)
602             unset($data[$dimension][$userid]);
603         else {
604             if (empty($data[$dimension]))
605                 $data[$dimension] = array($userid => (float)$rating);
606             else
607                 $data[$dimension][$userid] = (float)$rating;
608         }
609         $page->set('rating',$data);
610     }
611
612 }
613
614 /*
615 class RatingsDB_backend_PearDB
616 extends WikiDB_backend_PearDB {
617     function get_rating($dimension=null, $rater=null, $ratee=null,
618                         $orderby=null, $pageinfo = "ratee") {
619         $result = $this->_get_rating_result(
620                          $dimension, $rater, $ratee, $orderby, $pageinfo);
621         return new WikiDB_backend_PearDB_generic_iter($this, $result);
622     }
623
624     function get_users_rated($dimension=null, $orderby=null) {
625         $result = $this->_get_users_rated_result(
626                          $dimension, $orderby);
627         return new WikiDB_backend_PearDB_generic_iter($this, $result);
628     }
629
630     function get_rating_page($dimension=null, $rater=null, $ratee=null,
631                              $orderby=null, $pageinfo = "ratee") {
632         $result = $this->_get_rating_result(
633                          $dimension, $rater, $ratee, $orderby, $pageinfo);
634         return new WikiDB_backend_PearDB_iter($this, $result);
635     }
636
637     function _get_rating_result($dimension=null, $rater=null, $ratee=null,
638                                 $orderby=null, $pageinfo = "ratee") {
639         // pageinfo must be 'rater' or 'ratee'
640         if (($pageinfo != "ratee") && ($pageinfo != "rater"))
641             return;
642
643         $dbh = &$this->_dbh;
644         extract($this->_table_names);
645
646         $where = "WHERE r." . $pageinfo . "page = p.id";
647         if (isset($dimension)) {
648             $where .= " AND dimension=$dimension";
649         }
650         if (isset($rater)) {
651             $raterid = $this->_get_pageid($rater, true);
652             $where .= " AND raterpage=$raterid";
653         }
654         if (isset($ratee)) {
655             if(is_array($ratee)){
656                 $where .= " AND (";
657                 for($i = 0; $i < count($ratee); $i++){
658                     $rateeid = $this->_get_pageid($ratee[$i], true);
659                     $where .= "rateepage=$rateeid";
660                     if($i != (count($ratee) - 1)){
661                         $where .= " OR ";
662                     }
663                 }
664                 $where .= ")";
665             } else {
666                 $rateeid = $this->_get_pageid($ratee, true);
667                 $where .= " AND rateepage=$rateeid";
668             }
669         }
670
671         $orderbyStr = "";
672         if (isset($orderby)) {
673             $orderbyStr = " ORDER BY " . $orderby;
674         }
675
676         $query = "SELECT *"
677             . " FROM $rating_tbl r, $page_tbl p "
678             . $where
679             . $orderbyStr;
680
681         $result = $dbh->query($query);
682
683         return $result;
684     }
685
686     function _get_users_rated_result($dimension=null, $orderby=null) {
687         $dbh = &$this->_dbh;
688         extract($this->_table_names);
689
690         $where = "WHERE p.id=r.raterpage";
691         if (isset($dimension)) {
692             $where .= " AND dimension=$dimension";
693         }
694         $orderbyStr = "";
695         if (isset($orderby)) {
696             $orderbyStr = " ORDER BY " . $orderby;
697         }
698
699         $query = "SELECT DISTINCT p.pagename"
700             . " FROM $rating_tbl r, $page_tbl p "
701             . $where
702             . $orderbyStr;
703
704         $result = $dbh->query($query);
705
706         return $result;
707     }
708     function delete_rating($rater, $ratee, $dimension) {
709         $dbh = &$this->_dbh;
710         extract($this->_table_names);
711
712         $this->lock();
713         $raterid = $this->_get_pageid($rater, true);
714         $rateeid = $this->_get_pageid($ratee, true);
715
716         $dbh->query("DELETE FROM $rating_tbl WHERE raterpage=$raterid and rateepage=$rateeid and dimension=$dimension");
717         $this->unlock();
718         return true;
719     }
720
721     function rate($rater, $ratee, $rateeversion, $dimension, $rating, $isPrivate = 'no') {
722         $dbh = &$this->_dbh;
723         extract($this->_table_names);
724
725         $this->lock();
726         $raterid = $this->_get_pageid($rater, true);
727         $rateeid = $this->_get_pageid($ratee, true);
728
729         $dbh->query("DELETE FROM $rating_tbl WHERE raterpage=$raterid and rateepage=$rateeid and dimension=$dimension and isPrivate='$isPrivate'");
730         // NOTE: Leave tstamp off the insert, and MySQL automatically updates it
731         $dbh->query("INSERT INTO $rating_tbl (dimension, raterpage, rateepage, ratingvalue, rateeversion, isPrivate) VALUES ($dimension, $raterid, $rateeid, $rating, $rateeversion, '$isPrivate')");
732         $this->unlock();
733         return true;
734     }
735 }
736 */
737
738 // Local Variables:
739 // mode: php
740 // tab-width: 8
741 // c-basic-offset: 4
742 // c-hanging-comment-ender-p: nil
743 // indent-tabs-mode: nil
744 // End: