Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 100 |
|
0.00% |
0 / 8 |
CRAP | |
0.00% |
0 / 1 |
SpecialDownloadAsPdf | |
0.00% |
0 / 100 |
|
0.00% |
0 / 8 |
182 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
execute | |
0.00% |
0 / 35 |
|
0.00% |
0 / 1 |
42 | |||
showRenderModeSelectionPage | |
0.00% |
0 / 28 |
|
0.00% |
0 / 1 |
2 | |||
getLabeledHiddenField | |
0.00% |
0 / 16 |
|
0.00% |
0 / 1 |
2 | |||
getHiddenField | |
0.00% |
0 / 9 |
|
0.00% |
0 / 1 |
2 | |||
setHeaders | |
0.00% |
0 / 5 |
|
0.00% |
0 / 1 |
2 | |||
getServiceUrl | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
redirectToElectron | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | /** |
3 | * ElectronPdf SpecialPage for ElectronPdfService extension |
4 | * |
5 | * @file |
6 | * @ingroup Extensions |
7 | */ |
8 | |
9 | namespace MediaWiki\Extension\ElectronPdfService\Specials; |
10 | |
11 | use MediaWiki\Config\Config; |
12 | use MediaWiki\MediaWikiServices; |
13 | use MediaWiki\SpecialPage\SpecialPage; |
14 | use MediaWiki\Title\Title; |
15 | use OOUI\ButtonGroupWidget; |
16 | use OOUI\ButtonInputWidget; |
17 | use OOUI\FormLayout; |
18 | use OOUI\Tag; |
19 | |
20 | class SpecialDownloadAsPdf extends SpecialPage { |
21 | |
22 | /** |
23 | * @var Config |
24 | */ |
25 | public $config; |
26 | |
27 | public function __construct() { |
28 | parent::__construct( 'DownloadAsPdf', '', false ); |
29 | $this->config = MediaWikiServices::getInstance()->getMainConfig(); |
30 | } |
31 | |
32 | /** |
33 | * @param null|string $subPage |
34 | */ |
35 | public function execute( $subPage ) { |
36 | $request = $this->getRequest(); |
37 | $parts = ( $subPage === '' || $subPage === null ) ? [] : explode( '/', $subPage, 2 ); |
38 | $page = trim( $request->getVal( 'page', $parts[0] ?? '' ) ); |
39 | |
40 | $title = Title::newFromText( $page ); |
41 | if ( $title === null ) { |
42 | $this->getOutput()->showErrorPage( |
43 | 'electronpdfservice-invalid-page-title', |
44 | 'electronpdfservice-invalid-page-text' |
45 | ); |
46 | return; |
47 | } |
48 | |
49 | $action = $request->getVal( 'action', 'default' ); |
50 | $stats = MediaWikiServices::getInstance()->getStatsFactory(); |
51 | $dbName = $this->getConfig()->get( 'DBname' ); |
52 | |
53 | switch ( $action ) { |
54 | case 'redirect-to-electron': |
55 | $stats->getCounter( 'electronpdf_action_total' ) |
56 | ->setLabel( 'action', $action ) |
57 | ->copyToStatsdAt( 'electronpdf.action.' . $action ) |
58 | ->increment(); |
59 | $stats->getCounter( 'electronpdf_actions_per_wiki_total' ) |
60 | ->setLabel( 'action', $action ) |
61 | ->setLabel( 'wiki', $dbName ) |
62 | ->copyToStatsdAt( 'electronpdf.actionsPerWiki.' . $dbName . '.' . $action ) |
63 | ->increment(); |
64 | $this->redirectToElectron( $title ); |
65 | return; |
66 | default: |
67 | $stats->getCounter( 'electronpdf_action_total' ) |
68 | ->setLabel( 'action', 'show-download-screen' ) |
69 | ->copyToStatsdAt( 'electronpdf.action.show-download-screen' ) |
70 | ->increment(); |
71 | $stats->getCounter( 'electronpdf_actions_per_wiki_total' ) |
72 | ->setLabel( 'action', 'show-download-screen' ) |
73 | ->setLabel( 'wiki', $dbName ) |
74 | ->copyToStatsdAt( 'electronpdf.actionsPerWiki.' . $dbName . '.show-download-screen' ) |
75 | ->increment(); |
76 | |
77 | $this->showRenderModeSelectionPage( $title ); |
78 | } |
79 | } |
80 | |
81 | /** |
82 | * @param Title $title page to download as PDF |
83 | */ |
84 | public function showRenderModeSelectionPage( Title $title ) { |
85 | $this->setHeaders(); |
86 | |
87 | $out = $this->getOutput(); |
88 | $out->enableOOUI(); |
89 | $out->setPageTitleMsg( $this->msg( 'electronpdfservice-special-page-headline' ) ); |
90 | $out->addSubtitle( $title->getText() ); |
91 | |
92 | $form = new FormLayout( [ |
93 | 'method' => 'POST', |
94 | 'action' => $this->getPageTitle()->getLocalURL(), |
95 | ] ); |
96 | |
97 | $form->addClasses( [ 'mw-electronpdfservice-selection-form' ] ); |
98 | |
99 | $form->appendContent( |
100 | ( new Tag() ) |
101 | ->addClasses( [ 'mw-electronpdfservice-selection-body' ] ) |
102 | ->appendContent( |
103 | $this->getLabeledHiddenField( 'redirect-to-electron', $title->getDBKey() ), |
104 | $this->getHiddenField( 'page', $title->getPrefixedText() ), |
105 | new ButtonGroupWidget( [ |
106 | 'items' => [ |
107 | new ButtonInputWidget( [ |
108 | 'type' => 'submit', |
109 | 'flags' => [ 'primary', 'progressive' ], |
110 | 'label' => $this->msg( 'electronpdfservice-download-button' )->text(), |
111 | ] ), |
112 | ], |
113 | ] ) |
114 | ) |
115 | ); |
116 | $out->addHTML( $form ); |
117 | } |
118 | |
119 | /** |
120 | * @param string $action |
121 | * @param string $pageTitle |
122 | * @return Tag |
123 | */ |
124 | private function getLabeledHiddenField( $action, $pageTitle ) { |
125 | $image = ( new Tag() )->addClasses( [ |
126 | 'mw-electronpdfservice-selection-image', |
127 | 'mw-electronpdfservice-selection-download-image' |
128 | ] ); |
129 | |
130 | $field = ( new Tag() )->addClasses( [ 'mw-electronpdfservice-selection-field' ] ); |
131 | $field->appendContent( |
132 | $this->getHiddenField( 'action', $action ), |
133 | ( new Tag( 'div' ) )->addClasses( [ 'mw-electronpdfservice-selection-label-text' ] ) |
134 | ->appendContent( $this->msg( 'electronpdfservice-download-label' )->text() ), |
135 | ( new Tag( 'div' ) )->addClasses( [ 'mw-electronpdfservice-selection-label-desc' ] ) |
136 | ->appendContent( $pageTitle . '.pdf' ) |
137 | ); |
138 | |
139 | return ( new Tag( 'label' ) )->appendContent( |
140 | $image, |
141 | $field |
142 | ); |
143 | } |
144 | |
145 | /** |
146 | * @param string $name |
147 | * @param string $value |
148 | * @return Tag |
149 | */ |
150 | private function getHiddenField( $name, $value ) { |
151 | $element = new Tag( 'input' ); |
152 | $element->setAttributes( |
153 | [ |
154 | 'type' => 'hidden', |
155 | 'name' => $name, |
156 | 'value' => $value |
157 | ] |
158 | ); |
159 | |
160 | return $element; |
161 | } |
162 | |
163 | public function setHeaders() { |
164 | parent::setHeaders(); |
165 | $this->getOutput()->addModuleStyles( [ |
166 | 'ext.ElectronPdfService.special.styles', |
167 | 'ext.ElectronPdfService.special.selectionImages', |
168 | ] ); |
169 | } |
170 | |
171 | /** |
172 | * @param Title $title |
173 | * @return string |
174 | */ |
175 | private function getServiceUrl( Title $title ) { |
176 | $restBaseUrl = $this->config->get( 'ElectronPdfServiceRESTbaseURL' ); |
177 | |
178 | return $restBaseUrl . urlencode( $title->getPrefixedDBkey() ); |
179 | } |
180 | |
181 | private function redirectToElectron( Title $title ) { |
182 | $this->getOutput()->redirect( |
183 | $this->getServiceUrl( $title ) |
184 | ); |
185 | } |
186 | |
187 | } |