Merge "Make DBAccessBase use DBConnRef, rename $wiki, and hide getLoadBalancer()"
[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 Html;
24 use MessageLocalizer;
25 use Title;
26
27 /**
28 * Helper class for generating prev/next links for paging.
29 * @todo Use LinkTarget instead of Title
30 *
31 * @since 1.34
32 */
33 class PrevNextNavigationRenderer {
34
35 /**
36 * @var MessageLocalizer
37 */
38 private $messageLocalizer;
39
40 /**
41 * @param MessageLocalizer $messageLocalizer
42 */
43 public function __construct( MessageLocalizer $messageLocalizer ) {
44 $this->messageLocalizer = $messageLocalizer;
45 }
46
47 /**
48 * Generate (prev x| next x) (20|50|100...) type links for paging
49 *
50 * @param Title $title Title object to link
51 * @param int $offset
52 * @param int $limit
53 * @param array $query Optional URL query parameter string
54 * @param bool $atend Optional param for specified if this is the last page
55 * @return string
56 */
57 public function buildPrevNextNavigation(
58 Title $title,
59 $offset,
60 $limit,
61 array $query = [],
62 $atend = false
63 ) {
64 # Make 'previous' link
65 $prev = $this->messageLocalizer->msg( 'prevn' )->title( $title )
66 ->numParams( $limit )->text();
67
68 if ( $offset > 0 ) {
69 $plink = $this->numLink( $title, max( $offset - $limit, 0 ), $limit, $query,
70 $prev, 'prevn-title', 'mw-prevlink' );
71 } else {
72 $plink = htmlspecialchars( $prev );
73 }
74
75 # Make 'next' link
76 $next = $this->messageLocalizer->msg( 'nextn' )->title( $title )
77 ->numParams( $limit )->text();
78 if ( $atend ) {
79 $nlink = htmlspecialchars( $next );
80 } else {
81 $nlink = $this->numLink( $title, $offset + $limit, $limit,
82 $query, $next, 'nextn-title', 'mw-nextlink' );
83 }
84
85 # Make links to set number of items per page
86 $numLinks = [];
87 // @phan-suppress-next-next-line PhanUndeclaredMethod
88 // @fixme MessageLocalizer doesn't have a getLanguage() method!
89 $lang = $this->messageLocalizer->getLanguage();
90 foreach ( [ 20, 50, 100, 250, 500 ] as $num ) {
91 $numLinks[] = $this->numLink( $title, $offset, $num, $query,
92 $lang->formatNum( $num ), 'shown-title', 'mw-numlink' );
93 }
94
95 return $this->messageLocalizer->msg( 'viewprevnext' )->title( $title
96 )->rawParams( $plink, $nlink, $lang->pipeList( $numLinks ) )->escaped();
97 }
98
99 /**
100 * Helper function for buildPrevNextNavigation() that generates links
101 *
102 * @param Title $title Title object to link
103 * @param int $offset
104 * @param int $limit
105 * @param array $query Extra query parameters
106 * @param string $link Text to use for the link; will be escaped
107 * @param string $tooltipMsg Name of the message to use as tooltip
108 * @param string $class Value of the "class" attribute of the link
109 * @return string HTML fragment
110 */
111 private function numLink( Title $title, $offset, $limit, array $query, $link,
112 $tooltipMsg, $class
113 ) {
114 $query = [ 'limit' => $limit, 'offset' => $offset ] + $query;
115 $tooltip = $this->messageLocalizer->msg( $tooltipMsg )->title( $title )
116 ->numParams( $limit )->text();
117 return Html::element( 'a', [ 'href' => $title->getLocalURL( $query ),
118 'title' => $tooltip, 'class' => $class ], $link );
119 }
120
121 }