MediaWiki master
TemplatesOnThisPageFormatter.php
Go to the documentation of this file.
1<?php
7namespace MediaWiki\EditPage;
8
19
27
28 public function __construct(
29 private readonly IContextSource $context,
30 private readonly LinkRenderer $linkRenderer,
31 private readonly LinkBatchFactory $linkBatchFactory,
32 private readonly RestrictionStore $restrictionStore
33 ) {
34 }
35
47 public function format( array $templates, $type = false, $more = null ): string {
48 if ( !$templates ) {
49 // No templates
50 return '';
51 }
52
53 # Do a batch existence check
54 $batch = $this->linkBatchFactory->newLinkBatch( $templates );
55 $batch->setCaller( __METHOD__ );
56 $batch->execute();
57
58 # Construct the HTML
59 $outText = Html::openElement( 'div', [ 'class' => 'mw-templatesUsedExplanation' ] );
60 $count = count( $templates );
61 if ( $type === 'preview' ) {
62 $outText .= $this->context->msg( 'templatesusedpreview' )->numParams( $count )
63 ->parseAsBlock();
64 } elseif ( $type === 'section' ) {
65 $outText .= $this->context->msg( 'templatesusedsection' )->numParams( $count )
66 ->parseAsBlock();
67 } else {
68 $outText .= $this->context->msg( 'templatesused' )->numParams( $count )
69 ->parseAsBlock();
70 }
71 $outText .= Html::closeElement( 'div' ) . Html::openElement( 'ul' ) . "\n";
72
73 usort( $templates, Title::compare( ... ) );
74 foreach ( $templates as $template ) {
75 $outText .= $this->formatTemplate( $template );
76 }
77
78 if ( $more instanceof PageReference ) {
79 $outText .= Html::rawElement( 'li', [],
80 $this->linkRenderer->makeLink(
81 $more,
82 $this->context->msg( 'moredotdotdot' )->text()
83 )
84 );
85 } elseif ( $more ) {
86 // Documented as should already be escaped
87 $outText .= Html::rawElement( 'li', [], $more );
88 }
89
90 $outText .= Html::closeElement( 'ul' );
91 return $outText;
92 }
93
99 private function formatTemplate( PageIdentity $target ): string {
100 if ( !$target->canExist() ) {
101 return Html::rawElement( 'li', [], $this->linkRenderer->makeLink( $target ) );
102 }
103
104 $protected = $this->getRestrictionsText(
105 $this->restrictionStore->getRestrictions( $target, 'edit' )
106 );
107 $editLink = $this->buildEditLink( $target );
108 return Html::rawElement( 'li', [], $this->linkRenderer->makeLink( $target )
109 . $this->context->msg( 'word-separator' )->escaped()
110 . $this->context->msg( 'parentheses' )->rawParams( $editLink )->escaped()
111 . $this->context->msg( 'word-separator' )->escaped()
112 . $protected
113 );
114 }
115
122 private function getRestrictionsText( array $restrictions ): string {
123 if ( !$restrictions ) {
124 return '';
125 }
126
127 // Construct the message from restriction-level-*
128 // e.g. restriction-level-sysop, restriction-level-autoconfirmed
129 $msgs = array_map(
130 fn ( $r ) => $this->context->msg( "restriction-level-$r" ),
131 $restrictions
132 );
133
134 // Check backwards-compatible messages for the built-in protection levels
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 // By default wrap protection levels in parentheses
143 $msg = $this->context->msg( 'parentheses' );
144 }
145
146 return $msg->params( Message::listParam( $msgs, ListType::COMMA ) )->parse();
147 }
148
155 private function buildEditLink( PageIdentity $page ): string {
156 if ( $this->context->getAuthority()->probablyCan( 'edit', $page ) ) {
157 $linkMsg = 'editlink';
158 } else {
159 $linkMsg = 'viewsourcelink';
160 }
161
162 return $this->linkRenderer->makeLink(
163 $page,
164 $this->context->msg( $linkMsg )->text(),
165 [],
166 [ 'action' => 'edit' ]
167 );
168 }
169
170}
if(!defined('MW_SETUP_CALLBACK'))
Definition WebStart.php:71
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
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.
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