SamanthaNguyen has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/398710 )

Change subject: Abstract profile header out of UserProfilePage into its own 
class
......................................................................

Abstract profile header out of UserProfilePage into its own class

The header component of UserProfilePage is now more contained by
representing as an object constructor.

This abstraction refactor will help start the initial refactor of the
UserProfilePage class, so components are object-oriented. This refactor
also aims to make this header easier to maintain for the future, and allow
for faster improvements.

The next follow-up patch will be making the links in the UserProfileHeader
object-oriented (UserProfileHeaderLinks object).

Change-Id: I9941b21c8f90cb670b80b82f62c96741b0d74428
---
M SocialProfile.php
A UserProfile/UserProfileHeader.php
M UserProfile/UserProfilePage.php
3 files changed, 360 insertions(+), 148 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/SocialProfile 
refs/changes/10/398710/1

diff --git a/SocialProfile.php b/SocialProfile.php
index 92d2498..e0e03df 100644
--- a/SocialProfile.php
+++ b/SocialProfile.php
@@ -41,6 +41,7 @@
 $wgAutoloadClasses['UploadAvatar'] = __DIR__ . 
'/UserProfile/SpecialUploadAvatar.php';
 $wgAutoloadClasses['RemoveAvatar'] = __DIR__ . 
'/UserProfile/SpecialRemoveAvatar.php';
 $wgAutoloadClasses['UserProfile'] = __DIR__ . 
'/UserProfile/UserProfileClass.php';
+$wgAutoloadClasses['UserProfileHeader'] = __DIR__ . 
'/UserProfile/UserProfileHeader.php';
 $wgAutoloadClasses['UserProfileHooks'] = __DIR__ . 
'/UserProfile/UserProfileHooks.php';
 $wgAutoloadClasses['UserProfilePage'] = __DIR__ . 
'/UserProfile/UserProfilePage.php';
 $wgAutoloadClasses['UserSystemMessage'] = __DIR__ . 
