jenkins-bot has submitted this change and it was merged.

Change subject: Factor out DB work out of UserProfile to separate UI from 
backend
......................................................................


Factor out DB work out of UserProfile to separate UI from backend

Change-Id: Ieb2c1482ae86e3f781f3655fec172130f1b1f3cd
---
M MobileFrontend.php
A includes/MobileUserInfo.php
M includes/specials/SpecialUserProfile.php
3 files changed, 167 insertions(+), 127 deletions(-)

Approvals:
  Yuvipanda: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/MobileFrontend.php b/MobileFrontend.php
index d771d01..15b6e2d 100644
--- a/MobileFrontend.php
+++ b/MobileFrontend.php
@@ -39,6 +39,7 @@
        'DeviceDetection' => 'DeviceDetection',
        'HtmlDeviceProperties' => 'DeviceDetection',
        'MobileContext' => 'MobileContext',
+       'MobileUserInfo' => 'MobileUserInfo',
        'WmlContext' => 'WmlContext',
        'WmlDeviceProperties' => 'DeviceDetection',
 
diff --git a/includes/MobileUserInfo.php b/includes/MobileUserInfo.php
new file mode 100644
index 0000000..4dbcda0
--- /dev/null
+++ b/includes/MobileUserInfo.php
@@ -0,0 +1,116 @@
+<?php
+
+/**
+ * Retrieves information about a user for Special:MobileProfile
+ */
+class MobileUserInfo {
+       const LIMIT = 500;
+
+       /**
+        * @var User: User whose information is retrieved
+        */
+       private $user;
+
+       public function __construct( User $user ) {
+               if ( $user->isAnon() ) {
+                       throw new MWException( __CLASS__ . ' is intended for 
logged in users only' );
+               }
+               $this->user = $user;
+       }
+
+       /**
+        * Returns a count of the most recent edits since a given timestamp
+        *
+        * @param Integer $fromDate Time to measure from
+        * @return Integer the amount of edits
+        */
+       public function countRecentEdits( $fromDate ) {
+               wfProfileIn( __METHOD__ );
+               $dbr = wfGetDB( DB_SLAVE );
+               $where = array(
+                       'rc_user_text' => $this->user->getName(),
+               );
+               $where[] = 'rc_timestamp > ' . $dbr->addQuotes( 
$dbr->timestamp( $fromDate ) );
+               $constraints = array(
+                       'limit' => self::LIMIT + 1,
+               );
+               $innerSelect = $dbr->selectSQLText( 'recentchanges', 
'rc_timestamp', $where, __METHOD__, $constraints );
+               $res = $dbr->query( "SELECT COUNT(*) FROM ($innerSelect) t", 
__METHOD__ );
+               $row = $res->fetchRow();
+               $result = 0;
+               if ( $row ) {
+                       $result = $row[0];
+               }
+               wfProfileOut( __METHOD__ );
+               return $result;
+       }
+
+       /**
+        * Returns a count of the most recent uploads to $wgMFPhotoUploadWiki 
since a given timestamp
+        *
+        * @param Integer $fromDate Time to measure from
+        * @return Integer the amount of edits
+        */
+       public function countRecentUploads( $fromDate ) {
+               global $wgMFPhotoUploadWiki, $wgConf;
+
+               wfProfileIn( __METHOD__ );
+               if ( !$wgMFPhotoUploadWiki ) {
+                       $dbr = wfGetDB( DB_SLAVE );
+               } elseif (
+                       $wgMFPhotoUploadWiki &&
+                       !in_array( $wgMFPhotoUploadWiki, 
$wgConf->getLocalDatabases() )
+               ) {
+                       // early return if the database is invalid
+                       wfProfileOut( __METHOD__ );
+                       return false;
+               } else {
+                       $dbr = wfGetDB( DB_SLAVE, array(), $wgMFPhotoUploadWiki 
);
+               }
+
+               $where = array( 'img_user_text' => $this->user->getName() );
+               $where[] = 'img_timestamp > ' . $dbr->addQuotes( 
$dbr->timestamp( $fromDate ) );
+               $constraints = array(
+                       'limit' => self::LIMIT + 1,
+               );
+               $innerSelect = $dbr->selectSQLText( 'image', 'img_timestamp', 
$where, __METHOD__, $constraints );
+               $res = $dbr->query( "SELECT COUNT(*) FROM ($innerSelect) t", 
__METHOD__ );
+               $row = $res->fetchRow();
+               $result = 0;
+               if ( $row ) {
+                       $result = $row[0];
+               }
+               wfProfileOut( __METHOD__ );
+               return $result;
+       }
+
+       /**
+        * Returns last file uploaded by user
+        *
+        * @return File|null
+        */
+       public function getLastUpload() {
+               global $wgMFPhotoUploadWiki, $wgConf;
+
+               wfProfileIn( __METHOD__ );
+               if ( !$wgMFPhotoUploadWiki ) {
+                       $dbr = wfGetDB( DB_SLAVE );
+               } elseif (
+                       $wgMFPhotoUploadWiki &&
+                       !in_array( $wgMFPhotoUploadWiki, 
$wgConf->getLocalDatabases() )
+               ) {
+                       // early return if the database is invalid
+                       wfProfileOut( __METHOD__ );
+                       return false;
+               } else {
+                       $dbr = wfGetDB( DB_SLAVE, array(), $wgMFPhotoUploadWiki 
);
+               }
+
+               $where = array( 'img_user_text' => $this->user->getName() );
+               $constraints = array( 'ORDER BY' => 'img_timestamp DESC' );
+               $name = $dbr->selectField( 'image', 'img_name', $where, 
__METHOD__, $constraints );
+               $file = $name === false ? null : wfFindFile( $name );
+               wfProfileOut( __METHOD__ );
+               return $file;
+       }
+}
\ No newline at end of file
diff --git a/includes/specials/SpecialUserProfile.php 
b/includes/specials/SpecialUserProfile.php
index dac85ec..423a282 100644
--- a/includes/specials/SpecialUserProfile.php
+++ b/includes/specials/SpecialUserProfile.php
@@ -1,7 +1,14 @@
 <?php
 
 class SpecialUserProfile extends MobileSpecialPage {
-       const LIMIT = 500;
+       /**
+        * @var User
+        */
+       private $targetUser;
+       /**
+        * @var MobileUserInfo
+        */
+       private $userInfo;
 
        public function __construct() {
                parent::__construct( 'UserProfile' );
@@ -25,154 +32,69 @@
        }
 
        /**
-        * Returns a count of the most recent edits since a given timestamp
-        *
-        * @param User $user The user name to query against
-        * @param Integer $fromDate Time to measure from
-        * @return Integer the amount of edits
-        */
-       protected function countRecentEdits( $user, $fromDate ) {
-               wfProfileIn( __METHOD__ );
-               $dbr = wfGetDB( DB_SLAVE );
-               $where = array(
-                       'rc_user_text' => $user->getName(),
-               );
-               $where[] = 'rc_timestamp > ' . $dbr->addQuotes( 
$dbr->timestamp( $fromDate ) );
-               $constraints = array(
-                       'limit' => self::LIMIT + 1,
-               );
-               $innerSelect = $dbr->selectSQLText( 'recentchanges', 
'rc_timestamp', $where, __METHOD__, $constraints );
-               $res = $dbr->query( "SELECT COUNT(*) FROM ($innerSelect) t", 
__METHOD__ );
-               $row = $res->fetchRow();
-               $result = 0;
-               if ( $row ) {
-                       $result = $row[0];
-               }
-               wfProfileOut( __METHOD__ );
-               return $result;
-       }
-
-       /**
-        * Returns a count of the most recent uploads to $wgMFPhotoUploadWiki 
since a given timestamp
-        *
-        * @param User $user The user name to query against
-        * @param Integer $fromDate Time to measure from
-        * @return Integer the amount of edits
-        */
-       protected function countRecentUploads( $user, $fromDate ) {
-               global $wgMFPhotoUploadWiki, $wgConf;
-
-               wfProfileIn( __METHOD__ );
-               if ( !$wgMFPhotoUploadWiki ) {
-                       $dbr = wfGetDB( DB_SLAVE );
-               } elseif (
-                               $wgMFPhotoUploadWiki &&
-                               !in_array( $wgMFPhotoUploadWiki, 
$wgConf->getLocalDatabases() )
-                       ) {
-                       // early return if the database is invalid
-                       wfProfileOut( __METHOD__ );
-                       return false;
-               } else {
-                       $dbr = wfGetDB( DB_SLAVE, array(), $wgMFPhotoUploadWiki 
);
-               }
-
-               $where = array( 'img_user_text' => $user->getName() );
-               $where[] = 'img_timestamp > ' . $dbr->addQuotes( 
$dbr->timestamp( $fromDate ) );
-               $constraints = array(
-                       'limit' => self::LIMIT + 1,
-               );
-               $innerSelect = $dbr->selectSQLText( 'image', 'img_timestamp', 
$where, __METHOD__, $constraints );
-               $res = $dbr->query( "SELECT COUNT(*) FROM ($innerSelect) t", 
__METHOD__ );
-               $row = $res->fetchRow();
-               $result = 0;
-               if ( $row ) {
-                       $result = $row[0];
-               }
-               wfProfileOut( __METHOD__ );
-               return $result;
-       }
-
-       /**
         * Returns HTML to show the last upload or a message where there is no 
last upload
         *
-        * @param User $user The user name to query against
         * @return String HTML string representing the last upload by the user
         */
-       protected function getLastUpload( $user ) {
-               global $wgMFPhotoUploadWiki, $wgConf;
-
+       protected function getLastUpload() {
                wfProfileIn( __METHOD__ );
-               if ( !$wgMFPhotoUploadWiki ) {
-                       $dbr = wfGetDB( DB_SLAVE );
-               } elseif (
-                               $wgMFPhotoUploadWiki &&
-                               !in_array( $wgMFPhotoUploadWiki, 
$wgConf->getLocalDatabases() )
-                       ) {
-                       // early return if the database is invalid
+
+               $file = $this->userInfo->getLastUpload();
+               if ( !$file ) {
                        wfProfileOut( __METHOD__ );
-                       return false;
-               } else {
-                       $dbr = wfGetDB( DB_SLAVE, array(), $wgMFPhotoUploadWiki 
);
+                       return '';
                }
 
-               $where = array( 'img_user_text' => $user->getName() );
-               $constraints = array( 'LIMIT' => 1, 'ORDER BY' => 
'img_timestamp DESC' );
-               $res = $dbr->select( 'image', 'img_name, img_timestamp', 
$where, __METHOD__, $constraints );
-               foreach( $res as $row ) {
-                       $name = $row->img_name;
-                       $file = wfFindFile( $name );
-                       $title = Title::newFromText( $name, NS_FILE );
-                       $ts = new MWTimestamp( wfTimestamp( TS_UNIX, 
$row->img_timestamp ) );
-                       $daysAgo = $this->getDaysAgo( $ts );
+               $title = $file->getTitle();
+               $ts = new MWTimestamp( $file->getTimestamp() );
+               $daysAgo = $this->getDaysAgo( $ts );
 
-                       $img = Html::openElement( 'div', array( 'class' => 
'last-upload section-end' ) ) .
-                               Html::openElement( 'a', array( 'href' => 
$title->getLocalUrl() ) ) .
-                               $file->transform( array( 'width' => 320, 
'height' => 320 ) )->toHtml() .
-                               Html::openElement( 'div', array( 'class' => 
'thumbcaption secondary-statement' ) ) .
-                               $this->msg( 
'mobile-frontend-profile-last-upload-caption', $user, $daysAgo )->parse() .
-                               Html::closeElement( 'div' ) .
-                               Html::closeElement( 'a' ) .
-                               Html::closeElement( 'div' );
-                       wfProfileOut( __METHOD__ );
-                       return $img;
-               }
+               $img = Html::openElement( 'div', array( 'class' => 'last-upload 
section-end' ) ) .
+                       Html::openElement( 'a', array( 'href' => 
$title->getLocalUrl() ) ) .
+                       $file->transform( array( 'width' => 320, 'height' => 
320 ) )->toHtml() .
+                       Html::openElement( 'div', array( 'class' => 
'thumbcaption secondary-statement' ) ) .
+                       $this->msg( 
'mobile-frontend-profile-last-upload-caption', $this->targetUser->getName(), 
$daysAgo )->parse() .
+                       Html::closeElement( 'div' ) .
+                       Html::closeElement( 'a' ) .
+                       Html::closeElement( 'div' );
                wfProfileOut( __METHOD__ );
-               return '';
+               return $img;
        }
 
-       protected function getUserSummary( User $user ) {
-               $registered = $user->getRegistration();
-               $editCount = $user->getEditCount();
+       protected function getUserSummary() {
+               $registered = $this->targetUser->getRegistration();
+               $editCount = $this->targetUser->getEditCount();
                $ts = new MWTimestamp( wfTimestamp( TS_UNIX, $registered ) );
                $daysAgo = $this->getDaysAgo( $ts );
 
+               $name = $this->targetUser->getName();
                if( $editCount < 50 ) {
-                       $role = $this->msg( 
'mobile-frontend-profile-user-desc-1', $user );
+                       $role = $this->msg( 
'mobile-frontend-profile-user-desc-1', $name );
                } else if ( $editCount < 5000 ) {
-                       $role = $this->msg( 
'mobile-frontend-profile-user-desc-2', $user );
+                       $role = $this->msg( 
'mobile-frontend-profile-user-desc-2', $name );
                } else {
-                       $role = $this->msg( 
'mobile-frontend-profile-user-desc-3', $user );
+                       $role = $this->msg( 
'mobile-frontend-profile-user-desc-3', $name );
                }
 
                return Html::openElement( 'div', array( 'class' => 'section 
section-registered' ) ) .
                        Html::element( 'p', array( 'class' => 'statement' ),
-                       $this->msg( 'mobile-frontend-profile-registration', 
$user->getName(), $daysAgo, $editCount )->parse() ) .
+                       $this->msg( 'mobile-frontend-profile-registration', 
$name, $daysAgo, $editCount )->parse() ) .
                        Html::element( 'p', array( 'class' => 
'secondary-statement section-end' ), $role ) .
                        Html::closeElement( 'div' );
        }
 
-       protected function getRecentActivityHtml( User $user ) {
+       protected function getRecentActivityHtml() {
                wfProfileIn( __METHOD__ );
                // render
                $fromDate = time() - ( 3600 * 24 * 30 );
-               $count = $this->countRecentEdits( $user, $fromDate );
-               $uploadCount = $this->countRecentUploads( $user, $fromDate );
+               $count = $this->userInfo->countRecentEdits( $fromDate );
+               $uploadCount = $this->userInfo->countRecentUploads( $fromDate );
 
-               $urlContributions = SpecialPage::getTitleFor( 'Contributions', 
$user->getName() )->getLocalUrl();
-               $urlUploads = SpecialPage::getTitleFor( 'Uploads', 
$user->getName() )->getLocalUrl();
-               $msgUploads = $uploadCount > self::LIMIT ? $this->msg( 
'mobile-frontend-profile-uploads-limit', self::LIMIT ) :
+               $urlContributions = SpecialPage::getTitleFor( 'Contributions', 
$this->targetUser->getName() )->getLocalUrl();
+               $urlUploads = SpecialPage::getTitleFor( 'Uploads', 
$this->targetUser->getName() )->getLocalUrl();
+               $msgUploads = $uploadCount > MobileUserInfo::LIMIT ? 
$this->msg( 'mobile-frontend-profile-uploads-limit', MobileUserInfo::LIMIT ) :
                        $this->msg( 'mobile-frontend-profile-uploads', 
$uploadCount );
-               $msgEdits = $count > self::LIMIT ? $this->msg( 
'mobile-frontend-profile-edits-limit', self::LIMIT ) :
+               $msgEdits = $count > MobileUserInfo::LIMIT ? $this->msg( 
'mobile-frontend-profile-edits-limit', MobileUserInfo::LIMIT ) :
                                $this->msg( 'mobile-frontend-profile-edits', 
$count );
                $statsRecent = array(
                        Html::element( 'a',
@@ -182,7 +104,7 @@
                                array( 'href' => $urlUploads, 'class' => 
'statement' ),
                                $msgUploads ),
                );
-               $lastUploadHtml = $this->getLastUpload( $user );
+               $lastUploadHtml = $this->getLastUpload();
                if ( $lastUploadHtml ) {
                        $statsRecent[] = $lastUploadHtml;
                }
@@ -192,17 +114,17 @@
                return $html;
        }
 
-       protected function setUserProfileUIElements( User $user ) {
+       protected function setUserProfileUIElements() {
                // replace secondary icon
                $attrs = array(
                        'class' => 'talk',
                        'id' => 'secondary-button',
-                       'href' => $user->getTalkPage()->getLocalUrl(),
+                       'href' => 
$this->targetUser->getTalkPage()->getLocalUrl(),
                );
                $secondaryButton = Html::element( 'a', $attrs, $this->msg( 
'mobile-frontend-profile-usertalk' ) );
 
                // define heading
-               $heading = Html::element( 'h1', array(), $user->getName() );
+               $heading = Html::element( 'h1', array(), 
$this->targetUser->getName() );
 
                // set values
                $skin = $this->getSkin();
@@ -237,13 +159,14 @@
                if ( !$ctx->isBetaGroupMember() ) {
                        $html = $this->getHtmlBetaAlphaOptIn();
                } else if ( $par ) {
-                       $user = User::newFromName( $par );
+                       $this->targetUser = User::newFromName( $par );
                        // prepare content
-                       if ( $user ) {
-                               $this->setUserProfileUIElements( $user );
+                       if ( $this->targetUser ) {
+                               $this->userInfo = new MobileUserInfo( 
$this->targetUser );
+                               $this->setUserProfileUIElements();
                                $html = Html::openElement( 'div', array( 
'class' => 'profile' ) ) .
-                                       $this->getUserSummary( $user ) . 
$this->getRecentActivityHtml( $user ) .
-                                       Linker::link( $user->getUserPage(),
+                                       $this->getUserSummary() . 
$this->getRecentActivityHtml() .
+                                       Linker::link( 
$this->targetUser->getUserPage(),
                                                $this->msg( 
'mobile-frontend-profile-userpage-link' ),
                                                array( 'class' => 'user-page 
section-end' ) ) .
                                        Html::closeElement( 'div' );

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

Gerrit-MessageType: merged
Gerrit-Change-Id: Ieb2c1482ae86e3f781f3655fec172130f1b1f3cd
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/MobileFrontend
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>
Gerrit-Reviewer: Yuvipanda <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to