Index: trunk/extensions/MarkAsHelpful/MarkAsHelpful.i18n.php |
— | — | @@ -17,6 +17,7 @@ |
18 | 18 | 'mah-you-marked-text' => 'You think this is helpful', |
19 | 19 | 'mah-someone-marked-text' => '$1 thinks this is helpful', |
20 | 20 | 'mah-undo-mark-text' => 'undo', |
| 21 | + 'mah-action-error' => 'There is an error performing this action', |
21 | 22 | ); |
22 | 23 | |
23 | 24 | /** Message documentation (Message documentation) |
— | — | @@ -29,6 +30,7 @@ |
30 | 31 | 'mah-you-marked-text' => 'Text displayed to the logged in user if they mark an item helpful', |
31 | 32 | 'mah-someone-marked-text' => 'Text displayed as to who marked this is helpful, shown if not the user who marked {$1 is the username}', |
32 | 33 | 'mah-undo-mark-text' => 'Text for the the undo mark link', |
| 34 | + 'mah-action-error' => 'Generic error message', |
33 | 35 | ); |
34 | 36 | |
35 | 37 | /** German (Deutsch) |
Index: trunk/extensions/MarkAsHelpful/includes/MarkAsHelpfulUtil.php |
— | — | @@ -33,7 +33,7 @@ |
34 | 34 | return <<<HTML |
35 | 35 | <div class="mw-mah-wrapper"> |
36 | 36 | <span class='mah-helpful-marked-state'> |
37 | | - $mahMarkedText |
| 37 | + $mahMarkedText |
38 | 38 | </span> |
39 | 39 | (<a class='markashelpful-undo'>$undoLinkText</a>) |
40 | 40 | </div> |
Index: trunk/extensions/MarkAsHelpful/includes/MarkAsHelpfulItem.php |
— | — | @@ -26,6 +26,7 @@ |
27 | 27 | |
28 | 28 | /** |
29 | 29 | * Constructor |
| 30 | + * @param $mah_id int - an id that represents a unique mark as helpful record |
30 | 31 | */ |
31 | 32 | public function __construct( $mah_id = null ) { |
32 | 33 | if ( $mah_id == intval( $mah_id ) ) { |
— | — | @@ -33,18 +34,30 @@ |
34 | 35 | } |
35 | 36 | } |
36 | 37 | |
| 38 | + /** |
| 39 | + * Getter method |
| 40 | + * @param $key string - the name of a property |
| 41 | + */ |
37 | 42 | public function getProperty( $key ) { |
38 | 43 | if( isset( $key, $this->property) ) { |
39 | 44 | return $this->property[$key]; |
40 | 45 | } |
41 | 46 | } |
42 | 47 | |
| 48 | + /** |
| 49 | + * Setter method |
| 50 | + * @param $key string - the name of the property |
| 51 | + * @param $value mixed - the valud of the property |
| 52 | + */ |
43 | 53 | public function setProperty( $key, $value ) { |
44 | 54 | if( isset( $key, $this->property) ) { |
45 | 55 | $this->property[$key] = $value; |
46 | 56 | } |
47 | 57 | } |
48 | 58 | |
| 59 | + /** |
| 60 | + * get the owner of the 'mark as helpful' item |
| 61 | + */ |
49 | 62 | public function getUser() { |
50 | 63 | if ( !$this->user ) { |
51 | 64 | if ( $this->loadedFromDatabase ) { |
— | — | @@ -63,6 +76,11 @@ |
64 | 77 | return $this->user; |
65 | 78 | } |
66 | 79 | |
| 80 | + /** |
| 81 | + * Load data into object from external data |
| 82 | + * @param $params array - an array of data to be loaded into the object |
| 83 | + * @exception MWMarkAsHelpFulItemPropertyException |
| 84 | + */ |
67 | 85 | public function loadFromRequest( $params ) { |
68 | 86 | global $wgUser, $wgMarkAsHelpfulType; |
69 | 87 | |
— | — | @@ -113,9 +131,27 @@ |
114 | 132 | |
115 | 133 | /** |
116 | 134 | * Load from database |
117 | | - * @param $conds Array: keys to load unique item from database |
| 135 | + * @param $conds Array: keys to load unique item from database, it must be one of the allowed keys |
| 136 | + * @exception MWMarkAsHelpFulItemSearchKeyException |
118 | 137 | */ |
119 | | - public function loadFromDatabase( $conds = array() ) { |
| 138 | + public function loadFromDatabase( $conds ) { |
| 139 | + |
| 140 | + $searchKey = array_keys( $conds ); |
| 141 | + |
| 142 | + $flag = sort( $searchKey ); |
| 143 | + |
| 144 | + if ( !$flag ) { |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + $searchKey = implode(',', $searchKey ); |
| 149 | + |
| 150 | + $allowableSearchKey = array ( 'mah_id', 'mah_item,mah_type,mah_user_id', 'mah_item,mah_type,mah_user_ip' ); |
| 151 | + |
| 152 | + if ( !in_array( $searchKey, $allowableSearchKey ) ) { |
| 153 | + throw new MWMarkAsHelpFulItemSearchKeyException( 'Invalid search key!' ); |
| 154 | + } |
| 155 | + |
120 | 156 | $dbr = wfGetDB( DB_SLAVE ); |
121 | 157 | |
122 | 158 | $res = $dbr->selectRow( |
— | — | @@ -131,7 +167,6 @@ |
132 | 168 | } |
133 | 169 | |
134 | 170 | $this->loadedFromDatabase = true; |
135 | | - |
136 | 171 | return true; |
137 | 172 | } else { |
138 | 173 | return false; |
— | — | @@ -139,8 +174,9 @@ |
140 | 175 | } |
141 | 176 | |
142 | 177 | /** |
143 | | - * This function should be called after either prepareDataBeforeMark() or setProperty() |
| 178 | + * To mark an item as helpful, this function should be called after either loadFromRequest() or setProperty() |
144 | 179 | * data must be validated if called from setProperty() |
| 180 | + * |
145 | 181 | */ |
146 | 182 | public function mark() { |
147 | 183 | if ( $this->userHasMarked() ) { |
— | — | @@ -162,10 +198,14 @@ |
163 | 199 | $this->setProperty( 'mah_id', $dbw->insertId() ); |
164 | 200 | } |
165 | 201 | |
| 202 | + /** |
| 203 | + * Unmark an item as helpful |
| 204 | + * @param $currentUser Object - the current user who is browsing the site |
| 205 | + */ |
166 | 206 | public function unmark( $currentUser ) { |
167 | 207 | if ( $this->getProperty( 'mah_id' ) ) { |
168 | 208 | if ( !$this->getProperty( 'mah_type' ) ) { |
169 | | - if( !$this->loadFromDatabase() ) { |
| 209 | + if( !$this->loadFromDatabase( array( 'mah_id' => $this->getProperty( 'mah_id' ) ) ) ) { |
170 | 210 | return; |
171 | 211 | } |
172 | 212 | } |
— | — | @@ -196,6 +236,10 @@ |
197 | 237 | |
198 | 238 | } |
199 | 239 | |
| 240 | + /** |
| 241 | + * Check if this 'mark as helpful' recrod exists already |
| 242 | + * @return bool |
| 243 | + */ |
200 | 244 | public function userHasMarked() { |
201 | 245 | $dbr = wfGetDB( DB_SLAVE ); |
202 | 246 | |
— | — | @@ -232,6 +276,12 @@ |
233 | 277 | } |
234 | 278 | } |
235 | 279 | |
| 280 | + /** |
| 281 | + * Get a list of all users that marked this item as helpful |
| 282 | + * @param $type string - the object type |
| 283 | + * @param $item int - the object id |
| 284 | + * @return array |
| 285 | + */ |
236 | 286 | public static function getMarkAsHelpfulList( $type, $item ) { |
237 | 287 | $dbr = wfGetDB( DB_SLAVE ); |
238 | 288 | |
— | — | @@ -262,3 +312,4 @@ |
263 | 313 | } |
264 | 314 | |
265 | 315 | class MWMarkAsHelpFulItemPropertyException extends MWException {} |
| 316 | +class MWMarkAsHelpFulItemSearchKeyException extends MWException {} |
Index: trunk/extensions/MarkAsHelpful/api/ApiMarkAsHelpful.php |
— | — | @@ -16,6 +16,8 @@ |
17 | 17 | $this->dieUsage( "You don't have permission to do that", 'permission-denied' ); |
18 | 18 | } |
19 | 19 | |
| 20 | + $error = false; |
| 21 | + |
20 | 22 | switch ( $params['mahaction'] ) { |
21 | 23 | case 'mark': |
22 | 24 | $item = new MarkAsHelpfulItem(); |
— | — | @@ -25,22 +27,26 @@ |
26 | 28 | |
27 | 29 | case 'unmark': |
28 | 30 | $item = new MarkAsHelpfulItem(); |
29 | | - |
| 31 | + |
30 | 32 | if( $wgUser->isAnon() ) { |
31 | | - $item->loadFromDatabase( array( |
| 33 | + $status = $item->loadFromDatabase( array( |
32 | 34 | 'mah_type' => $params['type'], |
33 | 35 | 'mah_item' => $params['item'], |
34 | 36 | 'mah_user_ip' => $wgUser->getName() |
35 | 37 | ) ); |
36 | 38 | } else { |
37 | | - $item->loadFromDatabase( array( |
| 39 | + $status = $item->loadFromDatabase( array( |
38 | 40 | 'mah_type' => $params['type'], |
39 | 41 | 'mah_item' => $params['item'], |
40 | 42 | 'mah_user_id' => $wgUser->getId() |
41 | 43 | ) ); |
42 | 44 | } |
43 | | - |
44 | | - $item->unmark( $wgUser ); |
| 45 | + if ( $status ) { |
| 46 | + $item->unmark( $wgUser ); |
| 47 | + } |
| 48 | + else { |
| 49 | + $error = wfMessage( 'mah-action-error' )->escaped(); |
| 50 | + } |
45 | 51 | break; |
46 | 52 | |
47 | 53 | default: |
— | — | @@ -48,7 +54,12 @@ |
49 | 55 | break; |
50 | 56 | } |
51 | 57 | |
52 | | - $result = array( 'result' => 'success' ); |
| 58 | + if ( $error === false ) { |
| 59 | + $result = array( 'result' => 'success' ); |
| 60 | + } |
| 61 | + else { |
| 62 | + $result = array( 'result' => 'error', 'error' => $error ); |
| 63 | + } |
53 | 64 | $this->getResult()->addValue( null, $this->getModuleName(), $result ); |
54 | 65 | } |
55 | 66 | |