r85503 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r85502‎ | r85503 | r85504 >
Date:01:25, 6 April 2011
Author:ashley
Status:deferred
Tags:
Comment:
SocialProfile: in SpecialGiveGift.php:
*documentation tweaks
*renamed badly_named_variables to betterNamedVariables
*more $wgRequest->getVal() to $wgRequest->getInt() swaps
*broke some long lines
*fixed indentation
*marked some batshit insane code as such for further review
Modified paths:
  • /trunk/extensions/SocialProfile/UserGifts/SpecialGiveGift.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SocialProfile/UserGifts/SpecialGiveGift.php
@@ -1,4 +1,10 @@
22 <?php
 3+/**
 4+ * Special:GiveGift -- a special page for sending out user-to-user gifts
 5+ *
 6+ * @file
 7+ * @ingroup Extensions
 8+ */
39
410 class GiveGift extends SpecialPage {
511
@@ -9,7 +15,6 @@
1016 parent::__construct( 'GiveGift' );
1117 }
1218
13 -
1419 /**
1520 * Show the special page
1621 *
@@ -23,16 +28,16 @@
2429 $wgOut->addScriptFile( $wgUserGiftsScripts . '/UserGifts.js' );
2530 $wgOut->addExtensionStyle( $wgUserGiftsScripts . '/UserGifts.css' );
2631
27 - $usertitle = Title::newFromDBkey( $wgRequest->getVal( 'user' ) );
28 - if ( !$usertitle ) {
 32+ $userTitle = Title::newFromDBkey( $wgRequest->getVal( 'user' ) );
 33+ if ( !$userTitle ) {
2934 $wgOut->addHTML( $this->displayFormNoUser() );
3035 return false;
3136 }
3237
3338 $user_title = Title::makeTitle( NS_USER, $wgRequest->getVal( 'user' ) );
34 - $this->user_name_to = $usertitle->getText();
 39+ $this->user_name_to = $userTitle->getText();
3540 $this->user_id_to = User::idFromName( $this->user_name_to );
36 - $gift_id = $wgRequest->getVal( 'gift_id' );
 41+ $giftId = $wgRequest->getInt( 'gift_id' );
3742 $out = '';
3843
3944 if ( $wgUser->getID() === $this->user_id_to ) {
@@ -51,16 +56,14 @@
5257 $out .= wfMsg( 'g-error-message-login' );
5358 $wgOut->addHTML( $out );
5459 } else {
55 -
5660 $gift = new UserGifts( $wgUser->getName() );
5761
5862 if ( $wgRequest->wasPosted() && $_SESSION['alreadysubmitted'] == false ) {
59 -
6063 $_SESSION['alreadysubmitted'] = true;
6164
6265 $ug_gift_id = $gift->sendGift(
6366 $this->user_name_to,
64 - $wgRequest->getVal( 'gift_id' ),
 67+ $wgRequest->getInt( 'gift_id' ),
6568 0,
6669 $wgRequest->getVal( 'message' )
6770 );
@@ -72,12 +75,12 @@
7376 $data = $wgMemc->get( $key );
7477
7578 // check to see if this type of gift is in the unique list
76 - $last_unique_gifts = $data;
 79+ $lastUniqueGifts = $data;
7780 $found = 1;
7881
79 - if ( is_array( $last_unique_gifts ) ) {
80 - foreach ( $last_unique_gifts as $last_unique_gift ) {
81 - if ( $wgRequest->getVal( 'gift_id' ) == $last_unique_gift['gift_id'] ) {
 82+ if ( is_array( $lastUniqueGifts ) ) {
 83+ foreach ( $lastUniqueGifts as $lastUniqueGift ) {
 84+ if ( $wgRequest->getInt( 'gift_id' ) == $lastUniqueGift['gift_id'] ) {
8285 $found = 0;
8386 }
8487 }
@@ -85,38 +88,44 @@
8689
8790 if ( $found ) {
8891 // add new unique to array
89 - $last_unique_gifts[] = array(
 92+ $lastUniqueGifts[] = array(
9093 'id' => $ug_gift_id,
91 - 'gift_id' => $wgRequest->getVal( 'gift_id' )
 94+ 'gift_id' => $wgRequest->getInt( 'gift_id' )
9295 );
9396
9497 // remove oldest value
95 - if ( count( $last_unique_gifts ) > 4 ) {
96 - array_shift( $last_unique_gifts );
 98+ if ( count( $lastUniqueGifts ) > 4 ) {
 99+ array_shift( $lastUniqueGifts );
97100 }
98101
99102 // reset the cache
100 - $wgMemc->set( $key, $last_unique_gifts );
 103+ $wgMemc->set( $key, $lastUniqueGifts );
101104 }
102105
103106 $sent_gift = UserGifts::getUserGift( $ug_gift_id );
104 - $gift_image = '<img src="' . $wgUploadPath . '/awards/' . Gifts::getGiftImage( $sent_gift['gift_id'], 'l' ) . '" border="0" alt="" />';
 107+ $gift_image = '<img src="' . $wgUploadPath . '/awards/' .
 108+ Gifts::getGiftImage( $sent_gift['gift_id'], 'l' ) .
 109+ '" border="0" alt="" />';
105110
106111 $output .= $wgOut->setPageTitle( wfMsg( 'g-sent-title', $this->user_name_to ) );
107112
108113 $output .= '<div class="back-links">
109 - <a href="' . $user_title->escapeFullURL() . '">' . wfMsg( 'g-back-link', $this->user_name_to ) . '</a>
 114+ <a href="' . $user_title->escapeFullURL() . '">' .
 115+ wfMsg( 'g-back-link', $this->user_name_to ) .
 116+ '</a>
110117 </div>
111 - <div class="g-message">'
112 - . wfMsg( 'g-sent-message', $this->user_name_to ) .
 118+ <div class="g-message">' .
 119+ wfMsg( 'g-sent-message', $this->user_name_to ) .
113120 '</div>
114 - <div class="g-container">'
115 - . $gift_image .
 121+ <div class="g-container">' .
 122+ $gift_image .
116123 '<div class="g-title">' . $sent_gift['name'] . '</div>';
117 - if ( $sent_gift['message'] ) {
118 - $output .= '<div class="g-user-message">' . $sent_gift['message'] . '</div>';
119 - }
120 - $output .= '</div>
 124+ if ( $sent_gift['message'] ) {
 125+ $output .= '<div class="g-user-message">' .
 126+ $sent_gift['message'] .
 127+ '</div>';
 128+ }
 129+ $output .= '</div>
121130 <div class="cleared"></div>
122131 <div class="g-buttons">
123132 <input type="button" class="site-button" value="' . wfMsg( 'g-main-page' ) . '" size="20" onclick="window.location=\'index.php?title=' . wfMsgForContent( 'mainpage' ) . '\'" />
@@ -127,7 +136,7 @@
128137 } else {
129138 $_SESSION['alreadysubmitted'] = false;
130139
131 - if ( $gift_id ) {
 140+ if ( $giftId ) {
132141 $wgOut->addHTML( $this->displayFormSingle() );
133142 } else {
134143 $wgOut->addHTML( $this->displayFormAll() );
@@ -136,18 +145,24 @@
137146 }
138147 }
139148
 149+ /**
 150+ * Display the form for sending out a single gift.
 151+ * Relies on the gift_id URL parameter and bails out if it's not there.
 152+ *
 153+ * @return String: HTML
 154+ */
