MediaWiki REL1_39
TemplatesOnThisPageFormatter.php
Go to the documentation of this file.
1<?php
26
34
38 private $context;
39
43 private $linkRenderer;
44
48 private $linkBatchFactory;
49
53 private $restrictionStore;
54
61 public function __construct(
62 IContextSource $context,
63 LinkRenderer $linkRenderer,
64 LinkBatchFactory $linkBatchFactory,
65 RestrictionStore $restrictionStore
66 ) {
67 $this->context = $context;
68 $this->linkRenderer = $linkRenderer;
69 $this->linkBatchFactory = $linkBatchFactory;
70 $this->restrictionStore = $restrictionStore;
71 }
72
84 public function format( array $templates, $type = false, $more = null ) {
85 if ( !$templates ) {
86 // No templates
87 return '';
88 }
89
90 # Do a batch existence check
91 $batch = $this->linkBatchFactory->newLinkBatch( $templates );
92 $batch->setCaller( __METHOD__ );
93 $batch->execute();
94
95 # Construct the HTML
96 $outText = Html::openElement( 'div', [ 'class' => 'mw-templatesUsedExplanation' ] );
97 $count = count( $templates );
98 if ( $type === 'preview' ) {
99 $outText .= $this->context->msg( 'templatesusedpreview' )->numParams( $count )
100 ->parseAsBlock();
101 } elseif ( $type === 'section' ) {
102 $outText .= $this->context->msg( 'templatesusedsection' )->numParams( $count )
103 ->parseAsBlock();
104 } else {
105 $outText .= $this->context->msg( 'templatesused' )->numParams( $count )
106 ->parseAsBlock();
107 }
108 $outText .= Html::closeElement( 'div' ) . Html::openElement( 'ul' ) . "\n";
109
110 usort( $templates, [ Title::class, 'compare' ] );
111 foreach ( $templates as $template ) {
112 $outText .= $this->formatTemplate( $template );
113 }
114
115 if ( $more instanceof PageReference ) {
116 $outText .= Html::rawElement( 'li', [],
117 $this->linkRenderer->makeLink(
118 $more,
119 $this->context->msg( 'moredotdotdot' )->text()
120 )
121 );
122 } elseif ( $more ) {
123 // Documented as should already be escaped
124 $outText .= Html::rawElement( 'li', [], $more );
125 }
126
127 $outText .= Html::closeElement( 'ul' );
128 return $outText;
129 }
130
137 private function formatTemplate( PageIdentity $target ) {
138 $protected = $this->getRestrictionsText(
139 $this->restrictionStore->getRestrictions( $target, 'edit' )
140 );
141 $editLink = $this->buildEditLink( $target );
142 return Html::rawElement( 'li', [], $this->linkRenderer->makeLink( $target )
143 . $this->context->msg( 'word-separator' )->escaped()
144 . $this->context->msg( 'parentheses' )->rawParams( $editLink )->escaped()
145 . $this->context->msg( 'word-separator' )->escaped()
146 . $protected
147 );
148 }
149
157 private function getRestrictionsText( array $restrictions ) {
158 $protected = '';
159 if ( !$restrictions ) {
160 return $protected;
161 }
162
163 // Check backwards-compatible messages
164 $msg = null;
165 if ( $restrictions === [ 'sysop' ] ) {
166 $msg = $this->context->msg( 'template-protected' );
167 } elseif ( $restrictions === [ 'autoconfirmed' ] ) {
168 $msg = $this->context->msg( 'template-semiprotected' );
169 }
170 if ( $msg && !$msg->isDisabled() ) {
171 $protected = $msg->parse();
172 } else {
173 // Construct the message from restriction-level-*
174 // e.g. restriction-level-sysop, restriction-level-autoconfirmed
175 $msgs = [];
176 foreach ( $restrictions as $r ) {
177 $msgs[] = $this->context->msg( "restriction-level-$r" )->parse();
178 }
179 $protected = $this->context->msg( 'parentheses' )
180 ->rawParams( $this->context->getLanguage()->commaList( $msgs ) )->escaped();
181 }
182
183 return $protected;
184 }
185
193 private function buildEditLink( PageIdentity $page ) {
194 if ( $this->context->getAuthority()->probablyCan( 'edit', $page ) ) {
195 $linkMsg = 'editlink';
196 } else {
197 $linkMsg = 'viewsourcelink';
198 }
199
200 return $this->linkRenderer->makeLink(
201 $page,
202 $this->context->msg( $linkMsg )->text(),
203 [],
204 [ 'action' => 'edit' ]
205 );
206 }
207
208}
Class that generates HTML anchor link elements for pages.
Handles formatting for the "templates used on this page" lists.
__construct(IContextSource $context, LinkRenderer $linkRenderer, LinkBatchFactory $linkBatchFactory, RestrictionStore $restrictionStore)
format(array $templates, $type=false, $more=null)
Make an HTML list of templates, and then add a "More..." link at the bottom.
Interface for objects which can provide a MediaWiki context on request.
Interface for objects (potentially) representing an editable wiki page.
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.