Use display name in category page subheadings if provided
authorNiklas Laxström <niklas.laxstrom@gmail.com>
Thu, 19 May 2016 12:54:48 +0000 (14:54 +0200)
committerSébastien Santoro <dereckson@espace-win.org>
Wed, 8 Jun 2016 22:20:48 +0000 (22:20 +0000)
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".

A display title is now used if provided. There is one questionable thing
what to do with namespaces and display title. In this case I think it is
better to have the namespace prefix displayed (or not) according to what
the site admin wants, because we cannot safely strip the namespace prefix
from display title.

By using the page title from OutputPage, we already get a name which is
safe for HTML with no further processing. The name is passed as a raw
parameter which breaks magic words (if any) trying to access the value.
It is easy to fix this by using FULLPAGENAME magic word instead.

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

Bug: T43720
Change-Id: Ide7d4a9ee5c76b6360b53aefd76a2e17a139173f

includes/CategoryViewer.php

index 389b077..cf471d9 100644 (file)
@@ -407,11 +407,26 @@ class CategoryViewer extends ContextSource {
                return $r;
        }
 
+       /**
+        * Return pretty name which is display name if given and different from prefix text or
+        * the unprefixed page name.
+        *
+        * @return string HTML safe name.
+        */
+       function getPrettyPageNameHtml() {
+               $displayTitle = $this->getOutput()->getPageTitle();
+               if ( $displayTitle === $this->getTitle()->getPrefixedText() ) {
+                       return htmlspecialchars( $this->getTitle()->getText() );
+               } else {
+                       return $displayTitle;
+               }
+       }
+
        /**
         * @return string
         */
        function getPagesSection() {
-               $ti = wfEscapeWikiText( $this->title->getText() );
+               $name = $this->getPrettyPageNameHtml();
                # Don't show articles section if there are none.
                $r = '';
 
@@ -427,7 +442,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 );
@@ -441,6 +456,7 @@ class CategoryViewer extends ContextSource {
         * @return string
         */
        function getImageSection() {
+               $name = $this->getPrettyPageNameHtml();
                $r = '';
                $rescnt = $this->showGallery ? $this->gallery->count() : count( $this->imgsNoGallery );
                $dbcnt = $this->cat->getFileCount();
@@ -450,10 +466,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' );