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  $linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory();
71  $batch = $linkBatchFactory->newLinkBatch( $templates );
72  $batch->execute();
73 
74  # Construct the HTML
75  $outText = '<div class="mw-templatesUsedExplanation">';
76  $count = count( $templates );
77  if ( $type === 'preview' ) {
78  $outText .= $this->context->msg( 'templatesusedpreview' )->numParams( $count )
79  ->parseAsBlock();
80  } elseif ( $type === 'section' ) {
81  $outText .= $this->context->msg( 'templatesusedsection' )->numParams( $count )
82  ->parseAsBlock();
83  } else {
84  $outText .= $this->context->msg( 'templatesused' )->numParams( $count )
85  ->parseAsBlock();
86  }
87  $outText .= "</div><ul>\n";
88 
89  usort( $templates, 'Title::compare' );
90  foreach ( $templates as $template ) {
91  $outText .= $this->formatTemplate( $template );
92  }
93 
94  if ( $more instanceof LinkTarget ) {
95  $outText .= Html::rawElement( 'li', [], $this->linkRenderer->makeLink(
96  $more, $this->context->msg( 'moredotdotdot' )->text() ) );
97  } elseif ( $more ) {
98  // Documented as should already be escaped
99  $outText .= Html::rawElement( 'li', [], $more );
100  }
101 
102  $outText .= '</ul>';
103  return $outText;
104  }
105 
112  private function formatTemplate( LinkTarget $target ) {
113  // TODO Would be nice if we didn't have to use Title here
114  $titleObj = Title::newFromLinkTarget( $target );
115  $protected = $this->getRestrictionsText( $titleObj->getRestrictions( 'edit' ) );
116  $editLink = $this->buildEditLink( $titleObj );
117  return '<li>' . $this->linkRenderer->makeLink( $target )
118  . $this->context->msg( 'word-separator' )->escaped()
119  . $this->context->msg( 'parentheses' )->rawParams( $editLink )->escaped()
120  . $this->context->msg( 'word-separator' )->escaped()
121  . $protected . '</li>';
122  }
123 
131  private function getRestrictionsText( array $restrictions ) {
132  $protected = '';
133  if ( !$restrictions ) {
134  return $protected;
135  }
136 
137  // Check backwards-compatible messages
138  $msg = null;
139  if ( $restrictions === [ 'sysop' ] ) {
140  $msg = $this->context->msg( 'template-protected' );
141  } elseif ( $restrictions === [ 'autoconfirmed' ] ) {
142  $msg = $this->context->msg( 'template-semiprotected' );
143  }
144  if ( $msg && !$msg->isDisabled() ) {
145  $protected = $msg->parse();
146  } else {
147  // Construct the message from restriction-level-*
148  // e.g. restriction-level-sysop, restriction-level-autoconfirmed
149  $msgs = [];
150  foreach ( $restrictions as $r ) {
151  $msgs[] = $this->context->msg( "restriction-level-$r" )->parse();
152  }
153  $protected = $this->context->msg( 'parentheses' )
154  ->rawParams( $this->context->getLanguage()->commaList( $msgs ) )->escaped();
155  }
156 
157  return $protected;
158  }
159 
167  private function buildEditLink( LinkTarget $titleObj ) {
168  if ( MediaWikiServices::getInstance()->getPermissionManager()
169  ->quickUserCan( 'edit', $this->context->getUser(), $titleObj )
170  ) {
171  $linkMsg = 'editlink';
172  } else {
173  $linkMsg = 'viewsourcelink';
174  }
175 
176  return $this->linkRenderer->makeLink(
177  $titleObj,
178  $this->context->msg( $linkMsg )->text(),
179  [],
180  [ 'action' => 'edit' ]
181  );
182  }
183 
184 }
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:166
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:131
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:167
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:289
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:57
TemplatesOnThisPageFormatter\$context
IContextSource $context
Definition: TemplatesOnThisPageFormatter.php:36
TemplatesOnThisPageFormatter\formatTemplate
formatTemplate(LinkTarget $target)
Builds an item for an individual template.
Definition: TemplatesOnThisPageFormatter.php:112
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:212
TemplatesOnThisPageFormatter\$linkRenderer
LinkRenderer $linkRenderer
Definition: TemplatesOnThisPageFormatter.php:41
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
$type
$type
Definition: testCompression.php:52