Merge "Enable configuration to supply options for Special:Search form"
[lhc/web/wiklou.git] / includes / jobqueue / 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 use MediaWiki\MediaWikiServices;
25
26 /**
27 * Job to fix double redirects after moving a page
28 *
29 * @ingroup JobQueue
30 */
31 class DoubleRedirectJob extends Job {
32
33 /** @var Title The title which has changed, redirects pointing to this
34 * title are fixed
35 */
36 private $redirTitle;
37
38 /** @var User */
39 private static $user;
40
41 /**
42 * @param Title $title
43 * @param array $params Expected to contain these elements:
44 * - 'redirTitle' => string The title that changed and should be fixed.
45 * - 'reason' => string Reason for the change, can be "move" or "maintenance". Used as a suffix
46 * for the message keys "double-redirect-fixed-move" and
47 * "double-redirect-fixed-maintenance".
48 * ]
49 */
50 function __construct( Title $title, array $params ) {
51 parent::__construct( 'fixDoubleRedirect', $title, $params );
52 $this->redirTitle = Title::newFromText( $params['redirTitle'] );
53 }
54
55 /**
56 * Insert jobs into the job queue to fix redirects to the given title
57 * @param string $reason The reason for the fix, see message
58 * "double-redirect-fixed-<reason>"
59 * @param Title $redirTitle The title which has changed, redirects
60 * pointing to this title are fixed
61 * @param bool $destTitle Not used
62 */
63 public static function fixRedirects( $reason, $redirTitle, $destTitle = false ) {
64 # Need to use the master to get the redirect table updated in the same transaction
65 $dbw = wfGetDB( DB_MASTER );
66 $res = $dbw->select(
67 [ 'redirect', 'page' ],
68 [ 'page_namespace', 'page_title' ],
69 [
70 'page_id = rd_from',
71 'rd_namespace' => $redirTitle->getNamespace(),
72 'rd_title' => $redirTitle->getDBkey()
73 ], __METHOD__ );
74 if ( !$res->numRows() ) {
75 return;
76 }
77 $jobs = [];
78 foreach ( $res as $row ) {
79 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
80 if ( !$title ) {
81 continue;
82 }
83
84 $jobs[] = new self( $title, [
85 'reason' => $reason,
86 'redirTitle' => $redirTitle->getPrefixedDBkey() ] );
87 # Avoid excessive memory usage
88 if ( count( $jobs ) > 10000 ) {
89 JobQueueGroup::singleton()->push( $jobs );
90 $jobs = [];
91 }
92 }
93 JobQueueGroup::singleton()->push( $jobs );
94 }
95
96 /**
97 * @return bool
98 */
99 function run() {
100 if ( !$this->redirTitle ) {
101 $this->setLastError( 'Invalid title' );
102
103 return false;
104 }
105
106 $targetRev = Revision::newFromTitle( $this->title, false, Revision::READ_LATEST );
107 if ( !$targetRev ) {
108 wfDebug( __METHOD__ . ": target redirect already deleted, ignoring\n" );
109
110 return true;
111 }
112 $content = $targetRev->getContent();
113 $currentDest = $content ? $content->getRedirectTarget() : null;
114 if ( !$currentDest || !$currentDest->equals( $this->redirTitle ) ) {
115 wfDebug( __METHOD__ . ": Redirect has changed since the job was queued\n" );
116
117 return true;
118 }
119
120 // Check for a suppression tag (used e.g. in periodically archived discussions)
121 $mw = MediaWikiServices::getInstance()->getMagicWordFactory()->get( 'staticredirect' );
122 if ( $content->matchMagicWord( $mw ) ) {
123 wfDebug( __METHOD__ . ": skipping: suppressed with __STATICREDIRECT__\n" );
124
125 return true;
126 }
127
128 // Find the current final destination
129 $newTitle = self::getFinalDestination( $this->redirTitle );
130 if ( !$newTitle ) {
131 wfDebug( __METHOD__ .
132 ": skipping: single redirect, circular redirect or invalid redirect destination\n" );
133
134 return true;
135 }
136 if ( $newTitle->equals( $this->redirTitle ) ) {
137 // The redirect is already right, no need to change it
138 // This can happen if the page was moved back (say after vandalism)
139 wfDebug( __METHOD__ . " : skipping, already good\n" );
140 }
141
142 // Preserve fragment (T16904)
143 $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
144 $currentDest->getFragment(), $newTitle->getInterwiki() );
145
146 // Fix the text
147 $newContent = $content->updateRedirect( $newTitle );
148
149 if ( $newContent->equals( $content ) ) {
150 $this->setLastError( 'Content unchanged???' );
151
152 return false;
153 }
154
155 $user = $this->getUser();
156 if ( !$user ) {
157 $this->setLastError( 'Invalid user' );
158
159 return false;
160 }
161
162 // Save it
163 global $wgUser;
164 $oldUser = $wgUser;
165 $wgUser = $user;
166 $article = WikiPage::factory( $this->title );
167
168 // Messages: double-redirect-fixed-move, double-redirect-fixed-maintenance
169 $reason = wfMessage( 'double-redirect-fixed-' . $this->params['reason'],
170 $this->redirTitle->getPrefixedText(), $newTitle->getPrefixedText()
171 )->inContentLanguage()->text();
172 $flags = EDIT_UPDATE | EDIT_SUPPRESS_RC | EDIT_INTERNAL;
173 $article->doEditContent( $newContent, $reason, $flags, false, $user );
174 $wgUser = $oldUser;
175
176 return true;
177 }
178
179 /**
180 * Get the final destination of a redirect
181 *
182 * @param Title $title
183 *
184 * @return Title|bool The final Title after following all redirects, or false if
185 * the page is not a redirect or the redirect loops.
186 */
187 public static function getFinalDestination( $title ) {
188 $dbw = wfGetDB( DB_MASTER );
189
190 // Circular redirect check
191 $seenTitles = [];
192 $dest = false;
193
194 while ( true ) {
195 $titleText = $title->getPrefixedDBkey();
196 if ( isset( $seenTitles[$titleText] ) ) {
197 wfDebug( __METHOD__, "Circular redirect detected, aborting\n" );
198
199 return false;
200 }
201 $seenTitles[$titleText] = true;
202
203 if ( $title->isExternal() ) {
204 // If the target is interwiki, we have to break early (T42352).
205 // Otherwise it will look up a row in the local page table
206 // with the namespace/page of the interwiki target which can cause
207 // unexpected results (e.g. X -> foo:Bar -> Bar -> .. )
208 break;
209 }
210
211 $row = $dbw->selectRow(
212 [ 'redirect', 'page' ],
213 [ 'rd_namespace', 'rd_title', 'rd_interwiki' ],
214 [
215 'rd_from=page_id',
216 'page_namespace' => $title->getNamespace(),
217 'page_title' => $title->getDBkey()
218 ], __METHOD__ );
219 if ( !$row ) {
220 # No redirect from here, chain terminates
221 break;
222 } else {
223 $dest = $title = Title::makeTitle(
224 $row->rd_namespace,
225 $row->rd_title,
226 '',
227 $row->rd_interwiki
228 );
229 }
230 }
231
232 return $dest;
233 }
234
235 /**
236 * Get a user object for doing edits, from a request-lifetime cache
237 * False will be returned if the user name specified in the
238 * 'double-redirect-fixer' message is invalid.
239 *
240 * @return User|bool
241 */
242 function getUser() {
243 if ( !self::$user ) {
244 $username = wfMessage( 'double-redirect-fixer' )->inContentLanguage()->text();
245 self::$user = User::newFromName( $username );
246 # User::newFromName() can return false on a badly configured wiki.
247 if ( self::$user && !self::$user->isLoggedIn() ) {
248 self::$user->addToDatabase();
249 }
250 }
251
252 return self::$user;
253 }
254 }