MediaWiki master
TemplatesOnThisPageFormatter.php
Go to the documentation of this file.
1<?php
21namespace MediaWiki\EditPage;
22
31
39
43 private $context;
44
48 private $linkRenderer;
49
53 private $linkBatchFactory;
54
58 private $restrictionStore;
59
66 public function __construct(
67 IContextSource $context,
68 LinkRenderer $linkRenderer,
69 LinkBatchFactory $linkBatchFactory,
70 RestrictionStore $restrictionStore
71 ) {
72 $this->context = $context;
73 $this->linkRenderer = $linkRenderer;
74 $this->linkBatchFactory = $linkBatchFactory;
75 $this->restrictionStore = $restrictionStore;
76 }
77
89 public function format( array $templates, $type = false, $more = null ) {
90 if ( !$templates ) {
91 // No templates
92 return '';
93 }
94
95 # Do a batch existence check
96 $batch = $this->linkBatchFactory->newLinkBatch( $templates );
97 $batch->setCaller( __METHOD__ );
98 $batch->execute();
99
100 # Construct the HTML
101 $outText = Html::openElement( 'div', [ 'class' => 'mw-templatesUsedExplanation' ] );
102 $count = count( $templates );
103 if ( $type === 'preview' ) {
104 $outText .= $this->context->msg( 'templatesusedpreview' )->numParams( $count )
105 ->parseAsBlock();
106 } elseif ( $type === 'section' ) {
107 $outText .= $this->context->msg( 'templatesusedsection' )->numParams( $count )
108 ->parseAsBlock();
109 } else {
110 $outText .= $this->context->msg( 'templatesused' )->numParams( $count )
111 ->parseAsBlock();
112 }
113 $outText .= Html::closeElement( 'div' ) . Html::openElement( 'ul' ) . "\n";
114
115 usort( $templates, [ Title::class, 'compare' ] );
116 foreach ( $templates as $template ) {
117 $outText .= $this->formatTemplate( $template );
118 }
119
120 if ( $more instanceof PageReference ) {
121 $outText .= Html::rawElement( 'li', [],
122 $this->linkRenderer->makeLink(
123 $more,
124 $this->context->msg( 'moredotdotdot' )->text()
125 )
126 );
127 } elseif ( $more ) {
128 // Documented as should already be escaped
129 $outText .= Html::rawElement( 'li', [], $more );
130 }
131
132 $outText .= Html::closeElement( 'ul' );
133 return $outText;
134 }
135
144 private function formatTemplate( PageIdentity $target ) {
145 if ( !$target->canExist() ) {
146 return Html::rawElement( 'li', [], $this->linkRenderer->makeLink( $target ) );
147 }
148
149 $protected = $this->getRestrictionsText(
150 $this->restrictionStore->getRestrictions( $target, 'edit' )
151 );
152 $editLink = $this->buildEditLink( $target );
153 return Html::rawElement( 'li', [], $this->linkRenderer->makeLink( $target )
154 . $this->context->msg( 'word-separator' )->escaped()
155 . $this->context->msg( 'parentheses' )->rawParams( $editLink )->escaped()
156 . $this->context->msg( 'word-separator' )->escaped()
157 . $protected
158 );
159 }
160
168 private function getRestrictionsText( array $restrictions ) {
169 $protected = '';
170 if ( !$restrictions ) {
171 return $protected;
172 }
173
174 // Check backwards-compatible messages
175 $msg = null;
176 if ( $restrictions === [ 'sysop' ] ) {
177 $msg = $this->context->msg( 'template-protected' );
178 } elseif ( $restrictions === [ 'autoconfirmed' ] ) {
179 $msg = $this->context->msg( 'template-semiprotected' );
180 }
181 if ( $msg && !$msg->isDisabled() ) {
182 $protected = $msg->parse();
183 } else {
184 // Construct the message from restriction-level-*
185 // e.g. restriction-level-sysop, restriction-level-autoconfirmed
186 $msgs = [];
187 foreach ( $restrictions as $r ) {
188 $msgs[] = $this->context->msg( "restriction-level-$r" )->parse();
189 }
190 $protected = $this->context->msg( 'parentheses' )
191 ->rawParams( $this->context->getLanguage()->commaList( $msgs ) )->escaped();
192 }
193
194 return $protected;
195 }
196
204 private function buildEditLink( PageIdentity $page ) {
205 if ( $this->context->getAuthority()->probablyCan( 'edit', $page ) ) {
206 $linkMsg = 'editlink';
207 } else {
208 $linkMsg = 'viewsourcelink';
209 }
210
211 return $this->linkRenderer->makeLink(
212 $page,
213 $this->context->msg( $linkMsg )->text(),
214 [],
215 [ 'action' => 'edit' ]
216 );
217 }
218
219}
220
222class_alias( TemplatesOnThisPageFormatter::class, 'TemplatesOnThisPageFormatter' );
Handles formatting for the "templates used on this page" lists.
format(array $templates, $type=false, $more=null)
Make an HTML list of templates, and then add a "More..." link at the bottom.
__construct(IContextSource $context, LinkRenderer $linkRenderer, LinkBatchFactory $linkBatchFactory, RestrictionStore $restrictionStore)
This class is a collection of static functions that serve two purposes:
Definition Html.php:56
Class that generates HTML for internal links.
Represents a title within MediaWiki.
Definition Title.php:78
Interface for objects which can provide a MediaWiki context on request.
Interface for objects (potentially) representing an editable wiki page.
canExist()
Checks whether this PageIdentity represents a "proper" page, meaning that it could exist as an editab...
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.