MediaWiki  master
TemplatesOnThisPageFormatter.php
Go to the documentation of this file.
1 <?php
21 namespace MediaWiki\EditPage;
22 
23 use IContextSource;
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  $protected = $this->getRestrictionsText(
146  $this->restrictionStore->getRestrictions( $target, 'edit' )
147  );
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()
153  . $protected
154  );
155  }
156 
164  private function getRestrictionsText( array $restrictions ) {
165  $protected = '';
166  if ( !$restrictions ) {
167  return $protected;
168  }
169 
170  // Check backwards-compatible messages
171  $msg = null;
172  if ( $restrictions === [ 'sysop' ] ) {
173  $msg = $this->context->msg( 'template-protected' );
174  } elseif ( $restrictions === [ 'autoconfirmed' ] ) {
175  $msg = $this->context->msg( 'template-semiprotected' );
176  }
177  if ( $msg && !$msg->isDisabled() ) {
178  $protected = $msg->parse();
179  } else {
180  // Construct the message from restriction-level-*
181  // e.g. restriction-level-sysop, restriction-level-autoconfirmed
182  $msgs = [];
183  foreach ( $restrictions as $r ) {
184  $msgs[] = $this->context->msg( "restriction-level-$r" )->parse();
185  }
186  $protected = $this->context->msg( 'parentheses' )
187  ->rawParams( $this->context->getLanguage()->commaList( $msgs ) )->escaped();
188  }
189 
190  return $protected;
191  }
192 
200  private function buildEditLink( PageIdentity $page ) {
201  if ( $this->context->getAuthority()->probablyCan( 'edit', $page ) ) {
202  $linkMsg = 'editlink';
203  } else {
204  $linkMsg = 'viewsourcelink';
205  }
206 
207  return $this->linkRenderer->makeLink(
208  $page,
209  $this->context->msg( $linkMsg )->text(),
210  [],
211  [ 'action' => 'edit' ]
212  );
213  }
214 
215 }
216 
220 class_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:57
static openElement( $element, $attribs=[])
Identical to rawElement(), but has no third parameter and omits the end tag (and the self-closing '/'...
Definition: Html.php:288
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:239
static closeElement( $element)
Returns "</$element>".
Definition: Html.php:352
Class that generates HTML for internal links.
Represents a title within MediaWiki.
Definition: Title.php:76
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.