]> scripts.mit.edu Git - autoinstalls/mediawiki.git/blob - includes/DoubleRedirectJob.php
MediaWiki 1.16.4
[autoinstalls/mediawiki.git] / includes / DoubleRedirectJob.php
1 <?php
2
3 /**
4  * Job to fix double redirects after moving a page
5  *
6  * @ingroup JobQueue
7  */
8 class DoubleRedirectJob extends Job {
9         var $reason, $redirTitle, $destTitleText;
10         static $user;
11
12         /** 
13          * Insert jobs into the job queue to fix redirects to the given title
14          * @param $reason String: the reason for the fix, see message double-redirect-fixed-<reason>
15          * @param $redirTitle Title: the title which has changed, redirects pointing to this title are fixed
16          * @param $destTitle Not used
17          */
18         public static function fixRedirects( $reason, $redirTitle, $destTitle = false ) {
19                 # Need to use the master to get the redirect table updated in the same transaction
20                 $dbw = wfGetDB( DB_MASTER );
21                 $res = $dbw->select( 
22                         array( 'redirect', 'page' ), 
23                         array( 'page_namespace', 'page_title' ), 
24                         array( 
25                                 'page_id = rd_from',
26                                 'rd_namespace' => $redirTitle->getNamespace(),
27                                 'rd_title' => $redirTitle->getDBkey()
28                         ), __METHOD__ );
29                 if ( !$res->numRows() ) {
30                         return;
31                 }
32                 $jobs = array();
33                 foreach ( $res as $row ) {
34                         $title = Title::makeTitle( $row->page_namespace, $row->page_title );
35                         if ( !$title ) {
36                                 continue;
37                         }
38
39                         $jobs[] = new self( $title, array( 
40                                 'reason' => $reason,
41                                 'redirTitle' => $redirTitle->getPrefixedDBkey() ) );
42                         # Avoid excessive memory usage
43                         if ( count( $jobs ) > 10000 ) {
44                                 Job::batchInsert( $jobs );
45                                 $jobs = array();
46                         }
47                 }
48                 Job::batchInsert( $jobs );
49         }
50         function __construct( $title, $params = false, $id = 0 ) {
51                 parent::__construct( 'fixDoubleRedirect', $title, $params, $id );
52                 $this->reason = $params['reason'];
53                 $this->redirTitle = Title::newFromText( $params['redirTitle'] );
54                 $this->destTitleText = !empty( $params['destTitle'] ) ? $params['destTitle'] : '';
55         }
56
57         function run() {
58                 if ( !$this->redirTitle ) {
59                         $this->setLastError( 'Invalid title' );
60                         return false;
61                 }
62
63                 $targetRev = Revision::newFromTitle( $this->title );
64                 if ( !$targetRev ) {
65                         wfDebug( __METHOD__.": target redirect already deleted, ignoring\n" );
66                         return true;
67                 }
68                 $text = $targetRev->getText();
69                 $currentDest = Title::newFromRedirect( $text );
70                 if ( !$currentDest || !$currentDest->equals( $this->redirTitle ) ) {
71                         wfDebug( __METHOD__.": Redirect has changed since the job was queued\n" );
72                         return true;
73                 }
74
75                 # Check for a suppression tag (used e.g. in periodically archived discussions)
76                 $mw = MagicWord::get( 'staticredirect' );
77                 if ( $mw->match( $text ) ) {
78                         wfDebug( __METHOD__.": skipping: suppressed with __STATICREDIRECT__\n" );
79                         return true;
80                 }
81
82                 # Find the current final destination
83                 $newTitle = self::getFinalDestination( $this->redirTitle );
84                 if ( !$newTitle ) {
85                         wfDebug( __METHOD__.": skipping: single redirect, circular redirect or invalid redirect destination\n" );
86                         return true;
87                 }
88                 if ( $newTitle->equals( $this->redirTitle ) ) {
89                         # The redirect is already right, no need to change it
90                         # This can happen if the page was moved back (say after vandalism)
91                         wfDebug( __METHOD__.": skipping, already good\n" );
92                 }
93
94                 # Preserve fragment (bug 14904)
95                 $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(), 
96                         $currentDest->getFragment() );
97
98                 # Fix the text
99                 # Remember that redirect pages can have categories, templates, etc.,
100                 # so the regex has to be fairly general
101                 $newText = preg_replace( '/ \[ \[  [^\]]*  \] \] /x', 
102                         '[[' . $newTitle->getFullText() . ']]',
103                         $text, 1 );
104
105                 if ( $newText === $text ) {
106                         $this->setLastError( 'Text unchanged???' );
107                         return false;
108                 }
109
110                 # Save it
111                 global $wgUser;
112                 $oldUser = $wgUser;
113                 $wgUser = $this->getUser();
114                 $article = new Article( $this->title );
115                 $reason = wfMsgForContent( 'double-redirect-fixed-' . $this->reason, 
116                         $this->redirTitle->getPrefixedText(), $newTitle->getPrefixedText() );
117                 $article->doEdit( $newText, $reason, EDIT_UPDATE | EDIT_SUPPRESS_RC );
118                 $wgUser = $oldUser;
119
120                 return true;
121         }
122
123         /**
124          * Get the final destination of a redirect
125          * @return false if the specified title is not a redirect, or if it is a circular redirect
126          */
127         public static function getFinalDestination( $title ) {
128                 $dbw = wfGetDB( DB_MASTER );
129
130                 $seenTitles = array(); # Circular redirect check
131                 $dest = false;
132
133                 while ( true ) {
134                         $titleText = $title->getPrefixedDBkey();
135                         if ( isset( $seenTitles[$titleText] ) ) {
136                                 wfDebug( __METHOD__, "Circular redirect detected, aborting\n" );
137                                 return false;
138                         }
139                         $seenTitles[$titleText] = true;
140
141                         $row = $dbw->selectRow( 
142                                 array( 'redirect', 'page' ),
143                                 array( 'rd_namespace', 'rd_title' ),
144                                 array( 
145                                         'rd_from=page_id',
146                                         'page_namespace' => $title->getNamespace(),
147                                         'page_title' => $title->getDBkey()
148                                 ), __METHOD__ );
149                         if ( !$row ) {
150                                 # No redirect from here, chain terminates
151                                 break;
152                         } else {
153                                 $dest = $title = Title::makeTitle( $row->rd_namespace, $row->rd_title );
154                         }
155                 }
156                 return $dest;
157         }
158
159         /**
160          * Get a user object for doing edits, from a request-lifetime cache
161          */
162         function getUser() {
163                 if ( !self::$user ) {
164                         self::$user = User::newFromName( wfMsgForContent( 'double-redirect-fixer' ), false );
165                         if ( !self::$user->isLoggedIn() ) {
166                                 self::$user->addToDatabase();
167                         }
168                 }
169                 return self::$user;
170         }
171 }
172