Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
40.32% |
25 / 62 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
SkinMustache | |
40.98% |
25 / 61 |
|
0.00% |
0 / 5 |
47.74 | |
0.00% |
0 / 1 |
getTemplateParser | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
createTempUserBannerHTML | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
12 | |||
generateHTML | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
doEditSectionLinksHTML | |
0.00% |
0 / 7 |
|
0.00% |
0 / 1 |
6 | |||
getTemplateData | |
92.59% |
25 / 27 |
|
0.00% |
0 / 1 |
5.01 |
1 | <?php |
2 | /** |
3 | * This program is free software; you can redistribute it and/or modify |
4 | * it under the terms of the GNU General Public License as published by |
5 | * the Free Software Foundation; either version 2 of the License, or |
6 | * (at your option) any later version. |
7 | * |
8 | * This program is distributed in the hope that it will be useful, |
9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
11 | * GNU General Public License for more details. |
12 | * |
13 | * You should have received a copy of the GNU General Public License along |
14 | * with this program; if not, write to the Free Software Foundation, Inc., |
15 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
16 | * http://www.gnu.org/copyleft/gpl.html |
17 | * |
18 | * @file |
19 | */ |
20 | |
21 | namespace MediaWiki\Skin; |
22 | |
23 | use MediaWiki\Html\Html; |
24 | use MediaWiki\Html\TemplateParser; |
25 | use MediaWiki\Language\Language; |
26 | use MediaWiki\Title\Title; |
27 | |
28 | /** |
29 | * Generic template for use with Mustache templates. |
30 | * @since 1.35 |
31 | */ |
32 | class SkinMustache extends SkinTemplate { |
33 | /** |
34 | * @var TemplateParser|null |
35 | */ |
36 | private $templateParser = null; |
37 | |
38 | /** |
39 | * Get the template parser, it will be lazily created if not already set. |
40 | * The template directory is defined in the skin options passed to |
41 | * the class constructor. |
42 | * |
43 | * @return TemplateParser |
44 | */ |
45 | protected function getTemplateParser() { |
46 | if ( $this->templateParser === null ) { |
47 | $this->templateParser = new TemplateParser( $this->options['templateDirectory'] ); |
48 | // For table of contents rendering. |
49 | $this->templateParser->enableRecursivePartials( true ); |
50 | } |
51 | return $this->templateParser; |
52 | } |
53 | |
54 | /** |
55 | * Creates a banner notifying IP masked users (temporary accounts) |
56 | * That they are editing via a temporary account. |
57 | * |
58 | * @return string |
59 | */ |
60 | private function createTempUserBannerHTML() { |
61 | $isSupportedSkin = $this->getOptions()['tempUserBanner']; |
62 | $isTempUser = $this->getUser()->isTemp(); |
63 | |
64 | if ( !$isSupportedSkin || !$isTempUser ) { |
65 | return ''; |
66 | } |
67 | |
68 | $returntoParam = SkinComponentUtils::getReturnToParam( |
69 | $this->getTitle(), |
70 | $this->getRequest(), |
71 | $this->getAuthority() |
72 | ); |
73 | |
74 | $tempUserBanner = new SkinComponentTempUserBanner( |
75 | $returntoParam, |
76 | $this->getContext(), |
77 | $this->getUser(), |
78 | ); |
79 | return $tempUserBanner->getTemplateData()['html']; |
80 | } |
81 | |
82 | /** |
83 | * @inheritDoc |
84 | * Render the associated template. The master template is assumed |
85 | * to be 'skin' unless `template` has been passed in the skin options |
86 | * to the constructor. |
87 | */ |
88 | public function generateHTML() { |
89 | $this->setupTemplateContext(); |
90 | $out = $this->getOutput(); |
91 | $tp = $this->getTemplateParser(); |
92 | $template = $this->options['template'] ?? 'skin'; |
93 | $data = $this->getTemplateData(); |
94 | $html = $this->createTempUserBannerHTML(); |
95 | $html .= $tp->processTemplate( $template, $data ); |
96 | return $html; |
97 | } |
98 | |
99 | /** |
100 | * @inheritDoc |
101 | */ |
102 | protected function doEditSectionLinksHTML( array $links, Language $lang ) { |
103 | $template = $this->getOptions()['templateSectionLinks'] ?? null; |
104 | if ( !$template ) { |
105 | return parent::doEditSectionLinksHTML( $links, $lang ); |
106 | } |
107 | return $this->getTemplateParser()->processTemplate( $template, [ |
108 | 'class' => 'mw-editsection', |
109 | 'array-links' => $links |
110 | ] ); |
111 | } |
112 | |
113 | /** |
114 | * @inheritDoc |
115 | * @return array Data specific for a mustache template. See parent function for common data. |
116 | */ |
117 | public function getTemplateData() { |
118 | $out = $this->getOutput(); |
119 | $printSource = Html::rawElement( |
120 | 'div', |
121 | [ |
122 | 'class' => 'printfooter', |
123 | 'data-nosnippet' => '' |
124 | ] + $this->getUserLanguageAttributes(), |
125 | $this->printSource() |
126 | ); |
127 | $bodyContent = $out->getHTML() . "\n" . $printSource; |
128 | |
129 | $newTalksHtml = $this->getNewtalks() ?: null; |
130 | |
131 | $data = parent::getTemplateData() + [ |
132 | // Array objects |
133 | 'array-indicators' => $this->getIndicatorsData( $out->getIndicators() ), |
134 | // HTML strings |
135 | 'html-site-notice' => $this->getSiteNotice() ?: null, |
136 | 'html-user-message' => $newTalksHtml ? |
137 | Html::rawElement( 'div', [ 'class' => 'usermessage' ], $newTalksHtml ) : null, |
138 | 'html-subtitle' => $this->prepareSubtitle(), |
139 | 'html-body-content' => $this->wrapHTML( $out->getTitle(), $bodyContent ), |
140 | 'html-categories' => $this->getCategories(), |
141 | 'html-after-content' => $this->afterContentHook(), |
142 | 'html-undelete-link' => $this->prepareUndeleteLink(), |
143 | 'html-user-language-attributes' => $this->prepareUserLanguageAttributes(), |
144 | |
145 | // links |
146 | 'link-mainpage' => Title::newMainPage()->getLocalURL(), |
147 | ]; |
148 | |
149 | foreach ( $this->options['messages'] ?? [] as $message ) { |
150 | $data["msg-{$message}"] = $this->msg( $message )->text(); |
151 | } |
152 | return $data; |
153 | } |
154 | } |
155 | |
156 | /** @deprecated class alias since 1.44 */ |
157 | class_alias( SkinMustache::class, 'SkinMustache' ); |