From 7d129953f6f576c8ee88d792a5905a252c43c62d Mon Sep 17 00:00:00 2001 From: eranroz Date: Thu, 25 Oct 2018 21:07:00 +0300 Subject: [PATCH] Sunsetting viewPrevNext Removing viewPrevNext from Language and moving it to SpecialPage. Used soley in special pages, and we aim to remove/reduce the dependency of messages and language Bug:T207977 Change-Id: I49b41a89ba59cfc24982b321f02c5cca9939decd --- RELEASE-NOTES-1.33 | 2 + includes/specialpage/QueryPage.php | 4 +- includes/specialpage/SpecialPage.php | 66 ++++++++++++++++++++++++++++ includes/specials/SpecialSearch.php | 3 +- languages/Language.php | 2 + 5 files changed, 73 insertions(+), 4 deletions(-) diff --git a/RELEASE-NOTES-1.33 b/RELEASE-NOTES-1.33 index 7513334819..f455b95abf 100644 --- a/RELEASE-NOTES-1.33 +++ b/RELEASE-NOTES-1.33 @@ -408,6 +408,8 @@ because of Phabricator reports. deprecated and will be removed in the future. * The FileBasedSiteLookup class has been deprecated. For a cacheable SiteLookup implementation, use CachingSiteStore instead. +* Language::viewPrevNext function is deprecated, use + SpecialPage::buildPrevNextNavigation instead * ManualLogEntry::setTags() is deprecated, use ManualLogEntry::addTags() instead. The setTags() method was overriding the tags, addTags() doesn't override, only adds new tags. diff --git a/includes/specialpage/QueryPage.php b/includes/specialpage/QueryPage.php index f0cb7e51c6..46873b169a 100644 --- a/includes/specialpage/QueryPage.php +++ b/includes/specialpage/QueryPage.php @@ -658,8 +658,8 @@ abstract class QueryPage extends SpecialPage { $miserMaxResults = $this->getConfig()->get( 'MiserMode' ) && ( $this->offset + $this->limit >= $this->getMaxResults() ); $atEnd = ( $this->numRows <= $this->limit ) || $miserMaxResults; - $paging = $this->getLanguage()->viewPrevNext( $this->getPageTitle( $par ), $this->offset, - $this->limit, $this->linkParameters(), $atEnd ); + $paging = $this->buildPrevNextNavigation( $this->offset, + $this->limit, $this->linkParameters(), $atEnd, $par ); $out->addHTML( '

' . $paging . '

' ); } else { # No results to show, so don't bother with "showing X of Y" etc. diff --git a/includes/specialpage/SpecialPage.php b/includes/specialpage/SpecialPage.php index a9bbb8a0d4..bd0e24f2e1 100644 --- a/includes/specialpage/SpecialPage.php +++ b/includes/specialpage/SpecialPage.php @@ -920,4 +920,70 @@ class SpecialPage implements MessageLocalizer { public function setLinkRenderer( LinkRenderer $linkRenderer ) { $this->linkRenderer = $linkRenderer; } + + /** + * Generate (prev x| next x) (20|50|100...) type links for paging + * + * @param int $offset + * @param int $limit + * @param array $query Optional URL query parameter string + * @param bool $atend Optional param for specified if this is the last page + * @param string|bool $subpage Optional param for specifying subpage + * @return string + */ + protected function buildPrevNextNavigation( $offset, $limit, + array $query = [], $atend = false, $subpage = false + ) { + $lang = $this->getLanguage(); + + # Make 'previous' link + $prev = $this->msg( 'prevn' )->numParams( $limit )->text(); + if ( $offset > 0 ) { + $plink = $this->numLink( max( $offset - $limit, 0 ), $limit, $query, + $prev, 'prevn-title', 'mw-prevlink', $subpage ); + } else { + $plink = htmlspecialchars( $prev ); + } + + # Make 'next' link + $next = $this->msg( 'nextn' )->numParams( $limit )->text(); + if ( $atend ) { + $nlink = htmlspecialchars( $next ); + } else { + $nlink = $this->numLink( $offset + $limit, $limit, + $query, $next, 'nextn-title', 'mw-nextlink', $subpage ); + } + + # Make links to set number of items per page + $numLinks = []; + foreach ( [ 20, 50, 100, 250, 500 ] as $num ) { + $numLinks[] = $this->numLink( $offset, $num, $query, + $lang->formatNum( $num ), 'shown-title', 'mw-numlink', $subpage ); + } + + return $this->msg( 'viewprevnext' )->rawParams( $plink, $nlink, $lang->pipeList( $numLinks ) )-> + escaped(); + } + + /** + * Helper function for buildPrevNextNavigation() that generates links + * + * @param int $offset + * @param int $limit + * @param array $query Extra query parameters + * @param string $link Text to use for the link; will be escaped + * @param string $tooltipMsg Name of the message to use as tooltip + * @param string $class Value of the "class" attribute of the link + * @param string|bool $subpage Optional param for specifying subpage + * @return string HTML fragment + */ + private function numLink( $offset, $limit, array $query, $link, + $tooltipMsg, $class, $subpage = false + ) { + $query = [ 'limit' => $limit, 'offset' => $offset ] + $query; + $tooltip = $this->msg( $tooltipMsg )->numParams( $limit )->text(); + $href = $this->getPageTitle( $subpage )->getLocalURL( $query ); + return Html::element( 'a', [ 'href' => $href, + 'title' => $tooltip, 'class' => $class ], $link ); + } } diff --git a/includes/specials/SpecialSearch.php b/includes/specials/SpecialSearch.php index b60a2adbb7..a5203960c0 100644 --- a/includes/specials/SpecialSearch.php +++ b/includes/specials/SpecialSearch.php @@ -471,8 +471,7 @@ class SpecialSearch extends SpecialPage { $offset = $this->offset; } - $prevnext = $this->getLanguage()->viewPrevNext( - $this->getPageTitle(), + $prevnext = $this->buildPrevNextNavigation( $offset, $this->limit, $this->powerSearchOptions() + [ 'search' => $term ], diff --git a/languages/Language.php b/languages/Language.php index 71d350fd28..6b6e88c075 100644 --- a/languages/Language.php +++ b/languages/Language.php @@ -4857,6 +4857,8 @@ class Language { * @param array $query Optional URL query parameter string * @param bool $atend Optional param for specified if this is the last page * @return string + * @deprecated since 1.33, use SpecialPage::viewPrevNext() + * instead. */ public function viewPrevNext( Title $title, $offset, $limit, array $query = [], $atend = false -- 2.20.1