Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 30 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
| Hooks | |
0.00% |
0 / 30 |
|
0.00% |
0 / 4 |
182 | |
0.00% |
0 / 1 |
| onWebRequestPathInfoRouter | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| onSidebarBeforeOutput | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
| addToolboxLink | |
0.00% |
0 / 20 |
|
0.00% |
0 / 1 |
42 | |||
| onBeforePageDisplay | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * Hooks for ShortUrl for adding link to toolbox |
| 4 | * |
| 5 | * @file |
| 6 | * @ingroup Extensions |
| 7 | * @author Yuvi Panda, http://yuvi.in |
| 8 | * @copyright © 2011 Yuvaraj Pandian (yuvipanda@yuvi.in) |
| 9 | * @license BSD-3-Clause |
| 10 | */ |
| 11 | |
| 12 | namespace MediaWiki\Extension\ShortUrl; |
| 13 | |
| 14 | use MediaWiki\Hook\SidebarBeforeOutputHook; |
| 15 | use MediaWiki\Hook\WebRequestPathInfoRouterHook; |
| 16 | use MediaWiki\Output\Hook\BeforePageDisplayHook; |
| 17 | use MediaWiki\Output\OutputPage; |
| 18 | use MediaWiki\Request\PathRouter; |
| 19 | use MediaWiki\Skin\Skin; |
| 20 | use MediaWiki\SpecialPage\SpecialPage; |
| 21 | use Wikimedia\Rdbms\DBReadOnlyError; |
| 22 | |
| 23 | class Hooks implements |
| 24 | SidebarBeforeOutputHook, |
| 25 | BeforePageDisplayHook, |
| 26 | WebRequestPathInfoRouterHook |
| 27 | { |
| 28 | /** |
| 29 | * Add ShortURL rules to the URL router. |
| 30 | * @param PathRouter $router |
| 31 | */ |
| 32 | public function onWebRequestPathInfoRouter( $router ) { |
| 33 | global $wgShortUrlTemplate; |
| 34 | if ( $wgShortUrlTemplate ) { |
| 35 | // Hardcode full title to avoid T78018. It shouldn't matter because the |
| 36 | // page redirects immediately. |
| 37 | $router->add( $wgShortUrlTemplate, [ 'title' => 'Special:ShortUrl/$1' ] ); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | /** |
| 42 | * Add toolbox link the sidebar |
| 43 | * |
| 44 | * @param Skin $skin |
| 45 | * @param array &$sidebar |
| 46 | */ |
| 47 | public function onSidebarBeforeOutput( $skin, &$sidebar ): void { |
| 48 | $link = self::addToolboxLink( $skin ); |
| 49 | if ( $link ) { |
| 50 | $sidebar['TOOLBOX']['shorturl'] = $link; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Create toolbox link |
| 56 | * |
| 57 | * @param Skin $skin |
| 58 | * @return array|bool |
| 59 | */ |
| 60 | public static function addToolboxLink( Skin $skin ) { |
| 61 | global $wgShortUrlTemplate, $wgServer, $wgShortUrlReadOnly; |
| 62 | |
| 63 | if ( $wgShortUrlReadOnly ) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | if ( !is_string( $wgShortUrlTemplate ) ) { |
| 68 | $urlTemplate = SpecialPage::getTitleFor( 'ShortUrl', '$1' )->getFullUrl(); |
| 69 | } else { |
| 70 | $urlTemplate = $wgServer . $wgShortUrlTemplate; |
| 71 | } |
| 72 | |
| 73 | $title = $skin->getTitle(); |
| 74 | if ( Utils::needsShortUrl( $title ) ) { |
| 75 | try { |
| 76 | $shortId = Utils::encodeTitle( $title ); |
| 77 | } catch ( DBReadOnlyError ) { |
| 78 | $shortId = false; |
| 79 | } |
| 80 | if ( $shortId !== false ) { |
| 81 | $shortURL = str_replace( '$1', $shortId, $urlTemplate ); |
| 82 | |
| 83 | return [ |
| 84 | 'id' => 't-shorturl', |
| 85 | 'href' => $shortURL, |
| 86 | 'title' => $skin->msg( 'shorturl-toolbox-title' )->text(), |
| 87 | 'text' => $skin->msg( 'shorturl-toolbox-text' )->text(), |
| 88 | ]; |
| 89 | } |
| 90 | } |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * @param OutputPage $out |
| 96 | * @param Skin $skin |
| 97 | */ |
| 98 | public function onBeforePageDisplay( $out, $skin ): void { |
| 99 | global $wgShortUrlReadOnly; |
| 100 | $title = $out->getTitle(); |
| 101 | |
| 102 | if ( !$wgShortUrlReadOnly && Utils::needsShortUrl( $title ) ) { |
| 103 | $out->addModules( 'ext.shortUrl' ); |
| 104 | } |
| 105 | } |
| 106 | } |