MediaWiki  master
TemplatesOnThisPageFormatter.php
Go to the documentation of this file.
1 <?php
24 
32 
36  private $context;
37 
41  private $linkRenderer;
42 
48  $this->context = $context;
49  $this->linkRenderer = $linkRenderer;
50  }
51 
63  public function format( array $templates, $type = false, $more = null ) {
64  if ( !$templates ) {
65  // No templates
66  return '';
67  }
68 
69  # Do a batch existence check
70  ( new LinkBatch( $templates ) )->execute();
71 
72  # Construct the HTML
73  $outText = '<div class="mw-templatesUsedExplanation">';
74  $count = count( $templates );
75  if ( $type === 'preview' ) {
76  $outText .= $this->context->msg( 'templatesusedpreview' )->numParams( $count )
77  ->parseAsBlock();
78  } elseif ( $type === 'section' ) {
79  $outText .= $this->context->msg( 'templatesusedsection' )->numParams( $count )
80  ->parseAsBlock();
81  } else {
82  $outText .= $this->context->msg( 'templatesused' )->numParams( $count )
83  ->parseAsBlock();
84  }
85  $outText .= "</div><ul>\n";
86 
87  usort( $templates, 'Title::compare' );
88  foreach ( $templates as $template ) {
89  $outText .= $this->formatTemplate( $template );
90  }
91 
92  if ( $more instanceof LinkTarget ) {
93  $outText .= Html::rawElement( 'li', [], $this->linkRenderer->makeLink(
94  $more, $this->context->msg( 'moredotdotdot' )->text() ) );
95  } elseif ( $more ) {
96  // Documented as should already be escaped
97  $outText .= Html::rawElement( 'li', [], $more );
98  }
99 
100  $outText .= '</ul>';
101  return $outText;
102  }
103 
110  private function formatTemplate( LinkTarget $target ) {
111  // TODO Would be nice if we didn't have to use Title here
112  $titleObj = Title::newFromLinkTarget( $target );
113  $protected = $this->getRestrictionsText( $titleObj->getRestrictions( 'edit' ) );
114  $editLink = $this->buildEditLink( $titleObj );
115  return '<li>' . $this->linkRenderer->makeLink( $target )
116  . $this->context->msg( 'word-separator' )->escaped()
117  . $this->context->msg( 'parentheses' )->rawParams( $editLink )->escaped()
118  . $this->context->msg( 'word-separator' )->escaped()
119  . $protected . '</li>';
120  }
121 
129  private function getRestrictionsText( array $restrictions ) {
130  $protected = '';
131  if ( !$restrictions ) {
132  return $protected;
133  }
134 
135  // Check backwards-compatible messages
136  $msg = null;
137  if ( $restrictions === [ 'sysop' ] ) {
138  $msg = $this->context->msg( 'template-protected' );
139  } elseif ( $restrictions === [ 'autoconfirmed' ] ) {
140  $msg = $this->context->msg( 'template-semiprotected' );
141  }
142  if ( $msg && !$msg->isDisabled() ) {
143  $protected = $msg->parse();
144  } else {
145  // Construct the message from restriction-level-*
146  // e.g. restriction-level-sysop, restriction-level-autoconfirmed
147  $msgs = [];
148  foreach ( $restrictions as $r ) {
149  $msgs[] = $this->context->msg( "restriction-level-$r" )->parse();
150  }
151  $protected = $this->context->msg( 'parentheses' )
152  ->rawParams( $this->context->getLanguage()->commaList( $msgs ) )->escaped();
153  }
154 
155  return $protected;
156  }
157 
165  private function buildEditLink( LinkTarget $titleObj ) {
166  if ( MediaWikiServices::getInstance()->getPermissionManager()
167  ->quickUserCan( 'edit', $this->context->getUser(), $titleObj )
168  ) {
169  $linkMsg = 'editlink';
170  } else {
171  $linkMsg = 'viewsourcelink';
172  }
173 
174  return $this->linkRenderer->makeLink(
175  $titleObj,
176  $this->context->msg( $linkMsg )->text(),
177  [],
178  [ 'action' => 'edit' ]
179  );
180  }
181 
182 }
LinkBatch
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition: LinkBatch.php:34
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:129
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:41
TemplatesOnThisPageFormatter\__construct
__construct(IContextSource $context, LinkRenderer $linkRenderer)
Definition: TemplatesOnThisPageFormatter.php:47
TemplatesOnThisPageFormatter\getRestrictionsText
getRestrictionsText(array $restrictions)
If the page is protected, get the relevant text for those restrictions.
Definition: TemplatesOnThisPageFormatter.php:129
TemplatesOnThisPageFormatter\buildEditLink
buildEditLink(LinkTarget $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:165
getPermissionManager
getPermissionManager()
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:63
TemplatesOnThisPageFormatter
Handles formatting for the "templates used on this page" lists.
Definition: TemplatesOnThisPageFormatter.php:31
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:269
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:53
TemplatesOnThisPageFormatter\$context
IContextSource $context
Definition: TemplatesOnThisPageFormatter.php:36
TemplatesOnThisPageFormatter\formatTemplate
formatTemplate(LinkTarget $target)
Builds an item for an individual template.
Definition: TemplatesOnThisPageFormatter.php:110
TemplatesOnThisPageFormatter\$linkRenderer
LinkRenderer $linkRenderer
Definition: TemplatesOnThisPageFormatter.php:41
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
$type
$type
Definition: testCompression.php:50