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 # Check for a suppression tag (used e.g. in periodically archived discussions)
70 $mw = MagicWord::get( 'staticredirect' );
71 if ( $mw->match( $text ) ) {
72 wfDebug( __METHOD__.": skipping: suppressed with __STATICREDIRECT__\n" );
76 # Find the current final destination
77 $newTitle = self::getFinalDestination( $this->redirTitle );
79 wfDebug( __METHOD__.": skipping: single redirect, circular redirect or invalid redirect destination\n" );
82 if ( $newTitle->equals( $this->redirTitle ) ) {
83 # The redirect is already right, no need to change it
84 # This can happen if the page was moved back (say after vandalism)
85 wfDebug( __METHOD__.": skipping, already good\n" );
88 # Preserve fragment (bug 14904)
89 $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
90 $currentDest->getFragment() );
93 # Remember that redirect pages can have categories, templates, etc.,
94 # so the regex has to be fairly general
95 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
96 '[[' . $newTitle->getFullText() . ']]',
99 if ( $newText === $text ) {
100 $this->setLastError( 'Text unchanged???' );
107 $wgUser = $this->getUser();
108 $article = new Article( $this->title );
109 $reason = wfMsgForContent( 'double-redirect-fixed-' . $this->reason,
110 $this->redirTitle->getPrefixedText(), $newTitle->getPrefixedText() );
111 $article->doEdit( $newText, $reason, EDIT_UPDATE | EDIT_SUPPRESS_RC );
118 * Get the final destination of a redirect
119 * Returns false if the specified title is not a redirect, or if it is a circular redirect
121 public static function getFinalDestination( $title ) {
122 $dbw = wfGetDB( DB_MASTER );
124 $seenTitles = array(); # Circular redirect check
128 $titleText = $title->getPrefixedDBkey();
129 if ( isset( $seenTitles[$titleText] ) ) {
130 wfDebug( __METHOD__, "Circular redirect detected, aborting\n" );
133 $seenTitles[$titleText] = true;
135 $row = $dbw->selectRow(
136 array( 'redirect', 'page' ),
137 array( 'rd_namespace', 'rd_title' ),
140 'page_namespace' => $title->getNamespace(),
141 'page_title' => $title->getDBkey()
144 # No redirect from here, chain terminates
147 $dest = $title = Title::makeTitle( $row->rd_namespace, $row->rd_title );
154 * Get a user object for doing edits, from a request-lifetime cache
157 if ( !self::$user ) {
158 self::$user = User::newFromName( wfMsgForContent( 'double-redirect-fixer' ), false );
159 if ( !self::$user->isLoggedIn() ) {
160 self::$user->addToDatabase();