Update RELEASE-NOTES-1.34 for various backports
[lhc/web/wiklou.git] / includes / TemplatesOnThisPageFormatter.php
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 use MediaWiki\Linker\LinkRenderer;
22 use MediaWiki\Linker\LinkTarget;
23 use MediaWiki\MediaWikiServices;
24
25 /**
26 * Handles formatting for the "templates used on this page"
27 * lists. Formerly known as Linker::formatTemplates()
28 *
29 * @since 1.28
30 */
31 class TemplatesOnThisPageFormatter {
32
33 /**
34 * @var IContextSource
35 */
36 private $context;
37
38 /**
39 * @var LinkRenderer
40 */
41 private $linkRenderer;
42
43 /**
44 * @param IContextSource $context
45 * @param LinkRenderer $linkRenderer
46 */
47 public function __construct( IContextSource $context, LinkRenderer $linkRenderer ) {
48 $this->context = $context;
49 $this->linkRenderer = $linkRenderer;
50 }
51
52 /**
53 * Make an HTML list of templates, and then add a "More..." link at
54 * the bottom. If $more is null, do not add a "More..." link. If $more
55 * is a LinkTarget, make a link to that title and use it. If $more is a string,
56 * directly paste it in as the link (escaping needs to be done manually).
57 *
58 * @param LinkTarget[] $templates
59 * @param string|bool $type 'preview' if a preview, 'section' if a section edit, false if neither
60 * @param LinkTarget|string|null $more An escaped link for "More..." of the templates
61 * @return string HTML output
62 */
63 public function format( array $templates, $type = false, $more = null ) {
64 if ( !$templates ) {
65 // No templates
66 return '';
67 }
68
69 # Do a batch existence check
70 ( new LinkBatch( $templates ) )->execute();
71
72 # Construct the HTML
73 $outText = '<div class="mw-templatesUsedExplanation">';
74 $count = count( $templates );
75 if ( $type === 'preview' ) {
76 $outText .= $this->context->msg( 'templatesusedpreview' )->numParams( $count )
77 ->parseAsBlock();
78 } elseif ( $type === 'section' ) {
79 $outText .= $this->context->msg( 'templatesusedsection' )->numParams( $count )
80 ->parseAsBlock();
81 } else {
82 $outText .= $this->context->msg( 'templatesused' )->numParams( $count )
83 ->parseAsBlock();
84 }
85 $outText .= "</div><ul>\n";
86
87 usort( $templates, 'Title::compare' );
88 foreach ( $templates as $template ) {
89 $outText .= $this->formatTemplate( $template );
90 }
91
92 if ( $more instanceof LinkTarget ) {
93 $outText .= Html::rawElement( 'li', [], $this->linkRenderer->makeLink(
94 $more, $this->context->msg( 'moredotdotdot' )->text() ) );
95 } elseif ( $more ) {
96 // Documented as should already be escaped
97 $outText .= Html::rawElement( 'li', [], $more );
98 }
99
100 $outText .= '</ul>';
101 return $outText;
102 }
103
104 /**
105 * Builds an <li> item for an individual template
106 *
107 * @param LinkTarget $target
108 * @return string
109 */
110 private function formatTemplate( LinkTarget $target ) {
111 // TODO Would be nice if we didn't have to use Title here
112 $titleObj = Title::newFromLinkTarget( $target );
113 $protected = $this->getRestrictionsText( $titleObj->getRestrictions( 'edit' ) );
114 $editLink = $this->buildEditLink( $titleObj );
115 return '<li>' . $this->linkRenderer->makeLink( $target )
116 . $this->context->msg( 'word-separator' )->escaped()
117 . $this->context->msg( 'parentheses' )->rawParams( $editLink )->escaped()
118 . $this->context->msg( 'word-separator' )->escaped()
119 . $protected . '</li>';
120 }
121
122 /**
123 * If the page is protected, get the relevant text
124 * for those restrictions
125 *
126 * @param array $restrictions
127 * @return string
128 */
129 private function getRestrictionsText( array $restrictions ) {
130 $protected = '';
131 if ( !$restrictions ) {
132 return $protected;
133 }
134
135 // Check backwards-compatible messages
136 $msg = null;
137 if ( $restrictions === [ 'sysop' ] ) {
138 $msg = $this->context->msg( 'template-protected' );
139 } elseif ( $restrictions === [ 'autoconfirmed' ] ) {
140 $msg = $this->context->msg( 'template-semiprotected' );
141 }
142 if ( $msg && !$msg->isDisabled() ) {
143 $protected = $msg->parse();
144 } else {
145 // Construct the message from restriction-level-*
146 // e.g. restriction-level-sysop, restriction-level-autoconfirmed
147 $msgs = [];
148 foreach ( $restrictions as $r ) {
149 $msgs[] = $this->context->msg( "restriction-level-$r" )->parse();
150 }
151 $protected = $this->context->msg( 'parentheses' )
152 ->rawParams( $this->context->getLanguage()->commaList( $msgs ) )->escaped();
153 }
154
155 return $protected;
156 }
157
158 /**
159 * Return a link to the edit page, with the text
160 * saying "view source" if the user can't edit the page
161 *
162 * @param LinkTarget $titleObj
163 * @return string
164 */
165 private function buildEditLink( LinkTarget $titleObj ) {
166 if ( MediaWikiServices::getInstance()->getPermissionManager()
167 ->quickUserCan( 'edit', $this->context->getUser(), $titleObj )
168 ) {
169 $linkMsg = 'editlink';
170 } else {
171 $linkMsg = 'viewsourcelink';
172 }
173
174 return $this->linkRenderer->makeLink(
175 $titleObj,
176 $this->context->msg( $linkMsg )->text(),
177 [],
178 [ 'action' => 'edit' ]
179 );
180 }
181
182 }