TemplatesOnThisPageFormatter.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  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.
  7. *
  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.
  12. *
  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
  17. *
  18. * @file
  19. */
  20. use MediaWiki\Linker\LinkRenderer;
  21. use MediaWiki\Linker\LinkTarget;
  22. /**
  23. * Handles formatting for the "templates used on this page"
  24. * lists. Formerly known as Linker::formatTemplates()
  25. *
  26. * @since 1.28
  27. */
  28. class TemplatesOnThisPageFormatter {
  29. /**
  30. * @var IContextSource
  31. */
  32. private $context;
  33. /**
  34. * @var LinkRenderer
  35. */
  36. private $linkRenderer;
  37. /**
  38. * @param IContextSource $context
  39. * @param LinkRenderer $linkRenderer
  40. */
  41. public function __construct( IContextSource $context, LinkRenderer $linkRenderer ) {
  42. $this->context = $context;
  43. $this->linkRenderer = $linkRenderer;
  44. }
  45. /**
  46. * Make an HTML list of templates, and then add a "More..." link at
  47. * the bottom. If $more is null, do not add a "More..." link. If $more
  48. * is a LinkTarget, make a link to that title and use it. If $more is a string,
  49. * directly paste it in as the link (escaping needs to be done manually).
  50. *
  51. * @param LinkTarget[] $templates
  52. * @param string|bool $type 'preview' if a preview, 'section' if a section edit, false if neither
  53. * @param LinkTarget|string|null $more An escaped link for "More..." of the templates
  54. * @return string HTML output
  55. */
  56. public function format( array $templates, $type = false, $more = null ) {
  57. if ( !$templates ) {
  58. // No templates
  59. return '';
  60. }
  61. # Do a batch existence check
  62. $batch = new LinkBatch;
  63. foreach ( $templates as $title ) {
  64. $batch->addObj( $title );
  65. }
  66. $batch->execute();
  67. # Construct the HTML
  68. $outText = '<div class="mw-templatesUsedExplanation">';
  69. $count = count( $templates );
  70. if ( $type === 'preview' ) {
  71. $outText .= $this->context->msg( 'templatesusedpreview' )->numParams( $count )
  72. ->parseAsBlock();
  73. } elseif ( $type === 'section' ) {
  74. $outText .= $this->context->msg( 'templatesusedsection' )->numParams( $count )
  75. ->parseAsBlock();
  76. } else {
  77. $outText .= $this->context->msg( 'templatesused' )->numParams( $count )
  78. ->parseAsBlock();
  79. }
  80. $outText .= "</div><ul>\n";
  81. usort( $templates, 'Title::compare' );
  82. foreach ( $templates as $template ) {
  83. $outText .= $this->formatTemplate( $template );
  84. }
  85. if ( $more instanceof LinkTarget ) {
  86. $outText .= Html::rawElement( 'li', [], $this->linkRenderer->makeLink(
  87. $more, $this->context->msg( 'moredotdotdot' )->text() ) );
  88. } elseif ( $more ) {
  89. // Documented as should already be escaped
  90. $outText .= Html::rawElement( 'li', [], $more );
  91. }
  92. $outText .= '</ul>';
  93. return $outText;
  94. }
  95. /**
  96. * Builds an <li> item for an individual template
  97. *
  98. * @param LinkTarget $target
  99. * @return string
  100. */
  101. private function formatTemplate( LinkTarget $target ) {
  102. // TODO Would be nice if we didn't have to use Title here
  103. $titleObj = Title::newFromLinkTarget( $target );
  104. $protected = $this->getRestrictionsText( $titleObj->getRestrictions( 'edit' ) );
  105. $editLink = $this->buildEditLink( $titleObj );
  106. return '<li>' . $this->linkRenderer->makeLink( $target )
  107. . $this->context->msg( 'word-separator' )->escaped()
  108. . $this->context->msg( 'parentheses' )->rawParams( $editLink )->escaped()
  109. . $this->context->msg( 'word-separator' )->escaped()
  110. . $protected . '</li>';
  111. }
  112. /**
  113. * If the page is protected, get the relevant text
  114. * for those restrictions
  115. *
  116. * @param array $restrictions
  117. * @return string
  118. */
  119. private function getRestrictionsText( array $restrictions ) {
  120. $protected = '';
  121. if ( !$restrictions ) {
  122. return $protected;
  123. }
  124. // Check backwards-compatible messages
  125. $msg = null;
  126. if ( $restrictions === [ 'sysop' ] ) {
  127. $msg = $this->context->msg( 'template-protected' );
  128. } elseif ( $restrictions === [ 'autoconfirmed' ] ) {
  129. $msg = $this->context->msg( 'template-semiprotected' );
  130. }
  131. if ( $msg && !$msg->isDisabled() ) {
  132. $protected = $msg->parse();
  133. } else {
  134. // Construct the message from restriction-level-*
  135. // e.g. restriction-level-sysop, restriction-level-autoconfirmed
  136. $msgs = [];
  137. foreach ( $restrictions as $r ) {
  138. $msgs[] = $this->context->msg( "restriction-level-$r" )->parse();
  139. }
  140. $protected = $this->context->msg( 'parentheses' )
  141. ->rawParams( $this->context->getLanguage()->commaList( $msgs ) )->escaped();
  142. }
  143. return $protected;
  144. }
  145. /**
  146. * Return a link to the edit page, with the text
  147. * saying "view source" if the user can't edit the page
  148. *
  149. * @param Title $titleObj
  150. * @return string
  151. */
  152. private function buildEditLink( Title $titleObj ) {
  153. if ( $titleObj->quickUserCan( 'edit', $this->context->getUser() ) ) {
  154. $linkMsg = 'editlink';
  155. } else {
  156. $linkMsg = 'viewsourcelink';
  157. }
  158. return $this->linkRenderer->makeLink(
  159. $titleObj,
  160. $this->context->msg( $linkMsg )->text(),
  161. [],
  162. [ 'action' => 'edit' ]
  163. );
  164. }
  165. }