3 class DoubleRedirectJob
extends Job
{
4 var $reason, $redirTitle, $destTitleText;
8 * Insert jobs into the job queue to fix redirects to the given title
9 * @param string $type The reason for the fix, see message double-redirect-fixed-<reason>
10 * @param Title $redirTitle The title which has changed, redirects pointing to this title are fixed
12 public static function fixRedirects( $reason, $redirTitle, $destTitle = false ) {
13 # Need to use the master to get the redirect table updated in the same transaction
14 $dbw = wfGetDB( DB_MASTER
);
16 array( 'redirect', 'page' ),
17 array( 'page_namespace', 'page_title' ),
20 'rd_namespace' => $redirTitle->getNamespace(),
21 'rd_title' => $redirTitle->getDBkey()
23 if ( !$res->numRows() ) {
27 foreach ( $res as $row ) {
28 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
33 $jobs[] = new self( $title, array(
35 'redirTitle' => $redirTitle->getPrefixedDBkey() ) );
36 # Avoid excessive memory usage
37 if ( count( $jobs ) > 10000 ) {
38 Job
::batchInsert( $jobs );
42 Job
::batchInsert( $jobs );
44 function __construct( $title, $params = false, $id = 0 ) {
45 parent
::__construct( 'fixDoubleRedirect', $title, $params, $id );
46 $this->reason
= $params['reason'];
47 $this->redirTitle
= Title
::newFromText( $params['redirTitle'] );
48 $this->destTitleText
= !empty( $params['destTitle'] ) ?
$params['destTitle'] : '';
52 if ( !$this->redirTitle
) {
53 $this->setLastError( 'Invalid title' );
57 $targetRev = Revision
::newFromTitle( $this->title
);
59 wfDebug( __METHOD__
.": target redirect already deleted, ignoring\n" );
62 $text = $targetRev->getText();
63 $currentDest = Title
::newFromRedirect( $text );
64 if ( !$currentDest ||
!$currentDest->equals( $this->redirTitle
) ) {
65 wfDebug( __METHOD__
.": Redirect has changed since the job was queued\n" );
69 # Find the current final destination
70 $newTitle = self
::getFinalDestination( $this->redirTitle
);
72 wfDebug( __METHOD__
.": skipping: single redirect, circular redirect or invalid redirect destination\n" );
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" );
81 # Preserve fragment (bug 14904)
82 $newTitle = Title
::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
83 $currentDest->getFragment() );
86 # Remember that redirect pages can have categories, templates, etc.,
87 # so the regex has to be fairly general
88 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
89 '[[' . $newTitle->getFullText() . ']]',
92 if ( $newText === $text ) {
93 $this->setLastError( 'Text unchanged???' );
100 $wgUser = $this->getUser();
101 $article = new Article( $this->title
);
102 $reason = wfMsgForContent( 'double-redirect-fixed-' . $this->reason
,
103 $this->redirTitle
->getPrefixedText(), $newTitle->getPrefixedText() );
104 $article->doEdit( $newText, $reason, EDIT_UPDATE | EDIT_SUPPRESS_RC
);
111 * Get the final destination of a redirect
112 * Returns false if the specified title is not a redirect, or if it is a circular redirect
114 public static function getFinalDestination( $title ) {
115 $dbw = wfGetDB( DB_MASTER
);
117 $seenTitles = array(); # Circular redirect check
121 $titleText = $title->getPrefixedDBkey();
122 if ( isset( $seenTitles[$titleText] ) ) {
123 wfDebug( __METHOD__
, "Circular redirect detected, aborting\n" );
126 $seenTitles[$titleText] = true;
128 $row = $dbw->selectRow(
129 array( 'redirect', 'page' ),
130 array( 'rd_namespace', 'rd_title' ),
133 'page_namespace' => $title->getNamespace(),
134 'page_title' => $title->getDBkey()
137 # No redirect from here, chain terminates
140 $dest = $title = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
);
147 * Get a user object for doing edits, from a request-lifetime cache
150 if ( !self
::$user ) {
151 self
::$user = User
::newFromName( wfMsgForContent( 'double-redirect-fixer' ), false );
152 if ( !self
::$user->isLoggedIn() ) {
153 self
::$user->addToDatabase();