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