2015-01-27 05:34:04 +00:00
|
|
|
<?php
|
|
|
|
|
2021-02-11 02:03:06 +00:00
|
|
|
namespace MediaWiki\Extension\CiteThisPage;
|
|
|
|
|
2021-04-08 18:01:31 +00:00
|
|
|
use Config;
|
2021-02-11 02:03:06 +00:00
|
|
|
use SpecialPage;
|
|
|
|
use Title;
|
|
|
|
|
2021-04-08 18:01:31 +00:00
|
|
|
class Hooks implements \MediaWiki\Hook\SidebarBeforeOutputHook {
|
2016-11-04 20:08:12 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2021-12-06 18:09:45 +00:00
|
|
|
* named in $wgCiteThisPageAdditionalNamespaces.
|
2016-11-04 20:08:12 +00:00
|
|
|
*
|
2021-12-06 12:32:33 +00:00
|
|
|
* @param Title|null $title
|
|
|
|
* @param Config|null $config
|
2016-11-04 20:08:12 +00:00
|
|
|
* @return bool
|
|
|
|
*/
|
2021-12-06 12:32:33 +00:00
|
|
|
private static function shouldAddLink( ?Title $title, ?Config $config ) {
|
|
|
|
if ( !$title || !$config ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-04-08 18:01:31 +00:00
|
|
|
$additionalNamespaces = $config->get( 'CiteThisPageAdditionalNamespaces' );
|
2016-11-04 20:08:12 +00:00
|
|
|
|
|
|
|
return $title->isContentPage() ||
|
|
|
|
(
|
2021-04-08 18:01:31 +00:00
|
|
|
isset( $additionalNamespaces[$title->getNamespace()] ) &&
|
|
|
|
$additionalNamespaces[$title->getNamespace()]
|
2016-11-04 20:08:12 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-08 18:01:31 +00:00
|
|
|
/** @inheritDoc */
|
|
|
|
public function onSidebarBeforeOutput( $skin, &$sidebar ): void {
|
2020-05-28 07:41:05 +00:00
|
|
|
$out = $skin->getOutput();
|
|
|
|
$title = $out->getTitle();
|
|
|
|
|
2021-04-08 18:01:31 +00:00
|
|
|
if ( !self::shouldAddLink( $title, $out->getConfig() ) ) {
|
2020-06-17 00:06:30 +00:00
|
|
|
return;
|
2020-05-28 07:41:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
$revid = $out->getRevisionId();
|
|
|
|
|
2020-06-17 00:06:30 +00:00
|
|
|
if ( $revid === 0 || empty( $revid ) ) {
|
|
|
|
return;
|
2015-01-27 05:34:04 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 00:06:30 +00:00
|
|
|
$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;
|
2015-01-27 05:34:04 +00:00
|
|
|
}
|
|
|
|
}
|