DoubleRedirectJob.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. <?php
  2. class DoubleRedirectJob extends Job {
  3. var $reason, $redirTitle, $destTitleText;
  4. static $user;
  5. /**
  6. * Insert jobs into the job queue to fix redirects to the given title
  7. * @param string $type The reason for the fix, see message double-redirect-fixed-<reason>
  8. * @param Title $redirTitle The title which has changed, redirects pointing to this title are fixed
  9. */
  10. public static function fixRedirects( $reason, $redirTitle, $destTitle = false ) {
  11. # Need to use the master to get the redirect table updated in the same transaction
  12. $dbw = wfGetDB( DB_MASTER );
  13. $res = $dbw->select(
  14. array( 'redirect', 'page' ),
  15. array( 'page_namespace', 'page_title' ),
  16. array(
  17. 'page_id = rd_from',
  18. 'rd_namespace' => $redirTitle->getNamespace(),
  19. 'rd_title' => $redirTitle->getDBkey()
  20. ), __METHOD__ );
  21. if ( !$res->numRows() ) {
  22. return;
  23. }
  24. $jobs = array();
  25. foreach ( $res as $row ) {
  26. $title = Title::makeTitle( $row->page_namespace, $row->page_title );
  27. if ( !$title ) {
  28. continue;
  29. }
  30. $jobs[] = new self( $title, array(
  31. 'reason' => $reason,
  32. 'redirTitle' => $redirTitle->getPrefixedDBkey() ) );
  33. # Avoid excessive memory usage
  34. if ( count( $jobs ) > 10000 ) {
  35. Job::batchInsert( $jobs );
  36. $jobs = array();
  37. }
  38. }
  39. Job::batchInsert( $jobs );
  40. }
  41. function __construct( $title, $params = false, $id = 0 ) {
  42. parent::__construct( 'fixDoubleRedirect', $title, $params, $id );
  43. $this->reason = $params['reason'];
  44. $this->redirTitle = Title::newFromText( $params['redirTitle'] );
  45. $this->destTitleText = !empty( $params['destTitle'] ) ? $params['destTitle'] : '';
  46. }
  47. function run() {
  48. if ( !$this->redirTitle ) {
  49. $this->setLastError( 'Invalid title' );
  50. return false;
  51. }
  52. $targetRev = Revision::newFromTitle( $this->title );
  53. if ( !$targetRev ) {
  54. wfDebug( __METHOD__.": target redirect already deleted, ignoring\n" );
  55. return true;
  56. }
  57. $text = $targetRev->getText();
  58. $currentDest = Title::newFromRedirect( $text );
  59. if ( !$currentDest || !$currentDest->equals( $this->redirTitle ) ) {
  60. wfDebug( __METHOD__.": Redirect has changed since the job was queued\n" );
  61. return true;
  62. }
  63. # Check for a suppression tag (used e.g. in periodically archived discussions)
  64. $mw = MagicWord::get( 'staticredirect' );
  65. if ( $mw->match( $text ) ) {
  66. wfDebug( __METHOD__.": skipping: suppressed with __STATICREDIRECT__\n" );
  67. return true;
  68. }
  69. # Find the current final destination
  70. $newTitle = self::getFinalDestination( $this->redirTitle );
  71. if ( !$newTitle ) {
  72. wfDebug( __METHOD__.": skipping: single redirect, circular redirect or invalid redirect destination\n" );
  73. return true;
  74. }
  75. if ( $newTitle->equals( $this->redirTitle ) ) {
  76. # The redirect is already right, no need to change it
  77. # This can happen if the page was moved back (say after vandalism)
  78. wfDebug( __METHOD__.": skipping, already good\n" );
  79. }
  80. # Preserve fragment (bug 14904)
  81. $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
  82. $currentDest->getFragment() );
  83. # Fix the text
  84. # Remember that redirect pages can have categories, templates, etc.,
  85. # so the regex has to be fairly general
  86. $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
  87. '[[' . $newTitle->getFullText() . ']]',
  88. $text, 1 );
  89. if ( $newText === $text ) {
  90. $this->setLastError( 'Text unchanged???' );
  91. return false;
  92. }
  93. # Save it
  94. global $wgUser;
  95. $oldUser = $wgUser;
  96. $wgUser = $this->getUser();
  97. $article = new Article( $this->title );
  98. $reason = wfMsgForContent( 'double-redirect-fixed-' . $this->reason,
  99. $this->redirTitle->getPrefixedText(), $newTitle->getPrefixedText() );
  100. $article->doEdit( $newText, $reason, EDIT_UPDATE | EDIT_SUPPRESS_RC );
  101. $wgUser = $oldUser;
  102. return true;
  103. }
  104. /**
  105. * Get the final destination of a redirect
  106. * Returns false if the specified title is not a redirect, or if it is a circular redirect
  107. */
  108. public static function getFinalDestination( $title ) {
  109. $dbw = wfGetDB( DB_MASTER );
  110. $seenTitles = array(); # Circular redirect check
  111. $dest = false;
  112. while ( true ) {
  113. $titleText = $title->getPrefixedDBkey();
  114. if ( isset( $seenTitles[$titleText] ) ) {
  115. wfDebug( __METHOD__, "Circular redirect detected, aborting\n" );
  116. return false;
  117. }
  118. $seenTitles[$titleText] = true;
  119. $row = $dbw->selectRow(
  120. array( 'redirect', 'page' ),
  121. array( 'rd_namespace', 'rd_title' ),
  122. array(
  123. 'rd_from=page_id',
  124. 'page_namespace' => $title->getNamespace(),
  125. 'page_title' => $title->getDBkey()
  126. ), __METHOD__ );
  127. if ( !$row ) {
  128. # No redirect from here, chain terminates
  129. break;
  130. } else {
  131. $dest = $title = Title::makeTitle( $row->rd_namespace, $row->rd_title );
  132. }
  133. }
  134. return $dest;
  135. }
  136. /**
  137. * Get a user object for doing edits, from a request-lifetime cache
  138. */
  139. function getUser() {
  140. if ( !self::$user ) {
  141. self::$user = User::newFromName( wfMsgForContent( 'double-redirect-fixer' ), false );
  142. if ( !self::$user->isLoggedIn() ) {
  143. self::$user->addToDatabase();
  144. }
  145. }
  146. return self::$user;
  147. }
  148. }