| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\EditPage; |
| 22 |
|
| 23 |
use MediaWiki\Cache\LinkBatchFactory; |
| 24 |
use MediaWiki\Context\IContextSource; |
| 25 |
use MediaWiki\Html\Html; |
| 26 |
use MediaWiki\Linker\LinkRenderer; |
| 27 |
use MediaWiki\Page\PageIdentity; |
| 28 |
use MediaWiki\Page\PageReference; |
| 29 |
use MediaWiki\Permissions\RestrictionStore; |
| 30 |
use MediaWiki\Title\Title; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
class TemplatesOnThisPageFormatter { |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
private $context; |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
private $linkRenderer; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
private $linkBatchFactory; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
private $restrictionStore; |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public function __construct( |
| 67 |
IContextSource $context, |
| 68 |
LinkRenderer $linkRenderer, |
| 69 |
LinkBatchFactory $linkBatchFactory, |
| 70 |
RestrictionStore $restrictionStore |
| 71 |
) { |
| 72 |
$this->context = $context; |
| 73 |
$this->linkRenderer = $linkRenderer; |
| 74 |
$this->linkBatchFactory = $linkBatchFactory; |
| 75 |
$this->restrictionStore = $restrictionStore; |
| 76 |
} |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
public function format( array $templates, $type = false, $more = null ) { |
| 90 |
if ( !$templates ) { |
| 91 |
|
| 92 |
return ''; |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
$batch = $this->linkBatchFactory->newLinkBatch( $templates ); |
| 97 |
$batch->setCaller( __METHOD__ ); |
| 98 |
$batch->execute(); |
| 99 |
|
| 100 |
|
| 101 |
$outText = Html::openElement( 'div', [ 'class' => 'mw-templatesUsedExplanation' ] ); |
| 102 |
$count = count( $templates ); |
| 103 |
if ( $type === 'preview' ) { |
| 104 |
$outText .= $this->context->msg( 'templatesusedpreview' )->numParams( $count ) |
| 105 |
->parseAsBlock(); |
| 106 |
} elseif ( $type === 'section' ) { |
| 107 |
$outText .= $this->context->msg( 'templatesusedsection' )->numParams( $count ) |
| 108 |
->parseAsBlock(); |
| 109 |
} else { |
| 110 |
$outText .= $this->context->msg( 'templatesused' )->numParams( $count ) |
| 111 |
->parseAsBlock(); |
| 112 |
} |
| 113 |
$outText .= Html::closeElement( 'div' ) . Html::openElement( 'ul' ) . "\n"; |
| 114 |
|
| 115 |
usort( $templates, [ Title::class, 'compare' ] ); |
| 116 |
foreach ( $templates as $template ) { |
| 117 |
$outText .= $this->formatTemplate( $template ); |
| 118 |
} |
| 119 |
|
| 120 |
if ( $more instanceof PageReference ) { |
| 121 |
$outText .= Html::rawElement( 'li', [], |
| 122 |
$this->linkRenderer->makeLink( |
| 123 |
$more, |
| 124 |
$this->context->msg( 'moredotdotdot' )->text() |
| 125 |
) |
| 126 |
); |
| 127 |
} elseif ( $more ) { |
| 128 |
|
| 129 |
$outText .= Html::rawElement( 'li', [], $more ); |
| 130 |
} |
| 131 |
|
| 132 |
$outText .= Html::closeElement( 'ul' ); |
| 133 |
return $outText; |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
private function formatTemplate( PageIdentity $target ) { |
| 145 |
if ( !$target->canExist() ) { |
| 146 |
return Html::rawElement( 'li', [], $this->linkRenderer->makeLink( $target ) ); |
| 147 |
} |
| 148 |
|
| 149 |
$protected = $this->getRestrictionsText( |
| 150 |
$this->restrictionStore->getRestrictions( $target, 'edit' ) |
| 151 |
); |
| 152 |
$editLink = $this->buildEditLink( $target ); |
| 153 |
return Html::rawElement( 'li', [], $this->linkRenderer->makeLink( $target ) |
| 154 |
. $this->context->msg( 'word-separator' )->escaped() |
| 155 |
. $this->context->msg( 'parentheses' )->rawParams( $editLink )->escaped() |
| 156 |
. $this->context->msg( 'word-separator' )->escaped() |
| 157 |
. $protected |
| 158 |
); |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
private function getRestrictionsText( array $restrictions ) { |
| 169 |
$protected = ''; |
| 170 |
if ( !$restrictions ) { |
| 171 |
return $protected; |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
$msg = null; |
| 176 |
if ( $restrictions === [ 'sysop' ] ) { |
| 177 |
$msg = $this->context->msg( 'template-protected' ); |
| 178 |
} elseif ( $restrictions === [ 'autoconfirmed' ] ) { |
| 179 |
$msg = $this->context->msg( 'template-semiprotected' ); |
| 180 |
} |
| 181 |
if ( $msg && !$msg->isDisabled() ) { |
| 182 |
$protected = $msg->parse(); |
| 183 |
} else { |
| 184 |
|
| 185 |
|
| 186 |
$msgs = []; |
| 187 |
foreach ( $restrictions as $r ) { |
| 188 |
$msgs[] = $this->context->msg( "restriction-level-$r" )->parse(); |
| 189 |
} |
| 190 |
$protected = $this->context->msg( 'parentheses' ) |
| 191 |
->rawParams( $this->context->getLanguage()->commaList( $msgs ) )->escaped(); |
| 192 |
} |
| 193 |
|
| 194 |
return $protected; |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
private function buildEditLink( PageIdentity $page ) { |
| 205 |
if ( $this->context->getAuthority()->probablyCan( 'edit', $page ) ) { |
| 206 |
$linkMsg = 'editlink'; |
| 207 |
} else { |
| 208 |
$linkMsg = 'viewsourcelink'; |
| 209 |
} |
| 210 |
|
| 211 |
return $this->linkRenderer->makeLink( |
| 212 |
$page, |
| 213 |
$this->context->msg( $linkMsg )->text(), |
| 214 |
[], |
| 215 |
[ 'action' => 'edit' ] |
| 216 |
); |
| 217 |
} |
| 218 |
|
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
class_alias( TemplatesOnThisPageFormatter::class, 'TemplatesOnThisPageFormatter' ); |