Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.33% |
14 / 15 |
|
80.00% |
4 / 5 |
CRAP | |
0.00% |
0 / 1 |
| Hooks | |
93.33% |
14 / 15 |
|
80.00% |
4 / 5 |
7.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| onGetDoubleUnderscoreIDs | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getPageProperty | |
75.00% |
3 / 4 |
|
0.00% |
0 / 1 |
2.06 | |||
| onArticleViewRedirect | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| onArticleViewFooter | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\MultiTitle; |
| 4 | |
| 5 | use MediaWiki\Hook\GetDoubleUnderscoreIDsHook; |
| 6 | use MediaWiki\Page\Hook\ArticleViewFooterHook; |
| 7 | use MediaWiki\Page\Hook\ArticleViewRedirectHook; |
| 8 | use MediaWiki\Page\PageIdentity; |
| 9 | use MediaWiki\Page\PageProps; |
| 10 | |
| 11 | class Hooks implements GetDoubleUnderscoreIDsHook, ArticleViewRedirectHook, ArticleViewFooterHook { |
| 12 | |
| 13 | public function __construct( private readonly PageProps $pageProps ) { |
| 14 | } |
| 15 | |
| 16 | /** @inheritDoc */ |
| 17 | public function onGetDoubleUnderscoreIDs( &$ids ): void { |
| 18 | $ids[] = 'keeptitle'; |
| 19 | } |
| 20 | |
| 21 | /** |
| 22 | * Get the given property of a page, if it exists and has the property, or null otherwise. |
| 23 | */ |
| 24 | private function getPageProperty( ?PageIdentity $page, string $propertyName ): ?string { |
| 25 | if ( !$page ) { |
| 26 | return null; |
| 27 | } |
| 28 | $properties = $this->pageProps->getProperties( $page, $propertyName ); |
| 29 | return $properties[$page->getId()] ?? null; |
| 30 | } |
| 31 | |
| 32 | /** @inheritDoc */ |
| 33 | public function onArticleViewRedirect( $article ): bool { |
| 34 | return $this->getPageProperty( $article->getRedirectedFrom(), 'keeptitle' ) === null; |
| 35 | } |
| 36 | |
| 37 | /** @inheritDoc */ |
| 38 | public function onArticleViewFooter( $article, $patrolFooterShown ): void { |
| 39 | $redirectTitle = $article->getRedirectedFrom(); |
| 40 | if ( $this->getPageProperty( $redirectTitle, 'keeptitle' ) === null ) { |
| 41 | return; |
| 42 | } |
| 43 | |
| 44 | $outputPage = $article->getContext()->getOutput(); |
| 45 | $redirectDisplayTitle = $this->getPageProperty( $redirectTitle, 'displaytitle' ) |
| 46 | ?? $redirectTitle->getPrefixedText(); |
| 47 | $outputPage->setPageTitle( $redirectDisplayTitle ); |
| 48 | $outputPage->setDisplayTitle( $redirectDisplayTitle ); |
| 49 | } |
| 50 | } |