Catrope has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/284845

Change subject: Clean up section title handling in mention and edit-user-talk 
presentation models
......................................................................

Clean up section title handling in mention and edit-user-talk presentation 
models

They were using different code and different approaches.
I've left getSection() and the code for creating a Title
with a section duplicated for now, and I'll move them
into a trait in the next commit. I wanted to do that
separately because we aren't using traits in this
code base yet.

Change-Id: I56cf745d2cc8a6c43caec08024187de9598cecb8
---
M includes/formatters/EditUserTalkPresentationModel.php
M includes/formatters/EventPresentationModel.php
M includes/formatters/MentionPresentationModel.php
3 files changed, 41 insertions(+), 52 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Echo 
refs/changes/45/284845/1

diff --git a/includes/formatters/EditUserTalkPresentationModel.php 
b/includes/formatters/EditUserTalkPresentationModel.php
index d06222a..27ddf5a 100644
--- a/includes/formatters/EditUserTalkPresentationModel.php
+++ b/includes/formatters/EditUserTalkPresentationModel.php
@@ -2,6 +2,8 @@
 
 class EchoEditUserTalkPresentationModel extends EchoEventPresentationModel {
 
+       private $sectionTitle = null;
+
        public function canRender() {
                return (bool)$this->event->getTitle();
        }
@@ -16,11 +18,12 @@
                        $title = Title::makeTitle(
                                $title->getNamespace(),
                                $title->getDBkey(),
-                               $this->formatSubjectAnchor()
+                               $this->getSection()
                        );
                }
 
                return array(
+                       // Need FullURL so the section is included
                        'url' => $title->getFullURL(),
                        'label' => $this->msg( 
'notification-link-text-view-message' )->text()
                );
@@ -54,7 +57,7 @@
                } elseif ( $this->hasSection() ) {
                        $msg = $this->getMessageWithAgent( 
"notification-header-{$this->type}-with-section" );
                        $msg->params( $this->getViewingUserForGender() );
-                       $msg->plaintextParams( $this->language->embedBidi( 
$this->getSectionTitleSnippet() ) );
+                       $msg->plaintextParams( $this->getTruncatedSectionTitle( 
$this->getSection() ) );
                        return $msg;
                } else {
                        $msg = parent::getHeaderMessage();
@@ -66,7 +69,8 @@
        public function getBodyMessage() {
                if ( !$this->isBundled() && $this->hasSection() ) {
                        $msg = $this->msg( 
'notification-body-edit-user-talk-with-section' );
-                       $msg->plaintextParams( $this->getRevisionSnippet() );
+                       // section-text is safe to use here, because 
hasSection() returns false if the revision is deleted
+                       $msg->plaintextParams( $this->event->getExtraParam( 
'section-text' ) );
                        return $msg;
                } else {
                        return false;
@@ -74,53 +78,26 @@
        }
 
        private function hasSection() {
-               return (bool)$this->event->getExtraParam( 'section-title' );
+               return (bool)$this->getSection();
        }
 
-       /**
-        * Get the section title for a talk page post
-        * @return string
-        */
-       private function getSectionTitleSnippet() {
-               if ( $this->userCan( Revision::DELETED_TEXT ) ) {
-                       return EchoDiscussionParser::getTextSnippet(
-                               $this->event->getExtraParam( 'section-title' ),
-                               $this->language,
-                               self::SECTION_TITLE_RECOMMENDED_LENGTH
-                       );
-               } else {
-                       return $this->msg( 'echo-rev-deleted-text-view' 
)->text();
-               }
-       }
-
-       private function getRevisionSnippet() {
-               $sectionText = $this->event->getExtraParam( 'section-text' );
-               if ( $sectionText === null || !$this->userCan( 
Revision::DELETED_TEXT ) ) {
-                       return '';
-               }
-
-               return trim( $sectionText );
-       }
-
-       /**
-        * Extract the subject anchor (linkable portion of the edited page) from
-        * the event.
-        *
-        * @return string The anchor on page, or an empty string
-        */
-       private function formatSubjectAnchor() {
-               global $wgParser;
-
-               if ( !$this->userCan( Revision::DELETED_TEXT ) ) {
-                       return $this->msg( 'echo-rev-deleted-text-view' 
)->text();
+       private function getSection() {
+               if ( $this->sectionTitle !== null ) {
+                       return $this->sectionTitle;
                }
                $sectionTitle = $this->event->getExtraParam( 'section-title' );
-               if ( $sectionTitle === null ) {
-                       return '';
+               if ( !$sectionTitle ) {
+                       $this->sectionTitle = false;
+                       return false;
+               }
+               // Check permissions
+               if ( !$this->userCan( Revision::DELETED_TEXT ) ) {
+                       $this->sectionTitle = false;
+                       return false;
                }
 
-               // Strip out #
-               return substr( $wgParser->guessLegacySectionNameFromWikiText( 
$sectionTitle ), 1 );
+               $this->sectionTitle = $sectionTitle;
+               return $this->sectionTitle;
        }
 
        private function getDiffLinkUrl() {
diff --git a/includes/formatters/EventPresentationModel.php 
b/includes/formatters/EventPresentationModel.php
index 5f8fdad..bf18369 100644
--- a/includes/formatters/EventPresentationModel.php
+++ b/includes/formatters/EventPresentationModel.php
@@ -373,6 +373,15 @@
                return $this->language->embedBidi( $this->language->truncate( 
$text, self::PAGE_NAME_RECOMMENDED_LENGTH, '...', false ) );
        }
 
+       protected function getTruncatedSectionTitle( $section ) {
+               return $this->language->embedBidi( $this->language->truncate(
+                       EchoDiscussionParser::getTextSnippet( $section, 
$this->language ),
+                       self::SECTION_TITLE_RECOMMENDED_LENGTH,
+                       '...',
+                       false
+               ) );
+       }
+
        /**
         * @param User|null $user
         * @return array|null
diff --git a/includes/formatters/MentionPresentationModel.php 
b/includes/formatters/MentionPresentationModel.php
index 57debfb..eafeb0e 100644
--- a/includes/formatters/MentionPresentationModel.php
+++ b/includes/formatters/MentionPresentationModel.php
@@ -73,13 +73,7 @@
 
                $section = $this->getSection();
                if ( $section ) {
-                       $msg->plaintextParams( $this->language->embedBidi(
-                               EchoDiscussionParser::getTextSnippet(
-                                               $section,
-                                               $this->language,
-                                               
self::SECTION_TITLE_RECOMMENDED_LENGTH
-                               )
-                       ) );
+                       $msg->plaintextParams( $this->getTruncatedSectionTitle( 
$section ) );
                }
 
                return $msg;
@@ -119,9 +113,18 @@
        }
 
        public function getPrimaryLink() {
+               $title = $this->event->getTitle();
+               $section = $this->getSection();
+               if ( $section ) {
+                       $title = Title::makeTitle(
+                               $title->getNamespace(),
+                               $title->getDBkey(),
+                               $section
+                       );
+               }
                return array(
                        // Need FullURL so the section is included
-                       'url' => $this->getTitleWithSection()->getFullURL(),
+                       'url' => $title->getFullURL(),
                        'label' => $this->msg( 
'notification-link-text-view-mention' )->text()
                );
        }

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I56cf745d2cc8a6c43caec08024187de9598cecb8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Echo
Gerrit-Branch: master
Gerrit-Owner: Catrope <[email protected]>

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

Reply via email to