r90790 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r90789‎ | r90790 | r90791 >
Date:19:36, 25 June 2011
Author:ashley
Status:deferred
Tags:
Comment:
SocialProfile: in UserGifts/SpecialGiftManager.php:
*coding style tweaks
*wrap some vars in intval() for sanity's sake
*add/tweak comments
*use $this->getTitle() to build the URLs instead of hacky DIY stuff; we require MW 1.16 anyway, so it should be perfectly safe
Modified paths:
  • /trunk/extensions/SocialProfile/UserGifts/SpecialGiftManager.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SocialProfile/UserGifts/SpecialGiftManager.php
@@ -15,35 +15,61 @@
1616 * @param $par Mixed: parameter passed to the page or null
1717 */
1818 public function execute( $par ) {
19 - global $wgUser, $wgOut, $wgRequest, $wgScriptPath, $wgUserGiftsScripts;
 19+ global $wgUser, $wgOut, $wgRequest, $wgUserGiftsScripts;
2020
2121 $wgOut->setPageTitle( wfMsg( 'giftmanager' ) );
2222
 23+ // Make sure that the user is logged in and that they can use this
 24+ // special page
2325 if ( $wgUser->isAnon() || !$this->canUserManage() ) {
2426 throw new ErrorPageError( 'error', 'badaccess' );
2527 }
2628
27 - $wgOut->addStyle( $wgUserGiftsScripts . '/UserGifts.css' );
 29+ // Add CSS
 30+ $wgOut->addExtensionStyle( $wgUserGiftsScripts . '/UserGifts.css' );
2831
2932 if ( count( $_POST ) ) {
3033 if ( !( $_POST['id'] ) ) {
31 - $gift_id = Gifts::addGift( $_POST['gift_name'], $_POST['gift_description'], $_POST['access'] );
32 - $wgOut->addHTML( '<span class="view-status">' . wfMsg( 'giftmanager-giftcreated' ) . '</span><br /><br />' );
 34+ $giftId = Gifts::addGift(
 35+ $_POST['gift_name'],
 36+ $_POST['gift_description'],
 37+ intval( $_POST['access'] )
 38+ );
 39+ $wgOut->addHTML(
 40+ '<span class="view-status">' .
 41+ wfMsg( 'giftmanager-giftcreated' ) .
 42+ '</span><br /><br />'
 43+ );
3344 } else {
34 - $gift_id = $_POST['id'];
35 - Gifts::updateGift( $gift_id, $_POST['gift_name'], $_POST['gift_description'], $_POST['access'] );
36 - $wgOut->addHTML( '<span class="view-status">' . wfMsg( 'giftmanager-giftsaved' ) . '</span><br /><br />' );
 45+ $giftId = intval( $_POST['id'] );
 46+ Gifts::updateGift(
 47+ $giftId,
 48+ $_POST['gift_name'],
 49+ $_POST['gift_description'],
 50+ intval( $_POST['access'] )
 51+ );
 52+ $wgOut->addHTML(
 53+ '<span class="view-status">' .
 54+ wfMsg( 'giftmanager-giftsaved' ) .
 55+ '</span><br /><br />'
 56+ );
3757 }
3858
39 - $wgOut->addHTML( $this->displayForm( $gift_id ) );
 59+ $wgOut->addHTML( $this->displayForm( $giftId ) );
4060 } else {
41 - $gift_id = $wgRequest->getVal( 'id' );
42 - if ( $gift_id || $wgRequest->getVal( 'method' ) == 'edit' ) {
43 - $wgOut->addHTML( $this->displayForm( $gift_id ) );
 61+ $giftId = $wgRequest->getInt( 'id' );
 62+ if ( $giftId || $wgRequest->getVal( 'method' ) == 'edit' ) {
 63+ $wgOut->addHTML( $this->displayForm( $giftId ) );
4464 } else {
 65+ // If the user is allowed to create new gifts, show the
 66+ // "add a gift" link to them
4567 if ( $this->canUserCreateGift() ) {
46 - $wgOut->addHTML( '<div><b><a href="' . $wgScriptPath . '/index.php?title=Special:GiftManager&amp;method=edit">'
47 - . wfMsg( 'giftmanager-addgift' ) . '</a></b></div>' );
 68+ $wgOut->addHTML(
 69+ '<div><b><a href="' .
 70+ $this->getTitle()->escapeFullURL( 'method=edit' ) .
 71+ '">' . wfMsg( 'giftmanager-addgift' ) .
 72+ '</a></b></div>'
 73+ );
4874 }
4975 $wgOut->addHTML( $this->displayGiftList() );
5076 }
@@ -66,7 +92,11 @@
6793 return true;
6894 }
6995
70 - if ( $wgUser->isAllowed( 'giftadmin' ) || in_array( 'giftadmin', $wgUser->getGroups() ) ) {
 96+ if (
 97+ $wgUser->isAllowed( 'giftadmin' ) ||
 98+ in_array( 'giftadmin', $wgUser->getGroups() )
 99+ )
 100+ {
71101 return true;
72102 }
73103
@@ -85,7 +115,11 @@
86116 return false;
87117 }
88118
89 - if ( $wgUser->isAllowed( 'giftadmin' ) || in_array( 'giftadmin', $wgUser->getGroups() ) ) {
 119+ if (
 120+ $wgUser->isAllowed( 'giftadmin' ) ||
 121+ in_array( 'giftadmin', $wgUser->getGroups() )
 122+ )
 123+ {
90124 return true;
91125 }
92126
@@ -105,42 +139,69 @@
106140 return false;
107141 }
108142
109 - $created_count = Gifts::getCustomCreatedGiftCount( $wgUser->getID() );
110 - if ( $wgUser->isAllowed( 'giftadmin' ) || in_array( 'giftadmin', ( $wgUser->getGroups() ) ) || ( $wgMaxCustomUserGiftCount > 0 && $created_count < $wgMaxCustomUserGiftCount ) ) {
 143+ $createdCount = Gifts::getCustomCreatedGiftCount( $wgUser->getID() );
 144+ if (
 145+ $wgUser->isAllowed( 'giftadmin' ) ||
 146+ in_array( 'giftadmin', ( $wgUser->getGroups() ) ) ||
 147+ ( $wgMaxCustomUserGiftCount > 0 && $createdCount < $wgMaxCustomUserGiftCount )
 148+ )
 149+ {
111150 return true;
112151 } else {
113152 return false;
114153 }
115154 }
116155
 156+ /**
 157+ * Display the text list of all existing gifts and a delete link to users
 158+ * who are allowed to delete gifts.
 159+ *
 160+ * @return String: HTML
 161+ */
117162 function displayGiftList() {
118 - global $wgScriptPath;
119163 $output = ''; // Prevent E_NOTICE
120164 $page = 0;
121165 $per_page = 10;
122166 $gifts = Gifts::getManagedGiftList( $per_page, $page );
123167 if ( $gifts ) {
124168 foreach ( $gifts as $gift ) {
 169+ $deleteLink = '';
 170+ if ( $this->canUserDelete() ) {
 171+ $deleteLink = '<a href="' .
 172+ SpecialPage::getTitleFor( 'RemoveMasterGift' )->escapeFullURL( "gift_id={$gift['id']}" ) .
 173+ '" style="font-size:10px; color:red;">' .
 174+ wfMsg( 'delete' ) . '</a>';
 175+ }
 176+
125177 $output .= '<div class="Item">
126 - <a href="' . $wgScriptPath . '/index.php?title=Special:GiftManager&amp;id=' . $gift['id'] . '">' . $gift['gift_name'] . '</a> ' .
127 - ( ( $this->canUserDelete() ) ? '<a href="' . SpecialPage::getTitleFor( 'RemoveMasterGift' )->escapeFulLURL( "gift_id={$gift["id"]}" ) . '" style="font-size:10px; color:red;">' . wfMsg( 'delete' ) . '</a>' : '' )
128 - . "</div>\n";
 178+ <a href="' . $this->getTitle()->escapeFullURL( "id={$gift['id']}" ) . '">' .
 179+ $gift['gift_name'] . '</a> ' .
 180+ $deleteLink . "</div>\n";
129181 }
130182 }
131183 return '<div id="views">' . $output . '</div>';
132184 }
133185
134186 function displayForm( $gift_id ) {
135 - global $wgUser, $wgOut, $wgScriptPath;
 187+ global $wgUser;
136188
137189 if ( !$gift_id && !$this->canUserCreateGift() ) {
138190 return $this->displayGiftList();
139191 }
140 - $form = '<div><b><a href="' . $wgScriptPath . '/index.php?title=Special:GiftManager">' . wfMsg( 'giftmanager-view' ) . '</a></b></div>';
141192
 193+ $form = '<div><b><a href="' . $this->getTitle()->escapeFullURL() .
 194+ '">' . wfMsg( 'giftmanager-view' ) . '</a></b></div>';
 195+
142196 if ( $gift_id ) {
143197 $gift = Gifts::getGift( $gift_id );
144 - if ( $wgUser->getID() != $gift['creator_user_id'] && ( !in_array( 'giftadmin', $wgUser->getGroups() ) && !$wgUser->isAllowed( 'delete' ) ) ) {
 198+ if (
 199+ $wgUser->getID() != $gift['creator_user_id'] &&
 200+ (
 201+ !in_array( 'giftadmin', $wgUser->getGroups() ) &&
 202+ !$wgUser->isAllowed( 'delete' )
 203+ )
 204+ )
 205+ {
145206 throw new ErrorPageError( 'error', 'badaccess' );
146207 }
147208 }
@@ -149,20 +210,27 @@
150211 $form .= '<table border="0" cellpadding="5" cellspacing="0" width="500">';
151212 $form .= '<tr>
152213 <td width="200" class="view-form">' . wfMsg( 'g-gift-name' ) . '</td>
153 - <td width="695"><input type="text" size="45" class="createbox" name="gift_name" value="' . ( isset( $gift['gift_name'] ) ? $gift['gift_name'] : '' ) . '"/></td>
 214+ <td width="695"><input type="text" size="45" class="createbox" name="gift_name" value="' .
 215+ ( isset( $gift['gift_name'] ) ? $gift['gift_name'] : '' ) . '"/></td>
154216 </tr>
155217 <tr>
156218 <td width="200" class="view-form" valign="top">' . wfMsg( 'giftmanager-description' ) . '</td>
157 - <td width="695"><textarea class="createbox" name="gift_description" rows="2" cols="30">' . ( isset( $gift['gift_description'] ) ? $gift['gift_description'] : '' ) . '</textarea></td>
 219+ <td width="695"><textarea class="createbox" name="gift_description" rows="2" cols="30">' .
 220+ ( isset( $gift['gift_description'] ) ? $gift['gift_description'] : '' ) . '</textarea></td>
158221 </tr>';
159222 if ( $gift_id ) {
160223 $creator = Title::makeTitle( NS_USER, $gift['creator_user_name'] );
161224 $form .= '<tr>
162 - <td class="view-form">' . wfMsgExt( 'g-created-by', 'parsemag', $gift['creator_user_name'] ) . '</td>
163 - <td><a href="' . $creator->escapeFullURL() . '">' . $gift['creator_user_name'] . '</a></td>
 225+ <td class="view-form">' .
 226+ wfMsgExt( 'g-created-by', 'parsemag', $gift['creator_user_name'] ) .
 227+ '</td>
 228+ <td><a href="' . $creator->escapeFullURL() . '">' .
 229+ $gift['creator_user_name'] . '</a></td>
164230 </tr>';
165231 }
166 - global $wgUploadPath;
 232+
 233+ // If the user isn't in the gift admin group, they can only create
 234+ // private gifts
167235 if ( !in_array( 'giftadmin', $wgUser->getGroups() ) ) {
168236 $form .= '<input type="hidden" name="access" value="1" />';
169237 } else {
@@ -171,20 +239,29 @@
172240 <td class="view-form">' . wfMsg( 'giftmanager-access' ) . '</td>
173241 <td>
174242 <select name="access">
175 - <option value="0"' . ( ( $gift['access'] == 0 ) ? ' selected="selected"' : '' ) . '>' . wfMsg( 'giftmanager-public' ) . '</option>
176 - <option value="1"' . ( ( $gift['access'] == 1 ) ? ' selected="selected"' : '' ) . '>' . wfMsg( 'giftmanager-private' ) . '</option>
 243+ <option value="0"' . ( ( $gift['access'] == 0 ) ? ' selected="selected"' : '' ) . '>' .
 244+ wfMsg( 'giftmanager-public' ) .
 245+ '</option>
 246+ <option value="1"' . ( ( $gift['access'] == 1 ) ? ' selected="selected"' : '' ) . '>' .
 247+ wfMsg( 'giftmanager-private' ) .
 248+ '</option>
177249 </select>
178250 </td>
179251 </tr>';
180252 }
181253
182254 if ( $gift_id ) {
183 - $gift_image = '<img src="' . $wgUploadPath . '/awards/' . Gifts::getGiftImage( $gift_id, 'l' ) . '" border="0" alt="' . wfMsg( 'g-gift' ) . '" />';
 255+ global $wgUploadPath;
 256+ $gml = SpecialPage::getTitleFor( 'GiftManagerLogo' );
 257+ $gift_image = '<img src="' . $wgUploadPath . '/awards/' .
 258+ Gifts::getGiftImage( $gift_id, 'l' ) . '" border="0" alt="' .
 259+ wfMsg( 'g-gift' ) . '" />';
184260 $form .= '<tr>
185261 <td width="200" class="view-form" valign="top">' . wfMsg( 'giftmanager-giftimage' ) . '</td>
186262 <td width="695">' . $gift_image .
187263 '<p>
188 - <a href="' . $wgScriptPath . '/index.php?title=Special:GiftManagerLogo&gift_id=' . $gift_id . '">' . wfMsg( 'giftmanager-image' ) . '</a>
 264+ <a href="' . $gml->escapeFullURL( 'gift_id=' . $gift_id ) . '">' .
 265+ wfMsg( 'giftmanager-image' ) . '</a>
189266 </td>
190267 </tr>';
191268 }

Status & tagging log