140155 function displayFormSingle() {
141156 global $wgUser, $wgOut, $wgRequest, $wgUploadPath;
142157
143 - $gift_id = $wgRequest->getVal( 'gift_id' );
 158+ $giftId = $wgRequest->getInt( 'gift_id' );
144159
145 - if ( !$gift_id || !is_numeric( $gift_id ) ) {
 160+ if ( !$giftId || !is_numeric( $giftId ) ) {
146161 $wgOut->setPageTitle( wfMsg( 'g-error-title' ) );
147162 $wgOut->addHTML( wfMsg( 'g-error-message-invalid-link' ) );
148163 return false;
149164 }
150165
151 - $gift = Gifts::getGift( $gift_id );
 166+ $gift = Gifts::getGift( $giftId );
152167
153168 if ( empty( $gift ) ) {
154169 return false;
@@ -159,28 +174,35 @@
160175
161176 // Safe titles
162177 $user = Title::makeTitle( NS_USER, $this->user_name_to );
163 - $give_gift_link = SpecialPage::getTitleFor( 'GiveGift' );
 178+ $giveGiftLink = SpecialPage::getTitleFor( 'GiveGift' );
164179
165180 $wgOut->setPageTitle( wfMsg( 'g-give-to-user-title', $gift['gift_name'], $this->user_name_to ) );
166181
167 - $gift_image = "<img id=\"gift_image_{$gift['gift_id']}\" src=\"{$wgUploadPath}/awards/" . Gifts::getGiftImage( $gift['gift_id'], 'l' ) . '" border="0" alt="" />';
 182+ $gift_image = "<img id=\"gift_image_{$gift['gift_id']}\" src=\"{$wgUploadPath}/awards/" .
 183+ Gifts::getGiftImage( $gift['gift_id'], 'l' ) .
 184+ '" border="0" alt="" />';
168185
169186 $output = '<form action="" method="post" enctype="multipart/form-data" name="gift">
170 - <div class="g-message">'
171 - . wfMsg( 'g-give-to-user-message', $this->user_name_to, $give_gift_link->escapeFullURL( 'user=' . $this->user_name_to ) ) .
172 - "</div>
 187+ <div class="g-message">' .
 188+ wfMsg(
 189+ 'g-give-to-user-message',
 190+ $this->user_name_to,
 191+ $giveGiftLink->escapeFullURL( 'user=' . $this->user_name_to )
 192+ ) . "</div>
173193 <div id=\"give_gift_{$gift['gift_id']}\" class=\"g-container\">
174194 {$gift_image}
175195 <div class=\"g-title\">{$gift['gift_name']}</div>";
176 - if ( $gift['gift_description'] ) {
177 - $output .= '<div class="g-describe">' . $gift['gift_description'] . '</div>';
178 - }
179 - $output .= '</div>
 196+ if ( $gift['gift_description'] ) {
 197+ $output .= '<div class="g-describe">' .
 198+ $gift['gift_description'] .
 199+ '</div>';
 200+ }
 201+ $output .= '</div>
180202 <div class="cleared"></div>
181203 <div class="g-add-message">' . wfMsg( 'g-add-message' ) . '</div>
182204 <textarea name="message" id="message" rows="4" cols="50"></textarea>
183205 <div class="g-buttons">
184 - <input type="hidden" name="gift_id" value="' . $gift_id . '" />
 206+ <input type="hidden" name="gift_id" value="' . $giftId . '" />
185207 <input type="hidden" name="user_name" value="' . addslashes( $this->user_name_to ) . '" />
186208 <input type="button" class="site-button" value="' . wfMsg( 'g-send-gift' ) . '" size="20" onclick="document.gift.submit()" />
187209 <input type="button" class="site-button" value="' . wfMsg( 'g-cancel' ) . '" size="20" onclick="history.go(-1)" />
@@ -190,35 +212,61 @@
191213 return $output;
192214 }
193215
 216+ /**
 217+ * Display the form for giving out a gift to a user when there was no user
 218+ * parameter in the URL.
 219+ *
 220+ * @return String: HTML
 221+ */
194222 function displayFormNoUser() {
195223 global $wgUser, $wgOut, $wgRequest, $wgFriendingEnabled;
196224
197225 $output = $wgOut->setPageTitle( wfMsg( 'g-give-no-user-title' ) );
198226
 227+ // @todo FIXME: $wgRequest->getVal()...seriously?
 228+ // Seems that this should use the proper MW function (maybe
 229+ // $this->getTitle()->getFullURL() or something) instead.
 230+ // Maybe that's the reason why I (and other people) frequently have
 231+ // problems with this special page.
 232+ // --Jack Phoenix <jack@countervandalism.net>, 6 April 2011
199233 $output .= '<form action="" method="get" enctype="multipart/form-data" name="gift">
200234 <input type="hidden" name="title" value="' . $wgRequest->getVal( 'title' ) . '" />
201 - <div class="g-message">' . wfMsg( 'g-give-no-user-message' ) . '</div>
 235+ <div class="g-message">' .
 236+ wfMsg( 'g-give-no-user-message' ) .
 237+ '</div>
202238 <div class="g-give-container">';
203239
 240+ // If friending is enabled, build a dropdown menu of the user's
 241+ // friends
204242 if ( $wgFriendingEnabled ) {
205243 $rel = new UserRelationship( $wgUser->getName() );
206244 $friends = $rel->getRelationshipList( 1 );
207245
208246 if ( $friends ) {
209 - $output .= '<div class="g-give-title">' . wfMsg( 'g-give-list-friends-title' ) . '</div>
 247+ $output .= '<div class="g-give-title">' .
 248+ wfMsg( 'g-give-list-friends-title' ) .
 249+ '</div>
210250 <div class="g-gift-select">
211251 <select onchange="javascript:chooseFriend(this.value)">
212 - <option value="#" selected="selected">' . wfMsg( 'g-select-a-friend' ) . '</option>';
 252+ <option value="#" selected="selected">' .
 253+ wfMsg( 'g-select-a-friend' ) .
 254+ '</option>';
213255 foreach ( $friends as $friend ) {
214 - $output .= '<option value="' . urlencode( $friend['user_name'] ) . '">' . $friend['user_name'] . '</option>';
 256+ $output .= '<option value="' . urlencode( $friend['user_name'] ) . '">' .
 257+ $friend['user_name'] .
 258+ '</option>';
215259 }
216260 $output .= '</select>
217261 </div>
218 - <div class="g-give-separator">' . wfMsg( 'g-give-separator' ) . '</div>';
 262+ <div class="g-give-separator">' .
 263+ wfMsg( 'g-give-separator' ) .
 264+ '</div>';
219265 }
220266 }
221267
222 - $output .= '<div class="g-give-title">' . wfMsg( 'g-give-enter-friend-title' ) . '</div>
 268+ $output .= '<div class="g-give-title">' .
 269+ wfMsg( 'g-give-enter-friend-title' ) .
 270+ '</div>
223271 <div class="g-give-textbox">
224272 <input type="text" width="85" name="user" value="" />
225273 <input class="site-button" type="button" value="' . wfMsg( 'g-give-gift' ) . '" onclick="document.gift.submit()" />
@@ -233,7 +281,7 @@
234282 global $wgUser, $wgOut, $wgRequest, $wgGiveGiftPerRow, $wgUploadPath;
235283 $user = Title::makeTitle( NS_USER, $this->user_name_to );
236284
237 - $page = $wgRequest->getVal( 'page' );
 285+ $page = $wgRequest->getInt( 'page' );
238286 if ( !$page || !is_numeric( $page ) ) {
239287 $page = 1;
240288 }
@@ -252,25 +300,29 @@
253301 $wgOut->setPageTitle( wfMsg( 'g-give-all-title', $this->user_name_to ) );
254302
255303 $output .= '<div class="back-links">
256 - <a href="' . $user->escapeFullURL() . '">' . wfMsg( 'g-back-link', $this->user_name_to ) . '</a>
 304+ <a href="' . $user->escapeFullURL() . '">' .
 305+ wfMsg( 'g-back-link', $this->user_name_to ) .
 306+ '</a>
257307 </div>
258 - <div class="g-message">
259 - ' . wfMsg( 'g-give-all', $this->user_name_to ) . '
260 - </div>
 308+ <div class="g-message">' .
 309+ wfMsg( 'g-give-all', $this->user_name_to ) .
 310+ '</div>
261311 <form action="" method="post" enctype="multipart/form-data" name="gift">';
262312
263313 $x = 1;
264314
265315 foreach ( $gifts as $gift ) {
266 - $gift_image = "<img id=\"gift_image_{$gift['id']}\" src=\"{$wgUploadPath}/awards/" . Gifts::getGiftImage( $gift['id'], 'l' ) . '" border="0" alt="" />';
 316+ $gift_image = "<img id=\"gift_image_{$gift['id']}\" src=\"{$wgUploadPath}/awards/" .
 317+ Gifts::getGiftImage( $gift['id'], 'l' ) .
 318+ '" border="0" alt="" />';
267319
268320 $output .= "<div onclick=\"selectGift({$gift['id']})\" onmouseover=\"highlightGift({$gift['id']})\" onmouseout=\"unHighlightGift({$gift['id']})\" id=\"give_gift_{$gift['id']}\" class=\"g-give-all\">
269321 {$gift_image}
270322 <div class=\"g-title g-blue\">{$gift['gift_name']}</div>";
271 - if ( $gift['gift_description'] ) {
272 - $output .= "<div class=\"g-describe\">{$gift['gift_description']}</div>";
273 - }
274 - $output .= '<div class="cleared"></div>
 323+ if ( $gift['gift_description'] ) {
 324+ $output .= "<div class=\"g-describe\">{$gift['gift_description']}</div>";
 325+ }
 326+ $output .= '<div class="cleared"></div>
275327 </div>';
276328 if ( $x == count( $gifts ) || $x != 1 && $x % $per_row == 0 ) {
277329 $output .= '<div class="cleared"></div>';
@@ -281,14 +333,14 @@
282334 /**
283335 * Build next/prev nav
284336 */
285 - $give_gift_link = SpecialPage::getTitleFor( 'GiveGift' );
 337+ $giveGiftLink = SpecialPage::getTitleFor( 'GiveGift' );
286338
287339 $numofpages = $total / $per_page;
288340 $user_safe = urlencode( $user->getText() );
289341 if ( $numofpages > 1 ) {
290342 $output .= '<div class="page-nav">';
291343 if ( $page > 1 ) {
292 - $output .= '<a href="' . $give_gift_link->escapeFullURL( 'user=' . $user_safe . '&page=' . ( $page - 1 ) ) . '">' . wfMsg( 'g-previous' ) . '</a> ';
 344+ $output .= '<a href="' . $giveGiftLink->escapeFullURL( 'user=' . $user_safe . '&page=' . ( $page - 1 ) ) . '">' . wfMsg( 'g-previous' ) . '</a> ';
293345 }
294346
295347 if ( ( $total % $per_page ) != 0 ) {
@@ -301,12 +353,12 @@
302354 if ( $i == $page ) {
303355 $output .= ( $i . ' ' );
304356 } else {
305 - $output .= '<a href="' . $give_gift_link->escapeFullURL( 'user=' . $user_safe . '&page=' . $i ) . "\">$i</a> ";
 357+ $output .= '<a href="' . $giveGiftLink->escapeFullURL( 'user=' . $user_safe . '&page=' . $i ) . "\">$i</a> ";
306358 }
307359 }
308360
309361 if ( ( $total - ( $per_page * $page ) ) > 0 ) {
310 - $output .= ' <a href="' . $give_gift_link->escapeFullURL( 'user=' . $user_safe . '&page=' . ( $page + 1 ) ) . '">' . wfMsg( 'g-next' ) . '</a>';
 362+ $output .= ' <a href="' . $giveGiftLink->escapeFullURL( 'user=' . $user_safe . '&page=' . ( $page + 1 ) ) . '">' . wfMsg( 'g-next' ) . '</a>';
311363 }
312364 $output .= '</div>';
313365 }
@@ -314,7 +366,9 @@
315367 /**
316368 * Build next/prev nav
317369 */
318 - $output .= '<div class="g-give-all-message-title">' . wfMsg( 'g-give-all-message-title' ) . '</div>
 370+ $output .= '<div class="g-give-all-message-title">' .
 371+ wfMsg( 'g-give-all-message-title' ) .
 372+ '</div>
319373 <textarea name="message" id="message" rows="4" cols="50"></textarea>
320374 <div class="g-buttons">
321375 <input type="hidden" name="gift_id" value="0" />

Status & tagging log