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;
23 * Insert jobs into the job queue to fix redirects to the given title
24 * @param $reason String: the reason for the fix, see message double-redirect-fixed-<reason>
25 * @param $redirTitle Title: the title which has changed, redirects pointing to this title are fixed
26 * @param $destTitle bool Not used
28 public static function fixRedirects( $reason, $redirTitle, $destTitle = false ) {
29 # Need to use the master to get the redirect table updated in the same transaction
30 $dbw = wfGetDB( DB_MASTER
);
32 array( 'redirect', 'page' ),
33 array( 'page_namespace', 'page_title' ),
36 'rd_namespace' => $redirTitle->getNamespace(),
37 'rd_title' => $redirTitle->getDBkey()
39 if ( !$res->numRows() ) {
43 foreach ( $res as $row ) {
44 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
49 $jobs[] = new self( $title, array(
51 'redirTitle' => $redirTitle->getPrefixedDBkey() ) );
52 # Avoid excessive memory usage
53 if ( count( $jobs ) > 10000 ) {
54 Job
::batchInsert( $jobs );
58 Job
::batchInsert( $jobs );
61 function __construct( $title, $params = false, $id = 0 ) {
62 parent
::__construct( 'fixDoubleRedirect', $title, $params, $id );
63 $this->reason
= $params['reason'];
64 $this->redirTitle
= Title
::newFromText( $params['redirTitle'] );
65 $this->destTitleText
= !empty( $params['destTitle'] ) ?
$params['destTitle'] : '';
69 if ( !$this->redirTitle
) {
70 $this->setLastError( 'Invalid title' );
74 $targetRev = Revision
::newFromTitle( $this->title
);
76 wfDebug( __METHOD__
.": target redirect already deleted, ignoring\n" );
79 $text = $targetRev->getText();
80 $currentDest = Title
::newFromRedirect( $text );
81 if ( !$currentDest ||
!$currentDest->equals( $this->redirTitle
) ) {
82 wfDebug( __METHOD__
.": Redirect has changed since the job was queued\n" );
86 # Check for a suppression tag (used e.g. in periodically archived discussions)
87 $mw = MagicWord
::get( 'staticredirect' );
88 if ( $mw->match( $text ) ) {
89 wfDebug( __METHOD__
.": skipping: suppressed with __STATICREDIRECT__\n" );
93 # Find the current final destination
94 $newTitle = self
::getFinalDestination( $this->redirTitle
);
96 wfDebug( __METHOD__
.": skipping: single redirect, circular redirect or invalid redirect destination\n" );
99 if ( $newTitle->equals( $this->redirTitle
) ) {
100 # The redirect is already right, no need to change it
101 # This can happen if the page was moved back (say after vandalism)
102 wfDebug( __METHOD__
.": skipping, already good\n" );
105 # Preserve fragment (bug 14904)
106 $newTitle = Title
::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
107 $currentDest->getFragment() );
110 # Remember that redirect pages can have categories, templates, etc.,
111 # so the regex has to be fairly general
112 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
113 '[[' . $newTitle->getFullText() . ']]',
116 if ( $newText === $text ) {
117 $this->setLastError( 'Text unchanged???' );
124 $wgUser = $this->getUser();
125 $article = new Article( $this->title
);
126 $reason = wfMsgForContent( 'double-redirect-fixed-' . $this->reason
,
127 $this->redirTitle
->getPrefixedText(), $newTitle->getPrefixedText() );
128 $article->doEdit( $newText, $reason, EDIT_UPDATE | EDIT_SUPPRESS_RC
);
135 * Get the final destination of a redirect
137 * @param $title Title
139 * @return false if the specified title is not a redirect, or if it is a circular redirect
141 public static function getFinalDestination( $title ) {
142 $dbw = wfGetDB( DB_MASTER
);
144 $seenTitles = array(); # Circular redirect check
148 $titleText = $title->getPrefixedDBkey();
149 if ( isset( $seenTitles[$titleText] ) ) {
150 wfDebug( __METHOD__
, "Circular redirect detected, aborting\n" );
153 $seenTitles[$titleText] = true;
155 $row = $dbw->selectRow(
156 array( 'redirect', 'page' ),
157 array( 'rd_namespace', 'rd_title' ),
160 'page_namespace' => $title->getNamespace(),
161 'page_title' => $title->getDBkey()
164 # No redirect from here, chain terminates
167 $dest = $title = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
);
174 * Get a user object for doing edits, from a request-lifetime cache
177 if ( !self
::$user ) {
178 self
::$user = User
::newFromName( wfMsgForContent( 'double-redirect-fixer' ), false );
179 if ( !self
::$user->isLoggedIn() ) {
180 self
::$user->addToDatabase();