3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 namespace MediaWiki\Navigation
;
23 use MediaWiki\Linker\LinkTarget
;
28 * Helper class for generating prev/next links for paging.
32 class PrevNextNavigationRenderer
{
35 * @var MessageLocalizer
37 private $messageLocalizer;
39 public function __construct( MessageLocalizer
$messageLocalizer ) {
40 $this->messageLocalizer
= $messageLocalizer;
44 * Generate (prev x| next x) (20|50|100...) type links for paging
46 * @param LinkTarget $title LinkTarget object to link
49 * @param array $query Optional URL query parameter string
50 * @param bool $atend Optional param for specified if this is the last page
53 public function buildPrevNextNavigation( LinkTarget
$title, $offset, $limit,
54 array $query = [], $atend = false
56 # Make 'previous' link
57 $prev = $this->messageLocalizer
->msg( 'prevn' )->title( $title )
58 ->numParams( $limit )->text();
61 $plink = $this->numLink( $title, max( $offset - $limit, 0 ), $limit, $query,
62 $prev, 'prevn-title', 'mw-prevlink' );
64 $plink = htmlspecialchars( $prev );
68 $next = $this->messageLocalizer
->msg( 'nextn' )->title( $title )
69 ->numParams( $limit )->text();
71 $nlink = htmlspecialchars( $next );
73 $nlink = $this->numLink( $title, $offset +
$limit, $limit,
74 $query, $next, 'nextn-title', 'mw-nextlink' );
77 # Make links to set number of items per page
79 $lang = $this->messageLocalizer
->getLanguage();
80 foreach ( [ 20, 50, 100, 250, 500 ] as $num ) {
81 $numLinks[] = $this->numLink( $title, $offset, $num, $query,
82 $lang->formatNum( $num ), 'shown-title', 'mw-numlink' );
85 return $this->messageLocalizer
->msg( 'viewprevnext' )->title( $title
86 )->rawParams( $plink, $nlink, $lang->pipeList( $numLinks ) )->escaped();
90 * Helper function for buildPrevNextNavigation() that generates links
92 * @param LinkTarget $title LinkTarget object to link
95 * @param array $query Extra query parameters
96 * @param string $link Text to use for the link; will be escaped
97 * @param string $tooltipMsg Name of the message to use as tooltip
98 * @param string $class Value of the "class" attribute of the link
99 * @return string HTML fragment
101 private function numLink( LinkTarget
$title, $offset, $limit, array $query, $link,
104 $query = [ 'limit' => $limit, 'offset' => $offset ] +
$query;
105 $tooltip = $this->messageLocalizer
->msg( $tooltipMsg )->title( $title )
106 ->numParams( $limit )->text();
107 return Html
::element( 'a', [ 'href' => $title->getLocalURL( $query ),
108 'title' => $tooltip, 'class' => $class ], $link );