MediaWiki  master
TemplatesOnThisPageFormatter.php
Go to the documentation of this file.
1 <?php
23 
31 
35  private $context;
36 
40  private $linkRenderer;
41 
47  $this->context = $context;
48  $this->linkRenderer = $linkRenderer;
49  }
50 
62  public function format( array $templates, $type = false, $more = null ) {
63  if ( !$templates ) {
64  // No templates
65  return '';
66  }
67 
68  # Do a batch existence check
69  ( new LinkBatch( $templates ) )->execute();
70 
71  # Construct the HTML
72  $outText = '<div class="mw-templatesUsedExplanation">';
73  $count = count( $templates );
74  if ( $type === 'preview' ) {
75  $outText .= $this->context->msg( 'templatesusedpreview' )->numParams( $count )
76  ->parseAsBlock();
77  } elseif ( $type === 'section' ) {
78  $outText .= $this->context->msg( 'templatesusedsection' )->numParams( $count )
79  ->parseAsBlock();
80  } else {
81  $outText .= $this->context->msg( 'templatesused' )->numParams( $count )
82  ->parseAsBlock();
83  }
84  $outText .= "</div><ul>\n";
85 
86  usort( $templates, 'Title::compare' );
87  foreach ( $templates as $template ) {
88  $outText .= $this->formatTemplate( $template );
89  }
90 
91  if ( $more instanceof LinkTarget ) {
92  $outText .= Html::rawElement( 'li', [], $this->linkRenderer->makeLink(
93  $more, $this->context->msg( 'moredotdotdot' )->text() ) );
94  } elseif ( $more ) {
95  // Documented as should already be escaped
96  $outText .= Html::rawElement( 'li', [], $more );
97  }
98 
99  $outText .= '</ul>';
100  return $outText;
101  }
102 
109  private function formatTemplate( LinkTarget $target ) {
110  // TODO Would be nice if we didn't have to use Title here
111  $titleObj = Title::newFromLinkTarget( $target );
112  $protected = $this->getRestrictionsText( $titleObj->getRestrictions( 'edit' ) );
113  $editLink = $this->buildEditLink( $titleObj );
114  return '<li>' . $this->linkRenderer->makeLink( $target )
115  . $this->context->msg( 'word-separator' )->escaped()
116  . $this->context->msg( 'parentheses' )->rawParams( $editLink )->escaped()
117  . $this->context->msg( 'word-separator' )->escaped()
118  . $protected . '</li>';
119  }
120 
128  private function getRestrictionsText( array $restrictions ) {
129  $protected = '';
130  if ( !$restrictions ) {
131  return $protected;
132  }
133 
134  // Check backwards-compatible messages
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  $protected = $msg->parse();
143  } else {
144  // Construct the message from restriction-level-*
145  // e.g. restriction-level-sysop, restriction-level-autoconfirmed
146  $msgs = [];
147  foreach ( $restrictions as $r ) {
148  $msgs[] = $this->context->msg( "restriction-level-$r" )->parse();
149  }
150  $protected = $this->context->msg( 'parentheses' )
151  ->rawParams( $this->context->getLanguage()->commaList( $msgs ) )->escaped();
152  }
153 
154  return $protected;
155  }
156 
164  private function buildEditLink( Title $titleObj ) {
165  if ( $titleObj->quickUserCan( 'edit', $this->context->getUser() ) ) {
166  $linkMsg = 'editlink';
167  } else {
168  $linkMsg = 'viewsourcelink';
169  }
170 
171  return $this->linkRenderer->makeLink(
172  $titleObj,
173  $this->context->msg( $linkMsg )->text(),
174  [],
175  [ 'action' => 'edit' ]
176  );
177  }
178 
179 }
format(array $templates, $type=false, $more=null)
Make an HTML list of templates, and then add a "More..." link at the bottom.
$batch execute()
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
Handles formatting for the "templates used on this page" lists.
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:210
formatTemplate(LinkTarget $target)
Builds an item for an individual template.
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
quickUserCan( $action, $user=null)
Can $user perform $action on this page? This skips potentially expensive cascading permission checks ...
Definition: Title.php:2184
Class that generates HTML links for pages.
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not null
Definition: hooks.txt:780
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
buildEditLink(Title $titleObj)
Return a link to the edit page, with the text saying "view source" if the user can&#39;t edit the page...
__construct(IContextSource $context, LinkRenderer $linkRenderer)
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:271
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that When $user is not it can be in the form of< username >< more info > e g for bot passwords intended to be added to log contexts Fields it might only if the login was with a bot password it is not rendered in wiki pages or galleries in category pages allow injecting custom HTML after the section Any uses of the hook need to handle escaping $template
Definition: hooks.txt:780
getRestrictionsText(array $restrictions)
If the page is protected, get the relevant text for those restrictions.