SpecialMostlinkedtemplates.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup SpecialPage
  5. */
  6. /**
  7. * Special page lists templates with a large number of
  8. * transclusion links, i.e. "most used" templates
  9. *
  10. * @ingroup SpecialPage
  11. * @author Rob Church <robchur@gmail.com>
  12. */
  13. class SpecialMostlinkedtemplates extends QueryPage {
  14. /**
  15. * Name of the report
  16. *
  17. * @return string
  18. */
  19. public function getName() {
  20. return 'Mostlinkedtemplates';
  21. }
  22. /**
  23. * Is this report expensive, i.e should it be cached?
  24. *
  25. * @return bool
  26. */
  27. public function isExpensive() {
  28. return true;
  29. }
  30. /**
  31. * Is there a feed available?
  32. *
  33. * @return bool
  34. */
  35. public function isSyndicated() {
  36. return false;
  37. }
  38. /**
  39. * Sort the results in descending order?
  40. *
  41. * @return bool
  42. */
  43. public function sortDescending() {
  44. return true;
  45. }
  46. /**
  47. * Generate SQL for the report
  48. *
  49. * @return string
  50. */
  51. public function getSql() {
  52. $dbr = wfGetDB( DB_SLAVE );
  53. $templatelinks = $dbr->tableName( 'templatelinks' );
  54. $name = $dbr->addQuotes( $this->getName() );
  55. return "SELECT {$name} AS type,
  56. " . NS_TEMPLATE . " AS namespace,
  57. tl_title AS title,
  58. COUNT(*) AS value
  59. FROM {$templatelinks}
  60. WHERE tl_namespace = " . NS_TEMPLATE . "
  61. GROUP BY tl_title";
  62. }
  63. /**
  64. * Pre-cache page existence to speed up link generation
  65. *
  66. * @param Database $dbr Database connection
  67. * @param int $res Result pointer
  68. */
  69. public function preprocessResults( $db, $res ) {
  70. $batch = new LinkBatch();
  71. while( $row = $db->fetchObject( $res ) ) {
  72. $batch->add( $row->namespace, $row->title );
  73. }
  74. $batch->execute();
  75. if( $db->numRows( $res ) > 0 )
  76. $db->dataSeek( $res, 0 );
  77. }
  78. /**
  79. * Format a result row
  80. *
  81. * @param Skin $skin Skin to use for UI elements
  82. * @param object $result Result row
  83. * @return string
  84. */
  85. public function formatResult( $skin, $result ) {
  86. $title = Title::makeTitleSafe( $result->namespace, $result->title );
  87. $skin->link( $title );
  88. return wfSpecialList(
  89. $skin->makeLinkObj( $title ),
  90. $this->makeWlhLink( $title, $skin, $result )
  91. );
  92. }
  93. /**
  94. * Make a "what links here" link for a given title
  95. *
  96. * @param Title $title Title to make the link for
  97. * @param Skin $skin Skin to use
  98. * @param object $result Result row
  99. * @return string
  100. */
  101. private function makeWlhLink( $title, $skin, $result ) {
  102. global $wgLang;
  103. $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' );
  104. $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ),
  105. $wgLang->formatNum( $result->value ) );
  106. return $skin->link( $wlh, $label, array(), array( 'target' => $title->getPrefixedText() ) );
  107. }
  108. }
  109. /**
  110. * Execution function
  111. *
  112. * @param mixed $par Parameters passed to the page
  113. */
  114. function wfSpecialMostlinkedtemplates( $par = false ) {
  115. list( $limit, $offset ) = wfCheckLimits();
  116. $mlt = new SpecialMostlinkedtemplates();
  117. $mlt->doQuery( $offset, $limit );
  118. }