MediaWiki REL1_33
TemplatesOnThisPageFormatter.php
Go to the documentation of this file.
1<?php
23
31
35 private $context;
36
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}
and that you know you can do these things To protect your we need to make restrictions that forbid anyone to deny you these rights or to ask you to surrender the rights These restrictions translate to certain responsibilities for you if you distribute copies of the or if you modify it For if you distribute copies of such a whether gratis or for a you must give the recipients all the rights that you have You must make sure that receive or can get the source code And you must show them these terms so they know their rights We protect your rights with two and(2) offer you this license which gives you legal permission to copy
Class representing a list of titles The execute() method checks them all for existence and adds them ...
Definition LinkBatch.php:34
Class that generates HTML links for pages.
Handles formatting for the "templates used on this page" lists.
buildEditLink(Title $titleObj)
Return a link to the edit page, with the text saying "view source" if the user can't edit the page.
formatTemplate(LinkTarget $target)
Builds an item for an individual template.
format(array $templates, $type=false, $more=null)
Make an HTML list of templates, and then add a "More..." link at the bottom.
__construct(IContextSource $context, LinkRenderer $linkRenderer)
getRestrictionsText(array $restrictions)
If the page is protected, get the relevant text for those restrictions.
Represents a title within MediaWiki.
Definition Title.php:40
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:822
Interface for objects which can provide a MediaWiki context on request.
$batch execute()
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))