mediawiki-extensions-CiteTh.../includes/CiteThisPageHooks.php
Ammar Abdulhamid b9bdc3fc19 Use SidebarBeforeOutput hook to add 'cite this page' sidebar link.
* Remove usage of BaseTemplateToolbox in favor of SidebarBeforeOutput hook
* Also remove SkinTemplateBuildNavUrlsNav_urlsAfterPermalink hook, since SidebarBeforeOutput can provide functionalities of both.

Bug: T253687
Depens-On: Ie10a59048cdf882bad811d3c8fae386d90e93867
Change-Id: Ibe989364b9e500497759e79b789667d0a3430828
2020-05-30 04:49:18 +01:00

63 lines
1.5 KiB
PHP

<?php
class CiteThisPageHooks {
/**
* Checks, if the "cite this page" link should be added. By default the link is added to all
* pages in the main namespace, and additionally to pages, which are in one of the namespaces
* named in $wgCiteThisPageAddiotionalNamespaces.
*
* @param Title $title
* @return bool
*/
private static function shouldAddLink( Title $title ) {
global $wgCiteThisPageAdditionalNamespaces;
return $title->isContentPage() ||
(
isset( $wgCiteThisPageAdditionalNamespaces[$title->getNamespace()] ) &&
$wgCiteThisPageAdditionalNamespaces[$title->getNamespace()]
);
}
/**
* @param Skin $skin
* @param string[] &$sidebar
* @return bool
*/
public static function onSidebarBeforeOutput( Skin $skin, array &$sidebar ) {
$out = $skin->getOutput();
$title = $out->getTitle();
if ( !self::shouldAddLink( $title ) ) {
return false;
}
$revid = $out->getRevisionId();
if ( $revid !== 0 && !empty( $revid ) ) {
$specialPage = SpecialPage::getTitleFor( 'CiteThisPage' );
$citeURL = $specialPage->getLocalURL( [
'page' => $title->getPrefixedDBkey(),
'id' => $revid,
'wpFormIdentifier' => 'titleform'
]
);
$citeThisPageLink = [
'id' => 't-cite',
'href' => $citeURL,
'text' => $skin->msg( 'citethispage-link' )->text(),
// Message keys: 'tooltip-citethispage', 'accesskey-citethispage'
'single-id' => 'citethispage',
];
// Append link
$sidebar['TOOLBOX']['citethispage'] = $citeThisPageLink;
return true;
}
return false;
}
}