4 * Job to fix double redirects after moving a page
8 class DoubleRedirectJob
extends Job
{
9 var $reason, $redirTitle, $destTitleText;
13 * Insert jobs into the job queue to fix redirects to the given title
14 * @param $reason String: the reason for the fix, see message double-redirect-fixed-<reason>
15 * @param $redirTitle Title: the title which has changed, redirects pointing to this title are fixed
16 * @param $destTitle Not used
18 public static function fixRedirects( $reason, $redirTitle, $destTitle = false ) {
19 # Need to use the master to get the redirect table updated in the same transaction
20 $dbw = wfGetDB( DB_MASTER
);
22 array( 'redirect', 'page' ),
23 array( 'page_namespace', 'page_title' ),
26 'rd_namespace' => $redirTitle->getNamespace(),
27 'rd_title' => $redirTitle->getDBkey()
29 if ( !$res->numRows() ) {
33 foreach ( $res as $row ) {
34 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
39 $jobs[] = new self( $title, array(
41 'redirTitle' => $redirTitle->getPrefixedDBkey() ) );
42 # Avoid excessive memory usage
43 if ( count( $jobs ) > 10000 ) {
44 Job
::batchInsert( $jobs );
48 Job
::batchInsert( $jobs );
50 function __construct( $title, $params = false, $id = 0 ) {
51 parent
::__construct( 'fixDoubleRedirect', $title, $params, $id );
52 $this->reason
= $params['reason'];
53 $this->redirTitle
= Title
::newFromText( $params['redirTitle'] );
54 $this->destTitleText
= !empty( $params['destTitle'] ) ?
$params['destTitle'] : '';
58 if ( !$this->redirTitle
) {
59 $this->setLastError( 'Invalid title' );
63 $targetRev = Revision
::newFromTitle( $this->title
);
65 wfDebug( __METHOD__
.": target redirect already deleted, ignoring\n" );
68 $text = $targetRev->getText();
69 $currentDest = Title
::newFromRedirect( $text );
70 if ( !$currentDest ||
!$currentDest->equals( $this->redirTitle
) ) {
71 wfDebug( __METHOD__
.": Redirect has changed since the job was queued\n" );
75 # Check for a suppression tag (used e.g. in periodically archived discussions)
76 $mw = MagicWord
::get( 'staticredirect' );
77 if ( $mw->match( $text ) ) {
78 wfDebug( __METHOD__
.": skipping: suppressed with __STATICREDIRECT__\n" );
82 # Find the current final destination
83 $newTitle = self
::getFinalDestination( $this->redirTitle
);
85 wfDebug( __METHOD__
.": skipping: single redirect, circular redirect or invalid redirect destination\n" );
88 if ( $newTitle->equals( $this->redirTitle
) ) {
89 # The redirect is already right, no need to change it
90 # This can happen if the page was moved back (say after vandalism)
91 wfDebug( __METHOD__
.": skipping, already good\n" );
94 # Preserve fragment (bug 14904)
95 $newTitle = Title
::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
96 $currentDest->getFragment() );
99 # Remember that redirect pages can have categories, templates, etc.,
100 # so the regex has to be fairly general
101 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
102 '[[' . $newTitle->getFullText() . ']]',
105 if ( $newText === $text ) {
106 $this->setLastError( 'Text unchanged???' );
113 $wgUser = $this->getUser();
114 $article = new Article( $this->title
);
115 $reason = wfMsgForContent( 'double-redirect-fixed-' . $this->reason
,
116 $this->redirTitle
->getPrefixedText(), $newTitle->getPrefixedText() );
117 $article->doEdit( $newText, $reason, EDIT_UPDATE | EDIT_SUPPRESS_RC
);
124 * Get the final destination of a redirect
125 * @return false if the specified title is not a redirect, or if it is a circular redirect
127 public static function getFinalDestination( $title ) {
128 $dbw = wfGetDB( DB_MASTER
);
130 $seenTitles = array(); # Circular redirect check
134 $titleText = $title->getPrefixedDBkey();
135 if ( isset( $seenTitles[$titleText] ) ) {
136 wfDebug( __METHOD__
, "Circular redirect detected, aborting\n" );
139 $seenTitles[$titleText] = true;
141 $row = $dbw->selectRow(
142 array( 'redirect', 'page' ),
143 array( 'rd_namespace', 'rd_title' ),
146 'page_namespace' => $title->getNamespace(),
147 'page_title' => $title->getDBkey()
150 # No redirect from here, chain terminates
153 $dest = $title = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
);
160 * Get a user object for doing edits, from a request-lifetime cache
163 if ( !self
::$user ) {
164 self
::$user = User
::newFromName( wfMsgForContent( 'double-redirect-fixer' ), false );
165 if ( !self
::$user->isLoggedIn() ) {
166 self
::$user->addToDatabase();