r106889 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r106888‎ | r106889 | r106890 >
Date:23:17, 20 December 2011
Author:bsitu
Status:deferred
Tags:markashelpful 
Comment:
follow up to -r106843 - added code comment, allowable search keys and error message in language file
Modified paths:
  • /trunk/extensions/MarkAsHelpful/MarkAsHelpful.i18n.php (modified) (history)
  • /trunk/extensions/MarkAsHelpful/api/ApiMarkAsHelpful.php (modified) (history)
  • /trunk/extensions/MarkAsHelpful/includes/MarkAsHelpfulItem.php (modified) (history)
  • /trunk/extensions/MarkAsHelpful/includes/MarkAsHelpfulUtil.php (modified) (history)

Diff [purge]

Index: trunk/extensions/MarkAsHelpful/MarkAsHelpful.i18n.php
@@ -17,6 +17,7 @@
1818 'mah-you-marked-text' => 'You think this is helpful',
1919 'mah-someone-marked-text' => '$1 thinks this is helpful',
2020 'mah-undo-mark-text' => 'undo',
 21+ 'mah-action-error' => 'There is an error performing this action',
2122 );
2223
2324 /** Message documentation (Message documentation)
@@ -29,6 +30,7 @@
3031 'mah-you-marked-text' => 'Text displayed to the logged in user if they mark an item helpful',
3132 'mah-someone-marked-text' => 'Text displayed as to who marked this is helpful, shown if not the user who marked {$1 is the username}',
3233 'mah-undo-mark-text' => 'Text for the the undo mark link',
 34+ 'mah-action-error' => 'Generic error message',
3335 );
3436
3537 /** German (Deutsch)
Index: trunk/extensions/MarkAsHelpful/includes/MarkAsHelpfulUtil.php
@@ -33,7 +33,7 @@
3434 return <<<HTML
3535 <div class="mw-mah-wrapper">
3636 <span class='mah-helpful-marked-state'>
37 - $mahMarkedText
 37+ $mahMarkedText
3838 </span>
3939 &nbsp;(<a class='markashelpful-undo'>$undoLinkText</a>)
4040 </div>
Index: trunk/extensions/MarkAsHelpful/includes/MarkAsHelpfulItem.php
@@ -26,6 +26,7 @@
2727
2828 /**
2929 * Constructor
 30+ * @param $mah_id int - an id that represents a unique mark as helpful record
3031 */
3132 public function __construct( $mah_id = null ) {
3233 if ( $mah_id == intval( $mah_id ) ) {
@@ -33,18 +34,30 @@
3435 }
3536 }
3637
 38+ /**
 39+ * Getter method
 40+ * @param $key string - the name of a property
 41+ */
3742 public function getProperty( $key ) {
3843 if( isset( $key, $this->property) ) {
3944 return $this->property[$key];
4045 }
4146 }
4247
 48+ /**
 49+ * Setter method
 50+ * @param $key string - the name of the property
 51+ * @param $value mixed - the valud of the property
 52+ */
4353 public function setProperty( $key, $value ) {
4454 if( isset( $key, $this->property) ) {
4555 $this->property[$key] = $value;
4656 }
4757 }
4858
 59+ /**
 60+ * get the owner of the 'mark as helpful' item
 61+ */
4962 public function getUser() {
5063 if ( !$this->user ) {
5164 if ( $this->loadedFromDatabase ) {
@@ -63,6 +76,11 @@
6477 return $this->user;
6578 }
6679
 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+ */
6785 public function loadFromRequest( $params ) {
6886 global $wgUser, $wgMarkAsHelpfulType;
6987
@@ -113,9 +131,27 @@
114132
115133 /**
116134 * 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
118137 */
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+
120156 $dbr = wfGetDB( DB_SLAVE );
121157
122158 $res = $dbr->selectRow(
@@ -131,7 +167,6 @@
132168 }
133169
134170 $this->loadedFromDatabase = true;
135 -
136171 return true;
137172 } else {
138173 return false;
@@ -139,8 +174,9 @@
140175 }
141176
142177 /**
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()
144179 * data must be validated if called from setProperty()
 180+ *
145181 */
146182 public function mark() {
147183 if ( $this->userHasMarked() ) {
@@ -162,10 +198,14 @@
163199 $this->setProperty( 'mah_id', $dbw->insertId() );
164200 }
165201
 202+ /**
 203+ * Unmark an item as helpful
 204+ * @param $currentUser Object - the current user who is browsing the site
 205+ */
166206 public function unmark( $currentUser ) {
167207 if ( $this->getProperty( 'mah_id' ) ) {
168208 if ( !$this->getProperty( 'mah_type' ) ) {
169 - if( !$this->loadFromDatabase() ) {
 209+ if( !$this->loadFromDatabase( array( 'mah_id' => $this->getProperty( 'mah_id' ) ) ) ) {
170210 return;
171211 }
172212 }
@@ -196,6 +236,10 @@
197237
198238 }
199239
 240+ /**
 241+ * Check if this 'mark as helpful' recrod exists already
 242+ * @return bool
 243+ */
200244 public function userHasMarked() {
201245 $dbr = wfGetDB( DB_SLAVE );
202246
@@ -232,6 +276,12 @@
233277 }
234278 }
235279
 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+ */
236286 public static function getMarkAsHelpfulList( $type, $item ) {
237287 $dbr = wfGetDB( DB_SLAVE );
238288
@@ -262,3 +312,4 @@
263313 }
264314
265315 class MWMarkAsHelpFulItemPropertyException extends MWException {}
 316+class MWMarkAsHelpFulItemSearchKeyException extends MWException {}
Index: trunk/extensions/MarkAsHelpful/api/ApiMarkAsHelpful.php
@@ -16,6 +16,8 @@
1717 $this->dieUsage( "You don't have permission to do that", 'permission-denied' );
1818 }
1919
 20+ $error = false;
 21+
2022 switch ( $params['mahaction'] ) {
2123 case 'mark':
2224 $item = new MarkAsHelpfulItem();
@@ -25,22 +27,26 @@
2628
2729 case 'unmark':
2830 $item = new MarkAsHelpfulItem();
29 -
 31+
3032 if( $wgUser->isAnon() ) {
31 - $item->loadFromDatabase( array(
 33+ $status = $item->loadFromDatabase( array(
3234 'mah_type' => $params['type'],
3335 'mah_item' => $params['item'],
3436 'mah_user_ip' => $wgUser->getName()
3537 ) );
3638 } else {
37 - $item->loadFromDatabase( array(
 39+ $status = $item->loadFromDatabase( array(
3840 'mah_type' => $params['type'],
3941 'mah_item' => $params['item'],
4042 'mah_user_id' => $wgUser->getId()
4143 ) );
4244 }
43 -
44 - $item->unmark( $wgUser );
 45+ if ( $status ) {
 46+ $item->unmark( $wgUser );
 47+ }
 48+ else {
 49+ $error = wfMessage( 'mah-action-error' )->escaped();
 50+ }
4551 break;
4652
4753 default:
@@ -48,7 +54,12 @@
4955 break;
5056 }
5157
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+ }
5364 $this->getResult()->addValue( null, $this->getModuleName(), $result );
5465 }
5566

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r106843Mark As Helpful APIs and backendbsitu19:30, 20 December 2011

Status & tagging log