MediaWiki master
TemplatesOnThisPageFormatter.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\EditPage;
8
19
27
34 public function __construct(
35 private readonly IContextSource $context,
36 private readonly LinkRenderer $linkRenderer,
37 private readonly LinkBatchFactory $linkBatchFactory,
38 private readonly RestrictionStore $restrictionStore
39 ) {
40 }
41
53 public function format( array $templates, $type = false, $more = null ) {
54 if ( !$templates ) {
55 // No templates
56 return '';
57 }
58
59 # Do a batch existence check
60 $batch = $this->linkBatchFactory->newLinkBatch( $templates );
61 $batch->setCaller( __METHOD__ );
62 $batch->execute();
63
64 # Construct the HTML
65 $outText = Html::openElement( 'div', [ 'class' => 'mw-templatesUsedExplanation' ] );
66 $count = count( $templates );
67 if ( $type === 'preview' ) {
68 $outText .= $this->context->msg( 'templatesusedpreview' )->numParams( $count )
69 ->parseAsBlock();
70 } elseif ( $type === 'section' ) {
71 $outText .= $this->context->msg( 'templatesusedsection' )->numParams( $count )
72 ->parseAsBlock();
73 } else {
74 $outText .= $this->context->msg( 'templatesused' )->numParams( $count )
75 ->parseAsBlock();
76 }
77 $outText .= Html::closeElement( 'div' ) . Html::openElement( 'ul' ) . "\n";
78
79 usort( $templates, Title::compare( ... ) );
80 foreach ( $templates as $template ) {
81 $outText .= $this->formatTemplate( $template );
82 }
83
84 if ( $more instanceof PageReference ) {
85 $outText .= Html::rawElement( 'li', [],
86 $this->linkRenderer->makeLink(
87 $more,
88 $this->context->msg( 'moredotdotdot' )->text()
89 )
90 );
91 } elseif ( $more ) {
92 // Documented as should already be escaped
93 $outText .= Html::rawElement( 'li', [], $more );
94 }
95
96 $outText .= Html::closeElement( 'ul' );
97 return $outText;
98 }
99
108 private function formatTemplate( PageIdentity $target ) {
109 if ( !$target->canExist() ) {
110 return Html::rawElement( 'li', [], $this->linkRenderer->makeLink( $target ) );
111 }
112
113 $protected = $this->getRestrictionsText(
114 $this->restrictionStore->getRestrictions( $target, 'edit' )
115 );
116 $editLink = $this->buildEditLink( $target );
117 return Html::rawElement( 'li', [], $this->linkRenderer->makeLink( $target )
118 . $this->context->msg( 'word-separator' )->escaped()
119 . $this->context->msg( 'parentheses' )->rawParams( $editLink )->escaped()
120 . $this->context->msg( 'word-separator' )->escaped()
121 . $protected
122 );
123 }
124
132 private function getRestrictionsText( array $restrictions ) {
133 $protected = '';
134 if ( !$restrictions ) {
135 return $protected;
136 }
137
138 // Construct the message from restriction-level-*
139 // e.g. restriction-level-sysop, restriction-level-autoconfirmed
140 $msgs = [];
141 foreach ( $restrictions as $r ) {
142 $msgs[] = $this->context->msg( "restriction-level-$r" );
143 }
144
145 // Check backwards-compatible messages for the built-in protection levels
146 $msg = null;
147 if ( $restrictions === [ 'sysop' ] ) {
148 $msg = $this->context->msg( 'template-protected' );
149 } elseif ( $restrictions === [ 'autoconfirmed' ] ) {
150 $msg = $this->context->msg( 'template-semiprotected' );
151 }
152 if ( !$msg || $msg->isDisabled() ) {
153 // By default wrap protection levels in parentheses
154 $msg = $this->context->msg( 'parentheses' );
155 }
156
157 return $msg->params( Message::listParam( $msgs, ListType::COMMA ) )->parse();
158 }
159
167 private function buildEditLink( PageIdentity $page ) {
168 if ( $this->context->getAuthority()->probablyCan( 'edit', $page ) ) {
169 $linkMsg = 'editlink';
170 } else {
171 $linkMsg = 'viewsourcelink';
172 }
173
174 return $this->linkRenderer->makeLink(
175 $page,
176 $this->context->msg( $linkMsg )->text(),
177 [],
178 [ 'action' => 'edit' ]
179 );
180 }
181
182}
Handles formatting for the "templates used on this page" lists.
__construct(private readonly IContextSource $context, private readonly LinkRenderer $linkRenderer, private readonly LinkBatchFactory $linkBatchFactory, private readonly RestrictionStore $restrictionStore)
format(array $templates, $type=false, $more=null)
Make an HTML list of templates, and then add a "More..." link at the bottom.
This class is a collection of static functions that serve two purposes:
Definition Html.php:44
Class that generates HTML for internal links.
The Message class deals with fetching and processing of interface message into a variety of formats.
Definition Message.php:144
static listParam(array $list, $type=ListType::AND)
Definition Message.php:1355
Factory for LinkBatch objects to batch query page metadata.
Represents a title within MediaWiki.
Definition Title.php:69
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.
ListType
The constants used to specify list types.
Definition ListType.php:9