MediaWiki  master
PrevNextNavigationRenderer.php
Go to the documentation of this file.
1 <?php
22 
23 use Html;
25 use Title;
26 
34 
39 
44  $this->messageLocalizer = $messageLocalizer;
45  }
46 
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 
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 }
Helper class for generating prev/next links for paging.
static element( $element, $attribs=[], $contents='')
Identical to rawElement(), but HTML-escapes $contents (like Xml::element()).
Definition: Html.php:231
numLink(Title $title, $offset, $limit, array $query, $link, $tooltipMsg, $class)
Helper function for buildPrevNextNavigation() that generates links.
buildPrevNextNavigation(Title $title, $offset, $limit, array $query=[], $atend=false)
Generate (prev x| next x) (20|50|100...) type links for paging.
if(!isset( $args[0])) $lang
getLocalURL( $query='', $query2=false)
Get a URL with no fragment or server name (relative URL) from a Title object.
Definition: Title.php:2177