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
This commit is contained in:
Ammar Abdulhamid 2020-05-28 08:41:05 +01:00
parent f927278d05
commit b9bdc3fc19
2 changed files with 37 additions and 37 deletions

View file

@ -20,8 +20,9 @@
]
},
"Hooks": {
"SkinTemplateBuildNavUrlsNav_urlsAfterPermalink": "CiteThisPageHooks::onSkinTemplateBuildNavUrlsNav_urlsAfterPermalink",
"BaseTemplateToolbox": "CiteThisPageHooks::onBaseTemplateToolbox"
"SidebarBeforeOutput": [
"CiteThisPageHooks::onSidebarBeforeOutput"
]
},
"ResourceModules": {
"ext.citeThisPage": {

View file

@ -1,35 +1,6 @@
<?php
class CiteThisPageHooks {
/**
* @param SkinTemplate &$skintemplate
* @param array &$nav_urls
* @param int &$oldid
* @param int &$revid
* @return bool
*/
public static function onSkinTemplateBuildNavUrlsNav_urlsAfterPermalink(
&$skintemplate, &$nav_urls, &$oldid, &$revid
) {
// check whether were in the right namespace, the $revid has the correct type and is not empty
// (which would mean that the current page doesnt exist)
$title = $skintemplate->getTitle();
if ( self::shouldAddLink( $title ) && $revid !== 0 && !empty( $revid ) ) {
$nav_urls['citethispage'] = [
'text' => $skintemplate->msg( 'citethispage-link' )->text(),
'href' => SpecialPage::getTitleFor( 'CiteThisPage' )
->getLocalURL( [ 'page' => $title->getPrefixedDBkey(), 'id' => $revid,
'wpFormIdentifier' => 'titleform' ] ),
'id' => 't-cite',
# Used message keys: 'tooltip-citethispage', 'accesskey-citethispage'
'single-id' => 'citethispage',
];
}
return true;
}
/**
* 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
@ -49,15 +20,43 @@ class CiteThisPageHooks {
}
/**
* @param BaseTemplate $baseTemplate
* @param array &$toolbox
* @param Skin $skin
* @param string[] &$sidebar
* @return bool
*/
public static function onBaseTemplateToolbox( BaseTemplate $baseTemplate, array &$toolbox ) {
if ( isset( $baseTemplate->data['nav_urls']['citethispage'] ) ) {
$toolbox['citethispage'] = $baseTemplate->data['nav_urls']['citethispage'];
public static function onSidebarBeforeOutput( Skin $skin, array &$sidebar ) {
$out = $skin->getOutput();
$title = $out->getTitle();
if ( !self::shouldAddLink( $title ) ) {
return false;
}
return true;
$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;
}
}