Improve page display title handling for category pages
authorNiklas Laxström <niklas.laxstrom@gmail.com>
Wed, 9 Nov 2016 06:24:57 +0000 (07:24 +0100)
committerNiklas Laxström <niklas.laxstrom@gmail.com>
Mon, 10 Sep 2018 06:29:48 +0000 (08:29 +0200)
One use case of display title is to localise page names with Translate
extension or without. While the page title changes, the subheadings still
say something like "Pages in category Foo/de".

Also converted one raw HTML message to be a parsed message.

First version of this patch caused an issue when previewing
because page title in h1 is not the same as page display title.

This issue is fixed by promoting page display title as it's own member
in OutputPage. Also added getUnprefixedDisplayTitle that attempts to
strip away the namespace prefix to mimic Title::getText() but which
works with display title instead.

Bug: T43720
Bug: T46197
Change-Id: I6097a873297eb57759252fc56ad6d02c44e4c366

RELEASE-NOTES-1.32
includes/CategoryViewer.php
includes/EditPage.php
includes/OutputPage.php
includes/page/Article.php

index 0dca8f1..9784a7f 100644 (file)
@@ -114,6 +114,7 @@ production.
 === Bug fixes in 1.32 ===
 * SpecialPage::execute() will now only call checkLoginSecurityLevel() if
   getLoginSecurityLevel() returns non-false.
+* (T43720, T46197) Improved page display title handling for category pages
 
 === Action API changes in 1.32 ===
 * Added templated parameters.
index 46a1473..a07e1b4 100644 (file)
@@ -424,7 +424,7 @@ class CategoryViewer extends ContextSource {
         * @return string
         */
        function getPagesSection() {
-               $ti = wfEscapeWikiText( $this->title->getText() );
+               $name = $this->getOutput()->getUnprefixedDisplayTitle();
                # Don't show articles section if there are none.
                $r = '';
 
@@ -440,7 +440,7 @@ class CategoryViewer extends ContextSource {
 
                if ( $rescnt > 0 ) {
                        $r = "<div id=\"mw-pages\">\n";
-                       $r .= '<h2>' . $this->msg( 'category_header', $ti )->parse() . "</h2>\n";
+                       $r .= '<h2>' . $this->msg( 'category_header' )->rawParams( $name )->parse() . "</h2>\n";
                        $r .= $countmsg;
                        $r .= $this->getSectionPagingLinks( 'page' );
                        $r .= $this->formatList( $this->articles, $this->articles_start_char );
@@ -454,6 +454,7 @@ class CategoryViewer extends ContextSource {
         * @return string
         */
        function getImageSection() {
+               $name = $this->getOutput()->getUnprefixedDisplayTitle();
                $r = '';
                $rescnt = $this->showGallery ? $this->gallery->count() : count( $this->imgsNoGallery );
                $dbcnt = $this->cat->getFileCount();
@@ -463,10 +464,7 @@ class CategoryViewer extends ContextSource {
                if ( $rescnt > 0 ) {
                        $r .= "<div id=\"mw-category-media\">\n";
                        $r .= '<h2>' .
-                               $this->msg(
-                                       'category-media-header',
-                                       wfEscapeWikiText( $this->title->getText() )
-                               )->text() .
+                               $this->msg( 'category-media-header' )->rawParams( $name )->parse() .
                                "</h2>\n";
                        $r .= $countmsg;
                        $r .= $this->getSectionPagingLinks( 'file' );
index ef111c4..b3fe660 100644 (file)
@@ -2490,6 +2490,8 @@ ERROR;
                $displayTitle = isset( $this->mParserOutput ) ? $this->mParserOutput->getDisplayTitle() : false;
                if ( $displayTitle === false ) {
                        $displayTitle = $contextTitle->getPrefixedText();
+               } else {
+                       $out->setDisplayTitle( $displayTitle );
                }
                $out->setPageTitle( $this->context->msg( $msg, $displayTitle ) );
 
index 4f12e0c..99a4c2b 100644 (file)
@@ -58,6 +58,15 @@ class OutputPage extends ContextSource {
         * @var string The contents of <h1> */
        private $mPageTitle = '';
 
+       /**
+        * @var string The displayed title of the page. Different from page title
+        * if overridden by display title magic word or hooks. Can contain safe
+        * HTML. Different from page title which may contain messages such as
+        * "Editing X" which is displayed in h1. This can be used for other places
+        * where the page name is referred on the page.
+        */
+       private $displayTitle;
+
        /**
         * @var string Contains all of the "<body>" content. Should be private we
         *   got set/get accessors and the append() method.
@@ -964,6 +973,48 @@ class OutputPage extends ContextSource {
                return $this->mPageTitle;
        }
 
+       /**
+        * Same as page title but only contains name of the page, not any other text.
+        *
+        * @since 1.32
+        * @param string $html Page title text.
+        * @see OutputPage::setPageTitle
+        */
+       public function setDisplayTitle( $html ) {
+               $this->displayTitle = $html;
+       }
+
+       /**
+        * Returns page display title.
+        *
+        * Performs some normalization, but this not as strict the magic word.
+        *
+        * @since 1.32
+        * @return string HTML
+        */
+       public function getDisplayTitle() {
+               $html = $this->displayTitle;
+               if ( $html === null ) {
+                       $html = $this->getTitle()->getPrefixedText();
+               }
+
+               return Sanitizer::normalizeCharReferences( Sanitizer::removeHTMLtags( $html ) );
+       }
+
+       /**
+        * Returns page display title without namespace prefix if possible.
+        *
+        * @since 1.32
+        * @return string HTML
+        */
+       public function getUnprefixedDisplayTitle() {
+               $text = $this->getDisplayTitle();
+               $nsPrefix = $this->getTitle()->getNsText() . ':';
+               $prefix = preg_quote( $nsPrefix, '/' );
+
+               return preg_replace( "/^$prefix/i", '', $text );
+       }
+
        /**
         * Set the Title object to use
         *
index e90334f..589da2f 100644 (file)
@@ -702,10 +702,13 @@ class Article implements Page {
         * @param ParserOutput $pOutput
         */
        public function adjustDisplayTitle( ParserOutput $pOutput ) {
+               $out = $this->getContext()->getOutput();
+
                # Adjust the title if it was set by displaytitle, -{T|}- or language conversion
                $titleText = $pOutput->getTitleText();
                if ( strval( $titleText ) !== '' ) {
-                       $this->getContext()->getOutput()->setPageTitle( $titleText );
+                       $out->setPageTitle( $titleText );
+                       $out->setDisplayTitle( $titleText );
                }
        }