'/UserSystemMessages/UserSystemMessagesClass.php';
diff --git a/UserProfile/UserProfileHeader.php 
b/UserProfile/UserProfileHeader.php
new file mode 100644
index 0000000..bb67f0e
--- /dev/null
+++ b/UserProfile/UserProfileHeader.php
@@ -0,0 +1,344 @@
+<?php
+/**
+ * User profile header on user's social profile pages
+ *
+ * @file
+ */
+
+class UserProfileHeader {
+
+       /**
+        * user ID
+        *
+        * @var integer $user_id
+        */
+       protected $user_id;
+
+       /**
+        * user name
+        *
+        * @var string $user_name
+        */
+       protected $user_name;
+
+       protected $profile_data;
+
+       protected $title;
+
+       protected $is_owner;
+
+       function __construct( $user_id, $user_name, $profile_data, $title, 
$is_owner ) {
+               $this->user_id = $user_id;
+               $this->user_name = $user_name;
+               $this->profile_data = $profile_data;
+               $this->title = $title;
+               $this->is_owner = $is_owner;
+       }
+
+       public function __toString() {
+               return $this->getProfileHeader(
+                       $this->user_id,
+                       $this->user_name,
+                       $this->profile_data,
+                       $this->title,
+                       $this->is_owner
+               );
+       }
+
+       /**
+        * @todo Rewrite to access LinkRenderer service instead
+        *
+        * @param string $url Url (auto-escaped through htmlspecialchars)
+        * @param $label i18n message representing the label of the link
+        * @return string Html
+        */
+       static function getLink( $url, $label ) {
+               return '<a href="' . htmlspecialchars( $url ) . '" 
rel="nofollow">' . $label . '</a>';
+       }
+
+       static function getProfileToggleButton( $profile_data ) {
+               $toggle_title = SpecialPage::getTitleFor( 'ToggleUserPage' );
+
+               // Cast it to an int because PHP is stupid.
+               if (
+                       (int) $profile_data['user_page_type'] == 1 ||
+                       $profile_data['user_page_type'] === ''
+               ) {
+                       $toggleMessage = wfMessage( 'user-type-toggle-old' 
)->escaped();
+               } else {
+                       $toggleMessage = wfMessage( 'user-type-toggle-new' 
)->escaped();
+               }
+
+               return '<div id="profile-toggle-button">
+                               <a href="' . htmlspecialchars( 
$toggle_title->getFullURL() ) . '" rel="nofollow">' .
+                                       $toggleMessage . '</a>
+               </div>';
+       }
+
+       /**
+        * get the avatar of the user
+        *
+        * @param int $user_id
+        * @return string Html
+        */
+       static function getAvatar( $user_id ) {
+               $avatar = new wAvatar( $user_id, 'l' );
+
+               return '<div id="profile-image">' . $avatar->getAvatarURL() . 
'</div>';
+       }
+
+       /**
+        * Gets the user's level and points
+        *
+        * @param int $user_id
+        * @param string $user_name
+        * @return string Html
+        */
+       static function getUserLevelAndPoints( $user_id, $user_name ) {
+               global $wgLang, $wgUserLevels;
+
+               $stats = new UserStats( $user_id, $user_name );
+               $stats_data = $stats->getUserStats();
+               $user_level = new UserLevel( $stats_data['points'] );
+               $level_link = Title::makeTitle( NS_HELP, wfMessage( 
'user-profile-userlevels-link' )->inContentLanguage()->text() );
+
+               return '<div id="points-level">' .
+                               self::getLink( 
+                                       $level_link->getFullURL(),
+                                       wfMessage(
+                                               'user-profile-points',
+                                               $wgLang->formatNum( 
$stats_data['points'] )
+                                       )->escaped() 
+                               ) .
+                       '</div>
+                       <div id="honorific-level">' .
+                               self::getLink(
+                                       $level_link->getFullURL(),
+                                       $user_level->getLevelName()
+                               ) .
+                       '</div>';
+       }
+
+       /**
+        * Get the user's username
+        *
+        * @param string $user_name
+        * @return string Html
+        */
+       static function getUsername( $user_name ) {
+               return '<div id="profile-title">' . $user_name . '</div>';
+       }
+
+       /**
+        * Gets the title container for the profile header
+        *
+        * @param int $user_id
+        * @param string $user_name
+        * @return string Html
+        */
+       static function getProfileTitle( $user_id, $user_name ) {
+               return '<div id="profile-title-container">' .
+                               self::getUsername( $user_name ) .
+                               self::getUserLevelAndPoints( $user_id, 
$user_name ) .
+                               '<div class="visualClear"></div>
+               </div>';
+       }
+
+       /**
+        * Returns the 'pipe-separator' message
+        *
+        * @return string
+        */
+       static function getPipeSeparator() {
+               return wfMessage( 'pipe-separator' )->escaped();
+       }
+
+       /**
+        * Returns all profile header links
+        *
+        * @param $profile_data
+        * @param $title
+        * @param $is_owner
+        * @return string Html
+        */
+       static function getAllProfileLinks( $profile_data, $title, $is_owner ) {
+               global $wgUser, $wgLang, $wgUserLevels, $wgUserBoard;
+
+               // title + user variables
+               $page_title = $title->getText();
+               $title_parts = explode( '/', $title );
+               $user = $title_parts[0];
+               $id = User::idFromName( $user );
+               $user_safe = urlencode( $user );
+
+               // safe special page titles
+               $add_relationship = SpecialPage::getTitleFor( 'AddRelationship' 
);
+               $remove_relationship = SpecialPage::getTitleFor( 
'RemoveRelationship' );
+               $give_gift = SpecialPage::getTitleFor( 'GiveGift' );
+               $send_board_blast = SpecialPage::getTitleFor( 'SendBoardBlast' 
);
+               $update_profile = SpecialPage::getTitleFor( 'UpdateProfile' );
+               $watchlist = SpecialPage::getTitleFor( 'Watchlist' );
+               $contributions = SpecialPage::getTitleFor( 'Contributions', 
$user );
+               $send_message = SpecialPage::getTitleFor( 'UserBoard' );
+               $upload_avatar = SpecialPage::getTitleFor( 'UploadAvatar' );
+               $user_page = Title::makeTitle( NS_USER, $user );
+               $user_social_profile = Title::makeTitle( NS_USER_PROFILE, $user 
);
+               $user_wiki = Title::makeTitle( NS_USER_WIKI, $user );
+
+               if ( $id != 0 ) {
+                       $relationship = 
UserRelationship::getUserRelationshipByID( $id, $wgUser->getID() );
+               }
+
+               $output = '<div class="profile-actions">';
+
+               if ( $is_owner ) {
+                       $output .= $wgLang->pipeList( [
+                               self::getLink(
+                                       $update_profile->getFullURL(),
+                                       wfMessage( 'user-edit-profile' 
)->escaped()
+                               ),
+                               self::getLink(
+                                       $upload_avatar->getFullURL(),
+                                       wfMessage( 'user-upload-avatar' 
)->escaped()
+                               ),
+                               self::getLink(
+                                       $watchlist->getFullURL(),
+                                       wfMessage( 'user-watchlist' )->escaped()
+                               ),
+                               ''
+                       ] );
+               } elseif ( $wgUser->isLoggedIn() ) {
+                       if ( $relationship == false ) {
+                               $output .= $wgLang->pipeList( [
+                                       self::getLink(
+                                               $add_relationship->getFullURL( 
'user=' . $user_safe . '&rel_type=1' ),
+                                               wfMessage( 'user-add-friend' 
)->escaped()
+                                       ),
+                                       self::getLink(
+                                               $add_relationship->getFullURL( 
'user=' . $user_safe . '&rel_type=2' ),
+                                               wfMessage( 'user-add-foe' 
)->escaped() ),
+                                       ''
+                               ] );
+                       } else {
+                               if ( $relationship == 1 ) {
+                                       $output .= $wgLang->pipeList( [
+                                               self::getLink(
+                                                       
$remove_relationship->getFullURL( 'user=' . $user_safe ),
+                                                       wfMessage( 
'user-remove-friend' )->escaped()
+                                               ),
+                                               ''
+                                       ] );
+                               }
+                               if ( $relationship == 2 ) {
+                                       $output .= $wgLang->pipeList( [
+                                               self::getLink(
+                                                       
$remove_relationship->getFullURL( 'user=' . $user_safe ),
+                                                       wfMessage( 
'user-remove-foe' )->escaped()
+                                               ),
+                                               ''
+                                       ] );
+                               }
+                       }
+
+                       if ( $wgUserBoard ) {
+                               // Retrieves a user-specific link to 
Special:UserBoard
+                               $output .=self::getLink(
+                                       $send_message->getFullURL( 'user=' . 
$wgUser->getName() . '&conv=' . $user_safe ),
+                                       wfMessage( 'user-send-message' 
)->escaped()
+                               );
+                               $output .= wfMessage( 'pipe-separator' 
)->escaped();
+                       }
+
+                       // Retrieves a user-specific link to Special:GiveGift
+                       $output .= self::getLink(
+                               $give_gift->getFullURL( 'user=' . $user_safe ),
+                               wfMessage( 'user-send-gift' )->escaped()
+                       );
+                       $output .= wfMessage( 'pipe-separator' )->escaped();
+               }
+
+               // Retrieves a user-specific link to Special:Contributions
+               $output .= self::getLink(
+                       $contributions->getFullURL(),
+                       wfMessage( 'user-contributions' )->escaped()
+               );
+
+               // Links from NS_USER_PROFILE to NS_USER
+               if (
+                       $title->getNamespace() == NS_USER_PROFILE
+                       && $profile_data['user_id']
+                       && $profile_data['user_page_type'] == 0
+               ) {
+                       $output = self::getPipeSeparator();
+                       $output .= self::getLink(
+                               $user_page->getFullURL(),
+                               wfMessage( 'user-page-link' )->escaped()
+                       );
+               }
+
+               // Links from NS_USER to NS_USER_PROFILE
+               if (
+                       $title->getNamespace() == NS_USER
+                       && $profile_data['user_id']
+                       && $profile_data['user_page_type'] == 0
+               ) {
+                       $output .= self::getPipeSeparator();
+                       $output .= self::getLink(
+                               $user_social_profile->getFullURL(),
+                               wfMessage( 'user-social-profile-link' 
)->escaped()
+                       );
+               }
+               if (
+                       $title->getNamespace() == NS_USER && (
+                               !$profile_data['user_id'] ||
+                               $profile_data['user_page_type'] == 1 
+                       )
+               ) {
+                       $output .= self::getPipeSeparator();
+                       $output .= self::getLink(
+                               $user_wiki->getFullURL(),
+                               wfMessage( 'user-wiki-link' )->escaped()
+                       );
+               }
+               $output .= '</div>';
+
+               return $output;
+       }
+
+       /**
+        * Gets the main content header, which is
+        * the title container and profile links
+        *
+        * @param int $user_id
+        * @param string $user_name
+        * @param $profile_data
+        * @param $page_title
+        * @param $is_owner
+        * @return string Html
+        */
+       static function getMainContent( $user_id, $user_name, $profile_data, 
$title, $is_owner ) {
+               return '<div id="profile-right">' .
+                               self::getProfileTitle( $user_id, $user_name ) .
+                               self::getAllProfileLinks( $profile_data, 
$title, $is_owner ) .
+               '</div>';
+       }
+
+       /**
+        * Gets the entire profile header
+        *
+        * @param int $user_id
+        * @param string $user_name
+        * @param $profile_data
+        * @param $page_title
+        * @param $is_owner
+        * @return string Html
+        */
+       static function getProfileHeader( $user_id, $user_name, $profile_data, 
$title, $is_owner ) {
+               return '<div id="profile-top">' .
+                               self::getProfileToggleButton( $profile_data ) .
+                               self::getAvatar( $user_id ) .
+                               self::getMainContent( $user_id, $user_name, 
$profile_data, $title, $is_owner ) .
+                               '<div class="visualClear"></div>' .
+               '</div>';
+       }
+}
\ No newline at end of file
diff --git a/UserProfile/UserProfilePage.php b/UserProfile/UserProfilePage.php
index 61e40fa..e9a237f 100644
--- a/UserProfile/UserProfilePage.php
+++ b/UserProfile/UserProfilePage.php
@@ -90,7 +90,15 @@
                }
 
                $wgOut->addHTML( '<div id="profile-top">' );
