SpecialBrokenRedirects.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. /**
  3. * Implements Special:Brokenredirects
  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. */
  23. use MediaWiki\MediaWikiServices;
  24. use Wikimedia\Rdbms\IResultWrapper;
  25. use Wikimedia\Rdbms\IDatabase;
  26. /**
  27. * A special page listing redirects to non existent page. Those should be
  28. * fixed to point to an existing page.
  29. *
  30. * @ingroup SpecialPage
  31. */
  32. class SpecialBrokenRedirects extends QueryPage {
  33. function __construct( $name = 'BrokenRedirects' ) {
  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. function getPageHeader() {
  46. return $this->msg( 'brokenredirectstext' )->parseAsBlock();
  47. }
  48. public function getQueryInfo() {
  49. $dbr = wfGetDB( DB_REPLICA );
  50. return [
  51. 'tables' => [
  52. 'redirect',
  53. 'p1' => 'page',
  54. 'p2' => 'page',
  55. ],
  56. 'fields' => [
  57. 'namespace' => 'p1.page_namespace',
  58. 'title' => 'p1.page_title',
  59. 'rd_namespace',
  60. 'rd_title',
  61. 'rd_fragment',
  62. ],
  63. 'conds' => [
  64. // Exclude pages that don't exist locally as wiki pages,
  65. // but aren't "broken" either.
  66. // Special pages and interwiki links
  67. 'rd_namespace >= 0',
  68. 'rd_interwiki IS NULL OR rd_interwiki = ' . $dbr->addQuotes( '' ),
  69. 'p2.page_namespace IS NULL',
  70. ],
  71. 'join_conds' => [
  72. 'p1' => [ 'JOIN', [
  73. 'rd_from=p1.page_id',
  74. ] ],
  75. 'p2' => [ 'LEFT JOIN', [
  76. 'rd_namespace=p2.page_namespace',
  77. 'rd_title=p2.page_title'
  78. ] ],
  79. ],
  80. ];
  81. }
  82. /**
  83. * @return array
  84. */
  85. function getOrderFields() {
  86. return [ 'rd_namespace', 'rd_title', 'rd_from' ];
  87. }
  88. /**
  89. * @param Skin $skin
  90. * @param object $result Result row
  91. * @return string
  92. */
  93. function formatResult( $skin, $result ) {
  94. $fromObj = Title::makeTitle( $result->namespace, $result->title );
  95. if ( isset( $result->rd_title ) ) {
  96. $toObj = Title::makeTitle( $result->rd_namespace, $result->rd_title, $result->rd_fragment );
  97. } else {
  98. $blinks = $fromObj->getBrokenLinksFrom(); # TODO: check for redirect, not for links
  99. if ( $blinks ) {
  100. $toObj = $blinks[0];
  101. } else {
  102. $toObj = false;
  103. }
  104. }
  105. $linkRenderer = $this->getLinkRenderer();
  106. $permissionManager = MediaWikiServices::getInstance()->getPermissionManager();
  107. // $toObj may very easily be false if the $result list is cached
  108. if ( !is_object( $toObj ) ) {
  109. return '<del>' . $linkRenderer->makeLink( $fromObj ) . '</del>';
  110. }
  111. $from = $linkRenderer->makeKnownLink(
  112. $fromObj,
  113. null,
  114. [],
  115. [ 'redirect' => 'no' ]
  116. );
  117. $links = [];
  118. // if the page is editable, add an edit link
  119. if (
  120. // check user permissions
  121. $permissionManager->userHasRight( $this->getUser(), 'edit' ) &&
  122. // check, if the content model is editable through action=edit
  123. ContentHandler::getForTitle( $fromObj )->supportsDirectEditing()
  124. ) {
  125. $links[] = $linkRenderer->makeKnownLink(
  126. $fromObj,
  127. $this->msg( 'brokenredirects-edit' )->text(),
  128. [],
  129. [ 'action' => 'edit' ]
  130. );
  131. }
  132. $to = $linkRenderer->makeBrokenLink( $toObj, $toObj->getFullText() );
  133. $arr = $this->getLanguage()->getArrow();
  134. $out = $from . $this->msg( 'word-separator' )->escaped();
  135. if ( $permissionManager->userHasRight( $this->getUser(), 'delete' ) ) {
  136. $links[] = $linkRenderer->makeKnownLink(
  137. $fromObj,
  138. $this->msg( 'brokenredirects-delete' )->text(),
  139. [],
  140. [ 'action' => 'delete' ]
  141. );
  142. }
  143. if ( $links ) {
  144. $out .= $this->msg( 'parentheses' )->rawParams( $this->getLanguage()
  145. ->pipeList( $links ) )->escaped();
  146. }
  147. $out .= " {$arr} {$to}";
  148. return $out;
  149. }
  150. public function execute( $par ) {
  151. $this->addHelpLink( 'Help:Redirects' );
  152. parent::execute( $par );
  153. }
  154. /**
  155. * Cache page content model for performance
  156. *
  157. * @param IDatabase $db
  158. * @param IResultWrapper $res
  159. */
  160. function preprocessResults( $db, $res ) {
  161. $this->executeLBFromResultWrapper( $res );
  162. }
  163. protected function getGroupName() {
  164. return 'maintenance';
  165. }
  166. }