r62746 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r62745‎ | r62746 | r62747 >
Date:19:02, 20 February 2010
Author:ashley
Status:deferred
Tags:
Comment:
SocialProfile: general cleanup to UserRelationshipClass.php + rewrote raw SQL queries to use our wrapper class. *not* very extensively tested
Modified paths:
  • /trunk/extensions/SocialProfile/UserRelationship/UserRelationshipClass.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SocialProfile/UserRelationship/UserRelationshipClass.php
@@ -3,7 +3,7 @@
44 * Functions for managing relationship data
55 */
66 class UserRelationship {
7 - /**#@+
 7+ /**#@+
88 * @private
99 */
1010 var $user_id;
@@ -24,7 +24,8 @@
2525 $user_id_to = User::idFromName( $user_to );
2626 $dbw = wfGetDB( DB_MASTER );
2727
28 - $dbw->insert( 'user_relationship_request',
 28+ $dbw->insert(
 29+ 'user_relationship_request',
2930 array(
3031 'ur_user_id_from' => $this->user_id,
3132 'ur_user_name_from' => $this->user_name,
@@ -32,21 +33,23 @@
3334 'ur_user_name_to' => $user_to,
3435 'ur_type' => $type,
3536 'ur_message' => $message,
36 - 'ur_date' => date( "Y-m-d H:i:s" )
 37+ 'ur_date' => date( 'Y-m-d H:i:s' )
3738 ), __METHOD__
3839 );
3940 $request_id = $dbw->insertId();
4041
4142 $this->incNewRequestCount( $user_id_to, $type );
4243
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+ }
4447 return $request_id;
4548 }
4649
4750 public function sendRelationshipRequestEmail( $user_id_to, $user_from, $type ) {
4851 $user = User::newFromId( $user_id_to );
4952 $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 ) ) {
5154 $request_link = SpecialPage::getTitleFor( 'ViewRelationshipRequests' );
5255 $update_profile_link = SpecialPage::getTitleFor( 'UpdateProfile' );
5356 if ( $type == 1 ) {
@@ -77,7 +80,7 @@
7881 public function sendRelationshipAcceptEmail( $user_id_to, $user_from, $type ) {
7982 $user = User::newFromId( $user_id_to );
8083 $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 ) ) {
8285 $user_link = Title::makeTitle( NS_USER, $user_from );
8386 $update_profile_link = SpecialPage::getTitleFor( 'UpdateProfile' );
8487 if ( $type == 1 ) {
@@ -140,7 +143,8 @@
141144 global $wgMemc;
142145
143146 $dbw = wfGetDB( DB_MASTER );
144 - $s = $dbw->selectRow( 'user_relationship_request',
 147+ $s = $dbw->selectRow(
 148+ 'user_relationship_request',
145149 array( 'ur_user_id_from', 'ur_user_name_from', 'ur_type' ),
146150 array( 'ur_id' => $relationship_request_id ),
147151 __METHOD__
@@ -155,26 +159,30 @@
156160 return '';
157161 }
158162
159 - $dbw->insert( 'user_relationship',
 163+ $dbw->insert(
 164+ 'user_relationship',
160165 array(
161166 'r_user_id' => $this->user_id,
162167 'r_user_name' => $this->user_name,
163168 'r_user_id_relation' => $ur_user_id_from,
164169 'r_user_name_relation' => $ur_user_name_from,
165170 '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__
168174 );
169175
170 - $dbw->insert( 'user_relationship',
 176+ $dbw->insert(
 177+ 'user_relationship',
171178 array(
172179 'r_user_id' => $ur_user_id_from,
173180 'r_user_name' => $ur_user_name_from,
174181 'r_user_id_relation' => $this->user_id,
175182 'r_user_name_relation' => $this->user_name,
176183 '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__
179187 );
180188
181189 $stats = new UserStatsTrack( $this->user_id, $this->user_name );
@@ -191,7 +199,9 @@
192200 $stats->incStatField( 'foe' );
193201 }
194202
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+ }
196206
197207 $wgMemc->delete( wfMemcKey( 'relationship', 'profile', "{$this->user_id}-{$ur_type}" ) );
198208 $wgMemc->delete( wfMemcKey( 'relationship', 'profile', "{$ur_user_id_from}-{$ur_type}" ) );
@@ -210,8 +220,16 @@
211221 }
212222 // must delete record for each user involved in relationship
213223 $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+ );
216234
217235 $wgMemc->delete( wfMemcKey( 'relationship', 'profile', "{$user1}-1" ) );
218236 $wgMemc->delete( wfMemcKey( 'relationship', 'profile', "{$user2}-1" ) );
@@ -235,12 +253,17 @@
236254 $this->decNewRequestCount( $this->user_id, $request[0]['rel_type'] );
237255
238256 $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+ );
240262 }
241263
242264 public function updateRelationshipRequestStatus( $relationship_request_id, $status ) {
243265 $dbw = wfGetDB( DB_MASTER );
244 - $dbw->update( 'user_relationship_request',
 266+ $dbw->update(
 267+ 'user_relationship_request',
245268 /* SET */array( 'ur_status' => $status ),
246269 /* WHERE */array( 'ur_id' => $relationship_request_id ),
247270 __METHOD__
@@ -249,7 +272,12 @@
250273
251274 public function verifyRelationshipRequest( $relationship_request_id ) {
252275 $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+ );
254282 if ( $s !== false ) {
255283 if ( $this->user_id == $s->ur_user_id_to ) {
256284 return true;
@@ -260,7 +288,12 @@
261289
262290 static function getUserRelationshipByID( $user1, $user2 ) {
263291 $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+ );
265298 if ( $s !== false ) {
266299 return $s->r_type;
267300 } else {
@@ -270,7 +303,16 @@
271304
272305 static function userHasRequestByID( $user1, $user2 ) {
273306 $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+ );
275317 if ( $s === false ) {
276318 return false;
277319 } else {
@@ -280,12 +322,16 @@
281323
282324 public function getRequest( $id ) {
283325 $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+ ),
286332 array( "ur_id = {$id}" ),
287333 __METHOD__
288334 );
289 - while ( $row = $dbr->fetchObject( $res ) ) {
 335+ foreach ( $res as $row ) {
290336 if ( $row->ur_type == 1 ) {
291337 $type_name = 'Friend';
292338 } else {
@@ -304,20 +350,32 @@
305351 }
306352
307353 public function getRequestList( $status, $limit = 0 ) {
308 - global $wgDBprefix;
309354 $dbr = wfGetDB( DB_MASTER );
310355
311 - $limit_sql = $limit > 0 ? " LIMIT 0,{$limit} " : '';
 356+ $options = array();
312357
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+ }
319362
 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+
320378 $requests = array();
321 - while ( $row = $dbr->fetchObject( $res ) ) {
 379+ foreach ( $res as $row ) {
322380 if ( $row->ur_type == 1 ) {
323381 $type_name = 'Friend';
324382 } else {
@@ -354,8 +412,19 @@
355413 $key = wfMemcKey( 'user_relationship', 'open_request', $rel_type, $user_id );
356414 $dbr = wfGetDB( DB_SLAVE );
357415 $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+ }
360429
361430 $wgMemc->set( $key, $request_count );
362431
@@ -366,7 +435,6 @@
367436 global $wgMemc;
368437 $key = wfMemcKey( 'user_relationship', 'open_request', $rel_type, $user_id );
369438 $data = $wgMemc->get( $key );
370 - // $wgMemc->delete( $key );
371439 if ( $data != '' ) {
372440 wfDebug( "Got open request count of $data (type={$rel_type}) for id $user_id from cache\n" );
373441 return $data;
@@ -377,7 +445,9 @@
378446 $data = self::getOpenRequestCountCache( $user_id, $rel_type );
379447
380448 if ( $data != '' ) {
381 - if ( $data == - 1 ) $data = 0;
 449+ if ( $data == - 1 ) {
 450+ $data = 0;
 451+ }
382452 $count = $data;
383453 } else {
384454 $count = self::getOpenRequestCountDB( $user_id, $rel_type );
@@ -388,29 +458,33 @@
389459 public function getRelationshipList( $type = 0, $limit = 0, $page = 0 ) {
390460 $dbr = wfGetDB( DB_SLAVE );
391461
 462+ $where = array();
 463+ $options = array();
 464+ $where['r_user_id'] = $this->user_id;
 465+ if ( $type ) {
 466+ $where['r_type'] = $type;
 467+ }
392468 if ( $limit > 0 ) {
393469 $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;
398475 }
 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+ );
399486
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 );
413487 $requests = array();
414 - while ( $row = $dbr->fetchObject( $res ) ) {
 488+ foreach ( $res as $row ) {
415489 $requests[] = array(
416490 'id' => $row->r_id,
417491 'timestamp' => ( $row->r_date ),
@@ -426,15 +500,19 @@
427501 public function getRelationshipIDs( $type ) {
428502 $dbr = wfGetDB( DB_SLAVE );
429503
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+ );
435514
436515 $rel = array();
437 - $res = $dbr->query( $sql );
438 - while ( $row = $dbr->fetchObject( $res ) ) {
 516+ foreach ( $res as $row ) {
439517 $rel[] = $row->r_user_id_relation;
440518 }
441519 return $rel;
@@ -443,11 +521,13 @@
444522 static function getRelationshipCountByUsername( $user_name ) {
445523 $dbr = wfGetDB( DB_SLAVE );
446524 $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+ );
452532 $row = $dbr->fetchObject( $res );
453533 $friend_count = 0;
454534 $foe_count = 0;

Status & tagging log