-               $wgOut->addHTML( $this->getProfileTop( $this->user_id, 
$this->user_name ) );
+               $wgOut->addHTML(
+                       $this->getProfileHeader(
+                               $this->user_id,
+                               $this->user_name,
+                               $this->profile_data,
+                               $this->getTitle(),
+                               $this->isOwner()
+                       )
+               );
                $wgOut->addHTML( '<div class="visualClear"></div></div>' );
 
                // User does not want social profile for User:user_name, so we 
just
@@ -899,158 +907,17 @@
         *
         * @param $user_id Integer: user ID
         * @param $user_name String: user name
+        * @param $proile_data
+        * @param $title
+        * @param $is_owner
+        * @return UserProfileHeader
         */
-       function getProfileTop( $user_id, $user_name ) {
-               global $wgUser, $wgLang;
-               global $wgUserLevels;
-
-               $stats = new UserStats( $user_id, $user_name );
-               $stats_data = $stats->getUserStats();
-               $user_level = new UserLevel( $stats_data['points'] );
-               $level_link = Title::makeTitle( NS_HELP, wfMessage( 
'user-profile-userlevels-link' )->inContentLanguage()->text() );
-
+       function getProfileHeader( $user_id, $user_name, $profile_data, $title, 
$is_owner ) {
                $this->initializeProfileData( $user_name );
-               $profile_data = $this->profile_data;
-
-               // Variables and other crap
-               $page_title = $this->getTitle()->getText();
-               $title_parts = explode( '/', $page_title );
-               $user = $title_parts[0];
-               $id = User::idFromName( $user );
-               $user_safe = urlencode( $user );
-
-               // Safe urls
-               $add_relationship = SpecialPage::getTitleFor( 'AddRelationship' 
);
-               $remove_relationship = SpecialPage::getTitleFor( 
'RemoveRelationship' );
-               $give_gift = SpecialPage::getTitleFor( 'GiveGift' );
-               $send_board_blast = SpecialPage::getTitleFor( 'SendBoardBlast' 
);
-               $update_profile = SpecialPage::getTitleFor( 'UpdateProfile' );
-               $watchlist = SpecialPage::getTitleFor( 'Watchlist' );
-               $contributions = SpecialPage::getTitleFor( 'Contributions', 
$user );
-               $send_message = SpecialPage::getTitleFor( 'UserBoard' );
-               $upload_avatar = SpecialPage::getTitleFor( 'UploadAvatar' );
-               $user_page = Title::makeTitle( NS_USER, $user );
-               $user_social_profile = Title::makeTitle( NS_USER_PROFILE, $user 
);
-               $user_wiki = Title::makeTitle( NS_USER_WIKI, $user );
-
-               if ( $id != 0 ) {
-                       $relationship = 
UserRelationship::getUserRelationshipByID( $id, $wgUser->getID() );
-               }
-               $avatar = new wAvatar( $this->user_id, 'l' );
 
                wfDebug( 'profile type: ' . $profile_data['user_page_type'] . 
"\n" );
-               $output = '';
 
-               if ( $this->isOwner() ) {
-                       $toggle_title = SpecialPage::getTitleFor( 
'ToggleUserPage' );
-                       // Cast it to an int because PHP is stupid.
-                       if (
-                               (int) $profile_data['user_page_type'] == 1 ||
-                               $profile_data['user_page_type'] === ''
-                       )
-                       {
-                               $toggleMessage = wfMessage( 
'user-type-toggle-old' )->escaped();
-                       } else {
-                               $toggleMessage = wfMessage( 
'user-type-toggle-new' )->escaped();
-                       }
-                       $output .= '<div id="profile-toggle-button">
-                               <a href="' . htmlspecialchars( 
$toggle_title->getFullURL() ) . '" rel="nofollow">' .
-                                       $toggleMessage . '</a>
-                       </div>';
-               }
-
-               $output .= '<div id="profile-image">' . $avatar->getAvatarURL() 
.
-                       '</div>';
-
-               $output .= '<div id="profile-right">';
-
-               $output .= '<div id="profile-title-container">
-                               <div id="profile-title">' .
-                                       $user_name .
-                               '</div>';
-               // Show the user's level and the amount of points they have if
-               // UserLevels has been configured
-               if ( $wgUserLevels ) {
-                       $output .= '<div id="points-level">
-                                       <a href="' . htmlspecialchars( 
$level_link->getFullURL() ) . '">' .
-                                               wfMessage(
-                                                       'user-profile-points',
-                                                       $wgLang->formatNum( 
$stats_data['points'] )
-                                               )->escaped() .
-                                       '</a>
-                                       </div>
-                                       <div id="honorific-level">
-                                               <a href="' . htmlspecialchars( 
$level_link->getFullURL() ) . '" rel="nofollow">(' . 
$user_level->getLevelName() . ')</a>
-                                       </div>';
-               }
-               $output .= '<div class="visualClear"></div>
-                       </div>
-                       <div class="profile-actions">';
-
-               if ( $this->isOwner() ) {
-                       $output .= $wgLang->pipeList( array(
-                               '<a href="' . htmlspecialchars( 
$update_profile->getFullURL() ) . '">' . wfMessage( 'user-edit-profile' 
)->escaped() . '</a>',
-                               '<a href="' . htmlspecialchars( 
$upload_avatar->getFullURL() ) . '">' . wfMessage( 'user-upload-avatar' 
)->escaped() . '</a>',
-                               '<a href="' . htmlspecialchars( 
$watchlist->getFullURL() ) . '">' . wfMessage( 'user-watchlist' )->escaped() . 
'</a>',
-                               ''
-                       ) );
-               } elseif ( $wgUser->isLoggedIn() ) {
-                       if ( $relationship == false ) {
-                               $output .= $wgLang->pipeList( array(
-                                       '<a href="' . htmlspecialchars( 
$add_relationship->getFullURL( 'user=' . $user_safe . '&rel_type=1' ) ) . '" 
rel="nofollow">' . wfMessage( 'user-add-friend' )->escaped() . '</a>',
-                                       '<a href="' . htmlspecialchars( 
$add_relationship->getFullURL( 'user=' . $user_safe . '&rel_type=2' ) ) . '" 
rel="nofollow">' . wfMessage( 'user-add-foe' )->escaped() . '</a>',
-                                       ''
-                               ) );
-                       } else {
-                               if ( $relationship == 1 ) {
-                                       $output .= $wgLang->pipeList( array(
-                                               '<a href="' . htmlspecialchars( 
$remove_relationship->getFullURL( 'user=' . $user_safe ) ) . '">' . wfMessage( 
'user-remove-friend' )->escaped() . '</a>',
-                                               ''
-                                       ) );
-                               }
-                               if ( $relationship == 2 ) {
-                                       $output .= $wgLang->pipeList( array(
-                                               '<a href="' . htmlspecialchars( 
$remove_relationship->getFullURL( 'user=' . $user_safe ) ) . '">' . wfMessage( 
'user-remove-foe' )->escaped() . '</a>',
-                                               ''
-                                       ) );
-                               }
-                       }
-
-                       global $wgUserBoard;
-                       if ( $wgUserBoard ) {
-                               $output .= '<a href="' . htmlspecialchars( 
$send_message->getFullURL( 'user=' . $wgUser->getName() . '&conv=' . $user_safe 
) ) . '" rel="nofollow">' .
-                                       wfMessage( 'user-send-message' 
)->escaped() . '</a>';
-                               $output .= wfMessage( 'pipe-separator' 
)->escaped();
-                       }
-                       $output .= '<a href="' . htmlspecialchars( 
$give_gift->getFullURL( 'user=' . $user_safe ) ) . '" rel="nofollow">' .
-                               wfMessage( 'user-send-gift' )->escaped() . 
'</a>';
-                       $output .= wfMessage( 'pipe-separator' )->escaped();
-               }
-
-               $output .= '<a href="' . htmlspecialchars( 
$contributions->getFullURL() ) . '" rel="nofollow">' . wfMessage( 
'user-contributions' )->escaped() . '</a> ';
-
-               // Links to User:user_name from User_profile:
-               if ( $this->getTitle()->getNamespace() == NS_USER_PROFILE && 
$this->profile_data['user_id'] && $this->profile_data['user_page_type'] == 0 ) {
-                       $output .= '| <a href="' . htmlspecialchars( 
$user_page->getFullURL() ) . '" rel="nofollow">' .
-                               wfMessage( 'user-page-link' )->escaped() . 
'</a> ';
-               }
-
-               // Links to User:user_name from User_profile:
-               if ( $this->getTitle()->getNamespace() == NS_USER && 
$this->profile_data['user_id'] && $this->profile_data['user_page_type'] == 0 ) {
-                       $output .= '| <a href="' . htmlspecialchars( 
$user_social_profile->getFullURL() ) . '" rel="nofollow">' .
-                               wfMessage( 'user-social-profile-link' 
)->escaped() . '</a> ';
-               }
-
-               if ( $this->getTitle()->getNamespace() == NS_USER && ( 
!$this->profile_data['user_id'] || $this->profile_data['user_page_type'] == 1 ) 
) {
-                       $output .= '| <a href="' . htmlspecialchars( 
$user_wiki->getFullURL() ) . '" rel="nofollow">' .
-                               wfMessage( 'user-wiki-link' )->escaped() . 
'</a>';
-               }
-
-               $output .= '</div>
-
-               </div>';
-
-               return $output;
+               return new UserProfileHeader( $user_id, $user_name, 
$profile_data, $title, $is_owner );
        }
 
        /**

-- 
To view, visit https://gerrit.wikimedia.org/r/398710
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I9941b21c8f90cb670b80b82f62c96741b0d74428
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/SocialProfile
Gerrit-Branch: master
Gerrit-Owner: SamanthaNguyen <samanthanguyen1...@gmail.com>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to