Index: trunk/extensions/SocialProfile/UserRelationship/UserRelationshipClass.php |
— | — | @@ -19,8 +19,20 @@ |
20 | 20 | $this->user_id = User::idFromName( $this->user_name ); |
21 | 21 | } |
22 | 22 | |
23 | | - public function addRelationshipRequest( $user_to, $type, $message, $email = true ) { |
24 | | - $user_id_to = User::idFromName( $user_to ); |
| 23 | + /** |
| 24 | + * Add a relationship request to the database. |
| 25 | + * |
| 26 | + * @param $user_to String: user name of the recipient of the relationship |
| 27 | + * request |
| 28 | + * @param $type Integer: 1 for friend request, 2 (or anything else than 1) |
| 29 | + * for foe request |
| 30 | + * @param $message String: user-supplied message to to the recipient; may |
| 31 | + * be empty |
| 32 | + * @param $email Boolean: send out email to the recipient of the request? |
| 33 | + * @return Integer: ID of the new relationship request |
| 34 | + */ |
| 35 | + public function addRelationshipRequest( $userTo, $type, $message, $email = true ) { |
| 36 | + $userIdTo = User::idFromName( $userTo ); |
25 | 37 | $dbw = wfGetDB( DB_MASTER ); |
26 | 38 | |
27 | 39 | $dbw->insert( |
— | — | @@ -28,124 +40,172 @@ |
29 | 41 | array( |
30 | 42 | 'ur_user_id_from' => $this->user_id, |
31 | 43 | 'ur_user_name_from' => $this->user_name, |
32 | | - 'ur_user_id_to' => $user_id_to, |
33 | | - 'ur_user_name_to' => $user_to, |
| 44 | + 'ur_user_id_to' => $userIdTo, |
| 45 | + 'ur_user_name_to' => $userTo, |
34 | 46 | 'ur_type' => $type, |
35 | 47 | 'ur_message' => $message, |
36 | 48 | 'ur_date' => date( 'Y-m-d H:i:s' ) |
37 | 49 | ), __METHOD__ |
38 | 50 | ); |
39 | | - $request_id = $dbw->insertId(); |
| 51 | + $requestId = $dbw->insertId(); |
40 | 52 | |
41 | | - $this->incNewRequestCount( $user_id_to, $type ); |
| 53 | + $this->incNewRequestCount( $userIdTo, $type ); |
42 | 54 | |
43 | 55 | if ( $email ) { |
44 | | - $this->sendRelationshipRequestEmail( $user_id_to, $this->user_name, $type ); |
| 56 | + $this->sendRelationshipRequestEmail( $userIdTo, $this->user_name, $type ); |
45 | 57 | } |
46 | | - return $request_id; |
| 58 | + |
| 59 | + return $requestId; |
47 | 60 | } |
48 | 61 | |
49 | | - public function sendRelationshipRequestEmail( $user_id_to, $user_from, $type ) { |
50 | | - $user = User::newFromId( $user_id_to ); |
| 62 | + /** |
| 63 | + * Send e-mail about a new relationship request to the user whose user ID |
| 64 | + * is $userIdTo if they have opted in for these notification e-mails. |
| 65 | + * |
| 66 | + * @param $userIdTo Integer: user ID of the recipient |
| 67 | + * @param $userFrom String: name of the user who requested the relationship |
| 68 | + * @param $type Integer: 1 for friend request, 2 (or anything else than 1) |
| 69 | + * for foe request |
| 70 | + */ |
| 71 | + public function sendRelationshipRequestEmail( $userIdTo, $userFrom, $type ) { |
| 72 | + $user = User::newFromId( $userIdTo ); |
51 | 73 | $user->loadFromDatabase(); |
52 | 74 | if ( $user->getEmail() && $user->getIntOption( 'notifyfriendrequest', 1 ) ) { |
53 | | - $request_link = SpecialPage::getTitleFor( 'ViewRelationshipRequests' ); |
54 | | - $update_profile_link = SpecialPage::getTitleFor( 'UpdateProfile' ); |
| 75 | + $requestLink = SpecialPage::getTitleFor( 'ViewRelationshipRequests' ); |
| 76 | + $updateProfileLink = SpecialPage::getTitleFor( 'UpdateProfile' ); |
| 77 | + if ( trim( $user->getRealName() ) ) { |
| 78 | + $name = $user->getRealName(); |
| 79 | + } else { |
| 80 | + $name = $user->getName(); |
| 81 | + } |
55 | 82 | if ( $type == 1 ) { |
56 | 83 | $subject = wfMsgExt( 'friend_request_subject', 'parsemag', |
57 | | - $user_from |
| 84 | + $userFrom |
58 | 85 | ); |
59 | 86 | $body = wfMsgExt( 'friend_request_body', 'parsemag', |
60 | | - ( ( trim( $user->getRealName() ) ) ? $user->getRealName() : $user->getName() ), |
61 | | - $user_from, |
62 | | - $request_link->getFullURL(), |
63 | | - $update_profile_link->getFullURL() |
| 87 | + $name, |
| 88 | + $userFrom, |
| 89 | + $requestLink->getFullURL(), |
| 90 | + $updateProfileLink->getFullURL() |
64 | 91 | ); |
65 | 92 | } else { |
66 | 93 | $subject = wfMsgExt( 'foe_request_subject', 'parsemag', |
67 | | - $user_from |
| 94 | + $userFrom |
68 | 95 | ); |
69 | 96 | $body = wfMsgExt( 'foe_request_body', 'parsemag', |
70 | | - ( ( trim( $user->getRealName() ) ) ? $user->getRealName() : $user->getName() ), |
71 | | - $user_from, |
72 | | - $request_link->getFullURL(), |
73 | | - $update_profile_link->getFullURL() |
| 97 | + $name, |
| 98 | + $userFrom, |
| 99 | + $requestLink->getFullURL(), |
| 100 | + $updateProfileLink->getFullURL() |
74 | 101 | ); |
75 | 102 | } |
76 | 103 | $user->sendMail( $subject, $body ); |
77 | 104 | } |
78 | 105 | } |
79 | 106 | |
80 | | - public function sendRelationshipAcceptEmail( $user_id_to, $user_from, $type ) { |
81 | | - $user = User::newFromId( $user_id_to ); |
| 107 | + /** |
| 108 | + * Send an e-mail to the user whose user ID is $userIdTo about a new user |
| 109 | + * relationship. |
| 110 | + * |
| 111 | + * @param $userIdTo Integer: user ID of the recipient of the e-mail |
| 112 | + * @param $userFrom String: name of the user who removed the relationship |
| 113 | + * @param $type Integer: 1 for friend, 2 (or anything else but 1) for foe |
| 114 | + */ |
| 115 | + public function sendRelationshipAcceptEmail( $userIdTo, $userFrom, $type ) { |
| 116 | + $user = User::newFromId( $userIdTo ); |
82 | 117 | $user->loadFromDatabase(); |
83 | 118 | if ( $user->getEmail() && $user->getIntOption( 'notifyfriendrequest', 1 ) ) { |
84 | | - $user_link = Title::makeTitle( NS_USER, $user_from ); |
85 | | - $update_profile_link = SpecialPage::getTitleFor( 'UpdateProfile' ); |
| 119 | + $userLink = Title::makeTitle( NS_USER, $userFrom ); |
| 120 | + $updateProfileLink = SpecialPage::getTitleFor( 'UpdateProfile' ); |
| 121 | + if ( trim( $user->getRealName() ) { |
| 122 | + $name = $user->getRealName(); |
| 123 | + } else { |
| 124 | + $name = $user->getName(); |
| 125 | + } |
86 | 126 | if ( $type == 1 ) { |
87 | 127 | $subject = wfMsgExt( 'friend_accept_subject', 'parsemag', |
88 | | - $user_from |
| 128 | + $userFrom |
89 | 129 | ); |
90 | 130 | $body = wfMsgExt( 'friend_accept_body', 'parsemag', |
91 | | - ( ( trim( $user->getRealName() ) ) ? $user->getRealName() : $user->getName() ), |
92 | | - $user_from, |
93 | | - $user_link->getFullURL(), |
94 | | - $update_profile_link->getFullURL() |
| 131 | + $name, |
| 132 | + $userFrom, |
| 133 | + $userLink->getFullURL(), |
| 134 | + $updateProfileLink->getFullURL() |
95 | 135 | ); |
96 | 136 | } else { |
97 | 137 | $subject = wfMsgExt( 'foe_accept_subject', 'parsemag', |
98 | | - $user_from |
| 138 | + $userFrom |
99 | 139 | ); |
100 | 140 | $body = wfMsgExt( 'foe_accept_body', 'parsemag', |
101 | | - ( ( trim( $user->getRealName() ) ) ? $user->getRealName() : $user->getName() ), |
102 | | - $user_from, |
103 | | - $user_link->getFullURL(), |
104 | | - $update_profile_link->getFullURL() |
| 141 | + $name, |
| 142 | + $userFrom, |
| 143 | + $userLink->getFullURL(), |
| 144 | + $updateProfileLink->getFullURL() |
105 | 145 | ); |
106 | 146 | } |
107 | 147 | $user->sendMail( $subject, $body ); |
108 | 148 | } |
109 | 149 | } |
110 | 150 | |
111 | | - public function sendRelationshipRemoveEmail( $user_id_to, $user_from, $type ) { |
112 | | - $user = User::newFromId( $user_id_to ); |
| 151 | + /** |
| 152 | + * Send an e-mail to the user whose user ID is $userIdTo about a removed |
| 153 | + * relationship. |
| 154 | + * |
| 155 | + * @param $userIdTo Integer: user ID of the recipient of the e-mail |
| 156 | + * @param $userFrom String: name of the user who removed the relationship |
| 157 | + * @param $type Integer: 1 for friend, 2 (or anything else but 1) for foe |
| 158 | + */ |
| 159 | + public function sendRelationshipRemoveEmail( $userIdTo, $userFrom, $type ) { |
| 160 | + $user = User::newFromId( $userIdTo ); |
113 | 161 | $user->loadFromDatabase(); |
114 | 162 | if ( $user->isEmailConfirmed() && $user->getIntOption( 'notifyfriendrequest', 1 ) ) { |
115 | | - $user_link = Title::makeTitle( NS_USER, $user_from ); |
116 | | - $update_profile_link = SpecialPage::getTitleFor( 'UpdateProfile' ); |
| 163 | + $userLink = Title::makeTitle( NS_USER, $userFrom ); |
| 164 | + $updateProfileLink = SpecialPage::getTitleFor( 'UpdateProfile' ); |
| 165 | + if ( trim( $user->getRealName() ) { |
| 166 | + $name = $user->getRealName(); |
| 167 | + } else { |
| 168 | + $name = $user->getName(); |
| 169 | + } |
117 | 170 | if ( $type == 1 ) { |
118 | 171 | $subject = wfMsgExt( 'friend_removed_subject', 'parsemag', |
119 | | - $user_from |
| 172 | + $userFrom |
120 | 173 | ); |
121 | 174 | $body = wfMsgExt( 'friend_removed_body', 'parsemag', |
122 | | - ( ( trim( $user->getRealName() ) ) ? $user->getRealName() : $user->getName() ), |
123 | | - $user_from, |
124 | | - $user_link->getFullURL(), |
125 | | - $update_profile_link->getFullURL() |
| 175 | + $name, |
| 176 | + $userFrom, |
| 177 | + $userLink->getFullURL(), |
| 178 | + $updateProfileLink->getFullURL() |
126 | 179 | ); |
127 | 180 | } else { |
128 | 181 | $subject = wfMsgExt( 'foe_removed_subject', 'parsemag', |
129 | | - $user_from |
| 182 | + $userFrom |
130 | 183 | ); |
131 | 184 | $body = wfMsgExt( 'foe_removed_body', 'parsemag', |
132 | | - ( ( trim( $user->getRealName() ) ) ? $user->getRealName() : $user->getName() ), |
133 | | - $user_from, |
134 | | - $user_link->getFullURL(), |
135 | | - $update_profile_link->getFullURL() |
| 185 | + $name, |
| 186 | + $userFrom, |
| 187 | + $userLink->getFullURL(), |
| 188 | + $updateProfileLink->getFullURL() |
136 | 189 | ); |
137 | 190 | } |
138 | 191 | $user->sendMail( $subject, $body ); |
139 | 192 | } |
140 | 193 | } |
141 | 194 | |
142 | | - public function addRelationship( $relationship_request_id, $email = true ) { |
| 195 | + /** |
| 196 | + * Add a new relationship to the database. |
| 197 | + * |
| 198 | + * @param $relationshipRequestId Integer: relationship request ID number |
| 199 | + * @param $email Boolean: send out email to the recipient of the request? |
| 200 | + * @return Boolean: true if successful, otherwise false |
| 201 | + */ |
| 202 | + public function addRelationship( $relationshipRequestId, $email = true ) { |
143 | 203 | global $wgMemc; |
144 | 204 | |
145 | 205 | $dbw = wfGetDB( DB_MASTER ); |
146 | 206 | $s = $dbw->selectRow( |
147 | 207 | 'user_relationship_request', |
148 | 208 | array( 'ur_user_id_from', 'ur_user_name_from', 'ur_type' ), |
149 | | - array( 'ur_id' => $relationship_request_id ), |
| 209 | + array( 'ur_id' => $relationshipRequestId ), |
150 | 210 | __METHOD__ |
151 | 211 | ); |
152 | 212 | |
— | — | @@ -202,6 +262,7 @@ |
203 | 263 | $this->sendRelationshipAcceptEmail( $ur_user_id_from, $this->user_name, $ur_type ); |
204 | 264 | } |
205 | 265 | |
| 266 | + // Purge caches |
206 | 267 | $wgMemc->delete( wfMemcKey( 'relationship', 'profile', "{$this->user_id}-{$ur_type}" ) ); |
207 | 268 | $wgMemc->delete( wfMemcKey( 'relationship', 'profile', "{$ur_user_id_from}-{$ur_type}" ) ); |
208 | 269 | |
— | — | @@ -211,12 +272,19 @@ |
212 | 273 | } |
213 | 274 | } |
214 | 275 | |
| 276 | + /** |
| 277 | + * Remove a relationship between two users and clear caches afterwards. |
| 278 | + * |
| 279 | + * @param $user1 Integer: user ID of the first user |
| 280 | + * @param $user2 Integer: user ID of the second user |
| 281 | + */ |
215 | 282 | public function removeRelationshipByUserID( $user1, $user2 ) { |
216 | 283 | global $wgUser, $wgMemc; |
217 | 284 | |
218 | 285 | if ( $user1 != $wgUser->getID() && $user2 != $wgUser->getID() ) { |
219 | 286 | return false; // only logged in user should be able to delete |
220 | 287 | } |
| 288 | + |
221 | 289 | // must delete record for each user involved in relationship |
222 | 290 | $dbw = wfGetDB( DB_MASTER ); |
223 | 291 | $dbw->delete( |
— | — | @@ -247,6 +315,11 @@ |
248 | 316 | $stats->clearCache(); |
249 | 317 | } |
250 | 318 | |
| 319 | + /** |
| 320 | + * Delete a user relationship request from the database. |
| 321 | + * |
| 322 | + * @param $id Integer: relationship request ID number |
| 323 | + */ |
251 | 324 | public function deleteRequest( $id ) { |
252 | 325 | $request = $this->getRequest( $id ); |
253 | 326 | $this->decNewRequestCount( $this->user_id, $request[0]['rel_type'] ); |
— | — | @@ -259,22 +332,33 @@ |
260 | 333 | ); |
261 | 334 | } |
262 | 335 | |
263 | | - public function updateRelationshipRequestStatus( $relationship_request_id, $status ) { |
| 336 | + /** |
| 337 | + * @param $relationshipRequestId Integer: relationship request ID number |
| 338 | + * @param $status |
| 339 | + */ |
| 340 | + public function updateRelationshipRequestStatus( $relationshipRequestId, $status ) { |
264 | 341 | $dbw = wfGetDB( DB_MASTER ); |
265 | 342 | $dbw->update( |
266 | 343 | 'user_relationship_request', |
267 | 344 | /* SET */array( 'ur_status' => $status ), |
268 | | - /* WHERE */array( 'ur_id' => $relationship_request_id ), |
| 345 | + /* WHERE */array( 'ur_id' => $relationshipRequestId ), |
269 | 346 | __METHOD__ |
270 | 347 | ); |
271 | 348 | } |
272 | 349 | |
273 | | - public function verifyRelationshipRequest( $relationship_request_id ) { |
| 350 | + /** |
| 351 | + * Make sure that there is a pending user relationship request with the |
| 352 | + * given ID. |
| 353 | + * |
| 354 | + * @param $relationshipRequestId Integer: relationship request ID number |
| 355 | + * @return bool |
| 356 | + */ |
| 357 | + public function verifyRelationshipRequest( $relationshipRequestId ) { |
274 | 358 | $dbw = wfGetDB( DB_MASTER ); |
275 | 359 | $s = $dbw->selectRow( |
276 | 360 | 'user_relationship_request', |
277 | 361 | array( 'ur_user_id_to' ), |
278 | | - array( 'ur_id' => $relationship_request_id ), |
| 362 | + array( 'ur_id' => $relationshipRequestId ), |
279 | 363 | __METHOD__ |
280 | 364 | ); |
281 | 365 | if ( $s !== false ) { |
— | — | @@ -285,6 +369,11 @@ |
286 | 370 | return false; |
287 | 371 | } |
288 | 372 | |
| 373 | + /** |
| 374 | + * @param $user1 Integer: |
| 375 | + * @param $user2 Integer: |
| 376 | + * @return Mixed: integer or boolean false |
| 377 | + */ |
289 | 378 | static function getUserRelationshipByID( $user1, $user2 ) { |
290 | 379 | $dbw = wfGetDB( DB_MASTER ); |
291 | 380 | $s = $dbw->selectRow( |
— | — | @@ -300,6 +389,11 @@ |
301 | 390 | } |
302 | 391 | } |
303 | 392 | |
| 393 | + /** |
| 394 | + * @param $user1 Integer: user ID of the recipient of the request |
| 395 | + * @param $user2 Integer: user ID of the sender of the request |
| 396 | + * @return bool |
| 397 | + */ |
304 | 398 | static function userHasRequestByID( $user1, $user2 ) { |
305 | 399 | $dbw = wfGetDB( DB_MASTER ); |
306 | 400 | $s = $dbw->selectRow( |
— | — | @@ -319,6 +413,13 @@ |
320 | 414 | } |
321 | 415 | } |
322 | 416 | |
| 417 | + /** |
| 418 | + * Get an individual user relationship request via its ID. |
| 419 | + * |
| 420 | + * @param $id Integer: relationship request ID |
| 421 | + * @return Array: array containing relationship request info, such as its |
| 422 | + * ID, type, requester, etc. |
| 423 | + */ |
323 | 424 | public function getRequest( $id ) { |
324 | 425 | $dbr = wfGetDB( DB_MASTER ); |
325 | 426 | $res = $dbr->select( |
— | — | @@ -330,24 +431,33 @@ |
331 | 432 | array( "ur_id = {$id}" ), |
332 | 433 | __METHOD__ |
333 | 434 | ); |
| 435 | + |
334 | 436 | foreach ( $res as $row ) { |
335 | 437 | if ( $row->ur_type == 1 ) { |
336 | | - $type_name = 'Friend'; |
| 438 | + $typeName = 'Friend'; |
337 | 439 | } else { |
338 | | - $type_name = 'Foe'; |
| 440 | + $typeName = 'Foe'; |
339 | 441 | } |
340 | 442 | $request[] = array( |
341 | 443 | 'id' => $row->ur_id, |
342 | 444 | 'rel_type' => $row->ur_type, |
343 | | - 'type' => $type_name, |
| 445 | + 'type' => $typeName, |
344 | 446 | 'timestamp' => ( $row->ur_date ), |
345 | 447 | 'user_id_from' => $row->ur_user_id_from, |
346 | 448 | 'user_name_from' => $row->ur_user_name_from |
347 | 449 | ); |
348 | 450 | } |
| 451 | + |
349 | 452 | return $request; |
350 | 453 | } |
351 | 454 | |
| 455 | + /** |
| 456 | + * Get the list of open relationship requests. |
| 457 | + * |
| 458 | + * @param $status Integer: |
| 459 | + * @param $limit Integer: used as the LIMIT in the SQL query |
| 460 | + * @return Array: array of open relationship requests |
| 461 | + */ |
352 | 462 | public function getRequestList( $status, $limit = 0 ) { |
353 | 463 | $dbr = wfGetDB( DB_MASTER ); |
354 | 464 | |
— | — | @@ -389,71 +499,120 @@ |
390 | 500 | 'user_name_from' => $row->ur_user_name_from |
391 | 501 | ); |
392 | 502 | } |
| 503 | + |
393 | 504 | return $requests; |
394 | 505 | } |
395 | 506 | |
396 | | - private function incNewRequestCount( $user_id, $rel_type ) { |
| 507 | + /** |
| 508 | + * Increase the amount of open relationship requests for a user. |
| 509 | + * |
| 510 | + * @param $userId Integer: user ID for whom to get the requests |
| 511 | + * @param $relType Integer: 1 for friends, 2 (or anything else but 1) for foes |
| 512 | + */ |
| 513 | + private function incNewRequestCount( $userId, $relType ) { |
397 | 514 | global $wgMemc; |
398 | | - $key = wfMemcKey( 'user_relationship', 'open_request', $rel_type, $user_id ); |
| 515 | + $key = wfMemcKey( 'user_relationship', 'open_request', $relType, $userId ); |
399 | 516 | $wgMemc->incr( $key ); |
400 | 517 | } |
401 | 518 | |
402 | | - private function decNewRequestCount( $user_id, $rel_type ) { |
| 519 | + /** |
| 520 | + * Decrease the amount of open relationship requests for a user. |
| 521 | + * |
| 522 | + * @param $userId Integer: user ID for whom to get the requests |
| 523 | + * @param $relType Integer: 1 for friends, 2 (or anything else but 1) for foes |
| 524 | + */ |
| 525 | + private function decNewRequestCount( $userId, $relType ) { |
403 | 526 | global $wgMemc; |
404 | | - $key = wfMemcKey( 'user_relationship', 'open_request', $rel_type, $user_id ); |
| 527 | + $key = wfMemcKey( 'user_relationship', 'open_request', $relType, $userId ); |
405 | 528 | $wgMemc->decr( $key ); |
406 | 529 | } |
407 | 530 | |
408 | | - static function getOpenRequestCountDB( $user_id, $rel_type ) { |
409 | | - wfDebug( "Got open request count (type={$rel_type}) for id $user_id from DB\n" ); |
| 531 | + /** |
| 532 | + * Get the amount of open user relationship requests for a user from the |
| 533 | + * database and cache it. |
| 534 | + * |
| 535 | + * @param $userId Integer: user ID for whom to get the requests |
| 536 | + * @param $relType Integer: 1 for friends, 2 (or anything else but 1) for foes |
| 537 | + * @return Integer |
| 538 | + */ |
| 539 | + static function getOpenRequestCountDB( $userId, $relType ) { |
| 540 | + global $wgMemc; |
410 | 541 | |
411 | | - global $wgMemc; |
412 | | - $key = wfMemcKey( 'user_relationship', 'open_request', $rel_type, $user_id ); |
| 542 | + wfDebug( "Got open request count (type={$relType}) for id $userId from DB\n" ); |
| 543 | + |
| 544 | + $key = wfMemcKey( 'user_relationship', 'open_request', $relType, $userId ); |
413 | 545 | $dbr = wfGetDB( DB_SLAVE ); |
414 | | - $request_count = 0; |
| 546 | + $requestCount = 0; |
| 547 | + |
415 | 548 | $s = $dbr->selectRow( |
416 | 549 | 'user_relationship_request', |
417 | 550 | array( 'COUNT(*) AS count' ), |
418 | 551 | array( |
419 | | - 'ur_user_id_to' => $user_id, |
| 552 | + 'ur_user_id_to' => $userId, |
420 | 553 | 'ur_status' => 0, |
421 | | - 'ur_type' => $rel_type |
| 554 | + 'ur_type' => $relType |
422 | 555 | ), |
423 | 556 | __METHOD__ |
424 | 557 | ); |
| 558 | + |
425 | 559 | if ( $s !== false ) { |
426 | | - $request_count = $s->count; |
| 560 | + $requestCount = $s->count; |
427 | 561 | } |
428 | 562 | |
429 | | - $wgMemc->set( $key, $request_count ); |
| 563 | + $wgMemc->set( $key, $requestCount ); |
430 | 564 | |
431 | | - return $request_count; |
| 565 | + return $requestCount; |
432 | 566 | } |
433 | 567 | |
434 | | - static function getOpenRequestCountCache( $user_id, $rel_type ) { |
| 568 | + /** |
| 569 | + * Get the amount of open user relationship requests from cache. |
| 570 | + * |
| 571 | + * @param $userId Integer: user ID for whom to get the requests |
| 572 | + * @param $relType Integer: 1 for friends, 2 (or anything else but 1) for foes |
| 573 | + * @return Integer |
| 574 | + */ |
| 575 | + static function getOpenRequestCountCache( $userId, $relType ) { |
435 | 576 | global $wgMemc; |
436 | | - $key = wfMemcKey( 'user_relationship', 'open_request', $rel_type, $user_id ); |
| 577 | + $key = wfMemcKey( 'user_relationship', 'open_request', $relType, $userId ); |
437 | 578 | $data = $wgMemc->get( $key ); |
438 | 579 | if ( $data != '' ) { |
439 | | - wfDebug( "Got open request count of $data (type={$rel_type}) for id $user_id from cache\n" ); |
| 580 | + wfDebug( "Got open request count of $data (type={$relType}) for id $userId from cache\n" ); |
440 | 581 | return $data; |
441 | 582 | } |
442 | 583 | } |
443 | 584 | |
444 | | - static function getOpenRequestCount( $user_id, $rel_type ) { |
445 | | - $data = self::getOpenRequestCountCache( $user_id, $rel_type ); |
| 585 | + /** |
| 586 | + * Get the amount of open user relationship requests; first tries cache, |
| 587 | + * and if that fails, fetches the count from the database. |
| 588 | + * |
| 589 | + * @param $userId Integer: user ID for whom to get the requests |
| 590 | + * @param $relType Integer: 1 for friends, 2 (or anything else but 1) for foes |
| 591 | + * @return Integer |
| 592 | + */ |
| 593 | + static function getOpenRequestCount( $userId, $relType ) { |
| 594 | + $data = self::getOpenRequestCountCache( $userId, $relType ); |
446 | 595 | |
447 | 596 | if ( $data != '' ) { |
448 | | - if ( $data == - 1 ) { |
| 597 | + if ( $data == -1 ) { |
449 | 598 | $data = 0; |
450 | 599 | } |
451 | 600 | $count = $data; |
452 | 601 | } else { |
453 | | - $count = self::getOpenRequestCountDB( $user_id, $rel_type ); |
| 602 | + $count = self::getOpenRequestCountDB( $userId, $relType ); |
454 | 603 | } |
| 604 | + |
455 | 605 | return $count; |
456 | 606 | } |
457 | 607 | |
| 608 | + /** |
| 609 | + * Get the relationship list for the current user. |
| 610 | + * |
| 611 | + * @param $type Integer: 1 for friends, 2 (or anything else but 1) for foes |
| 612 | + * @param $limit Integer: used as the LIMIT in the SQL query |
| 613 | + * @param $page Integer: if greater than 0, will be used to calculate the |
| 614 | + * OFFSET for the SQL query |
| 615 | + * @return Array: array of relationship information |
| 616 | + */ |
458 | 617 | public function getRelationshipList( $type = 0, $limit = 0, $page = 0 ) { |
459 | 618 | $dbr = wfGetDB( DB_SLAVE ); |
460 | 619 | |
— | — | @@ -496,6 +655,12 @@ |
497 | 656 | return $requests; |
498 | 657 | } |
499 | 658 | |
| 659 | + /** |
| 660 | + * Get the relationship IDs for the current user. |
| 661 | + * |
| 662 | + * @param $type Integer: 1 for friends, 2 (or anything else but 1) for foes |
| 663 | + * @return Array: array of relationship ID numbers |
| 664 | + */ |
500 | 665 | public function getRelationshipIDs( $type ) { |
501 | 666 | $dbr = wfGetDB( DB_SLAVE ); |
502 | 667 | |
— | — | @@ -514,28 +679,36 @@ |
515 | 680 | foreach ( $res as $row ) { |
516 | 681 | $rel[] = $row->r_user_id_relation; |
517 | 682 | } |
| 683 | + |
518 | 684 | return $rel; |
519 | 685 | } |
520 | 686 | |
521 | | - static function getRelationshipCountByUsername( $user_name ) { |
| 687 | + /** |
| 688 | + * Get the amount of friends and foes a user has from the |
| 689 | + * user_relationship_stats database table. |
| 690 | + * |
| 691 | + * @param $userName String: name of the user whose stats we're looking up |
| 692 | + * @return Array: array containing the amount of friends and foes |
| 693 | + */ |
| 694 | + static function getRelationshipCountByUsername( $userName ) { |
522 | 695 | $dbr = wfGetDB( DB_SLAVE ); |
523 | | - $user_id = User::idFromName( $user_name ); |
| 696 | + $userId = User::idFromName( $userName ); |
524 | 697 | $res = $dbr->select( |
525 | 698 | 'user_relationship_stats', |
526 | 699 | array( 'rs_friend_count', 'rs_foe_count' ), |
527 | | - array( "rs_user_id = {$user_id}" ), |
| 700 | + array( "rs_user_id = {$userId}" ), |
528 | 701 | __METHOD__, |
529 | 702 | array( 'LIMIT' => 1, 'OFFSET' => 0 ) |
530 | 703 | ); |
531 | 704 | $row = $dbr->fetchObject( $res ); |
532 | | - $friend_count = 0; |
533 | | - $foe_count = 0; |
| 705 | + $friendCount = 0; |
| 706 | + $foeCount = 0; |
534 | 707 | if ( $row ) { |
535 | | - $friend_count = $row->rs_friend_count; |
536 | | - $foe_count = $row->rs_foe_count; |
| 708 | + $friendCount = $row->rs_friend_count; |
| 709 | + $foeCount = $row->rs_foe_count; |
537 | 710 | } |
538 | | - $stats['friend_count'] = $friend_count; |
539 | | - $stats['foe_count'] = $foe_count; |
| 711 | + $stats['friend_count'] = $friendCount; |
| 712 | + $stats['foe_count'] = $foeCount; |
540 | 713 | return $stats; |
541 | 714 | } |
542 | 715 | } |