Jdlrobson has uploaded a new change for review. https://gerrit.wikimedia.org/r/133680
Change subject: WIP Hygiene: Start using models for history and contributions page ...................................................................... WIP Hygiene: Start using models for history and contributions page Remove code duplication. Create class to capture what an edit is. Change-Id: I73ce4208f5f3d2f56920202e6c7dd6be86c46fdf --- M MobileFrontend.php A includes/models/MobileFrontendEdit.php M includes/specials/MobileSpecialPageFeed.php M includes/specials/SpecialMobileContributions.php M includes/specials/SpecialMobileHistory.php 5 files changed, 143 insertions(+), 130 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/MobileFrontend refs/changes/80/133680/1 diff --git a/MobileFrontend.php b/MobileFrontend.php index 383ce05..81a2e3b 100644 --- a/MobileFrontend.php +++ b/MobileFrontend.php @@ -85,6 +85,8 @@ 'MinervaTemplateBeta' => 'skins/MinervaTemplateBeta', 'MinervaTemplateAlpha' => 'skins/MinervaTemplateAlpha', + 'MobileFrontendEdit' => 'models/MobileFrontendEdit', + 'SkinMinerva' => 'skins/SkinMinerva', 'SkinMinervaBeta' => 'skins/SkinMinervaBeta', 'SkinMinervaAlpha' => 'skins/SkinMinervaAlpha', diff --git a/includes/models/MobileFrontendEdit.php b/includes/models/MobileFrontendEdit.php new file mode 100644 index 0000000..9e3d0d1 --- /dev/null +++ b/includes/models/MobileFrontendEdit.php @@ -0,0 +1,114 @@ +<?php +/** + * Models a single MediaWiki edit + * @todo FIXME: Upstream to core under generic Edit class name + */ +class MobileFrontendEdit { + private $isAnon; + private $username; + private $currentRevision; + private $previousRevision; + + /** + * Constructs a representation of an edit + * @param Revision $currentRevision the revision that was the result of the edit + * @param User $user the user who wants to view this edit. + * @param Revision $previousRevision an earlier revision you want to compare to. + * @return string HTML representing the link in the header bar + */ + public function __construct( Revision $currentRevision, User $user, $previousRevision = null ) { + $this->user = $user; + $this->currentRevision = $currentRevision; + $this->previousRevision = $previousRevision; + + $userId = $this->currentRevision->getUser( Revision::FOR_THIS_USER, $user ); + if ( $userId === 0 ) { + $this->username = IP::prettifyIP( $currentRevision->getRawUserText() ); + $this->isAnon = true; + } else { + $this->username = $currentRevision->getUserText( Revision::FOR_THIS_USER, $user ); + $this->isAnon = false; + } + // FIXME: Style differently user comment when this is the case + if ( !$currentRevision->userCan( Revision::DELETED_USER, $user ) ) { + $username = $this->msg( 'rev-deleted-user' )->plain(); + } + } + + public function isMinor() { + return $this->currentRevision->isMinor(); + } + + public function getUsername() { + return $this->username; + } + + public function isAnon() { + return $this->isAnon; + } + + public function getBytes() { + $bytes = $this->currentRevision->getSize(); + $prev = $this->previousRevision; + if ( $prev ) { + $bytes -= $prev->getSize(); + } + return $bytes; + } + + public function getTitle() { + return $this->currentRevision->getTitle(); + } + + public function getSummary() { + $user = $this->user; + $rev = $this->currentRevision; + // FIXME: Surface this so callers can style content differently + if ( $rev->userCan( Revision::DELETED_COMMENT, $user ) ) { + $comment = $this->formatComment( + $rev->getComment( Revision::FOR_THIS_USER, $user ) ); + } else { + $comment = $this->msg( 'rev-deleted-comment' )->plain(); + } + return $comment; + } + + /** + * Formats an edit comment + * @param string $comment The raw comment text + * @fixme: Duplication with SpecialMobileWatchlist + * + * @return string HTML code + */ + protected function formatComment( $comment ) { + $title = $this->currentRevision->getTitle(); + if ( $comment === '' ) { + $comment = wfMessage( 'mobile-frontend-changeslist-nocomment' )->plain(); + } else { + $comment = Linker::formatComment( $comment, $title ); + // flatten back to text + $comment = Sanitizer::stripAllTags( $comment ); + } + return $comment; + } + + public function getLink() { + $rev = $this->currentRevision; + $prev = $this->previousRevision; + $canSeeText = $rev->userCan( Revision::DELETED_TEXT, $this->user ); + if ( $canSeeText && $prev && $prev->userCan( Revision::DELETED_TEXT, $this->user ) ) { + $diffLink = SpecialPage::getTitleFor( 'MobileDiff', $rev->getId() )->getLocalUrl(); + } elseif ( $canSeeText && $rev->getTitle() !== null ) { + $diffLink = $rev->getTitle()->getLocalUrl( array( 'oldid' => $rev->getId() ) ); + } else { + $diffLink = false; + } + return $diffLink; + } + + public function getTimestamp() { + $ts = $this->currentRevision->getTimestamp(); + $ts = new MWTimestamp( $ts ); + return $ts; + } +} diff --git a/includes/specials/MobileSpecialPageFeed.php b/includes/specials/MobileSpecialPageFeed.php index ad55a15..f1ced54 100644 --- a/includes/specials/MobileSpecialPageFeed.php +++ b/includes/specials/MobileSpecialPageFeed.php @@ -7,6 +7,8 @@ abstract class MobileSpecialPageFeed extends MobileSpecialPage { /** @var bool Whether to show the username in results or not */ protected $showUsername = true; + /** @var bool Whether to show the title in results or not */ + protected $showTitle = true; public function execute( $par ) { $out = $this->getOutput(); @@ -14,25 +16,6 @@ $this->setHeaders(); $out->setProperty( 'unstyledContent', true ); parent::execute( $par ); - } - - /** - * Formats an edit comment - * @param string $comment The raw comment text - * @param Title $title The title of the page that was edited - * @fixme: Duplication with SpecialMobileWatchlist - * - * @return string HTML code - */ - protected function formatComment( $comment, $title ) { - if ( $comment === '' ) { - $comment = $this->msg( 'mobile-frontend-changeslist-nocomment' )->plain(); - } else { - $comment = Linker::formatComment( $comment, $title ); - // flatten back to text - $comment = Sanitizer::stripAllTags( $comment ); - } - return $comment; } /** @@ -58,59 +41,52 @@ /** * Renders an item in the feed - * FIXME: Rename to renderFeedItemHtml when pushed to stable - * @param MWTimestamp $ts The time the edit occurred - * @param string $diffLink url to the diff for the edit - * @param string $username The username of the user that made the edit (absent if anonymous) - * @param string $comment The edit summary - * @param Title|null $title The title of the page that was edited - * @param bool $isAnon Is the edit anonymous? - * @param int|null $bytes Net number of bytes changed or null if not applicable - * @param bool $isMinor Is the edit minor? - * @return string HTML code + * @param MobileFrontendEdit $edit */ - // FIXME: use an array as an argument? - protected function renderFeedItemHtml( $ts, $diffLink = '', $username = '', $comment = '', - $title = null, $isAnon = false, $bytes = 0, $isMinor = false ) { + protected function renderFeedItemHtml( $edit ) { wfProfileIn( __METHOD__ ); $output = $this->getOutput(); - $user = $this->getUser(); $lang = $this->getLanguage(); - if ( $isAnon ) { + if ( $edit->isAnon() ) { $usernameClass = 'mw-mf-user mw-mf-anon icon icon-text icon-16px'; } else { $usernameClass = 'mw-mf-user icon icon-16px icon-text'; } $html = Html::openElement( 'li' ); - + $diffLink = $edit->getLink(); if ( $diffLink ) { $html .= Html::openElement( 'a', array( 'href' => $diffLink, 'class' => 'title' ) ); } else { $html .= Html::openElement( 'div', array( 'class' => 'title' ) ); } - if ( $title ) { - $html .= Html::element( 'h3', array(), $title->getPrefixedText() ); + if ( $this->showTitle ) { + $html .= Html::element( 'h3', array(), $edit->getTitle()->getPrefixedText() ); } + $username = $edit->getUsername(); if ( $username && $this->showUsername ) { $html .= Html::element( 'p', array( 'class' => $usernameClass ), $username ); } $html .= Html::element( - 'p', array( 'class' => 'edit-summary component truncated-text multi-line two-line' ), $comment + 'p', array( + 'class' => 'edit-summary component truncated-text multi-line two-line' + ), $edit->getSummary() ); - if ( $isMinor ) { + if ( $edit->isMinor() ) { $html .= ChangesList::flag( 'minor' ); } $html .= Html::openElement( 'div', array( 'class' => 'listThumb' ) ) . - Html::element( 'p', array( 'class' => 'timestamp' ), $lang->userTime( $ts, $user ) ); + Html::element( 'p', array( 'class' => 'timestamp' ), + $lang->userTime( $edit->getTimestamp(), $this->getUser() ) ); + $bytes = $edit->getBytes(); if ( $bytes ) { $formattedBytes = $lang->formatNum( $bytes ); if ( $bytes > 0 ) { diff --git a/includes/specials/SpecialMobileContributions.php b/includes/specials/SpecialMobileContributions.php index 3497dbf..f5c139f 100644 --- a/includes/specials/SpecialMobileContributions.php +++ b/includes/specials/SpecialMobileContributions.php @@ -9,6 +9,7 @@ protected $lastDate; /** @var bool Whether to show the username in results or not */ protected $showUsername = false; + protected $showTitle = true; /** @var array Lengths of previous revisions */ protected $prevLengths = array(); @@ -83,47 +84,10 @@ protected function showContributionsRow( Revision $rev ) { wfProfileIn( __METHOD__ ); $user = $this->getUser(); - $userId = $rev->getUser( Revision::FOR_THIS_USER, $user ); - if ( $userId === 0 ) { - $username = IP::prettifyIP( $rev->getRawUserText() ); - $isAnon = true; - } else { - $username = $rev->getUserText( Revision::FOR_THIS_USER, $user ); - $isAnon = false; - } - - // FIXME: Style differently user comment when this is the case - if ( $rev->userCan( Revision::DELETED_COMMENT, $user ) ) { - $comment = $rev->getComment( Revision::FOR_THIS_USER, $user ); - $comment = $this->formatComment( $comment, $this->title ); - } else { - $comment = $this->msg( 'rev-deleted-comment' )->plain(); - } - - $ts = $rev->getTimestamp(); - $this->renderListHeaderWhereNeeded( $this->getLanguage()->userDate( $ts, $this->getUser() ) ); - $ts = new MWTimestamp( $ts ); - - if ( $rev->userCan( Revision::DELETED_TEXT, $user ) ) { - $diffLink = SpecialPage::getTitleFor( 'MobileDiff', $rev->getId() )->getLocalUrl(); - } else { - $diffLink = false; - } - - // FIXME: Style differently user comment when this is the case - if ( !$rev->userCan( Revision::DELETED_USER, $user ) ) { - $username = $this->msg( 'rev-deleted-user' )->plain(); - } - - $bytes = null; - if ( isset( $this->prevLengths[$rev->getParentId()] ) ) { - $bytes = $rev->getSize() - $this->prevLengths[$rev->getParentId()]; - } - $isMinor = $rev->isMinor(); - $this->renderFeedItemHtml( $ts, $diffLink, $username, $comment, - $rev->getTitle(), $isAnon, $bytes, $isMinor - ); - + $edit = new MobileFrontendEdit( $rev, $user ); + $date = $this->getLanguage()->userDate( $edit->getTimestamp(), $user ); + $this->renderListHeaderWhereNeeded( $date ); + $this->renderFeedItemHtml( $edit ); wfProfileOut( __METHOD__ ); } diff --git a/includes/specials/SpecialMobileHistory.php b/includes/specials/SpecialMobileHistory.php index 953fa61..975ab85 100644 --- a/includes/specials/SpecialMobileHistory.php +++ b/includes/specials/SpecialMobileHistory.php @@ -6,6 +6,7 @@ const DB_REVISIONS_TABLE = 'revision'; /** @var String|null timestamp to offset results from */ protected $offset; + protected $showTitle = false; /** @var String name of the special page */ protected $specialPageName = 'History'; @@ -115,55 +116,10 @@ protected function showRow( Revision $rev, $prev ) { wfProfileIn( __METHOD__ ); $user = $this->getUser(); - $userId = $rev->getUser( Revision::FOR_THIS_USER, $user ); - if ( $userId === 0 ) { - $username = IP::prettifyIP( $rev->getRawUserText() ); - $isAnon = true; - } else { - $username = $rev->getUserText( Revision::FOR_THIS_USER, $user ); - $isAnon = false; - } - - // FIXME: Style differently user comment when this is the case - if ( $rev->userCan( Revision::DELETED_COMMENT, $user ) ) { - $comment = $rev->getComment( Revision::FOR_THIS_USER, $user ); - $comment = $this->formatComment( $comment, $this->title ); - } else { - $comment = $this->msg( 'rev-deleted-comment' )->plain(); - } - - $ts = $rev->getTimestamp(); - $this->renderListHeaderWhereNeeded( $this->getLanguage()->userDate( $ts, $this->getUser() ) ); - $ts = new MWTimestamp( $ts ); - - $canSeeText = $rev->userCan( Revision::DELETED_TEXT, $user ); - if ( $canSeeText && $prev && $prev->userCan( Revision::DELETED_TEXT, $user ) ) { - $diffLink = SpecialPage::getTitleFor( 'MobileDiff', $rev->getId() )->getLocalUrl(); - } elseif ( $canSeeText && $rev->getTitle() !== null ) { - $diffLink = $rev->getTitle()->getLocalUrl( array( 'oldid' => $rev->getId() ) ); - } else { - $diffLink = false; - } - - // FIXME: Style differently user comment when this is the case - if ( !$rev->userCan( Revision::DELETED_USER, $user ) ) { - $username = $this->msg( 'rev-deleted-user' )->plain(); - } - - // When the page is named there is no need to print it in output - if ( $this->title ) { - $title = null; - } else { - $title = $rev->getTitle(); - } - $bytes = $rev->getSize(); - if ( $prev ) { - $bytes -= $prev->getSize(); - } - $isMinor = $rev->isMinor(); - $this->renderFeedItemHtml( $ts, $diffLink, $username, $comment, $title, $isAnon, $bytes, - $isMinor ); - + $edit = new MobileFrontendEdit( $rev, $user, $prev ); + $date = $this->getLanguage()->userDate( $edit->getTimestamp(), $user ); + $this->renderListHeaderWhereNeeded( $date ); + $this->renderFeedItemHtml( $edit ); wfProfileOut( __METHOD__ ); } @@ -193,6 +149,7 @@ } $rev2 = $rev1; } + // Render the original edit which had no preceeding edit if ( $rev1 && $numRows < self::LIMIT + 1 ) { $this->showRow( $rev1, null ); } -- To view, visit https://gerrit.wikimedia.org/r/133680 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I73ce4208f5f3d2f56920202e6c7dd6be86c46fdf Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/MobileFrontend Gerrit-Branch: master Gerrit-Owner: Jdlrobson <jrob...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits