47 public function format( array $templates, $type =
false, $more =
null ): string {
53 # Do a batch existence check
54 $batch = $this->linkBatchFactory->newLinkBatch( $templates );
55 $batch->setCaller( __METHOD__ );
59 $outText = Html::openElement(
'div', [
'class' =>
'mw-templatesUsedExplanation' ] );
60 $count = count( $templates );
61 if ( $type ===
'preview' ) {
62 $outText .= $this->context->msg(
'templatesusedpreview' )->numParams( $count )
64 } elseif ( $type ===
'section' ) {
65 $outText .= $this->context->msg(
'templatesusedsection' )->numParams( $count )
68 $outText .= $this->context->msg(
'templatesused' )->numParams( $count )
71 $outText .= Html::closeElement(
'div' ) . Html::openElement(
'ul' ) .
"\n";
73 usort( $templates, Title::compare( ... ) );
74 foreach ( $templates as $template ) {
75 $outText .= $this->formatTemplate( $template );
78 if ( $more instanceof PageReference ) {
79 $outText .= Html::rawElement(
'li', [],
80 $this->linkRenderer->makeLink(
82 $this->context->msg(
'moredotdotdot' )->text()
87 $outText .= Html::rawElement(
'li', [], $more );
90 $outText .= Html::closeElement(
'ul' );
99 private function formatTemplate( PageIdentity $target ): string {
100 if ( !$target->canExist() ) {
101 return Html::rawElement(
'li', [], $this->linkRenderer->makeLink( $target ) );
104 $protected = $this->getRestrictionsText(
105 $this->restrictionStore->getRestrictions( $target,
'edit' )
107 $editLink = $this->buildEditLink( $target );
108 return Html::rawElement(
'li', [], $this->linkRenderer->makeLink( $target )
109 . $this->context->msg(
'word-separator' )->escaped()
110 . $this->context->msg(
'parentheses' )->rawParams( $editLink )->escaped()
111 . $this->context->msg(
'word-separator' )->escaped()
122 private function getRestrictionsText( array $restrictions ): string {
123 if ( !$restrictions ) {
130 fn ( $r ) => $this->context->msg(
"restriction-level-$r" ),
136 if ( $restrictions === [
'sysop' ] ) {
137 $msg = $this->context->msg(
'template-protected' );
138 } elseif ( $restrictions === [
'autoconfirmed' ] ) {
139 $msg = $this->context->msg(
'template-semiprotected' );
141 if ( !$msg || $msg->isDisabled() ) {
143 $msg = $this->context->msg(
'parentheses' );
146 return $msg->params( Message::listParam( $msgs, ListType::COMMA ) )->parse();
155 private function buildEditLink( PageIdentity $page ): string {
156 if ( $this->context->
getAuthority()->probablyCan(
'edit', $page ) ) {
157 $linkMsg =
'editlink';
159 $linkMsg =
'viewsourcelink';
162 return $this->linkRenderer->makeLink(
164 $this->context->msg( $linkMsg )->text(),
166 [
'action' =>
'edit' ]