Index: trunk/extensions/SocialProfile/UserRelationship/UserRelationshipClass.php |
— | — | @@ -3,7 +3,7 @@ |
4 | 4 | * Functions for managing relationship data |
5 | 5 | */ |
6 | 6 | class UserRelationship { |
7 | | - /**#@+ |
| 7 | + /**#@+ |
8 | 8 | * @private |
9 | 9 | */ |
10 | 10 | var $user_id; |
— | — | @@ -24,7 +24,8 @@ |
25 | 25 | $user_id_to = User::idFromName( $user_to ); |
26 | 26 | $dbw = wfGetDB( DB_MASTER ); |
27 | 27 | |
28 | | - $dbw->insert( 'user_relationship_request', |
| 28 | + $dbw->insert( |
| 29 | + 'user_relationship_request', |
29 | 30 | array( |
30 | 31 | 'ur_user_id_from' => $this->user_id, |
31 | 32 | 'ur_user_name_from' => $this->user_name, |
— | — | @@ -32,21 +33,23 @@ |
33 | 34 | 'ur_user_name_to' => $user_to, |
34 | 35 | 'ur_type' => $type, |
35 | 36 | 'ur_message' => $message, |
36 | | - 'ur_date' => date( "Y-m-d H:i:s" ) |
| 37 | + 'ur_date' => date( 'Y-m-d H:i:s' ) |
37 | 38 | ), __METHOD__ |
38 | 39 | ); |
39 | 40 | $request_id = $dbw->insertId(); |
40 | 41 | |
41 | 42 | $this->incNewRequestCount( $user_id_to, $type ); |
42 | 43 | |
43 | | - if ( $email ) $this->sendRelationshipRequestEmail( $user_id_to, $this->user_name, $type ); |
| 44 | + if ( $email ) { |
| 45 | + $this->sendRelationshipRequestEmail( $user_id_to, $this->user_name, $type ); |
| 46 | + } |
44 | 47 | return $request_id; |
45 | 48 | } |
46 | 49 | |
47 | 50 | public function sendRelationshipRequestEmail( $user_id_to, $user_from, $type ) { |
48 | 51 | $user = User::newFromId( $user_id_to ); |
49 | 52 | $user->loadFromDatabase(); |
50 | | - if ( $user->getEmail() && $user->getIntOption( 'notifyfriendrequest', 1 ) ) { // if($user->isEmailConfirmed() && $user->getIntOption("notifyfriendrequest",1)){ |
| 53 | + if ( $user->getEmail() && $user->getIntOption( 'notifyfriendrequest', 1 ) ) { |
51 | 54 | $request_link = SpecialPage::getTitleFor( 'ViewRelationshipRequests' ); |
52 | 55 | $update_profile_link = SpecialPage::getTitleFor( 'UpdateProfile' ); |
53 | 56 | if ( $type == 1 ) { |
— | — | @@ -77,7 +80,7 @@ |
78 | 81 | public function sendRelationshipAcceptEmail( $user_id_to, $user_from, $type ) { |
79 | 82 | $user = User::newFromId( $user_id_to ); |
80 | 83 | $user->loadFromDatabase(); |
81 | | - if ( $user->getEmail() && $user->getIntOption( 'notifyfriendrequest', 1 ) ) { // if($user->isEmailConfirmed() && $user->getIntOption("notifyfriendrequest",1)){ |
| 84 | + if ( $user->getEmail() && $user->getIntOption( 'notifyfriendrequest', 1 ) ) { |
82 | 85 | $user_link = Title::makeTitle( NS_USER, $user_from ); |
83 | 86 | $update_profile_link = SpecialPage::getTitleFor( 'UpdateProfile' ); |
84 | 87 | if ( $type == 1 ) { |
— | — | @@ -140,7 +143,8 @@ |
141 | 144 | global $wgMemc; |
142 | 145 | |
143 | 146 | $dbw = wfGetDB( DB_MASTER ); |
144 | | - $s = $dbw->selectRow( 'user_relationship_request', |
| 147 | + $s = $dbw->selectRow( |
| 148 | + 'user_relationship_request', |
145 | 149 | array( 'ur_user_id_from', 'ur_user_name_from', 'ur_type' ), |
146 | 150 | array( 'ur_id' => $relationship_request_id ), |
147 | 151 | __METHOD__ |
— | — | @@ -155,26 +159,30 @@ |
156 | 160 | return ''; |
157 | 161 | } |
158 | 162 | |
159 | | - $dbw->insert( 'user_relationship', |
| 163 | + $dbw->insert( |
| 164 | + 'user_relationship', |
160 | 165 | array( |
161 | 166 | 'r_user_id' => $this->user_id, |
162 | 167 | 'r_user_name' => $this->user_name, |
163 | 168 | 'r_user_id_relation' => $ur_user_id_from, |
164 | 169 | 'r_user_name_relation' => $ur_user_name_from, |
165 | 170 | 'r_type' => $ur_type, |
166 | | - 'r_date' => date( "Y-m-d H:i:s" ) |
167 | | - ), __METHOD__ |
| 171 | + 'r_date' => date( 'Y-m-d H:i:s' ) |
| 172 | + ), |
| 173 | + __METHOD__ |
168 | 174 | ); |
169 | 175 | |
170 | | - $dbw->insert( 'user_relationship', |
| 176 | + $dbw->insert( |
| 177 | + 'user_relationship', |
171 | 178 | array( |
172 | 179 | 'r_user_id' => $ur_user_id_from, |
173 | 180 | 'r_user_name' => $ur_user_name_from, |
174 | 181 | 'r_user_id_relation' => $this->user_id, |
175 | 182 | 'r_user_name_relation' => $this->user_name, |
176 | 183 | 'r_type' => $ur_type, |
177 | | - 'r_date' => date( "Y-m-d H:i:s" ) |
178 | | - ), __METHOD__ |
| 184 | + 'r_date' => date( 'Y-m-d H:i:s' ) |
| 185 | + ), |
| 186 | + __METHOD__ |
179 | 187 | ); |
180 | 188 | |
181 | 189 | $stats = new UserStatsTrack( $this->user_id, $this->user_name ); |
— | — | @@ -191,7 +199,9 @@ |
192 | 200 | $stats->incStatField( 'foe' ); |
193 | 201 | } |
194 | 202 | |
195 | | - if ( $email ) $this->sendRelationshipAcceptEmail( $ur_user_id_from, $this->user_name, $ur_type ); |
| 203 | + if ( $email ) { |
| 204 | + $this->sendRelationshipAcceptEmail( $ur_user_id_from, $this->user_name, $ur_type ); |
| 205 | + } |
196 | 206 | |
197 | 207 | $wgMemc->delete( wfMemcKey( 'relationship', 'profile', "{$this->user_id}-{$ur_type}" ) ); |
198 | 208 | $wgMemc->delete( wfMemcKey( 'relationship', 'profile', "{$ur_user_id_from}-{$ur_type}" ) ); |
— | — | @@ -210,8 +220,16 @@ |
211 | 221 | } |
212 | 222 | // must delete record for each user involved in relationship |
213 | 223 | $dbw = wfGetDB( DB_MASTER ); |
214 | | - $dbw->delete( 'user_relationship', array( 'r_user_id' => $user1, 'r_user_id_relation' => $user2 ), __METHOD__ ); |
215 | | - $dbw->delete( 'user_relationship', array( 'r_user_id' => $user2, 'r_user_id_relation' => $user1 ), __METHOD__ ); |
| 224 | + $dbw->delete( |
| 225 | + 'user_relationship', |
| 226 | + array( 'r_user_id' => $user1, 'r_user_id_relation' => $user2 ), |
| 227 | + __METHOD__ |
| 228 | + ); |
| 229 | + $dbw->delete( |
| 230 | + 'user_relationship', |
| 231 | + array( 'r_user_id' => $user2, 'r_user_id_relation' => $user1 ), |
| 232 | + __METHOD__ |
| 233 | + ); |
216 | 234 | |
217 | 235 | $wgMemc->delete( wfMemcKey( 'relationship', 'profile', "{$user1}-1" ) ); |
218 | 236 | $wgMemc->delete( wfMemcKey( 'relationship', 'profile', "{$user2}-1" ) ); |
— | — | @@ -235,12 +253,17 @@ |
236 | 254 | $this->decNewRequestCount( $this->user_id, $request[0]['rel_type'] ); |
237 | 255 | |
238 | 256 | $dbw = wfGetDB( DB_MASTER ); |
239 | | - $dbw->delete( 'user_relationship_request', array( 'ur_id' => $id ), __METHOD__ ); |
| 257 | + $dbw->delete( |
| 258 | + 'user_relationship_request', |
| 259 | + array( 'ur_id' => $id ), |
| 260 | + __METHOD__ |
| 261 | + ); |
240 | 262 | } |
241 | 263 | |
242 | 264 | public function updateRelationshipRequestStatus( $relationship_request_id, $status ) { |
243 | 265 | $dbw = wfGetDB( DB_MASTER ); |
244 | | - $dbw->update( 'user_relationship_request', |
| 266 | + $dbw->update( |
| 267 | + 'user_relationship_request', |
245 | 268 | /* SET */array( 'ur_status' => $status ), |
246 | 269 | /* WHERE */array( 'ur_id' => $relationship_request_id ), |
247 | 270 | __METHOD__ |
— | — | @@ -249,7 +272,12 @@ |
250 | 273 | |
251 | 274 | public function verifyRelationshipRequest( $relationship_request_id ) { |
252 | 275 | $dbw = wfGetDB( DB_MASTER ); |
253 | | - $s = $dbw->selectRow( 'user_relationship_request', array( 'ur_user_id_to' ), array( 'ur_id' => $relationship_request_id ), __METHOD__ ); |
| 276 | + $s = $dbw->selectRow( |
| 277 | + 'user_relationship_request', |
| 278 | + array( 'ur_user_id_to' ), |
| 279 | + array( 'ur_id' => $relationship_request_id ), |
| 280 | + __METHOD__ |
| 281 | + ); |
254 | 282 | if ( $s !== false ) { |
255 | 283 | if ( $this->user_id == $s->ur_user_id_to ) { |
256 | 284 | return true; |
— | — | @@ -260,7 +288,12 @@ |
261 | 289 | |
262 | 290 | static function getUserRelationshipByID( $user1, $user2 ) { |
263 | 291 | $dbw = wfGetDB( DB_MASTER ); |
264 | | - $s = $dbw->selectRow( 'user_relationship', array( 'r_type' ), array( 'r_user_id' => $user1, 'r_user_id_relation' => $user2 ), __METHOD__ ); |
| 292 | + $s = $dbw->selectRow( |
| 293 | + 'user_relationship', |
| 294 | + array( 'r_type' ), |
| 295 | + array( 'r_user_id' => $user1, 'r_user_id_relation' => $user2 ), |
| 296 | + __METHOD__ |
| 297 | + ); |
265 | 298 | if ( $s !== false ) { |
266 | 299 | return $s->r_type; |
267 | 300 | } else { |
— | — | @@ -270,7 +303,16 @@ |
271 | 304 | |
272 | 305 | static function userHasRequestByID( $user1, $user2 ) { |
273 | 306 | $dbw = wfGetDB( DB_MASTER ); |
274 | | - $s = $dbw->selectRow( 'user_relationship_request', array( 'ur_type' ), array( 'ur_user_id_to' => $user1, 'ur_user_id_from' => $user2, 'ur_status' => 0 ), __METHOD__ ); |
| 307 | + $s = $dbw->selectRow( |
| 308 | + 'user_relationship_request', |
| 309 | + array( 'ur_type' ), |
| 310 | + array( |
| 311 | + 'ur_user_id_to' => $user1, |
| 312 | + 'ur_user_id_from' => $user2, |
| 313 | + 'ur_status' => 0 |
| 314 | + ), |
| 315 | + __METHOD__ |
| 316 | + ); |
275 | 317 | if ( $s === false ) { |
276 | 318 | return false; |
277 | 319 | } else { |
— | — | @@ -280,12 +322,16 @@ |
281 | 323 | |
282 | 324 | public function getRequest( $id ) { |
283 | 325 | $dbr = wfGetDB( DB_MASTER ); |
284 | | - $res = $dbr->select( 'user_relationship_request', |
285 | | - array( 'ur_id', 'ur_user_id_from', 'ur_user_name_from', 'ur_type', 'ur_message', 'ur_date' ), |
| 326 | + $res = $dbr->select( |
| 327 | + 'user_relationship_request', |
| 328 | + array( |
| 329 | + 'ur_id', 'ur_user_id_from', 'ur_user_name_from', 'ur_type', |
| 330 | + 'ur_message', 'ur_date' |
| 331 | + ), |
286 | 332 | array( "ur_id = {$id}" ), |
287 | 333 | __METHOD__ |
288 | 334 | ); |
289 | | - while ( $row = $dbr->fetchObject( $res ) ) { |
| 335 | + foreach ( $res as $row ) { |
290 | 336 | if ( $row->ur_type == 1 ) { |
291 | 337 | $type_name = 'Friend'; |
292 | 338 | } else { |
— | — | @@ -304,20 +350,32 @@ |
305 | 351 | } |
306 | 352 | |
307 | 353 | public function getRequestList( $status, $limit = 0 ) { |
308 | | - global $wgDBprefix; |
309 | 354 | $dbr = wfGetDB( DB_MASTER ); |
310 | 355 | |
311 | | - $limit_sql = $limit > 0 ? " LIMIT 0,{$limit} " : ''; |
| 356 | + $options = array(); |
312 | 357 | |
313 | | - $sql = "SELECT ur_id, ur_user_id_from, ur_user_name_from, ur_type, ur_message, ur_date |
314 | | - FROM " . $wgDBprefix . "user_relationship_request |
315 | | - WHERE ur_user_id_to = {$this->user_id} AND ur_status = {$status} |
316 | | - {$limit_sql} |
317 | | - ORDER BY ur_id DESC"; |
318 | | - $res = $dbr->query( $sql ); |
| 358 | + if ( $limit > 0 ) { |
| 359 | + $options['OFFSET'] = 0; |
| 360 | + $options['LIMIT'] = $limit; |
| 361 | + } |
319 | 362 | |
| 363 | + $options['ORDER BY'] = 'ur_id DESC'; |
| 364 | + $res = $dbr->select( |
| 365 | + 'user_relationship_request', |
| 366 | + array( |
| 367 | + 'ur_id', 'ur_user_id_from', 'ur_user_name_from', 'ur_type', |
| 368 | + 'ur_message', 'ur_date' |
| 369 | + ), |
| 370 | + array( |
| 371 | + "ur_user_id_to = {$this->user_id}", |
| 372 | + "ur_status = {$status}" |
| 373 | + ), |
| 374 | + __METHOD__, |
| 375 | + $options |
| 376 | + ); |
| 377 | + |
320 | 378 | $requests = array(); |
321 | | - while ( $row = $dbr->fetchObject( $res ) ) { |
| 379 | + foreach ( $res as $row ) { |
322 | 380 | if ( $row->ur_type == 1 ) { |
323 | 381 | $type_name = 'Friend'; |
324 | 382 | } else { |
— | — | @@ -354,8 +412,19 @@ |
355 | 413 | $key = wfMemcKey( 'user_relationship', 'open_request', $rel_type, $user_id ); |
356 | 414 | $dbr = wfGetDB( DB_SLAVE ); |
357 | 415 | $request_count = 0; |
358 | | - $s = $dbr->selectRow( 'user_relationship_request', array( 'count(*) AS count' ), array( 'ur_user_id_to' => $user_id, 'ur_status' => 0, 'ur_type' => $rel_type ), __METHOD__ ); |
359 | | - if ( $s !== false ) $request_count = $s->count; |
| 416 | + $s = $dbr->selectRow( |
| 417 | + 'user_relationship_request', |
| 418 | + array( 'COUNT(*) AS count' ), |
| 419 | + array( |
| 420 | + 'ur_user_id_to' => $user_id, |
| 421 | + 'ur_status' => 0, |
| 422 | + 'ur_type' => $rel_type |
| 423 | + ), |
| 424 | + __METHOD__ |
| 425 | + ); |
| 426 | + if ( $s !== false ) { |
| 427 | + $request_count = $s->count; |
| 428 | + } |
360 | 429 | |
361 | 430 | $wgMemc->set( $key, $request_count ); |
362 | 431 | |
— | — | @@ -366,7 +435,6 @@ |
367 | 436 | global $wgMemc; |
368 | 437 | $key = wfMemcKey( 'user_relationship', 'open_request', $rel_type, $user_id ); |
369 | 438 | $data = $wgMemc->get( $key ); |
370 | | - // $wgMemc->delete( $key ); |
371 | 439 | if ( $data != '' ) { |
372 | 440 | wfDebug( "Got open request count of $data (type={$rel_type}) for id $user_id from cache\n" ); |
373 | 441 | return $data; |
— | — | @@ -377,7 +445,9 @@ |
378 | 446 | $data = self::getOpenRequestCountCache( $user_id, $rel_type ); |
379 | 447 | |
380 | 448 | if ( $data != '' ) { |
381 | | - if ( $data == - 1 ) $data = 0; |
| 449 | + if ( $data == - 1 ) { |
| 450 | + $data = 0; |
| 451 | + } |
382 | 452 | $count = $data; |
383 | 453 | } else { |
384 | 454 | $count = self::getOpenRequestCountDB( $user_id, $rel_type ); |
— | — | @@ -388,29 +458,33 @@ |
389 | 459 | public function getRelationshipList( $type = 0, $limit = 0, $page = 0 ) { |
390 | 460 | $dbr = wfGetDB( DB_SLAVE ); |
391 | 461 | |
| 462 | + $where = array(); |
| 463 | + $options = array(); |
| 464 | + $where['r_user_id'] = $this->user_id; |
| 465 | + if ( $type ) { |
| 466 | + $where['r_type'] = $type; |
| 467 | + } |
392 | 468 | if ( $limit > 0 ) { |
393 | 469 | $limitvalue = 0; |
394 | | - if ( $page ) $limitvalue = $page * $limit - ( $limit ); |
395 | | - $limit_sql = " LIMIT {$limitvalue},{$limit} "; |
396 | | - } else { |
397 | | - $limit_sql = ''; |
| 470 | + if ( $page ) { |
| 471 | + $limitvalue = $page * $limit - ( $limit ); |
| 472 | + } |
| 473 | + $options['LIMIT'] = $limit; |
| 474 | + $options['OFFSET'] = $limitvalue; |
398 | 475 | } |
| 476 | + $res = $dbr->select( |
| 477 | + 'user_relationship', |
| 478 | + array( |
| 479 | + 'r_id', 'r_user_id_relation', 'r_user_name_relation', 'r_date', |
| 480 | + 'r_type' |
| 481 | + ), |
| 482 | + $where, |
| 483 | + __METHOD__, |
| 484 | + $options |
| 485 | + ); |
399 | 486 | |
400 | | - if ( $type ) { |
401 | | - $type_sql = " AND r_type = {$type} "; |
402 | | - } else { |
403 | | - $type_sql = ''; |
404 | | - } |
405 | | - |
406 | | - $sql = "SELECT r_id, r_user_id_relation, r_user_name_relation, r_date, r_type |
407 | | - FROM {$dbr->tableName( 'user_relationship' )} |
408 | | - WHERE r_user_id = {$this->user_id} $type_sql |
409 | | - ORDER BY r_user_name_relation |
410 | | - {$limit_sql}"; |
411 | | - |
412 | | - $res = $dbr->query( $sql ); |
413 | 487 | $requests = array(); |
414 | | - while ( $row = $dbr->fetchObject( $res ) ) { |
| 488 | + foreach ( $res as $row ) { |
415 | 489 | $requests[] = array( |
416 | 490 | 'id' => $row->r_id, |
417 | 491 | 'timestamp' => ( $row->r_date ), |
— | — | @@ -426,15 +500,19 @@ |
427 | 501 | public function getRelationshipIDs( $type ) { |
428 | 502 | $dbr = wfGetDB( DB_SLAVE ); |
429 | 503 | |
430 | | - $sql = "SELECT r_id, r_user_id_relation, r_user_name_relation, r_date |
431 | | - FROM {$dbr->tableName( 'user_relationship' )} |
432 | | - WHERE r_user_id = {$this->user_id} AND r_type = {$type} |
433 | | - ORDER BY r_user_name_relation |
434 | | - {$limit_sql}"; |
| 504 | + $res = $dbr->select( |
| 505 | + 'user_relationship', |
| 506 | + array( |
| 507 | + 'r_id', 'r_user_id_relation', |
| 508 | + 'r_user_name_relation', 'r_date' |
| 509 | + ), |
| 510 | + array( "r_user_id = {$this->user_id}", "r_type = {$type}" ), |
| 511 | + __METHOD__, |
| 512 | + array( 'ORDER BY' => 'r_user_name_relation' ) |
| 513 | + ); |
435 | 514 | |
436 | 515 | $rel = array(); |
437 | | - $res = $dbr->query( $sql ); |
438 | | - while ( $row = $dbr->fetchObject( $res ) ) { |
| 516 | + foreach ( $res as $row ) { |
439 | 517 | $rel[] = $row->r_user_id_relation; |
440 | 518 | } |
441 | 519 | return $rel; |
— | — | @@ -443,11 +521,13 @@ |
444 | 522 | static function getRelationshipCountByUsername( $user_name ) { |
445 | 523 | $dbr = wfGetDB( DB_SLAVE ); |
446 | 524 | $user_id = User::idFromName( $user_name ); |
447 | | - $sql = "SELECT rs_friend_count, rs_foe_count |
448 | | - FROM {$dbr->tableName( 'user_relationship_stats' )} |
449 | | - WHERE rs_user_id = {$user_id} |
450 | | - LIMIT 0,1"; |
451 | | - $res = $dbr->query( $sql ); |
| 525 | + $res = $dbr->select( |
| 526 | + 'user_relationship_stats', |
| 527 | + array( 'rs_friend_count', 'rs_foe_count' ), |
| 528 | + array( "rs_user_id = {$user_id}" ), |
| 529 | + __METHOD__, |
| 530 | + array( 'LIMIT' => 1, 'OFFSET' => 0 ) |
| 531 | + ); |
452 | 532 | $row = $dbr->fetchObject( $res ); |
453 | 533 | $friend_count = 0; |
454 | 534 | $foe_count = 0; |