SpecialWantedpages.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. /**
  3. * @file
  4. * @ingroup SpecialPage
  5. */
  6. /**
  7. * implements Special:Wantedpages
  8. * @ingroup SpecialPage
  9. */
  10. class WantedPagesPage extends QueryPage {
  11. var $nlinks;
  12. function WantedPagesPage( $inc = false, $nlinks = true ) {
  13. $this->setListoutput( $inc );
  14. $this->nlinks = $nlinks;
  15. }
  16. function getName() {
  17. return 'Wantedpages';
  18. }
  19. function isExpensive() {
  20. return true;
  21. }
  22. function isSyndicated() { return false; }
  23. function getSQL() {
  24. global $wgWantedPagesThreshold;
  25. $count = $wgWantedPagesThreshold - 1;
  26. $dbr = wfGetDB( DB_SLAVE );
  27. $pagelinks = $dbr->tableName( 'pagelinks' );
  28. $page = $dbr->tableName( 'page' );
  29. $sql = "SELECT 'Wantedpages' AS type,
  30. pl_namespace AS namespace,
  31. pl_title AS title,
  32. COUNT(*) AS value
  33. FROM $pagelinks
  34. LEFT JOIN $page AS pg1
  35. ON pl_namespace = pg1.page_namespace AND pl_title = pg1.page_title
  36. LEFT JOIN $page AS pg2
  37. ON pl_from = pg2.page_id
  38. WHERE pg1.page_namespace IS NULL
  39. AND pl_namespace NOT IN ( 2, 3 )
  40. AND pg2.page_namespace != 8
  41. GROUP BY pl_namespace, pl_title
  42. HAVING COUNT(*) > $count";
  43. wfRunHooks( 'WantedPages::getSQL', array( &$this, &$sql ) );
  44. return $sql;
  45. }
  46. /**
  47. * Cache page existence for performance
  48. */
  49. function preprocessResults( $db, $res ) {
  50. $batch = new LinkBatch;
  51. while ( $row = $db->fetchObject( $res ) )
  52. $batch->add( $row->namespace, $row->title );
  53. $batch->execute();
  54. // Back to start for display
  55. if ( $db->numRows( $res ) > 0 )
  56. // If there are no rows we get an error seeking.
  57. $db->dataSeek( $res, 0 );
  58. }
  59. /**
  60. * Format an individual result
  61. *
  62. * @param $skin Skin to use for UI elements
  63. * @param $result Result row
  64. * @return string
  65. */
  66. public function formatResult( $skin, $result ) {
  67. $title = Title::makeTitleSafe( $result->namespace, $result->title );
  68. if( $title instanceof Title ) {
  69. if( $this->isCached() ) {
  70. $pageLink = $title->exists()
  71. ? '<s>' . $skin->makeLinkObj( $title ) . '</s>'
  72. : $skin->makeBrokenLinkObj( $title );
  73. } else {
  74. $pageLink = $skin->makeBrokenLinkObj( $title );
  75. }
  76. return wfSpecialList( $pageLink, $this->makeWlhLink( $title, $skin, $result ) );
  77. } else {
  78. $tsafe = htmlspecialchars( $result->title );
  79. return wfMsg( 'wantedpages-badtitle', $tsafe );
  80. }
  81. }
  82. /**
  83. * Make a "what links here" link for a specified result if required
  84. *
  85. * @param $title Title to make the link for
  86. * @param $skin Skin to use
  87. * @param $result Result row
  88. * @return string
  89. */
  90. private function makeWlhLink( $title, $skin, $result ) {
  91. global $wgLang;
  92. if( $this->nlinks ) {
  93. $wlh = SpecialPage::getTitleFor( 'Whatlinkshere' );
  94. $label = wfMsgExt( 'nlinks', array( 'parsemag', 'escape' ),
  95. $wgLang->formatNum( $result->value ) );
  96. return $skin->makeKnownLinkObj( $wlh, $label, 'target=' . $title->getPrefixedUrl() );
  97. } else {
  98. return null;
  99. }
  100. }
  101. }
  102. /**
  103. * constructor
  104. */
  105. function wfSpecialWantedpages( $par = null, $specialPage ) {
  106. $inc = $specialPage->including();
  107. if ( $inc ) {
  108. @list( $limit, $nlinks ) = explode( '/', $par, 2 );
  109. $limit = (int)$limit;
  110. $nlinks = $nlinks === 'nlinks';
  111. $offset = 0;
  112. } else {
  113. list( $limit, $offset ) = wfCheckLimits();
  114. $nlinks = true;
  115. }
  116. $wpp = new WantedPagesPage( $inc, $nlinks );
  117. $wpp->doQuery( $offset, $limit, !$inc );
  118. }