Merge "docs: Fix typo"
[mediawiki.git] / includes / skins / SkinMustache.php
blobce8db25d2ad15532c80abcd1910b80740500d3a7
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.
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.
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
18 * @file
21 use MediaWiki\Html\Html;
22 use MediaWiki\Html\TemplateParser;
23 use MediaWiki\Language\Language;
24 use MediaWiki\Skin\SkinComponentTempUserBanner;
25 use MediaWiki\Skin\SkinComponentUtils;
26 use MediaWiki\Title\Title;
28 /**
29 * Generic template for use with Mustache templates.
30 * @since 1.35
32 class SkinMustache extends SkinTemplate {
33 /**
34 * @var TemplateParser|null
36 private $templateParser = null;
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.
43 * @return TemplateParser
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 );
51 return $this->templateParser;
54 /**
55 * Creates a banner notifying IP masked users (temporary accounts)
56 * That they are editing via a temporary account.
58 * @return string
60 private function createTempUserBannerHTML() {
61 $isSupportedSkin = $this->getOptions()['tempUserBanner'];
62 $isTempUser = $this->getUser()->isTemp();
64 if ( !$isSupportedSkin || !$isTempUser ) {
65 return '';
68 $returntoParam = SkinComponentUtils::getReturnToParam(
69 $this->getTitle(),
70 $this->getRequest(),
71 $this->getAuthority()
74 $tempUserBanner = new SkinComponentTempUserBanner(
75 $returntoParam,
76 $this->getContext(),
77 $this->getUser(),
79 return $tempUserBanner->getTemplateData()['html'];
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.
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;
99 /**
100 * @inheritDoc
102 protected function doEditSectionLinksHTML( array $links, Language $lang ) {
103 $template = $this->getOptions()['templateSectionLinks'] ?? null;
104 if ( !$template ) {
105 return parent::doEditSectionLinksHTML( $links, $lang );
107 return $this->getTemplateParser()->processTemplate( $template, [
108 'class' => 'mw-editsection',
109 'array-links' => $links
110 ] );
114 * @inheritDoc
115 * @return array Data specific for a mustache template. See parent function for common data.
117 public function getTemplateData() {
118 $out = $this->getOutput();
119 $printSource = Html::rawElement(
120 'div',
122 'class' => 'printfooter',
123 'data-nosnippet' => ''
124 ] + $this->getUserLanguageAttributes(),
125 $this->printSource()
127 $bodyContent = $out->getHTML() . "\n" . $printSource;
129 $newTalksHtml = $this->getNewtalks() ?: null;
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(),
145 // links
146 'link-mainpage' => Title::newMainPage()->getLocalURL(),
149 foreach ( $this->options['messages'] ?? [] as $message ) {
150 $data["msg-{$message}"] = $this->msg( $message )->text();
152 return $data;