Merge changes I8b6b8510,I1cae6174
[lhc/web/wiklou.git] / includes / job / jobs / DoubleRedirectJob.php
1 <?php
2 /**
3 * Job to fix double redirects after moving a page.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup JobQueue
22 */
23
24 /**
25 * Job to fix double redirects after moving a page
26 *
27 * @ingroup JobQueue
28 */
29 class DoubleRedirectJob extends Job {
30 /** @var string Reason for the change, 'maintenance' or 'move'. Suffix fo
31 * message key 'double-redirect-fixed-'.
32 */
33 private $reason;
34
35 /** @var Title The title which has changed, redirects pointing to this
36 * title are fixed
37 */
38 private $redirTitle;
39
40 /** @var User */
41 private static $user;
42
43 /**
44 * Insert jobs into the job queue to fix redirects to the given title
45 * @param string $reason the reason for the fix, see message
46 * "double-redirect-fixed-<reason>"
47 * @param $redirTitle Title: the title which has changed, redirects
48 * pointing to this title are fixed
49 * @param bool $destTitle Not used
50 */
51 public static function fixRedirects( $reason, $redirTitle, $destTitle = false ) {
52 # Need to use the master to get the redirect table updated in the same transaction
53 $dbw = wfGetDB( DB_MASTER );
54 $res = $dbw->select(
55 array( 'redirect', 'page' ),
56 array( 'page_namespace', 'page_title' ),
57 array(
58 'page_id = rd_from',
59 'rd_namespace' => $redirTitle->getNamespace(),
60 'rd_title' => $redirTitle->getDBkey()
61 ), __METHOD__ );
62 if ( !$res->numRows() ) {
63 return;
64 }
65 $jobs = array();
66 foreach ( $res as $row ) {
67 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
68 if ( !$title ) {
69 continue;
70 }
71
72 $jobs[] = new self( $title, array(
73 'reason' => $reason,
74 'redirTitle' => $redirTitle->getPrefixedDBkey() ) );
75 # Avoid excessive memory usage
76 if ( count( $jobs ) > 10000 ) {
77 JobQueueGroup::singleton()->push( $jobs );
78 $jobs = array();
79 }
80 }
81 JobQueueGroup::singleton()->push( $jobs );
82 }
83
84 function __construct( $title, $params = false, $id = 0 ) {
85 parent::__construct( 'fixDoubleRedirect', $title, $params, $id );
86 $this->reason = $params['reason'];
87 $this->redirTitle = Title::newFromText( $params['redirTitle'] );
88 }
89
90 /**
91 * @return bool
92 */
93 function run() {
94 if ( !$this->redirTitle ) {
95 $this->setLastError( 'Invalid title' );
96
97 return false;
98 }
99
100 $targetRev = Revision::newFromTitle( $this->title, false, Revision::READ_LATEST );
101 if ( !$targetRev ) {
102 wfDebug( __METHOD__ . ": target redirect already deleted, ignoring\n" );
103
104 return true;
105 }
106 $content = $targetRev->getContent();
107 $currentDest = $content ? $content->getRedirectTarget() : null;
108 if ( !$currentDest || !$currentDest->equals( $this->redirTitle ) ) {
109 wfDebug( __METHOD__ . ": Redirect has changed since the job was queued\n" );
110
111 return true;
112 }
113
114 // Check for a suppression tag (used e.g. in periodically archived discussions)
115 $mw = MagicWord::get( 'staticredirect' );
116 if ( $content->matchMagicWord( $mw ) ) {
117 wfDebug( __METHOD__ . ": skipping: suppressed with __STATICREDIRECT__\n" );
118
119 return true;
120 }
121
122 // Find the current final destination
123 $newTitle = self::getFinalDestination( $this->redirTitle );
124 if ( !$newTitle ) {
125 wfDebug( __METHOD__ .
126 ": skipping: single redirect, circular redirect or invalid redirect destination\n" );
127
128 return true;
129 }
130 if ( $newTitle->equals( $this->redirTitle ) ) {
131 // The redirect is already right, no need to change it
132 // This can happen if the page was moved back (say after vandalism)
133 wfDebug( __METHOD__ . " : skipping, already good\n" );
134 }
135
136 // Preserve fragment (bug 14904)
137 $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
138 $currentDest->getFragment(), $newTitle->getInterwiki() );
139
140 // Fix the text
141 $newContent = $content->updateRedirect( $newTitle );
142
143 if ( $newContent->equals( $content ) ) {
144 $this->setLastError( 'Content unchanged???' );
145
146 return false;
147 }
148
149 $user = $this->getUser();
150 if ( !$user ) {
151 $this->setLastError( 'Invalid user' );
152
153 return false;
154 }
155
156 // Save it
157 global $wgUser;
158 $oldUser = $wgUser;
159 $wgUser = $user;
160 $article = WikiPage::factory( $this->title );
161
162 // Messages: double-redirect-fixed-move, double-redirect-fixed-maintenance
163 $reason = wfMessage( 'double-redirect-fixed-' . $this->reason,
164 $this->redirTitle->getPrefixedText(), $newTitle->getPrefixedText()
165 )->inContentLanguage()->text();
166 $article->doEditContent( $newContent, $reason, EDIT_UPDATE | EDIT_SUPPRESS_RC, false, $user );
167 $wgUser = $oldUser;
168
169 return true;
170 }
171
172 /**
173 * Get the final destination of a redirect
174 *
175 * @param $title Title
176 *
177 * @return bool if the specified title is not a redirect, or if it is a circular redirect
178 */
179 public static function getFinalDestination( $title ) {
180 $dbw = wfGetDB( DB_MASTER );
181
182 // Circular redirect check
183 $seenTitles = array();
184 $dest = false;
185
186 while ( true ) {
187 $titleText = $title->getPrefixedDBkey();
188 if ( isset( $seenTitles[$titleText] ) ) {
189 wfDebug( __METHOD__, "Circular redirect detected, aborting\n" );
190
191 return false;
192 }
193 $seenTitles[$titleText] = true;
194
195 if ( $title->getInterwiki() ) {
196 // If the target is interwiki, we have to break early (bug 40352).
197 // Otherwise it will look up a row in the local page table
198 // with the namespace/page of the interwiki target which can cause
199 // unexpected results (e.g. X -> foo:Bar -> Bar -> .. )
200 break;
201 }
202
203 $row = $dbw->selectRow(
204 array( 'redirect', 'page' ),
205 array( 'rd_namespace', 'rd_title', 'rd_interwiki' ),
206 array(
207 'rd_from=page_id',
208 'page_namespace' => $title->getNamespace(),
209 'page_title' => $title->getDBkey()
210 ), __METHOD__ );
211 if ( !$row ) {
212 # No redirect from here, chain terminates
213 break;
214 } else {
215 $dest = $title = Title::makeTitle(
216 $row->rd_namespace,
217 $row->rd_title,
218 '',
219 $row->rd_interwiki
220 );
221 }
222 }
223
224 return $dest;
225 }
226
227 /**
228 * Get a user object for doing edits, from a request-lifetime cache
229 * False will be returned if the user name specified in the
230 * 'double-redirect-fixer' message is invalid.
231 *
232 * @return User|bool
233 */
234 function getUser() {
235 if ( !self::$user ) {
236 $username = wfMessage( 'double-redirect-fixer' )->inContentLanguage()->text();
237 self::$user = User::newFromName( $username );
238 # User::newFromName() can return false on a badly configured wiki.
239 if ( self::$user && !self::$user->isLoggedIn() ) {
240 self::$user->addToDatabase();
241 }
242 }
243
244 return self::$user;
245 }
246 }