48 private $linkRenderer;
53 private $linkBatchFactory;
58 private $restrictionStore;
72 $this->context = $context;
73 $this->linkRenderer = $linkRenderer;
74 $this->linkBatchFactory = $linkBatchFactory;
75 $this->restrictionStore = $restrictionStore;
89 public function format( array $templates,
$type =
false, $more =
null ) {
95 # Do a batch existence check
96 $batch = $this->linkBatchFactory->newLinkBatch( $templates );
97 $batch->setCaller( __METHOD__ );
101 $outText = Html::openElement(
'div', [
'class' =>
'mw-templatesUsedExplanation' ] );
102 $count = count( $templates );
103 if (
$type ===
'preview' ) {
104 $outText .= $this->context->msg(
'templatesusedpreview' )->numParams( $count )
106 } elseif (
$type ===
'section' ) {
107 $outText .= $this->context->msg(
'templatesusedsection' )->numParams( $count )
110 $outText .= $this->context->msg(
'templatesused' )->numParams( $count )
113 $outText .= Html::closeElement(
'div' ) . Html::openElement(
'ul' ) .
"\n";
115 usort( $templates, [ Title::class,
'compare' ] );
116 foreach ( $templates as $template ) {
117 $outText .= $this->formatTemplate( $template );
121 $outText .= Html::rawElement(
'li', [],
122 $this->linkRenderer->makeLink(
124 $this->context->msg(
'moredotdotdot' )->text()
129 $outText .= Html::rawElement(
'li', [], $more );
132 $outText .= Html::closeElement(
'ul' );
144 private function formatTemplate(
PageIdentity $target ) {
145 $protected = $this->getRestrictionsText(
146 $this->restrictionStore->getRestrictions( $target,
'edit' )
148 $editLink = $this->buildEditLink( $target );
149 return Html::rawElement(
'li', [], $this->linkRenderer->makeLink( $target )
150 . $this->context->msg(
'word-separator' )->escaped()
151 . $this->context->msg(
'parentheses' )->rawParams( $editLink )->escaped()
152 . $this->context->msg(
'word-separator' )->escaped()
164 private function getRestrictionsText( array $restrictions ) {
166 if ( !$restrictions ) {
172 if ( $restrictions === [
'sysop' ] ) {
173 $msg = $this->context->msg(
'template-protected' );
174 } elseif ( $restrictions === [
'autoconfirmed' ] ) {
175 $msg = $this->context->msg(
'template-semiprotected' );
177 if ( $msg && !$msg->isDisabled() ) {
178 $protected = $msg->parse();
183 foreach ( $restrictions as $r ) {
184 $msgs[] = $this->context->msg(
"restriction-level-$r" )->parse();
186 $protected = $this->context->msg(
'parentheses' )
187 ->rawParams( $this->context->getLanguage()->commaList( $msgs ) )->escaped();
200 private function buildEditLink( PageIdentity $page ) {
201 if ( $this->context->getAuthority()->probablyCan(
'edit', $page ) ) {
202 $linkMsg =
'editlink';
204 $linkMsg =
'viewsourcelink';
207 return $this->linkRenderer->makeLink(
209 $this->context->msg( $linkMsg )->text(),
211 [
'action' =>
'edit' ]
Interface for objects which can provide a MediaWiki context on request.