347594279729fbbe2d530fb5f97f160cf61ee989
[GitHub/WoltLab/WCF.git] /
1 <?php
2 namespace wcf\system\user\activity\point;
3 use wcf\data\like\Like;
4 use wcf\data\object\type\ObjectType;
5 use wcf\system\database\util\PreparedStatementConditionBuilder;
6 use wcf\system\user\activity\point\IUserActivityPointObjectProcessor;
7 use wcf\system\WCF;
8
9 /**
10 * Updates events for received likes.
11 *
12 * @author Tim Duesterhus
13 * @copyright 2001-2013 WoltLab GmbH
14 * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
15 * @package com.woltlab.wcf.like
16 * @subpackage system.user.activity.point
17 * @category Community Framework
18 */
19 class ReceivedLikesUserActivityPointObjectProcessor implements IUserActivityPointObjectProcessor {
20 public $limit = 500;
21 public $objectType = null;
22
23 /**
24 * Creates a new instance of ReceivedLikesUserActivityPointObjectProcessor.
25 *
26 * @param wcf\data\object\type\ObjectType $objectType
27 */
28 public function __construct(ObjectType $objectType) {
29 $this->objectType = $objectType;
30 }
31
32 /**
33 * @see wcf\system\user\activity\point\IUserActivityPointObject::countRequests();
34 */
35 public function countRequests() {
36 $sql = "SELECT COUNT(*) AS count
37 FROM wcf".WCF_N."_like
38 WHERE likeValue = ?";
39 $statement = WCF::getDB()->prepareStatement($sql);
40 $statement->execute(array(Like::LIKE));
41 $row = $statement->fetchArray();
42
43 return ceil($row['count'] / $this->limit) + 1;
44 }
45
46 /**
47 * @see wcf\system\user\activity\point\IUserActivityPointObject::updateActivityPointEvents();
48 */
49 public function updateActivityPointEvents($request) {
50 if ($request == 0) {
51 // first request
52 $sql = "DELETE FROM wcf".WCF_N."_user_activity_point_event
53 WHERE objectTypeID = ?";
54 $statement = WCF::getDB()->prepareStatement($sql);
55 $statement->execute(array($this->objectType->objectTypeID));
56 }
57 else {
58 $sql = "SELECT likeID
59 FROM wcf".WCF_N."_like
60 WHERE likeValue = ?
61 ORDER BY likeID ASC";
62 $statement = WCF::getDB()->prepareStatement($sql, $this->limit, ($this->limit * ($request - 1)));
63 $statement->execute(array(Like::LIKE));
64 $likeIDs = array();
65 while ($row = $statement->fetchArray()) {
66 $likeIDs[] = $row['likeID'];
67 }
68
69 if (empty($likeIDs)) return;
70
71 $conditionBuilder = new PreparedStatementConditionBuilder();
72 $conditionBuilder->add("objectTypeID = ?", array($this->objectType->objectTypeID));
73 $conditionBuilder->add("objectID IN (?)", array($likeIDs));
74
75 // avoid problems with duplicate keys, as likes may be created in the meantime
76 $sql = "DELETE FROM wcf".WCF_N."_user_activity_point_event
77 ".$conditionBuilder;
78 $statement = WCF::getDB()->prepareStatement($sql);
79 $statement->execute($conditionBuilder->getParameters());
80
81 $conditionBuilder = new PreparedStatementConditionBuilder();
82 $conditionBuilder->add("likeID IN (?)", array($likeIDs));
83 // use INSERT … SELECT as this makes bulk updating easier
84 $sql = "INSERT INTO wcf".WCF_N."_user_activity_point_event
85 (userID, objectTypeID, objectID, additionalData)
86 SELECT objectUserID AS userID,
87 ?,
88 likeID AS objectID,
89 ?
90 FROM wcf".WCF_N."_like
91 ".$conditionBuilder;
92 $statement = WCF::getDB()->prepareStatement($sql);
93 $statement->execute(array_merge(array($this->objectType->objectTypeID, serialize(array())), $conditionBuilder->getParameters()));
94 }
95 }
96 }