r107453 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r107452‎ | r107453 | r107454 >
Date:00:25, 28 December 2011
Author:bsitu
Status:ok
Tags:
Comment:
Disallow anonymous user to mark an item as helpful, add limit 1 to helpful user list query and make mah_type,mah_item,mah_user_id a unique key
Modified paths:
  • /trunk/extensions/MarkAsHelpful/api/ApiMarkAsHelpful.php (modified) (history)
  • /trunk/extensions/MarkAsHelpful/includes/MarkAsHelpfulItem.php (modified) (history)
  • /trunk/extensions/MarkAsHelpful/includes/MarkAsHelpfulUtil.php (modified) (history)
  • /trunk/extensions/MarkAsHelpful/sql/mark_as_helpful.sql (modified) (history)

Diff [purge]

Index: trunk/extensions/MarkAsHelpful/sql/mark_as_helpful.sql
@@ -3,13 +3,11 @@
44
55 mah_type varbinary(32) NOT NULL, -- the object type that is being marked as helpful
66 mah_item int unsigned NOT NULL, -- the object item that is being marked as helpful
7 - mah_user_id int unsigned NOT NULL, -- User ID, or zero
8 - mah_user_ip varbinary(40) NULL, -- If anonymous, user's IP address
 7+ mah_user_id int unsigned NOT NULL, -- User ID
98 mah_user_editcount int unsigned NOT NULL, -- number of edit for the user
109
1110 mah_namespace int,
1211 mah_title varchar(255) binary,
13 - mah_active tinyint unsigned NOT NULL default 1, -- is this an active 'mark as helpful'
1412
1513 -- Options and context
1614 mah_timestamp varchar(14) binary NOT NULL, -- When response was received
@@ -18,4 +16,4 @@
1917 mah_locale varchar(32) binary NULL -- The locale of the user's browser
2018 ) /*$wgDBTableOptions*/;
2119
22 -CREATE INDEX /*i*/mah_type_item_user_id_ip ON /*_*/mark_as_helpful (mah_type, mah_item, mah_user_id, mah_user_ip);
 20+CREATE UNIQUE INDEX /*i*/mah_type_item_user_id ON /*_*/mark_as_helpful (mah_type, mah_item, mah_user_id);
Index: trunk/extensions/MarkAsHelpful/includes/MarkAsHelpfulUtil.php
@@ -54,12 +54,7 @@
5555 $user = current( $helpfulUserList );
5656
5757 // show the first user 'in mark as helpful' template
58 - if ( $user['user_name'] ) {
59 - $data = wfMessage( 'mah-someone-marked-text' )->params( $user['user_name'] )->escaped();
60 - }
61 - else {
62 - $data = wfMessage( 'mah-someone-marked-text' )->params( $user['user_ip'] )->escaped();
63 - }
 58+ $data = wfMessage( 'mah-someone-marked-text' )->params( $user['user_name'] )->escaped();
6459
6560 // Add other user in user list to a hidden div, this is for future enhancement
6661
Index: trunk/extensions/MarkAsHelpful/includes/MarkAsHelpfulItem.php
@@ -10,11 +10,9 @@
1111 'mah_type' => null,
1212 'mah_item' => null,
1313 'mah_user_id' => null,
14 - 'mah_user_ip' => null,
1514 'mah_user_editcount' => null,
1615 'mah_namespace' => null,
1716 'mah_title' => null,
18 - 'mah_active' => null,
1917 'mah_timestamp' => null,
2018 'mah_system_type' => null,
2119 'mah_user_agent' => null,
@@ -65,9 +63,7 @@
6664 if ( $this->loadedFromDatabase ) {
6765 if ( $this->getProperty( 'mah_user_id' ) ) {
6866 $this->user = User::newFromId( $this->getProperty( 'mah_user_id' ) );
69 - } elseif ( $this->getProperty( 'mah_user_ip' ) ) {
70 - $this->user = User::newFromName( $this->getProperty( 'mah_user_ip' ) );
71 - }
 67+ }
7268 } else {
7369 global $wgUser;
7470
@@ -99,12 +95,11 @@
10096 }
10197
10298 if ( $wgUser->isAnon() ) {
103 - $this->setProperty( 'mah_user_ip', $wgUser->getName() );
104 - $this->setProperty( 'mah_user_editcount', 0 );
105 - } else {
106 - $this->setProperty( 'mah_user_id', $wgUser->getId() );
107 - $this->setProperty( 'mah_user_editcount', $wgUser->getEditCount() );
 99+ throw new MWMarkAsHelpFulItemPropertyException( 'User not logged in!' );
108100 }
 101+
 102+ $this->setProperty( 'mah_user_id', $wgUser->getId() );
 103+ $this->setProperty( 'mah_user_editcount', $wgUser->getEditCount() );
