3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
21 use MediaWiki\Linker\LinkRenderer
;
22 use MediaWiki\Linker\LinkTarget
;
23 use MediaWiki\MediaWikiServices
;
26 * Handles formatting for the "templates used on this page"
27 * lists. Formerly known as Linker::formatTemplates()
31 class TemplatesOnThisPageFormatter
{
41 private $linkRenderer;
44 * @param IContextSource $context
45 * @param LinkRenderer $linkRenderer
47 public function __construct( IContextSource
$context, LinkRenderer
$linkRenderer ) {
48 $this->context
= $context;
49 $this->linkRenderer
= $linkRenderer;
53 * Make an HTML list of templates, and then add a "More..." link at
54 * the bottom. If $more is null, do not add a "More..." link. If $more
55 * is a LinkTarget, make a link to that title and use it. If $more is a string,
56 * directly paste it in as the link (escaping needs to be done manually).
58 * @param LinkTarget[] $templates
59 * @param string|bool $type 'preview' if a preview, 'section' if a section edit, false if neither
60 * @param LinkTarget|string|null $more An escaped link for "More..." of the templates
61 * @return string HTML output
63 public function format( array $templates, $type = false, $more = null ) {
69 # Do a batch existence check
70 ( new LinkBatch( $templates ) )->execute();
73 $outText = '<div class="mw-templatesUsedExplanation">';
74 $count = count( $templates );
75 if ( $type === 'preview' ) {
76 $outText .= $this->context
->msg( 'templatesusedpreview' )->numParams( $count )
78 } elseif ( $type === 'section' ) {
79 $outText .= $this->context
->msg( 'templatesusedsection' )->numParams( $count )
82 $outText .= $this->context
->msg( 'templatesused' )->numParams( $count )
85 $outText .= "</div><ul>\n";
87 usort( $templates, 'Title::compare' );
88 foreach ( $templates as $template ) {
89 $outText .= $this->formatTemplate( $template );
92 if ( $more instanceof LinkTarget
) {
93 $outText .= Html
::rawElement( 'li', [], $this->linkRenderer
->makeLink(
94 $more, $this->context
->msg( 'moredotdotdot' )->text() ) );
96 // Documented as should already be escaped
97 $outText .= Html
::rawElement( 'li', [], $more );
105 * Builds an <li> item for an individual template
107 * @param LinkTarget $target
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>';
123 * If the page is protected, get the relevant text
124 * for those restrictions
126 * @param array $restrictions
129 private function getRestrictionsText( array $restrictions ) {
131 if ( !$restrictions ) {
135 // Check backwards-compatible messages
137 if ( $restrictions === [ 'sysop' ] ) {
138 $msg = $this->context
->msg( 'template-protected' );
139 } elseif ( $restrictions === [ 'autoconfirmed' ] ) {
140 $msg = $this->context
->msg( 'template-semiprotected' );
142 if ( $msg && !$msg->isDisabled() ) {
143 $protected = $msg->parse();
145 // Construct the message from restriction-level-*
146 // e.g. restriction-level-sysop, restriction-level-autoconfirmed
148 foreach ( $restrictions as $r ) {
149 $msgs[] = $this->context
->msg( "restriction-level-$r" )->parse();
151 $protected = $this->context
->msg( 'parentheses' )
152 ->rawParams( $this->context
->getLanguage()->commaList( $msgs ) )->escaped();
159 * Return a link to the edit page, with the text
160 * saying "view source" if the user can't edit the page
162 * @param LinkTarget $titleObj
165 private function buildEditLink( LinkTarget
$titleObj ) {
166 if ( MediaWikiServices
::getInstance()->getPermissionManager()
167 ->quickUserCan( 'edit', $this->context
->getUser(), $titleObj )
169 $linkMsg = 'editlink';
171 $linkMsg = 'viewsourcelink';
174 return $this->linkRenderer
->makeLink(
176 $this->context
->msg( $linkMsg )->text(),
178 [ 'action' => 'edit' ]