Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 51 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
Hooks | |
0.00% |
0 / 51 |
|
0.00% |
0 / 4 |
380 | |
0.00% |
0 / 1 |
onSidebarBeforeOutput | |
0.00% |
0 / 36 |
|
0.00% |
0 / 1 |
156 | |||
onBeforePageDisplay | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
6 | |||
getIndexOfDownloadPdfSidebarItem | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
20 | |||
generateDownloadScreenLink | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * Hooks for ElectronPdfService extension |
4 | * |
5 | * @file |
6 | * @ingroup Extensions |
7 | * @license GPL-2.0-or-later |
8 | */ |
9 | |
10 | namespace MediaWiki\Extension\ElectronPdfService; |
11 | |
12 | use MediaWiki\Hook\BeforePageDisplayHook; |
13 | use MediaWiki\Hook\SidebarBeforeOutputHook; |
14 | use MediaWiki\Output\OutputPage; |
15 | use MediaWiki\SpecialPage\SpecialPage; |
16 | use MediaWiki\Title\Title; |
17 | use Skin; |
18 | |
19 | class Hooks implements |
20 | SidebarBeforeOutputHook, |
21 | BeforePageDisplayHook |
22 | { |
23 | |
24 | /** |
25 | * If present, make the "Download as PDF" link in the sidebar point to the download screen, |
26 | * add a new link otherwise. |
27 | * |
28 | * @param Skin $skin |
29 | * @param array &$bar |
30 | */ |
31 | public function onSidebarBeforeOutput( $skin, &$bar ): void { |
32 | $title = $skin->getTitle(); |
33 | if ( $title === null || !$title->exists() ) { |
34 | return; |
35 | } |
36 | |
37 | $action = $skin->getActionName(); |
38 | if ( $action !== 'view' && $action !== 'purge' ) { |
39 | return; |
40 | } |
41 | |
42 | $output = $skin->getOutput(); |
43 | $config = $skin->getConfig(); |
44 | |
45 | if ( |
46 | $output->isRevisionCurrent() && |
47 | $config->has( 'CollectionFormats' ) && |
48 | array_key_exists( 'coll-print_export', $bar ) |
49 | ) { |
50 | $index = self::getIndexOfDownloadPdfSidebarItem( |
51 | $bar['coll-print_export'], |
52 | $config->get( 'CollectionFormats' ) |
53 | ); |
54 | // if Collection extension provides a download-as-pdf link, make it point to the download screen |
55 | if ( $index !== false ) { |
56 | $bar['coll-print_export'][$index]['href'] = self::generateDownloadScreenLink( |
57 | $title |
58 | ); |
59 | // if no download-as-pdf link is there, add one and point to the download screen |
60 | } else { |
61 | $bar['coll-print_export'][] = [ |
62 | 'text' => $skin->msg( 'electronpdfservice-sidebar-portlet-print-text' )->text(), |
63 | 'id' => 'electron-print_pdf', |
64 | 'href' => self::generateDownloadScreenLink( $title ) |
65 | ]; |
66 | } |
67 | } else { |
68 | // in case Collection is not installed, let's add our own portlet |
69 | // with a link to the download screen |
70 | $out = []; |
71 | if ( $output->isRevisionCurrent() ) { |
72 | $out[] = [ |
73 | 'text' => $skin->msg( 'electronpdfservice-sidebar-portlet-print-text' )->text(), |
74 | 'id' => 'electron-print_pdf', |
75 | 'href' => self::generateDownloadScreenLink( $title ) |
76 | ]; |
77 | } |
78 | |
79 | if ( !$skin->getOutput()->isPrintable() && isset( $bar['TOOLBOX']['print'] ) ) { |
80 | $printItem = $bar['TOOLBOX']['print']; |
81 | |
82 | // Unset 'print' item and move it to our section |
83 | unset( $bar['TOOLBOX']['print'] ); |
84 | $out[] = $printItem; |
85 | } |
86 | |
87 | $bar['electronpdfservice-sidebar-portlet-heading'] = $out; |
88 | } |
89 | } |
90 | |
91 | /** |
92 | * @param OutputPage $out |
93 | * @param Skin $skin |
94 | */ |
95 | public function onBeforePageDisplay( $out, $skin ): void { |
96 | $userAgent = $out->getRequest()->getHeader( 'User-Agent' ); |
97 | |
98 | if ( strstr( $userAgent, 'electron-render-service' ) ) { |
99 | $out->addModuleStyles( 'ext.ElectronPdfService.print.styles' ); |
100 | } |
101 | } |
102 | |
103 | private static function getIndexOfDownloadPdfSidebarItem( $portlet, $collectionFormats ) { |
104 | $usedPdfLib = array_search( 'PDF', $collectionFormats ); |
105 | if ( $usedPdfLib !== false ) { |
106 | foreach ( $portlet as $index => $element ) { |
107 | if ( $element['id'] === 'coll-download-as-' . $usedPdfLib ) { |
108 | return $index; |
109 | } |
110 | } |
111 | } |
112 | |
113 | return false; |
114 | } |
115 | |
116 | private static function generateDownloadScreenLink( Title $title ) { |
117 | return SpecialPage::getTitleFor( 'DownloadAsPdf' )->getLocalURL( |
118 | [ |
119 | 'page' => $title->getPrefixedDBkey(), |
120 | 'action' => 'show-download-screen' |
121 | ] |
122 | ); |
123 | } |
124 | |
125 | } |