MediaWiki  master
TemplatesOnThisPageFormatter.php
Go to the documentation of this file.
1 <?php
25 
33 
37  private $context;
38 
42  private $linkRenderer;
43 
49  $this->context = $context;
50  $this->linkRenderer = $linkRenderer;
51  }
52 
64  public function format( array $templates, $type = false, $more = null ) {
65  if ( !$templates ) {
66  // No templates
67  return '';
68  }
69 
70  # Do a batch existence check
71  $linkBatchFactory = MediaWikiServices::getInstance()->getLinkBatchFactory();
72  $batch = $linkBatchFactory->newLinkBatch( $templates );
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::class, '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( PageIdentity $page ) {
169  if ( $this->context->getAuthority()->probablyCan( 'edit', $page ) ) {
170  $linkMsg = 'editlink';
171  } else {
172  $linkMsg = 'viewsourcelink';
173  }
174 
175  return $this->linkRenderer->makeLink(
176  new TitleValue( $page->getNamespace(), $page->getDBkey() ),
177  $this->context->msg( $linkMsg )->text(),
178  [],
179  [ 'action' => 'edit' ]
180  );
181  }
182 
183 }
Page\PageIdentity
Interface for objects (potentially) representing an editable wiki page.
Definition: PageIdentity.php:64
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
MediaWiki\Linker\LinkRenderer
Class that generates HTML links for pages.
Definition: LinkRenderer.php:43
TemplatesOnThisPageFormatter\__construct
__construct(IContextSource $context, LinkRenderer $linkRenderer)
Definition: TemplatesOnThisPageFormatter.php:48
TemplatesOnThisPageFormatter\getRestrictionsText
getRestrictionsText(array $restrictions)
If the page is protected, get the relevant text for those restrictions.
Definition: TemplatesOnThisPageFormatter.php:132
TemplatesOnThisPageFormatter\buildEditLink
buildEditLink(PageIdentity $page)
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
Page\PageReference\getNamespace
getNamespace()
Returns the page's namespace number.
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:64
TemplatesOnThisPageFormatter
Handles formatting for the "templates used on this page" lists.
Definition: TemplatesOnThisPageFormatter.php:32
Page\PageReference\getDBkey
getDBkey()
Get the page title in DB key form.
Title\newFromLinkTarget
static newFromLinkTarget(LinkTarget $linkTarget, $forceClone='')
Returns a Title given a LinkTarget.
Definition: Title.php:295
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
TemplatesOnThisPageFormatter\$context
IContextSource $context
Definition: TemplatesOnThisPageFormatter.php:37
TemplatesOnThisPageFormatter\formatTemplate
formatTemplate(LinkTarget $target)
Builds an item for an individual template.
Definition: TemplatesOnThisPageFormatter.php:113
Html\rawElement
static rawElement( $element, $attribs=[], $contents='')
Returns an HTML element in a string.
Definition: Html.php:210
TemplatesOnThisPageFormatter\$linkRenderer
LinkRenderer $linkRenderer
Definition: TemplatesOnThisPageFormatter.php:42
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
TitleValue
Represents a page (or page fragment) title within MediaWiki.
Definition: TitleValue.php:40
$type
$type
Definition: testCompression.php:52