| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\CiteThisPage; |
| 4 |
|
| 5 |
use MediaWiki\Config\Config; |
| 6 |
use MediaWiki\SpecialPage\SpecialPage; |
| 7 |
use MediaWiki\Title\Title; |
| 8 |
|
| 9 |
class Hooks implements \MediaWiki\Hook\SidebarBeforeOutputHook { |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
private static function shouldAddLink( ?Title $title, ?Config $config ) { |
| 20 |
if ( !$title || !$config ) { |
| 21 |
return false; |
| 22 |
} |
| 23 |
|
| 24 |
$additionalNamespaces = $config->get( 'CiteThisPageAdditionalNamespaces' ); |
| 25 |
|
| 26 |
return $title->isContentPage() || |
| 27 |
( |
| 28 |
isset( $additionalNamespaces[$title->getNamespace()] ) && |
| 29 |
$additionalNamespaces[$title->getNamespace()] |
| 30 |
); |
| 31 |
} |
| 32 |
|
| 33 |
|
| 34 |
public function onSidebarBeforeOutput( $skin, &$sidebar ): void { |
| 35 |
$out = $skin->getOutput(); |
| 36 |
$title = $out->getTitle(); |
| 37 |
|
| 38 |
if ( !self::shouldAddLink( $title, $out->getConfig() ) ) { |
| 39 |
return; |
| 40 |
} |
| 41 |
|
| 42 |
$revid = $out->getRevisionId(); |
| 43 |
|
| 44 |
if ( $revid === 0 || $revid === null ) { |
| 45 |
return; |
| 46 |
} |
| 47 |
|
| 48 |
$specialPage = SpecialPage::getTitleFor( 'CiteThisPage' ); |
| 49 |
$citeURL = $specialPage->getLocalURL( [ |
| 50 |
'page' => $title->getPrefixedDBkey(), |
| 51 |
'id' => $revid, |
| 52 |
'wpFormIdentifier' => 'titleform' |
| 53 |
] |
| 54 |
); |
| 55 |
|
| 56 |
$citeThisPageLink = [ |
| 57 |
'id' => 't-cite', |
| 58 |
'href' => $citeURL, |
| 59 |
'icon' => 'quotes', |
| 60 |
'text' => $skin->msg( 'citethispage-link' )->text(), |
| 61 |
|
| 62 |
'single-id' => 'citethispage', |
| 63 |
]; |
| 64 |
|
| 65 |
|
| 66 |
$sidebar['TOOLBOX']['citethispage'] = $citeThisPageLink; |
| 67 |
} |
| 68 |
} |