Index: trunk/extensions/MarkAsHelpful/sql/mark_as_helpful.sql |
— | — | @@ -3,13 +3,11 @@ |
4 | 4 | |
5 | 5 | mah_type varbinary(32) NOT NULL, -- the object type that is being marked as helpful |
6 | 6 | 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 |
9 | 8 | mah_user_editcount int unsigned NOT NULL, -- number of edit for the user |
10 | 9 | |
11 | 10 | mah_namespace int, |
12 | 11 | mah_title varchar(255) binary, |
13 | | - mah_active tinyint unsigned NOT NULL default 1, -- is this an active 'mark as helpful' |
14 | 12 | |
15 | 13 | -- Options and context |
16 | 14 | mah_timestamp varchar(14) binary NOT NULL, -- When response was received |
— | — | @@ -18,4 +16,4 @@ |
19 | 17 | mah_locale varchar(32) binary NULL -- The locale of the user's browser |
20 | 18 | ) /*$wgDBTableOptions*/; |
21 | 19 | |
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 @@ |
55 | 55 | $user = current( $helpfulUserList ); |
56 | 56 | |
57 | 57 | // 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(); |
64 | 59 | |
65 | 60 | // Add other user in user list to a hidden div, this is for future enhancement |
66 | 61 | |
Index: trunk/extensions/MarkAsHelpful/includes/MarkAsHelpfulItem.php |
— | — | @@ -10,11 +10,9 @@ |
11 | 11 | 'mah_type' => null, |
12 | 12 | 'mah_item' => null, |
13 | 13 | 'mah_user_id' => null, |
14 | | - 'mah_user_ip' => null, |
15 | 14 | 'mah_user_editcount' => null, |
16 | 15 | 'mah_namespace' => null, |
17 | 16 | 'mah_title' => null, |
18 | | - 'mah_active' => null, |
19 | 17 | 'mah_timestamp' => null, |
20 | 18 | 'mah_system_type' => null, |
21 | 19 | 'mah_user_agent' => null, |
— | — | @@ -65,9 +63,7 @@ |
66 | 64 | if ( $this->loadedFromDatabase ) { |
67 | 65 | if ( $this->getProperty( 'mah_user_id' ) ) { |
68 | 66 | $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 | + } |
72 | 68 | } else { |
73 | 69 | global $wgUser; |
74 | 70 | |
— | — | @@ -99,12 +95,11 @@ |
100 | 96 | } |
101 | 97 | |
102 | 98 | 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!' ); |
108 | 100 | } |
| 101 | + |
| 102 | + $this->setProperty( 'mah_user_id', $wgUser->getId() ); |
| 103 | + $this->setProperty( 'mah_user_editcount', $wgUser->getEditCount() ); |
109 | 104 | |
110 | 105 | if ( isset( $params['page'] ) ) { |
111 | 106 | $page = Title::newFromText( $params['page'] ); |
— | — | @@ -117,7 +112,6 @@ |
118 | 113 | } |
119 | 114 | } |
120 | 115 | |
121 | | - $this->setProperty( 'mah_active', '1' ); |
122 | 116 | $this->setProperty( 'mah_timestamp', wfTimestampNow() ); |
123 | 117 | |
124 | 118 | if ( isset( $params['system'] ) ) { |
— | — | @@ -148,7 +142,7 @@ |
149 | 143 | |
150 | 144 | $searchKey = implode( ',', $searchKey ); |
151 | 145 | |
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' ); |
153 | 147 | |
154 | 148 | if ( !in_array( $searchKey, $allowableSearchKey ) ) { |
155 | 149 | throw new MWMarkAsHelpFulItemSearchKeyException( 'Invalid search key!' ); |
— | — | @@ -178,13 +172,9 @@ |
179 | 173 | /** |
180 | 174 | * To mark an item as helpful, this function should be called after either loadFromRequest() or setProperty() |
181 | 175 | * data must be validated if called from setProperty() |
182 | | - * |
183 | 176 | */ |
184 | 177 | public function mark() { |
185 | | - if ( $this->userHasMarked() ) { |
186 | | - return; |
187 | | - } |
188 | | - |
| 178 | + |
189 | 179 | $dbw = wfGetDB( DB_MASTER ); |
190 | 180 | |
191 | 181 | $row = array(); |
— | — | @@ -196,8 +186,9 @@ |
197 | 187 | } |
198 | 188 | |
199 | 189 | $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' ) ); |
201 | 191 | $this->setProperty( 'mah_id', $dbw->insertId() ); |
| 192 | + |
202 | 193 | } |
203 | 194 | |
204 | 195 | /** |
— | — | @@ -212,10 +203,11 @@ |
213 | 204 | |
214 | 205 | if ( $this->getProperty( 'mah_id' ) ) { |
215 | 206 | |
216 | | - if ( !$this->getProperty( 'mah_type' ) ) { |
| 207 | + // Attempt to load from database if not loaded yet |
| 208 | + if ( !$this->loadedFromDatabase ) { |
217 | 209 | if ( !$this->loadFromDatabase( array( 'mah_id' => $this->getProperty( 'mah_id' ) ) ) ) { |
218 | 210 | return; |
219 | | - } |
| 211 | + } |
220 | 212 | } |
221 | 213 | |
222 | 214 | $user = $this->getUser(); |
— | — | @@ -244,48 +236,6 @@ |
245 | 237 | } |
246 | 238 | |
247 | 239 | /** |
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 | | - /** |
290 | 240 | * Get a list of all users that marked this item as helpful |
291 | 241 | * @param $type string - the object type |
292 | 242 | * @param $item int - the object id |
— | — | @@ -299,21 +249,22 @@ |
300 | 250 | 'mah_item' => intval( $item ) |
301 | 251 | ); |
302 | 252 | |
| 253 | + $conds[] = 'mah_user_id = user_id'; |
| 254 | + |
| 255 | + // Grab only one record for the 1st phase |
303 | 256 | $res = $dbr->select( |
304 | 257 | array( 'mark_as_helpful', 'user' ), |
305 | | - array( 'mah_id', 'user_id', 'user_name', 'mah_user_ip' ), |
| 258 | + array( 'mah_id', 'user_id', 'user_name' ), |
306 | 259 | $conds, |
307 | 260 | __METHOD__, |
308 | | - array(), |
309 | | - array( 'user' => array( 'LEFT JOIN', 'mah_user_id=user_id' ) ) |
| 261 | + array( 'LIMIT' => 1 ) |
310 | 262 | ); |
311 | 263 | |
312 | 264 | $list = array(); |
313 | 265 | |
314 | 266 | foreach ( $res as $val ) { |
315 | 267 | $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 ); |
318 | 269 | } |
319 | 270 | |
320 | 271 | return $list; |
Index: trunk/extensions/MarkAsHelpful/api/ApiMarkAsHelpful.php |
— | — | @@ -9,8 +9,8 @@ |
10 | 10 | $this->dieUsageMsg( array( 'blockedtext' ) ); |
11 | 11 | } |
12 | 12 | |
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() ) { |
15 | 15 | $this->noPermissionError(); |
16 | 16 | } |
17 | 17 | |
— | — | @@ -39,16 +39,16 @@ |
40 | 40 | |
41 | 41 | $conds = array( 'mah_type' => $params['type'], |
42 | 42 | 'mah_item' => $params['item'], |
43 | | - 'mah_user_id' => $wgUser->getId(), |
44 | | - 'mah_user_ip' => null ); |
| 43 | + 'mah_user_id' => $wgUser->getId() ); |
45 | 44 | |
46 | 45 | $status = $item->loadFromDatabase( $conds ); |
47 | 46 | |
48 | 47 | if ( $status ) { |
49 | 48 | $item->unmark( $wgUser ); |
50 | | - } else { |
51 | | - $error = wfMessage( 'mah-action-error' )->escaped(); |
52 | 49 | } |
| 50 | + else { |
| 51 | + $error = true; |
| 52 | + } |
53 | 53 | break; |
54 | 54 | |
55 | 55 | default: |
— | — | @@ -58,10 +58,9 @@ |
59 | 59 | |
60 | 60 | if ( $error === false ) { |
61 | 61 | $result = array( 'result' => 'success' ); |
| 62 | + } else { |
| 63 | + $result = array( 'result' => 'error', 'error' => 'mah-action-error' ); |
62 | 64 | } |
63 | | - else { |
64 | | - $result = array( 'result' => 'error', 'error' => $error ); |
65 | | - } |
66 | 65 | $this->getResult()->addValue( null, $this->getModuleName(), $result ); |
67 | 66 | } |
68 | 67 | |