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'] : '';
72 if ( !$this->redirTitle
) {
73 $this->setLastError( 'Invalid title' );
77 $targetRev = Revision
::newFromTitle( $this->title
);
79 wfDebug( __METHOD__
.": target redirect already deleted, ignoring\n" );
82 $text = $targetRev->getText();
83 $currentDest = Title
::newFromRedirect( $text );
84 if ( !$currentDest ||
!$currentDest->equals( $this->redirTitle
) ) {
85 wfDebug( __METHOD__
.": Redirect has changed since the job was queued\n" );
89 # Check for a suppression tag (used e.g. in periodically archived discussions)
90 $mw = MagicWord
::get( 'staticredirect' );
91 if ( $mw->match( $text ) ) {
92 wfDebug( __METHOD__
.": skipping: suppressed with __STATICREDIRECT__\n" );
96 # Find the current final destination
97 $newTitle = self
::getFinalDestination( $this->redirTitle
);
99 wfDebug( __METHOD__
.": skipping: single redirect, circular redirect or invalid redirect destination\n" );
102 if ( $newTitle->equals( $this->redirTitle
) ) {
103 # The redirect is already right, no need to change it
104 # This can happen if the page was moved back (say after vandalism)
105 wfDebug( __METHOD__
.": skipping, already good\n" );
108 # Preserve fragment (bug 14904)
109 $newTitle = Title
::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
110 $currentDest->getFragment() );
113 # Remember that redirect pages can have categories, templates, etc.,
114 # so the regex has to be fairly general
115 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
116 '[[' . $newTitle->getFullText() . ']]',
119 if ( $newText === $text ) {
120 $this->setLastError( 'Text unchanged???' );
127 $wgUser = $this->getUser();
128 $article = WikiPage
::factory( $this->title
);
129 $reason = wfMsgForContent( 'double-redirect-fixed-' . $this->reason
,
130 $this->redirTitle
->getPrefixedText(), $newTitle->getPrefixedText() );
131 $article->doEdit( $newText, $reason, EDIT_UPDATE | EDIT_SUPPRESS_RC
, false, $this->getUser() );
138 * Get the final destination of a redirect
140 * @param $title Title
142 * @return bool if the specified title is not a redirect, or if it is a circular redirect
144 public static function getFinalDestination( $title ) {
145 $dbw = wfGetDB( DB_MASTER
);
147 $seenTitles = array(); # Circular redirect check
151 $titleText = $title->getPrefixedDBkey();
152 if ( isset( $seenTitles[$titleText] ) ) {
153 wfDebug( __METHOD__
, "Circular redirect detected, aborting\n" );
156 $seenTitles[$titleText] = true;
158 $row = $dbw->selectRow(
159 array( 'redirect', 'page' ),
160 array( 'rd_namespace', 'rd_title' ),
163 'page_namespace' => $title->getNamespace(),
164 'page_title' => $title->getDBkey()
167 # No redirect from here, chain terminates
170 $dest = $title = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
);
177 * Get a user object for doing edits, from a request-lifetime cache
181 if ( !self
::$user ) {
182 self
::$user = User
::newFromName( wfMsgForContent( 'double-redirect-fixer' ), false );
183 # FIXME: newFromName could return false on a badly configured wiki.
184 if ( !self
::$user->isLoggedIn() ) {
185 self
::$user->addToDatabase();