wiki.techinc.nl/includes/Navigation/PrevNextNavigationRenderer.php
Bartosz Dziewoński cfd6ffe7bb Introduce PagerNavigationBuilder for making pagination links
We had several implementations of almost identical paging links:

* PrevNextNavigationRenderer: The nicest one, somewhat recently added
  (4ca72763ec). Unfortunately it was also the least featureful: only
  supporting paging by numeric offset and not by index, and not able
  to generate "first"/"last" links. Also, I didn't realize that it
  exists when working on 94553a1bcb and b95d208340, so it was missing
  those changes too.

* IndexPager/ReverseChronologicalPager/AlphabeticPager: These have
  been here forever. The most featureful, but not configurable, so
  a large part of the implementation was copy-pasted in two classes.

* SpecialWhatLinksHere: Through some accident of history, this one
  special page ended up with its own implementation???

They are all replaced to use the new PagerNavigationBuilder.
It may be slightly too much, but I had fun writing it.

Notable changes compared to PrevNextNavigationRenderer:
* Adds <div class="mw-pager-navigation-bar"> wrapper around the
  navigation and <span class="…"> wrappers on inactive links
* The current limit link is made inactive
  (like the "prev" link when on first page, etc.)

Notable changes compared to ...Pager/...Pager/...Pager:
* Does not generate useless tooltips that contain only the
  title of the page, can use custom tooltips
* The current limit link is made inactive
  (like the "prev" link when on first page, etc.)
* All links have query parameters in a consistent order:
  ?title= &... &dir= &offset= &limit= (some of them are optional)

These changes affect many special pages and actions. I tested on:
* Special:Contributions (ReverseChronologicalPager)
* action=history (ReverseChronologicalPager)
* Special:Categories (AlphabeticPager)
* Special:WantedPages (PrevNextNavigationRenderer)
* Special:Search (PrevNextNavigationRenderer)
* Special:WhatLinksHere

Bug: T308364
Change-Id: Ic75bd597b210e14612ca3aebb531b659897e8294
2022-09-05 16:10:36 -04:00

83 lines
2.4 KiB
PHP

<?php
/**
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
*/
namespace MediaWiki\Navigation;
use MessageLocalizer;
use Title;
/**
* Helper class for generating prev/next links for paging.
*
* @deprecated since 1.39 Use PagerNavigationBuilder instead
* @since 1.34
*/
class PrevNextNavigationRenderer {
/**
* @var MessageLocalizer
*/
private $messageLocalizer;
/**
* @param MessageLocalizer $messageLocalizer
*/
public function __construct( MessageLocalizer $messageLocalizer ) {
$this->messageLocalizer = $messageLocalizer;
}
/**
* Generate (prev x| next x) (20|50|100...) type links for paging (only suitable when paging by
* numeric offset, not when paging by index)
*
* @param Title $title Title object to link
* @param int $offset
* @param int $limit
* @param array $query Optional URL query parameter string
* @param bool $atend Optional param for specified if this is the last page
* @return string
*/
public function buildPrevNextNavigation(
Title $title,
$offset,
$limit,
array $query = [],
$atend = false
) {
$navBuilder = new PagerNavigationBuilder( $this->messageLocalizer );
$navBuilder
->setPage( $title )
->setLinkQuery( [ 'limit' => $limit, 'offset' => $offset ] + $query )
->setLimitLinkQueryParam( 'limit' )
->setCurrentLimit( $limit )
->setPrevTooltipMsg( 'prevn-title' )
->setNextTooltipMsg( 'nextn-title' )
->setLimitTooltipMsg( 'shown-title' );
if ( $offset > 0 ) {
$navBuilder->setPrevLinkQuery( [ 'offset' => (string)max( $offset - $limit, 0 ) ] );
}
if ( !$atend ) {
$navBuilder->setNextLinkQuery( [ 'offset' => (string)( $offset + $limit ) ] );
}
return $navBuilder->getHtml();
}
}