Merge changes I70b15ea6,I5989410f,I49d96181,Ib3381bca,Ia608df21
[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 /**
85 * @param Title $title
86 * @param array|bool $params
87 * @param int $id
88 */
89 function __construct( $title, $params = false, $id = 0 ) {
90 parent::__construct( 'fixDoubleRedirect', $title, $params, $id );
91 $this->reason = $params['reason'];
92 $this->redirTitle = Title::newFromText( $params['redirTitle'] );
93 }
94
95 /**
96 * @return bool
97 */
98 function run() {
99 if ( !$this->redirTitle ) {
100 $this->setLastError( 'Invalid title' );
101
102 return false;
103 }
104
105 $targetRev = Revision::newFromTitle( $this->title, false, Revision::READ_LATEST );
106 if ( !$targetRev ) {
107 wfDebug( __METHOD__ . ": target redirect already deleted, ignoring\n" );
108
109 return true;
110 }
111 $content = $targetRev->getContent();
112 $currentDest = $content ? $content->getRedirectTarget() : null;
113 if ( !$currentDest || !$currentDest->equals( $this->redirTitle ) ) {
114 wfDebug( __METHOD__ . ": Redirect has changed since the job was queued\n" );
115
116 return true;
117 }
118
119 // Check for a suppression tag (used e.g. in periodically archived discussions)
120 $mw = MagicWord::get( 'staticredirect' );
121 if ( $content->matchMagicWord( $mw ) ) {
122 wfDebug( __METHOD__ . ": skipping: suppressed with __STATICREDIRECT__\n" );
123
124 return true;
125 }
126
127 // Find the current final destination
128 $newTitle = self::getFinalDestination( $this->redirTitle );
129 if ( !$newTitle ) {
130 wfDebug( __METHOD__ .
131 ": skipping: single redirect, circular redirect or invalid redirect destination\n" );
132
133 return true;
134 }
135 if ( $newTitle->equals( $this->redirTitle ) ) {
136 // The redirect is already right, no need to change it
137 // This can happen if the page was moved back (say after vandalism)
138 wfDebug( __METHOD__ . " : skipping, already good\n" );
139 }
140
141 // Preserve fragment (bug 14904)
142 $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
143 $currentDest->getFragment(), $newTitle->getInterwiki() );
144
145 // Fix the text
146 $newContent = $content->updateRedirect( $newTitle );
147
148 if ( $newContent->equals( $content ) ) {
149 $this->setLastError( 'Content unchanged???' );
150
151 return false;
152 }
153
154 $user = $this->getUser();
155 if ( !$user ) {
156 $this->setLastError( 'Invalid user' );
157
158 return false;
159 }
160
161 // Save it
162 global $wgUser;
163 $oldUser = $wgUser;
164 $wgUser = $user;
165 $article = WikiPage::factory( $this->title );
166
167 // Messages: double-redirect-fixed-move, double-redirect-fixed-maintenance
168 $reason = wfMessage( 'double-redirect-fixed-' . $this->reason,
169 $this->redirTitle->getPrefixedText(), $newTitle->getPrefixedText()
170 )->inContentLanguage()->text();
171 $article->doEditContent( $newContent, $reason, EDIT_UPDATE | EDIT_SUPPRESS_RC, false, $user );
172 $wgUser = $oldUser;
173
174 return true;
175 }
176
177 /**
178 * Get the final destination of a redirect
179 *
180 * @param $title Title
181 *
182 * @return bool if the specified title is not a redirect, or if it is a circular redirect
183 */
184 public static function getFinalDestination( $title ) {
185 $dbw = wfGetDB( DB_MASTER );
186
187 // Circular redirect check
188 $seenTitles = array();
189 $dest = false;
190
191 while ( true ) {
192 $titleText = $title->getPrefixedDBkey();
193 if ( isset( $seenTitles[$titleText] ) ) {
194 wfDebug( __METHOD__, "Circular redirect detected, aborting\n" );
195
196 return false;
197 }
198 $seenTitles[$titleText] = true;
199
200 if ( $title->getInterwiki() ) {
201 // If the target is interwiki, we have to break early (bug 40352).
202 // Otherwise it will look up a row in the local page table
203 // with the namespace/page of the interwiki target which can cause
204 // unexpected results (e.g. X -> foo:Bar -> Bar -> .. )
205 break;
206 }
207
208 $row = $dbw->selectRow(
209 array( 'redirect', 'page' ),
210 array( 'rd_namespace', 'rd_title', 'rd_interwiki' ),
211 array(
212 'rd_from=page_id',
213 'page_namespace' => $title->getNamespace(),
214 'page_title' => $title->getDBkey()
215 ), __METHOD__ );
216 if ( !$row ) {
217 # No redirect from here, chain terminates
218 break;
219 } else {
220 $dest = $title = Title::makeTitle(
221 $row->rd_namespace,
222 $row->rd_title,
223 '',
224 $row->rd_interwiki
225 );
226 }
227 }
228
229 return $dest;
230 }
231
232 /**
233 * Get a user object for doing edits, from a request-lifetime cache
234 * False will be returned if the user name specified in the
235 * 'double-redirect-fixer' message is invalid.
236 *
237 * @return User|bool
238 */
239 function getUser() {
240 if ( !self::$user ) {
241 $username = wfMessage( 'double-redirect-fixer' )->inContentLanguage()->text();
242 self::$user = User::newFromName( $username );
243 # User::newFromName() can return false on a badly configured wiki.
244 if ( self::$user && !self::$user->isLoggedIn() ) {
245 self::$user->addToDatabase();
246 }
247 }
248
249 return self::$user;
250 }
251 }