Merge "Selenium: replace UserLoginPage with BlankPage where possible"
[lhc/web/wiklou.git] / includes / Navigation / PrevNextNavigationRenderer.php
1 <?php
2 /**
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.
7 *
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.
12 *
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
17 *
18 * @file
19 */
20
21 namespace MediaWiki\Navigation;
22
23 use MediaWiki\Linker\LinkTarget;
24 use MessageLocalizer;
25 use Html;
26
27 /**
28 * Helper class for generating prev/next links for paging.
29 *
30 * @since 1.34
31 */
32 class PrevNextNavigationRenderer {
33
34 /**
35 * @var MessageLocalizer
36 */
37 private $messageLocalizer;
38
39 public function __construct( MessageLocalizer $messageLocalizer ) {
40 $this->messageLocalizer = $messageLocalizer;
41 }
42
43 /**
44 * Generate (prev x| next x) (20|50|100...) type links for paging
45 *
46 * @param LinkTarget $title LinkTarget object to link
47 * @param int $offset
48 * @param int $limit
49 * @param array $query Optional URL query parameter string
50 * @param bool $atend Optional param for specified if this is the last page
51 * @return string
52 */
53 public function buildPrevNextNavigation( LinkTarget $title, $offset, $limit,
54 array $query = [], $atend = false
55 ) {
56 # Make 'previous' link
57 $prev = $this->messageLocalizer->msg( 'prevn' )->title( $title )
58 ->numParams( $limit )->text();
59
60 if ( $offset > 0 ) {
61 $plink = $this->numLink( $title, max( $offset - $limit, 0 ), $limit, $query,
62 $prev, 'prevn-title', 'mw-prevlink' );
63 } else {
64 $plink = htmlspecialchars( $prev );
65 }
66
67 # Make 'next' link
68 $next = $this->messageLocalizer->msg( 'nextn' )->title( $title )
69 ->numParams( $limit )->text();
70 if ( $atend ) {
71 $nlink = htmlspecialchars( $next );
72 } else {
73 $nlink = $this->numLink( $title, $offset + $limit, $limit,
74 $query, $next, 'nextn-title', 'mw-nextlink' );
75 }
76
77 # Make links to set number of items per page
78 $numLinks = [];
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' );
83 }
84
85 return $this->messageLocalizer->msg( 'viewprevnext' )->title( $title
86 )->rawParams( $plink, $nlink, $lang->pipeList( $numLinks ) )->escaped();
87 }
88
89 /**
90 * Helper function for buildPrevNextNavigation() that generates links
91 *
92 * @param LinkTarget $title LinkTarget object to link
93 * @param int $offset
94 * @param int $limit
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
100 */
101 private function numLink( LinkTarget $title, $offset, $limit, array $query, $link,
102 $tooltipMsg, $class
103 ) {
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 );
109 }
110
111 }