SpecialMostLinkedTemplates.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Implements Special:Mostlinkedtemplates
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. * @ingroup SpecialPage
  22. * @author Rob Church <robchur@gmail.com>
  23. */
  24. use Wikimedia\Rdbms\IResultWrapper;
  25. use Wikimedia\Rdbms\IDatabase;
  26. /**
  27. * Special page lists templates with a large number of
  28. * transclusion links, i.e. "most used" templates
  29. *
  30. * @ingroup SpecialPage
  31. */
  32. class SpecialMostLinkedTemplates extends QueryPage {
  33. function __construct( $name = 'Mostlinkedtemplates' ) {
  34. parent::__construct( $name );
  35. }
  36. /**
  37. * Is this report expensive, i.e should it be cached?
  38. *
  39. * @return bool
  40. */
  41. public function isExpensive() {
  42. return true;
  43. }
  44. /**
  45. * Is there a feed available?
  46. *
  47. * @return bool
  48. */
  49. public function isSyndicated() {
  50. return false;
  51. }
  52. /**
  53. * Sort the results in descending order?
  54. *
  55. * @return bool
  56. */
  57. public function sortDescending() {
  58. return true;
  59. }
  60. public function getQueryInfo() {
  61. return [
  62. 'tables' => [ 'templatelinks' ],
  63. 'fields' => [
  64. 'namespace' => 'tl_namespace',
  65. 'title' => 'tl_title',
  66. 'value' => 'COUNT(*)'
  67. ],
  68. 'options' => [ 'GROUP BY' => [ 'tl_namespace', 'tl_title' ] ]
  69. ];
  70. }
  71. /**
  72. * Pre-cache page existence to speed up link generation
  73. *
  74. * @param IDatabase $db
  75. * @param IResultWrapper $res
  76. */
  77. public function preprocessResults( $db, $res ) {
  78. $this->executeLBFromResultWrapper( $res );
  79. }
  80. /**
  81. * Format a result row
  82. *
  83. * @param Skin $skin
  84. * @param object $result Result row
  85. * @return string
  86. */
  87. public function formatResult( $skin, $result ) {
  88. $title = Title::makeTitleSafe( $result->namespace, $result->title );
  89. if ( !$title ) {
  90. return Html::element(
  91. 'span',
  92. [ 'class' => 'mw-invalidtitle' ],
  93. Linker::getInvalidTitleDescription(
  94. $this->getContext(),
  95. $result->namespace,
  96. $result->title
  97. )
  98. );
  99. }
  100. return $this->getLanguage()->specialList(
  101. $this->getLinkRenderer()->makeLink( $title ),
  102. $this->makeWlhLink( $title, $result )
  103. );
  104. }
  105. /**
  106. * Make a "what links here" link for a given title
  107. *
  108. * @param Title $title Title to make the link for
  109. * @param object $result Result row
  110. * @return string
  111. */
  112. private function makeWlhLink( $title, $result ) {
  113. $wlh = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedText() );
  114. $label = $this->msg( 'ntransclusions' )->numParams( $result->value )->text();
  115. return $this->getLinkRenderer()->makeLink( $wlh, $label );
  116. }
  117. protected function getGroupName() {
  118. return 'highuse';
  119. }
  120. }