]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - includes/DoubleRedirectJob.php
MediaWiki 1.15.5
[autoinstallsdev/mediawiki.git] / includes / DoubleRedirectJob.php
1 <?php
2
3 class DoubleRedirectJob extends Job {
4         var $reason, $redirTitle, $destTitleText;
5         static $user;
6
7         /** 
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
11          */
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 );
15                 $res = $dbw->select( 
16                         array( 'redirect', 'page' ), 
17                         array( 'page_namespace', 'page_title' ), 
18                         array( 
19                                 'page_id = rd_from',
20                                 'rd_namespace' => $redirTitle->getNamespace(),
21                                 'rd_title' => $redirTitle->getDBkey()
22                         ), __METHOD__ );
23                 if ( !$res->numRows() ) {
24                         return;
25                 }
26                 $jobs = array();
27                 foreach ( $res as $row ) {
28                         $title = Title::makeTitle( $row->page_namespace, $row->page_title );
29                         if ( !$title ) {
30                                 continue;
31                         }
32
33                         $jobs[] = new self( $title, array( 
34                                 'reason' => $reason,
35                                 'redirTitle' => $redirTitle->getPrefixedDBkey() ) );
36                         # Avoid excessive memory usage
37                         if ( count( $jobs ) > 10000 ) {
38                                 Job::batchInsert( $jobs );
39                                 $jobs = array();
40                         }
41                 }
42                 Job::batchInsert( $jobs );
43         }
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'] : '';
49         }
50
51         function run() {
52                 if ( !$this->redirTitle ) {
53                         $this->setLastError( 'Invalid title' );
54                         return false;
55                 }
56
57                 $targetRev = Revision::newFromTitle( $this->title );
58                 if ( !$targetRev ) {
59                         wfDebug( __METHOD__.": target redirect already deleted, ignoring\n" );
60                         return true;
61                 }
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" );
66                         return true;
67                 }
68
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" );
73                         return true;
74                 }
75
76                 # Find the current final destination
77                 $newTitle = self::getFinalDestination( $this->redirTitle );
78                 if ( !$newTitle ) {
79                         wfDebug( __METHOD__.": skipping: single redirect, circular redirect or invalid redirect destination\n" );
80                         return true;
81                 }
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" );
86                 }
87
88                 # Preserve fragment (bug 14904)
89                 $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(), 
90                         $currentDest->getFragment() );
91
92                 # Fix the text
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() . ']]',
97                         $text, 1 );
98
99                 if ( $newText === $text ) {
100                         $this->setLastError( 'Text unchanged???' );
101                         return false;
102                 }
103
104                 # Save it
105                 global $wgUser;
106                 $oldUser = $wgUser;
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 );
112                 $wgUser = $oldUser;
113
114                 return true;
115         }
116
117         /**
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
120          */
121         public static function getFinalDestination( $title ) {
122                 $dbw = wfGetDB( DB_MASTER );
123
124                 $seenTitles = array(); # Circular redirect check
125                 $dest = false;
126
127                 while ( true ) {
128                         $titleText = $title->getPrefixedDBkey();
129                         if ( isset( $seenTitles[$titleText] ) ) {
130                                 wfDebug( __METHOD__, "Circular redirect detected, aborting\n" );
131                                 return false;
132                         }
133                         $seenTitles[$titleText] = true;
134
135                         $row = $dbw->selectRow( 
136                                 array( 'redirect', 'page' ),
137                                 array( 'rd_namespace', 'rd_title' ),
138                                 array( 
139                                         'rd_from=page_id',
140                                         'page_namespace' => $title->getNamespace(),
141                                         'page_title' => $title->getDBkey()
142                                 ), __METHOD__ );
143                         if ( !$row ) {
144                                 # No redirect from here, chain terminates
145                                 break;
146                         } else {
147                                 $dest = $title = Title::makeTitle( $row->rd_namespace, $row->rd_title );
148                         }
149                 }
150                 return $dest;
151         }
152
153         /**
154          * Get a user object for doing edits, from a request-lifetime cache
155          */
156         function getUser() {
157                 if ( !self::$user ) {
158                         self::$user = User::newFromName( wfMsgForContent( 'double-redirect-fixer' ), false );
159                         if ( !self::$user->isLoggedIn() ) {
160                                 self::$user->addToDatabase();
161                         }
162                 }
163                 return self::$user;
164         }
165 }
166