3 * Job to fix double redirects after moving a page
10 * Job to fix double redirects after moving a page
14 class DoubleRedirectJob
extends Job
{
15 var $reason, $redirTitle, $destTitleText;
19 * Insert jobs into the job queue to fix redirects to the given title
20 * @param $reason String: the reason for the fix, see message double-redirect-fixed-<reason>
21 * @param $redirTitle Title: the title which has changed, redirects pointing to this title are fixed
22 * @param $destTitle Not used
24 public static function fixRedirects( $reason, $redirTitle, $destTitle = false ) {
25 # Need to use the master to get the redirect table updated in the same transaction
26 $dbw = wfGetDB( DB_MASTER
);
28 array( 'redirect', 'page' ),
29 array( 'page_namespace', 'page_title' ),
32 'rd_namespace' => $redirTitle->getNamespace(),
33 'rd_title' => $redirTitle->getDBkey()
35 if ( !$res->numRows() ) {
39 foreach ( $res as $row ) {
40 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
45 $jobs[] = new self( $title, array(
47 'redirTitle' => $redirTitle->getPrefixedDBkey() ) );
48 # Avoid excessive memory usage
49 if ( count( $jobs ) > 10000 ) {
50 Job
::batchInsert( $jobs );
54 Job
::batchInsert( $jobs );
56 function __construct( $title, $params = false, $id = 0 ) {
57 parent
::__construct( 'fixDoubleRedirect', $title, $params, $id );
58 $this->reason
= $params['reason'];
59 $this->redirTitle
= Title
::newFromText( $params['redirTitle'] );
60 $this->destTitleText
= !empty( $params['destTitle'] ) ?
$params['destTitle'] : '';
64 if ( !$this->redirTitle
) {
65 $this->setLastError( 'Invalid title' );
69 $targetRev = Revision
::newFromTitle( $this->title
);
71 wfDebug( __METHOD__
.": target redirect already deleted, ignoring\n" );
74 $text = $targetRev->getText();
75 $currentDest = Title
::newFromRedirect( $text );
76 if ( !$currentDest ||
!$currentDest->equals( $this->redirTitle
) ) {
77 wfDebug( __METHOD__
.": Redirect has changed since the job was queued\n" );
81 # Check for a suppression tag (used e.g. in periodically archived discussions)
82 $mw = MagicWord
::get( 'staticredirect' );
83 if ( $mw->match( $text ) ) {
84 wfDebug( __METHOD__
.": skipping: suppressed with __STATICREDIRECT__\n" );
88 # Find the current final destination
89 $newTitle = self
::getFinalDestination( $this->redirTitle
);
91 wfDebug( __METHOD__
.": skipping: single redirect, circular redirect or invalid redirect destination\n" );
94 if ( $newTitle->equals( $this->redirTitle
) ) {
95 # The redirect is already right, no need to change it
96 # This can happen if the page was moved back (say after vandalism)
97 wfDebug( __METHOD__
.": skipping, already good\n" );
100 # Preserve fragment (bug 14904)
101 $newTitle = Title
::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
102 $currentDest->getFragment() );
105 # Remember that redirect pages can have categories, templates, etc.,
106 # so the regex has to be fairly general
107 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
108 '[[' . $newTitle->getFullText() . ']]',
111 if ( $newText === $text ) {
112 $this->setLastError( 'Text unchanged???' );
119 $wgUser = $this->getUser();
120 $article = new Article( $this->title
);
121 $reason = wfMsgForContent( 'double-redirect-fixed-' . $this->reason
,
122 $this->redirTitle
->getPrefixedText(), $newTitle->getPrefixedText() );
123 $article->doEdit( $newText, $reason, EDIT_UPDATE | EDIT_SUPPRESS_RC
);
130 * Get the final destination of a redirect
131 * @return false if the specified title is not a redirect, or if it is a circular redirect
133 public static function getFinalDestination( $title ) {
134 $dbw = wfGetDB( DB_MASTER
);
136 $seenTitles = array(); # Circular redirect check
140 $titleText = $title->getPrefixedDBkey();
141 if ( isset( $seenTitles[$titleText] ) ) {
142 wfDebug( __METHOD__
, "Circular redirect detected, aborting\n" );
145 $seenTitles[$titleText] = true;
147 $row = $dbw->selectRow(
148 array( 'redirect', 'page' ),
149 array( 'rd_namespace', 'rd_title' ),
152 'page_namespace' => $title->getNamespace(),
153 'page_title' => $title->getDBkey()
156 # No redirect from here, chain terminates
159 $dest = $title = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
);
166 * Get a user object for doing edits, from a request-lifetime cache
169 if ( !self
::$user ) {
170 self
::$user = User
::newFromName( wfMsgForContent( 'double-redirect-fixer' ), false );
171 if ( !self
::$user->isLoggedIn() ) {
172 self
::$user->addToDatabase();