PrevNextNavigationRenderer.php 3.7 KB

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