SpecialListRedirects.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. <?php
  2. /**
  3. * Implements Special:Listredirects
  4. *
  5. * Copyright © 2006 Rob Church
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  20. * http://www.gnu.org/copyleft/gpl.html
  21. *
  22. * @file
  23. * @ingroup SpecialPage
  24. * @author Rob Church <robchur@gmail.com>
  25. */
  26. use Wikimedia\Rdbms\IResultWrapper;
  27. use Wikimedia\Rdbms\IDatabase;
  28. /**
  29. * Special:Listredirects - Lists all the redirects on the wiki.
  30. * @ingroup SpecialPage
  31. */
  32. class SpecialListRedirects extends QueryPage {
  33. function __construct( $name = 'Listredirects' ) {
  34. parent::__construct( $name );
  35. }
  36. public function isExpensive() {
  37. return true;
  38. }
  39. function isSyndicated() {
  40. return false;
  41. }
  42. function sortDescending() {
  43. return false;
  44. }
  45. public function getQueryInfo() {
  46. return [
  47. 'tables' => [ 'p1' => 'page', 'redirect', 'p2' => 'page' ],
  48. 'fields' => [ 'namespace' => 'p1.page_namespace',
  49. 'title' => 'p1.page_title',
  50. 'rd_namespace',
  51. 'rd_title',
  52. 'rd_fragment',
  53. 'rd_interwiki',
  54. 'redirid' => 'p2.page_id' ],
  55. 'conds' => [ 'p1.page_is_redirect' => 1 ],
  56. 'join_conds' => [ 'redirect' => [
  57. 'LEFT JOIN', 'rd_from=p1.page_id' ],
  58. 'p2' => [ 'LEFT JOIN', [
  59. 'p2.page_namespace=rd_namespace',
  60. 'p2.page_title=rd_title' ] ] ]
  61. ];
  62. }
  63. function getOrderFields() {
  64. return [ 'p1.page_namespace', 'p1.page_title' ];
  65. }
  66. /**
  67. * Cache page existence for performance
  68. *
  69. * @param IDatabase $db
  70. * @param IResultWrapper $res
  71. */
  72. function preprocessResults( $db, $res ) {
  73. if ( !$res->numRows() ) {
  74. return;
  75. }
  76. $batch = new LinkBatch;
  77. foreach ( $res as $row ) {
  78. $batch->add( $row->namespace, $row->title );
  79. $redirTarget = $this->getRedirectTarget( $row );
  80. if ( $redirTarget ) {
  81. $batch->addObj( $redirTarget );
  82. }
  83. }
  84. $batch->execute();
  85. // Back to start for display
  86. $res->seek( 0 );
  87. }
  88. /**
  89. * @param stdClass $row
  90. * @return Title|null
  91. */
  92. protected function getRedirectTarget( $row ) {
  93. if ( isset( $row->rd_title ) ) {
  94. return Title::makeTitle( $row->rd_namespace,
  95. $row->rd_title, $row->rd_fragment,
  96. $row->rd_interwiki
  97. );
  98. } else {
  99. $title = Title::makeTitle( $row->namespace, $row->title );
  100. $article = WikiPage::factory( $title );
  101. return $article->getRedirectTarget();
  102. }
  103. }
  104. /**
  105. * @param Skin $skin
  106. * @param object $result Result row
  107. * @return string
  108. */
  109. function formatResult( $skin, $result ) {
  110. $linkRenderer = $this->getLinkRenderer();
  111. # Make a link to the redirect itself
  112. $rd_title = Title::makeTitle( $result->namespace, $result->title );
  113. $rd_link = $linkRenderer->makeLink(
  114. $rd_title,
  115. null,
  116. [],
  117. [ 'redirect' => 'no' ]
  118. );
  119. # Find out where the redirect leads
  120. $target = $this->getRedirectTarget( $result );
  121. if ( $target ) {
  122. # Make a link to the destination page
  123. $lang = $this->getLanguage();
  124. $arr = $lang->getArrow() . $lang->getDirMark();
  125. $targetLink = $linkRenderer->makeLink( $target, $target->getFullText() );
  126. return "$rd_link $arr $targetLink";
  127. } else {
  128. return "<del>$rd_link</del>";
  129. }
  130. }
  131. public function execute( $par ) {
  132. $this->addHelpLink( 'Help:Redirects' );
  133. parent::execute( $par );
  134. }
  135. protected function getGroupName() {
  136. return 'pages';
  137. }
  138. }