PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/gforge/plugins/wiki/www/lib/wikilens/RatingsDb.php

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