Merge "Deleting a page and then immediately create-protecting it caused a PHP Fatal...
[lhc/web/wiklou.git] / includes / job / jobs / DoubleRedirectJob.php
index 918d9b3..f5f0d63 100644 (file)
  * @ingroup JobQueue
  */
 class DoubleRedirectJob extends Job {
-       var $reason, $redirTitle;
+       /** @var string Reason for the change, 'maintenance' or 'move'. Suffix fo
+        *    message key 'double-redirect-fixed-'.
+        */
+       private $reason;
 
-       /**
-        * @var User
+       /** @var Title The title which has changed, redirects pointing to this
+        *    title are fixed
         */
-       static $user;
+       private $redirTitle;
+
+       /** @var User */
+       private static $user;
 
        /**
         * Insert jobs into the job queue to fix redirects to the given title
-        * @param string $reason the reason for the fix, see message "double-redirect-fixed-<reason>"
-        * @param $redirTitle Title: the title which has changed, redirects pointing to this title are fixed
+        * @param string $reason the reason for the fix, see message
+        *   "double-redirect-fixed-<reason>"
+        * @param $redirTitle Title: the title which has changed, redirects
+        *   pointing to this title are fixed
         * @param bool $destTitle Not used
         */
        public static function fixRedirects( $reason, $redirTitle, $destTitle = false ) {
@@ -73,6 +81,11 @@ class DoubleRedirectJob extends Job {
                JobQueueGroup::singleton()->push( $jobs );
        }
 
+       /**
+        * @param Title $title
+        * @param array|bool $params
+        * @param int $id
+        */
        function __construct( $title, $params = false, $id = 0 ) {
                parent::__construct( 'fixDoubleRedirect', $title, $params, $id );
                $this->reason = $params['reason'];
@@ -85,65 +98,73 @@ class DoubleRedirectJob extends Job {
        function run() {
                if ( !$this->redirTitle ) {
                        $this->setLastError( 'Invalid title' );
+
                        return false;
                }
 
                $targetRev = Revision::newFromTitle( $this->title, false, Revision::READ_LATEST );
                if ( !$targetRev ) {
                        wfDebug( __METHOD__ . ": target redirect already deleted, ignoring\n" );
+
                        return true;
                }
                $content = $targetRev->getContent();
                $currentDest = $content ? $content->getRedirectTarget() : null;
                if ( !$currentDest || !$currentDest->equals( $this->redirTitle ) ) {
                        wfDebug( __METHOD__ . ": Redirect has changed since the job was queued\n" );
+
                        return true;
                }
 
-               # Check for a suppression tag (used e.g. in periodically archived discussions)
+               // Check for a suppression tag (used e.g. in periodically archived discussions)
                $mw = MagicWord::get( 'staticredirect' );
                if ( $content->matchMagicWord( $mw ) ) {
                        wfDebug( __METHOD__ . ": skipping: suppressed with __STATICREDIRECT__\n" );
+
                        return true;
                }
 
-               # Find the current final destination
+               // Find the current final destination
                $newTitle = self::getFinalDestination( $this->redirTitle );
                if ( !$newTitle ) {
-                       wfDebug( __METHOD__ . ": skipping: single redirect, circular redirect or invalid redirect destination\n" );
+                       wfDebug( __METHOD__ .
+                               ": skipping: single redirect, circular redirect or invalid redirect destination\n" );
+
                        return true;
                }
                if ( $newTitle->equals( $this->redirTitle ) ) {
-                       # The redirect is already right, no need to change it
-                       # This can happen if the page was moved back (say after vandalism)
+                       // The redirect is already right, no need to change it
+                       // This can happen if the page was moved back (say after vandalism)
                        wfDebug( __METHOD__ . " : skipping, already good\n" );
                }
 
-               # Preserve fragment (bug 14904)
+               // Preserve fragment (bug 14904)
                $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
                        $currentDest->getFragment(), $newTitle->getInterwiki() );
 
-               # Fix the text
+               // Fix the text
                $newContent = $content->updateRedirect( $newTitle );
 
                if ( $newContent->equals( $content ) ) {
                        $this->setLastError( 'Content unchanged???' );
+
                        return false;
                }
 
                $user = $this->getUser();
                if ( !$user ) {
                        $this->setLastError( 'Invalid user' );
+
                        return false;
                }
 
-               # Save it
+               // Save it
                global $wgUser;
                $oldUser = $wgUser;
                $wgUser = $user;
                $article = WikiPage::factory( $this->title );
-               // Give grep a chance to find the usages:
-               // double-redirect-fixed-move, double-redirect-fixed-maintenance
+
+               // Messages: double-redirect-fixed-move, double-redirect-fixed-maintenance
                $reason = wfMessage( 'double-redirect-fixed-' . $this->reason,
                        $this->redirTitle->getPrefixedText(), $newTitle->getPrefixedText()
                )->inContentLanguage()->text();
@@ -163,13 +184,15 @@ class DoubleRedirectJob extends Job {
        public static function getFinalDestination( $title ) {
                $dbw = wfGetDB( DB_MASTER );
 
-               $seenTitles = array(); # Circular redirect check
+               // Circular redirect check
+               $seenTitles = array();
                $dest = false;
 
                while ( true ) {
                        $titleText = $title->getPrefixedDBkey();
                        if ( isset( $seenTitles[$titleText] ) ) {
                                wfDebug( __METHOD__, "Circular redirect detected, aborting\n" );
+
                                return false;
                        }
                        $seenTitles[$titleText] = true;
@@ -194,9 +217,15 @@ class DoubleRedirectJob extends Job {
                                # No redirect from here, chain terminates
                                break;
                        } else {
-                               $dest = $title = Title::makeTitle( $row->rd_namespace, $row->rd_title, '', $row->rd_interwiki );
+                               $dest = $title = Title::makeTitle(
+                                       $row->rd_namespace,
+                                       $row->rd_title,
+                                       '',
+                                       $row->rd_interwiki
+                               );
                        }
                }
+
                return $dest;
        }
 
@@ -209,12 +238,14 @@ class DoubleRedirectJob extends Job {
         */
        function getUser() {
                if ( !self::$user ) {
-                       self::$user = User::newFromName( wfMessage( 'double-redirect-fixer' )->inContentLanguage()->text() );
+                       $username = wfMessage( 'double-redirect-fixer' )->inContentLanguage()->text();
+                       self::$user = User::newFromName( $username );
                        # User::newFromName() can return false on a badly configured wiki.
                        if ( self::$user && !self::$user->isLoggedIn() ) {
                                self::$user->addToDatabase();
                        }
                }
+
                return self::$user;
        }
 }