109104
110105 if ( isset( $params['page'] ) ) {
111106 $page = Title::newFromText( $params['page'] );
@@ -117,7 +112,6 @@
118113 }
119114 }
120115
121 - $this->setProperty( 'mah_active', '1' );
122116 $this->setProperty( 'mah_timestamp', wfTimestampNow() );
123117
124118 if ( isset( $params['system'] ) ) {
@@ -148,7 +142,7 @@
149143
150144 $searchKey = implode( ',', $searchKey );
151145
152 - $allowableSearchKey = array( 'mah_id', 'mah_item,mah_type,mah_user_id,mah_user_ip' );
 146+ $allowableSearchKey = array( 'mah_id', 'mah_item,mah_type,mah_user_id' );
153147
154148 if ( !in_array( $searchKey, $allowableSearchKey ) ) {
155149 throw new MWMarkAsHelpFulItemSearchKeyException( 'Invalid search key!' );
@@ -178,13 +172,9 @@
179173 /**
180174 * To mark an item as helpful, this function should be called after either loadFromRequest() or setProperty()
181175 * data must be validated if called from setProperty()
182 - *
183176 */
184177 public function mark() {
185 - if ( $this->userHasMarked() ) {
186 - return;
187 - }
188 -
 178+
189179 $dbw = wfGetDB( DB_MASTER );
190180
191181 $row = array();
@@ -196,8 +186,9 @@
197187 }
198188
199189 $this->property['mah_id'] = $dbw->nextSequenceValue( 'mark_as_helpful_mah_id' );
200 - $dbw->insert( 'mark_as_helpful', $row, __METHOD__ );
 190+ $dbw->insert( 'mark_as_helpful', $row, __METHOD__, array( 'IGNORE' ) );
201191 $this->setProperty( 'mah_id', $dbw->insertId() );
 192+
202193 }
203194
204195 /**
@@ -212,10 +203,11 @@
213204
214205 if ( $this->getProperty( 'mah_id' ) ) {
215206
216 - if ( !$this->getProperty( 'mah_type' ) ) {
 207+ // Attempt to load from database if not loaded yet
 208+ if ( !$this->loadedFromDatabase ) {
217209 if ( !$this->loadFromDatabase( array( 'mah_id' => $this->getProperty( 'mah_id' ) ) ) ) {
218210 return;
219 - }
 211+ }
220212 }
221213
222214 $user = $this->getUser();
@@ -244,48 +236,6 @@
245237 }
246238
247239 /**
248 - * Check if this 'mark as helpful' recrod exists already
249 - * @return bool
250 - */
251 - public function userHasMarked() {
252 - $dbr = wfGetDB( DB_SLAVE );
253 -
254 - $conds = array(
255 - 'mah_type' => $this->getProperty( 'mah_type' ),
256 - 'mah_item' => intval( $this->getProperty( 'mah_item' ) )
257 - );
258 -
259 - $user = $this->getUser();
260 -
261 - if ( $user ) {
262 - if ( $user->isAnon() ) {
263 - $conds['mah_user_ip'] = $user->getName();
264 - $conds['mah_user_id'] = 0;
265 - } else {
266 - $conds['mah_user_id'] = $user->getId();
267 - $conds['mah_user_ip'] = null;
268 - }
269 - } else {
270 - // Invalid User object, we can't allow this user to mark an item
271 - return true;
272 - }
273 -
274 - $res = $dbr->selectRow(
275 - array( 'mark_as_helpful' ),
276 - array( 'mah_id' ),
277 - $conds,
278 - __METHOD__
279 - );
280 -
281 - // user has not marked this item
282 - if ( $res === false ) {
283 - return false;
284 - } else {
285 - return true;
286 - }
287 - }
288 -
289 - /**
290240 * Get a list of all users that marked this item as helpful
291241 * @param $type string - the object type
292242 * @param $item int - the object id
@@ -299,21 +249,22 @@
300250 'mah_item' => intval( $item )
301251 );
302252
 253+ $conds[] = 'mah_user_id = user_id';
 254+
 255+ // Grab only one record for the 1st phase
303256 $res = $dbr->select(
304257 array( 'mark_as_helpful', 'user' ),
305 - array( 'mah_id', 'user_id', 'user_name', 'mah_user_ip' ),
 258+ array( 'mah_id', 'user_id', 'user_name' ),
306259 $conds,
307260 __METHOD__,
308 - array(),
309 - array( 'user' => array( 'LEFT JOIN', 'mah_user_id=user_id' ) )
 261+ array( 'LIMIT' => 1 )
310262 );
311263
312264 $list = array();
313265
314266 foreach ( $res as $val ) {
315267 $list[$val->user_id] = array( 'user_name' => $val->user_name,
316 - 'user_id' => $val->user_id,
317 - 'user_ip' => $val->mah_user_ip );
 268+ 'user_id' => $val->user_id );
318269 }
319270
320271 return $list;
Index: trunk/extensions/MarkAsHelpful/api/ApiMarkAsHelpful.php
@@ -9,8 +9,8 @@
1010 $this->dieUsageMsg( array( 'blockedtext' ) );
1111 }
1212
13 - // Disallow anonymous user to unmark an 'Mark As Helpful' item
14 - if ( $wgUser->isAnon() && $params['mahaction'] === 'unmark' ) {
 13+ // Disallow anonymous user to mark/unmark an 'Mark As Helpful' item
 14+ if ( $wgUser->isAnon() ) {
1515 $this->noPermissionError();
1616 }
1717
@@ -39,16 +39,16 @@
4040
4141 $conds = array( 'mah_type' => $params['type'],
4242 'mah_item' => $params['item'],
43 - 'mah_user_id' => $wgUser->getId(),
44 - 'mah_user_ip' => null );
 43+ 'mah_user_id' => $wgUser->getId() );
4544
4645 $status = $item->loadFromDatabase( $conds );
4746
4847 if ( $status ) {
4948 $item->unmark( $wgUser );
50 - } else {
51 - $error = wfMessage( 'mah-action-error' )->escaped();
5249 }
 50+ else {
 51+ $error = true;
 52+ }
5353 break;
5454
5555 default:
@@ -58,10 +58,9 @@
5959
6060 if ( $error === false ) {
6161 $result = array( 'result' => 'success' );
 62+ } else {
 63+ $result = array( 'result' => 'error', 'error' => 'mah-action-error' );
6264 }
63 - else {
64 - $result = array( 'result' => 'error', 'error' => $error );
65 - }
6665 $this->getResult()->addValue( null, $this->getModuleName(), $result );
6766 }
6867

Status & tagging log