SpecialMostlinked.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup SpecialPage
  5. */
  6. /**
  7. * A special page to show pages ordered by the number of pages linking to them.
  8. * Implements Special:Mostlinked
  9. *
  10. * @ingroup SpecialPage
  11. *
  12. * @author Ævar Arnfjörð Bjarmason <avarab@gmail.com>
  13. * @author Rob Church <robchur@gmail.com>
  14. * @copyright Copyright © 2005, Ævar Arnfjörð Bjarmason
  15. * @copyright © 2006 Rob Church
  16. * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
  17. */
  18. class MostlinkedPage extends QueryPage {
  19. function getName() { return 'Mostlinked'; }
  20. function isExpensive() { return true; }
  21. function isSyndicated() { return false; }
  22. /**
  23. * Note: Getting page_namespace only works if $this->isCached() is false
  24. */
  25. function getSQL() {
  26. $dbr = wfGetDB( DB_SLAVE );
  27. list( $pagelinks, $page ) = $dbr->tableNamesN( 'pagelinks', 'page' );
  28. return
  29. "SELECT 'Mostlinked' AS type,
  30. pl_namespace AS namespace,
  31. pl_title AS title,
  32. COUNT(*) AS value,
  33. page_namespace
  34. FROM $pagelinks
  35. LEFT JOIN $page ON pl_namespace=page_namespace AND pl_title=page_title
  36. GROUP BY pl_namespace, pl_title, page_namespace
  37. HAVING COUNT(*) > 1";
  38. }
  39. /**
  40. * Pre-fill the link cache
  41. */
  42. function preprocessResults( $db, $res ) {
  43. if( $db->numRows( $res ) > 0 ) {
  44. $linkBatch = new LinkBatch();
  45. while( $row = $db->fetchObject( $res ) )
  46. $linkBatch->add( $row->namespace, $row->title );
  47. $db->dataSeek( $res, 0 );
  48. $linkBatch->execute();
  49. }
  50. }
  51. /**
  52. * Make a link to "what links here" for the specified title
  53. *
  54. * @param $title Title being queried
  55. * @param $skin Skin to use
  56. * @return string
  57. */
  58. function makeWlhLink( &$title, $caption, &$skin ) {
  59. $wlh = SpecialPage::getTitleFor( 'Whatlinkshere', $title->getPrefixedDBkey() );
  60. return $skin->makeKnownLinkObj( $wlh, $caption );
  61. }
  62. /**
  63. * Make links to the page corresponding to the item, and the "what links here" page for it
  64. *
  65. * @param $skin Skin to be used
  66. * @param $result Result row
  67. * @return string
  68. */
  69. function formatResult( $skin, $result ) {
  70. global $wgLang;
  71. $title = Title::makeTitleSafe( $result->namespace, $result->title );
  72. $link = $skin->makeLinkObj( $title );
  73. $wlh = $this->makeWlhLink( $title,
  74. wfMsgExt( 'nlinks', array( 'parsemag', 'escape'),
  75. $wgLang->formatNum( $result->value ) ), $skin );
  76. return wfSpecialList( $link, $wlh );
  77. }
  78. }
  79. /**
  80. * constructor
  81. */
  82. function wfSpecialMostlinked() {
  83. list( $limit, $offset ) = wfCheckLimits();
  84. $wpp = new MostlinkedPage();
  85. $wpp->doQuery( $offset, $limit );
  86. }