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