MediaWiki r115379 - Code Review

Jump to: navigation, search
Repository:MediaWiki
Revision:r115378‎ | r115379 (on ViewVC)‎ | r115380 >
Date:03:41, 15 May 2012
Author:ashley
Status:new (Comments)
Tags:
Comment:
SocialProfile: fix bug #30953 - make Special:UploadAvatar actually work under MediaWiki 1.19.0.

There was an unannounced, subtle, yet major core change in 1.19: SpecialUpload now wants the edit token (wpEditToken) _unconditionally_ and UploadAvatar wasn't passing it as it wasn't required before, hence why all avatar uploads were failing.
I lazily copypasted a few lines of code from includes/HTMLForm.php to fix that.
I also applied my own patch from that same bug to use context stuff instead of globals.

THIS BREAKS SOCIALPROFILE'S BACKWARDS COMPATIBILITY WITH PRE-1.19 WIKIS. BEWARE!
Modified paths:

Diff [purge]

Index: trunk/extensions/SocialProfile/UserProfile/SpecialUploadAvatar.php
@@ -22,17 +22,15 @@
2323 * Constructor
2424 */
2525 public function __construct( $request = null ) {
26 - global $wgRequest;
27 -
2826 SpecialPage::__construct( 'UploadAvatar', 'upload', false/* listed? */ );
29 - $this->loadRequest( is_null( $request ) ? $wgRequest : $request );
3027 }
3128
3229 /**
3330 * Let the parent handle most of the request, but specify the Upload
3431 * class ourselves
3532 */
36 - protected function loadRequest( $request ) {
 33+ protected function loadRequest() {
 34+ $request = $this->getRequest();
3735 parent::loadRequest( $request );
3836 $this->mUpload = new UploadAvatar();
3937 $this->mUpload->initializeFromRequest( $request );
@@ -45,18 +43,19 @@
4644 * @param $params Mixed: parameter(s) passed to the page or null
4745 */
4846 public function execute( $params ) {
49 - global $wgOut, $wgUser, $wgUserProfileScripts;
 47+ global $wgUserProfileScripts;
5048
51 - $wgOut->addExtensionStyle( $wgUserProfileScripts . '/UserProfile.css' );
 49+ $out = $this->getOutput();
 50+ $out->addExtensionStyle( $wgUserProfileScripts . '/UserProfile.css' );
5251 parent::execute( $params );
5352
5453 if ( $this->mUploadSuccessful ) {
5554 // Cancel redirect
56 - $wgOut->redirect( '' );
 55+ $out->redirect( '' );
5756
5857 $this->showSuccess( $this->mUpload->mExtension );
5958 // Run a hook on avatar change
60 - wfRunHooks( 'NewAvatarUploaded', array( $wgUser ) );
 59+ wfRunHooks( 'NewAvatarUploaded', array( $this->getUser() ) );
6160 }
6261 }
6362
@@ -66,19 +65,20 @@
6766 * @param $ext String: file extension (gif, jpg or png)
6867 */
6968 private function showSuccess( $ext ) {
70 - global $wgUser, $wgOut, $wgDBname, $wgUploadPath, $wgUploadAvatarInRecentChanges;
 69+ global $wgDBname, $wgUploadPath, $wgUploadAvatarInRecentChanges;
7170
 71+ $user = $this->getUser();
7272 $log = new LogPage( 'avatar' );
7373 if ( !$wgUploadAvatarInRecentChanges ) {
7474 $log->updateRecentChanges = false;
7575 }
7676 $log->addEntry(
7777 'avatar',
78 - $wgUser->getUserPage(),
 78+ $user->getUserPage(),
7979 wfMsgForContent( 'user-profile-picture-log-entry' )
8080 );
8181
82 - $uid = $wgUser->getId();
 82+ $uid = $user->getId();
8383
8484 $output = '<h1>' . wfMsg( 'uploadavatar' ) . '</h1>';
8585 $output .= UserProfile::getEditProfileNav( wfMsg( 'user-profile-section-picture' ) );
@@ -128,7 +128,7 @@
129129 $output .= '</table>';
130130 $output .= '</div>';
131131
132 - $wgOut->addHTML( $output );
 132+ $this->getOutput()->addHTML( $output );
133133 }
134134
135135 /**
@@ -141,14 +141,13 @@
142142 * @return HTML output
143143 */
144144 protected function getUploadForm( $message = '', $sessionKey = '', $hideIgnoreWarning = false ) {
145 - global $wgOut, $wgUser, $wgUseCopyrightUpload;
 145+ global $wgUseCopyrightUpload;
146146
147147 if ( $message != '' ) {
148148 $sub = wfMsg( 'uploaderror' );
149 - $wgOut->addHTML( "<h2>{$sub}</h2>\n" .
 149+ $this->getOutput()->addHTML( "<h2>{$sub}</h2>\n" .
150150 "<h4 class='error'>{$message}</h4>\n" );
151151 }
152 - $sk = $wgUser->getSkin();
153152
154153 $ulb = wfMsg( 'uploadbtn' );
155154
@@ -183,8 +182,16 @@
184183 </table>';
185184 }
186185
187 - $output .= '<form id="upload" method="post" enctype="multipart/form-data" action="">
188 - <table border="0">
 186+ $output .= '<form id="upload" method="post" enctype="multipart/form-data" action="">';
 187+ // The following two lines are delicious copypasta from HTMLForm.php,
 188+ // function getHiddenFields() and they are required; wpEditToken is, as
 189+ // of MediaWiki 1.19, checked _unconditionally_ in
 190+ // SpecialUpload::loadRequest() and having the hidden title doesn't
 191+ // hurt either
 192+ // @see https://bugzilla.wikimedia.org/show_bug.cgi?id=30953
 193+ $output .= Html::hidden( 'wpEditToken', $this->getUser()->getEditToken(), array( 'id' => 'wpEditToken' ) ) . "\n";
 194+ $output .= Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) . "\n";
 195+ $output .= '<table border="0">
189196 <tr>
190197 <td>
191198 <p class="profile-update-title">' .
@@ -218,10 +225,10 @@
219226 * @return String: full img HTML tag
220227 */
221228 function getAvatar( $size ) {
222 - global $wgUser, $wgDBname, $wgUploadDirectory, $wgUploadPath;
 229+ global $wgDBname, $wgUploadDirectory, $wgUploadPath;
223230 $files = glob(
224231 $wgUploadDirectory . '/avatars/' . $wgDBname . '_' .
225 - $wgUser->getID() . '_' . $size . "*"
 232+ $this->getUser()->getID() . '_' . $size . '*'
226233 );
227234 if ( isset( $files[0] ) && $files[0] ) {
228235 return "<img src=\"{$wgUploadPath}/avatars/" .
@@ -335,7 +342,7 @@
336343 * Create the thumbnails and delete old files
337344 */
338345 public function performUpload( $comment, $pageText, $watch, $user ) {
339 - global $wgUploadDirectory, $wgUser, $wgDBname, $wgMemc;
 346+ global $wgUploadDirectory, $wgDBname, $wgMemc;
340347
341348 $this->avatarUploadDirectory = $wgUploadDirectory . '/avatars';
342349
@@ -356,13 +363,13 @@
357364
358365 $dest = $this->avatarUploadDirectory;
359366
360 - $uid = $wgUser->getId();
 367+ $uid = $user->getId();
361368 $avatar = new wAvatar( $uid, 'l' );
362369 // If this is the user's first custom avatar, update statistics (in
363370 // case if we want to give out some points to the user for uploading
364371 // their first avatar)
365372 if ( strpos( $avatar->getAvatarImage(), 'default_' ) !== false ) {
366 - $stats = new UserStatsTrack( $uid, $wgUser->getName() );
 373+ $stats = new UserStatsTrack( $uid, $user->getName() );
367374 $stats->incStatField( 'user_image' );
368375 }
369376

Comments

#Comment by Hevercking (talk | contribs)   08:34, 13 June 2012

Hi, i check this revision, i report this bug in http://www.mediawiki.org/wiki/Extension_talk:SocialProfile#My_Users_can.27t_upload_new_avatar and Jack Phoenix reported in this page. now i see the upload page in mediawiki mediawiki-1.19.0 but when i try upload avatar i see this error:

Fatal error: Call to a member function getID() on a non-object in /var/www/mediawiki/mediawiki/extensions/SocialProfile/UserProfile/SpecialUploadAvatar.php on line 329

My code in this linies are:

           327    $uid = $user->getId(); //ESTE NO ESTAVA
           328

329 $avatar = new wAvatar( $wgUser->getID(), 'l' ); 330 if ( strpos( $avatar->getAvatarImage(), 'default_' ) !== false ) { 331 //$stats = new UserStatsTrack( $wgUser->getID(), $wgUser->getName() ); 332 $stats = new UserStatsTrack( $uid, $user->getName() ); 333 $stats->incStatField( 'user_image' ); 334 }

Any solution? many thanks

#Comment by Jack Phoenix (talk | contribs)   11:34, 13 June 2012

Your error basically means that $wgUser isn't declared as a global in the function; the fix is as easy as global $wgUser; somewhere in the function, preferrably at the top or alternatively, right before the code which accesses $wgUser.

However, that's not the real problem. The real problem is that you have some pretty strange code in there. In SVN HEAD, there are no instances of $wgUser in extensions/SocialProfile/UserProfile/SpecialUploadAvatar.php (there are two instances of $wgUserProfileScripts, though, but that's unrelated) and in SVN HEAD line 329 is a comment ("Clean up."). It seems that your version of the UploadAvatar class, specifically its performUpload method, is out-of-date.
Assuming that you haven't touched SocialProfile's files in any way, I'd suggest getting rid of your current SocialProfile files and performing a fresh checkout from SVN.