From d89704bc4cef71af0a4fcebab2c828caf9186c7b Mon Sep 17 00:00:00 2001 From: =?utf8?q?Niklas=20Laxstr=C3=B6m?= Date: Wed, 9 Nov 2016 07:24:57 +0100 Subject: [PATCH] Improve page display title handling for category pages 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 | 1 + includes/CategoryViewer.php | 10 +++----- includes/EditPage.php | 2 ++ includes/OutputPage.php | 51 +++++++++++++++++++++++++++++++++++++ includes/page/Article.php | 5 +++- 5 files changed, 62 insertions(+), 7 deletions(-) diff --git a/RELEASE-NOTES-1.32 b/RELEASE-NOTES-1.32 index 0dca8f13d5..9784a7ff97 100644 --- a/RELEASE-NOTES-1.32 +++ b/RELEASE-NOTES-1.32 @@ -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. diff --git a/includes/CategoryViewer.php b/includes/CategoryViewer.php index 46a1473a91..a07e1b4775 100644 --- a/includes/CategoryViewer.php +++ b/includes/CategoryViewer.php @@ -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 = "
\n"; - $r .= '

' . $this->msg( 'category_header', $ti )->parse() . "

\n"; + $r .= '

' . $this->msg( 'category_header' )->rawParams( $name )->parse() . "

\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 .= "
\n"; $r .= '

' . - $this->msg( - 'category-media-header', - wfEscapeWikiText( $this->title->getText() ) - )->text() . + $this->msg( 'category-media-header' )->rawParams( $name )->parse() . "

\n"; $r .= $countmsg; $r .= $this->getSectionPagingLinks( 'file' ); diff --git a/includes/EditPage.php b/includes/EditPage.php index ef111c46cc..b3fe660c63 100644 --- a/includes/EditPage.php +++ b/includes/EditPage.php @@ -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 ) ); diff --git a/includes/OutputPage.php b/includes/OutputPage.php index 4f12e0cfd0..99a4c2b895 100644 --- a/includes/OutputPage.php +++ b/includes/OutputPage.php @@ -58,6 +58,15 @@ class OutputPage extends ContextSource { * @var string The contents of

*/ 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 "" 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 * diff --git a/includes/page/Article.php b/includes/page/Article.php index e90334fce6..589da2f106 100644 --- a/includes/page/Article.php +++ b/includes/page/Article.php @@ -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 ); } } -- 2.20.1