MediaWiki  1.29.2
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  $batch = new LinkBatch;
70  foreach ( $templates as $title ) {
71  $batch->addObj( $title );
72  }
73  $batch->execute();
74 
75  # Construct the HTML
76  $outText = '<div class="mw-templatesUsedExplanation">';
77  $count = count( $templates );
78  if ( $type === 'preview' ) {
79  $outText .= $this->context->msg( 'templatesusedpreview' )->numParams( $count )
80  ->parseAsBlock();
81  } elseif ( $type === 'section' ) {
82  $outText .= $this->context->msg( 'templatesusedsection' )->numParams( $count )
83  ->parseAsBlock();
84  } else {
85  $outText .= $this->context->msg( 'templatesused' )->numParams( $count )
86  ->parseAsBlock();
87  }
88  $outText .= "</div><ul>\n";
89 
90  usort( $templates, 'Title::compare' );
91  foreach ( $templates as $template ) {
92  $outText .= $this->formatTemplate( $template );
93  }
94 
95  if ( $more instanceof LinkTarget ) {
96  $outText .= Html::rawElement( 'li', [], $this->linkRenderer->makeLink(
97  $more, $this->context->msg( 'moredotdotdot' )->text() ) );
98  } elseif ( $more ) {
99  // Documented as should already be escaped
100  $outText .= Html::rawElement( 'li', [], $more );
101  }
102 
103  $outText .= '</ul>';
104  return $outText;
105  }
106 
113  private function formatTemplate( LinkTarget $target ) {
114  // TODO Would be nice if we didn't have to use Title here
115  $titleObj = Title::newFromLinkTarget( $target );
116  $protected = $this->getRestrictionsText( $titleObj->getRestrictions( 'edit' ) );
117  $editLink = $this->buildEditLink( $titleObj );
118  return '<li>' . $this->linkRenderer->makeLink( $target )
119  . $this->context->msg( 'word-separator' )->escaped()
120  . $this->context->msg( 'parentheses' )->rawParams( $editLink )->escaped()
121  . $this->context->msg( 'word-separator' )->escaped()
122  . $protected . '</li>';
123  }
124 
132  private function getRestrictionsText( array $restrictions ) {
133  $protected = '';
134  if ( !$restrictions ) {
135  return $protected;
136  }
137 
138  // Check backwards-compatible messages
139  $msg = null;
140  if ( $restrictions === [ 'sysop' ] ) {
141  $msg = $this->context->msg( 'template-protected' );
142  } elseif ( $restrictions === [ 'autoconfirmed' ] ) {
143  $msg = $this->context->msg( 'template-semiprotected' );
144  }
145  if ( $msg && !$msg->isDisabled() ) {
146  $protected = $msg->parse();
147  } else {
148  // Construct the message from restriction-level-*
149  // e.g. restriction-level-sysop, restriction-level-autoconfirmed
150  $msgs = [];
151  foreach ( $restrictions as $r ) {
152  $msgs[] = $this->context->msg( "restriction-level-$r" )->parse();
153  }
154  $protected = $this->context->msg( 'parentheses' )
155  ->rawParams( $this->context->getLanguage()->commaList( $msgs ) )->escaped();
156  }
157 
158  return $protected;
159  }
160 
168  private function buildEditLink( Title $titleObj ) {
169  if ( $titleObj->quickUserCan( 'edit', $this->context->getUser() ) ) {
170  $linkMsg = 'editlink';
171  } else {
172  $linkMsg = 'viewsourcelink';
173  }
174 
175  return $this->linkRenderer->makeLink(
176  $titleObj,
177  $this->context->msg( $linkMsg )->text(),
178  [],
179  [ 'action' => 'edit' ]
180  );
181  }
182 
183 }
$template
this hook is for auditing only or null if authentication failed before getting that far or null if we can t even determine that probably a stub 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:783
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
captcha-old.count
count
Definition: captcha-old.py:225
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:42
TemplatesOnThisPageFormatter\buildEditLink
buildEditLink(Title $titleObj)
Return a link to the edit page, with the text saying "view source" if the user can't edit the page.
Definition: TemplatesOnThisPageFormatter.php:168
TemplatesOnThisPageFormatter\__construct
__construct(IContextSource $context, LinkRenderer $linkRenderer)
Definition: TemplatesOnThisPageFormatter.php:46
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
Title\quickUserCan
quickUserCan( $action, $user=null)
Can $user perform $action on this page? This skips potentially expensive cascading permission checks ...
Definition: Title.php:1917
$type
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached my talk my contributions etc etc otherwise the built in rate limiting checks are if enabled allows for interception of redirect as a string mapping parameter names to values & $type
Definition: hooks.txt:2536
TemplatesOnThisPageFormatter\getRestrictionsText
getRestrictionsText(array $restrictions)
If the page is protected, get the relevant text for those restrictions.
Definition: TemplatesOnThisPageFormatter.php:132
php
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
$title
namespace and then decline to actually register it file or subcat img or subcat $title
Definition: hooks.txt:934
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget)
Create a new Title from a LinkTarget.
Definition: Title.php:239
TemplatesOnThisPageFormatter\format
format(array $templates, $type=false, $more=null)
Make an HTML list of templates, and then add a "More..." link at the bottom.
Definition: TemplatesOnThisPageFormatter.php:62
TemplatesOnThisPageFormatter
Handles formatting for the "templates used on this page" lists.
Definition: TemplatesOnThisPageFormatter.php:30
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:55
TemplatesOnThisPageFormatter\$context
IContextSource $context
Definition: TemplatesOnThisPageFormatter.php:35
Title
Represents a title within MediaWiki.
Definition: Title.php:39
TemplatesOnThisPageFormatter\formatTemplate
formatTemplate(LinkTarget $target)
Builds an item for an individual template.
Definition: TemplatesOnThisPageFormatter.php:113
as
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
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:209
$batch
$batch
Definition: linkcache.txt:23
TemplatesOnThisPageFormatter\$linkRenderer
LinkRenderer $linkRenderer
Definition: TemplatesOnThisPageFormatter.php:40
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:27
array
the array() calling protocol came about after MediaWiki 1.4rc1.