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-05-31 20:27:39 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use MediaWiki\Navigation\PagerNavigationBuilder;
|
|
|
|
|
use MediaWiki\Page\PageReferenceValue;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers \MediaWiki\Navigation\PagerNavigationBuilder
|
|
|
|
|
*/
|
|
|
|
|
class PagerNavigationBuilderTest extends MediaWikiIntegrationTestCase {
|
|
|
|
|
|
|
|
|
|
protected function setUp(): void {
|
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
|
|
$this->setMwGlobals( [
|
2022-11-02 20:46:06 +00:00
|
|
|
'wgArticlePath' => '/wiki/$1',
|
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-05-31 20:27:39 +00:00
|
|
|
'wgScriptPath' => '/w',
|
|
|
|
|
'wgScript' => '/w/index.php',
|
|
|
|
|
] );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private const EXPECTED_BASIC = '<div class="mw-pager-navigation-bar">(viewprevnext: <span class="mw-prevlink">(prevn: 50)</span>, <span class="mw-nextlink">(nextn: 50)</span>, <a href="/w/index.php?title=A&limit=20" class="mw-numlink">20</a>(pipe-separator)<span class="mw-numlink">50</span>(pipe-separator)<a href="/w/index.php?title=A&limit=100" class="mw-numlink">100</a>(pipe-separator)<a href="/w/index.php?title=A&limit=250" class="mw-numlink">250</a>(pipe-separator)<a href="/w/index.php?title=A&limit=500" class="mw-numlink">500</a>)</div>';
|
2022-11-02 18:40:29 +00:00
|
|
|
private const EXPECTED_OVERRIDES = '<div class="mw-pager-navigation-bar">(parentheses: <a href="/w/index.php?title=A&a=a&d=d" title="(m)" class="mw-firstlink">(i)</a>(pipe-separator)<a href="/w/index.php?title=A&a=a&e=e" title="(n)" class="mw-lastlink">(j)</a>) (viewprevnext: <a href="/w/index.php?title=A&a=a&b=b" rel="prev" title="(k: 1)" class="mw-prevlink">(g: 1)</a>, <a href="/w/index.php?title=A&a=a&c=c" rel="next" title="(l: 1)" class="mw-nextlink">(h: 1)</a>, <span class="mw-numlink">1</span>(pipe-separator)<a href="/w/index.php?title=A&a=a&f=2" title="(o: 2)" class="mw-numlink">2</a>)</div>';
|
2022-11-02 20:46:06 +00:00
|
|
|
private const EXPECTED_NULL_LINK_QUERY = '<div class="mw-pager-navigation-bar">(parentheses: <a href="/wiki/A" class="mw-firstlink">(i)</a>(pipe-separator)<a href="/w/index.php?title=A&dir=prev" class="mw-lastlink">(j)</a>) (viewprevnext: <a href="/w/index.php?title=A&dir=prev&offset=1" rel="prev" class="mw-prevlink">(prevn: 1)</a>, <a href="/w/index.php?title=A&dir=next&offset=2" rel="next" class="mw-nextlink">(nextn: 1)</a>, <span class="mw-numlink">1</span>)</div>';
|
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-05-31 20:27:39 +00:00
|
|
|
|
|
|
|
|
public function testBasic() {
|
|
|
|
|
$navBuilder = new PagerNavigationBuilder( new MockMessageLocalizer() );
|
|
|
|
|
$navBuilder->setPage( PageReferenceValue::localReference( NS_MAIN, 'A' ) );
|
|
|
|
|
$this->assertEquals( self::EXPECTED_BASIC, $navBuilder->getHtml() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testOverrides() {
|
|
|
|
|
$navBuilder = new PagerNavigationBuilder( new MockMessageLocalizer() );
|
|
|
|
|
$navBuilder
|
|
|
|
|
->setPage( PageReferenceValue::localReference( NS_MAIN, 'A' ) )
|
|
|
|
|
->setLinkQuery( [ 'a' => 'a' ] )
|
|
|
|
|
->setPrevLinkQuery( [ 'b' => 'b' ] )
|
|
|
|
|
->setNextLinkQuery( [ 'c' => 'c' ] )
|
|
|
|
|
->setFirstLinkQuery( [ 'd' => 'd' ] )
|
|
|
|
|
->setLastLinkQuery( [ 'e' => 'e' ] )
|
|
|
|
|
->setLimitLinkQueryParam( 'f' )
|
|
|
|
|
->setPrevMsg( 'g' )
|
|
|
|
|
->setNextMsg( 'h' )
|
|
|
|
|
->setFirstMsg( 'i' )
|
|
|
|
|
->setLastMsg( 'j' )
|
|
|
|
|
->setPrevTooltipMsg( 'k' )
|
|
|
|
|
->setNextTooltipMsg( 'l' )
|
|
|
|
|
->setFirstTooltipMsg( 'm' )
|
|
|
|
|
->setLastTooltipMsg( 'n' )
|
|
|
|
|
->setLimitTooltipMsg( 'o' )
|
|
|
|
|
->setCurrentLimit( 1 )
|
2022-11-02 18:40:29 +00:00
|
|
|
->setLimits( [ 1, 2 ] );
|
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-05-31 20:27:39 +00:00
|
|
|
$this->assertEquals( self::EXPECTED_OVERRIDES, $navBuilder->getHtml() );
|
|
|
|
|
}
|
|
|
|
|
|
2022-11-02 20:46:06 +00:00
|
|
|
public function testNullLinkQuery() {
|
|
|
|
|
$navBuilder = new PagerNavigationBuilder( new MockMessageLocalizer() );
|
|
|
|
|
$navBuilder
|
|
|
|
|
->setPage( PageReferenceValue::localReference( NS_MAIN, 'A' ) )
|
|
|
|
|
->setFirstMsg( 'i' )
|
|
|
|
|
->setLastMsg( 'j' )
|
|
|
|
|
->setCurrentLimit( 1 )
|
|
|
|
|
->setLimits( [ 1 ] )
|
|
|
|
|
// null is allowed here, and it establishes the order of parameters for the output
|
|
|
|
|
->setLinkQuery( [ 'dir' => null, 'offset' => '1' ] )
|
|
|
|
|
->setPrevLinkQuery( [ 'dir' => 'prev' ] )
|
|
|
|
|
->setNextLinkQuery( [ 'offset' => '2', 'dir' => 'next' ] )
|
|
|
|
|
// null is allowed here, and it overrides defaults from setLinkQuery()
|
|
|
|
|
->setFirstLinkQuery( [ 'offset' => null ] )
|
|
|
|
|
->setLastLinkQuery( [ 'offset' => null, 'dir' => 'prev' ] );
|
|
|
|
|
$this->assertEquals( self::EXPECTED_NULL_LINK_QUERY, $navBuilder->getHtml() );
|
|
|
|
|
}
|
|
|
|
|
|
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-05-31 20:27:39 +00:00
|
|
|
}
|