MediaWiki REL1_39
DoubleRedirectJob.php
Go to the documentation of this file.
1<?php
30
36class DoubleRedirectJob extends Job {
42 public const MAX_DR_JOBS_COUNTER = 10000;
43
47 private $redirTitle;
48
50 private static $user;
51
61 public function __construct( PageReference $page, array $params ) {
62 parent::__construct( 'fixDoubleRedirect', $page, $params );
63 $this->redirTitle = Title::newFromText( $params['redirTitle'] );
64 }
65
73 public static function fixRedirects( $reason, $redirTitle ) {
74 # Need to use the primary DB to get the redirect table updated in the same transaction
75 $dbw = wfGetDB( DB_PRIMARY );
76 $res = $dbw->select(
77 [ 'redirect', 'page' ],
78 [ 'page_namespace', 'page_title' ],
79 [
80 'page_id = rd_from',
81 'rd_namespace' => $redirTitle->getNamespace(),
82 'rd_title' => $redirTitle->getDBkey()
83 ], __METHOD__ );
84 if ( !$res->numRows() ) {
85 return;
86 }
87 $jobs = [];
88 $jobQueueGroup = MediaWikiServices::getInstance()->getJobQueueGroup();
89 foreach ( $res as $row ) {
90 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
91 if ( !$title ) {
92 continue;
93 }
94
95 $jobs[] = new self( $title, [
96 'reason' => $reason,
97 'redirTitle' => MediaWikiServices::getInstance()
98 ->getTitleFormatter()
99 ->getPrefixedDBkey( $redirTitle ),
100 ] );
101 # Avoid excessive memory usage
102 if ( count( $jobs ) > self::MAX_DR_JOBS_COUNTER ) {
103 $jobQueueGroup->push( $jobs );
104 $jobs = [];
105 }
106 }
107 $jobQueueGroup->push( $jobs );
108 }
109
113 public function run() {
114 if ( !$this->redirTitle ) {
115 $this->setLastError( 'Invalid title' );
116
117 return false;
118 }
119
120 $services = MediaWikiServices::getInstance();
121 $targetRev = $services->getRevisionLookup()
122 ->getRevisionByTitle( $this->title, 0, RevisionLookup::READ_LATEST );
123 if ( !$targetRev ) {
124 wfDebug( __METHOD__ . ": target redirect already deleted, ignoring" );
125
126 return true;
127 }
128 $content = $targetRev->getContent( SlotRecord::MAIN );
129 $currentDest = $content ? $content->getRedirectTarget() : null;
130 if ( !$currentDest || !$currentDest->equals( $this->redirTitle ) ) {
131 wfDebug( __METHOD__ . ": Redirect has changed since the job was queued" );
132
133 return true;
134 }
135
136 // Check for a suppression tag (used e.g. in periodically archived discussions)
137 $mw = $services->getMagicWordFactory()->get( 'staticredirect' );
138 if ( $content->matchMagicWord( $mw ) ) {
139 wfDebug( __METHOD__ . ": skipping: suppressed with __STATICREDIRECT__" );
140
141 return true;
142 }
143
144 // Find the current final destination
145 $newTitle = self::getFinalDestination( $this->redirTitle );
146 if ( !$newTitle ) {
147 wfDebug( __METHOD__ .
148 ": skipping: single redirect, circular redirect or invalid redirect destination" );
149
150 return true;
151 }
152 if ( $newTitle->equals( $this->redirTitle ) ) {
153 // The redirect is already right, no need to change it
154 // This can happen if the page was moved back (say after vandalism)
155 wfDebug( __METHOD__ . " : skipping, already good" );
156 }
157
158 // Preserve fragment (T16904)
159 $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
160 $currentDest->getFragment(), $newTitle->getInterwiki() );
161
162 // Fix the text
163 $newContent = $content->updateRedirect( $newTitle );
164
165 if ( $newContent->equals( $content ) ) {
166 $this->setLastError( 'Content unchanged???' );
167
168 return false;
169 }
170
171 $user = $this->getUser();
172 if ( !$user ) {
173 $this->setLastError( 'Invalid user' );
174
175 return false;
176 }
177
178 // Save it
179 // phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgUser
180 global $wgUser;
181 $oldUser = $wgUser;
182 $wgUser = $user;
183 $article = $services->getWikiPageFactory()->newFromTitle( $this->title );
184
185 // Messages: double-redirect-fixed-move, double-redirect-fixed-maintenance
186 $reason = wfMessage( 'double-redirect-fixed-' . $this->params['reason'],
187 $this->redirTitle->getPrefixedText(), $newTitle->getPrefixedText()
188 )->inContentLanguage()->text();
189 // Avoid RC flood, and use minor to avoid email notifs
191 $article->doUserEditContent( $newContent, $user, $reason, $flags );
192 $wgUser = $oldUser;
193
194 return true;
195 }
196
205 public static function getFinalDestination( $title ) {
206 $dbw = wfGetDB( DB_PRIMARY );
207
208 // Circular redirect check
209 $seenTitles = [];
210 $dest = false;
211
212 while ( true ) {
213 $titleText = CacheKeyHelper::getKeyForPage( $title );
214 if ( isset( $seenTitles[$titleText] ) ) {
215 wfDebug( __METHOD__, "Circular redirect detected, aborting" );
216
217 return false;
218 }
219 $seenTitles[$titleText] = true;
220
221 if ( $title->isExternal() ) {
222 // If the target is interwiki, we have to break early (T42352).
223 // Otherwise it will look up a row in the local page table
224 // with the namespace/page of the interwiki target which can cause
225 // unexpected results (e.g. X -> foo:Bar -> Bar -> .. )
226 break;
227 }
228
229 $row = $dbw->selectRow(
230 [ 'redirect', 'page' ],
231 [ 'rd_namespace', 'rd_title', 'rd_interwiki' ],
232 [
233 'rd_from=page_id',
234 'page_namespace' => $title->getNamespace(),
235 'page_title' => $title->getDBkey()
236 ], __METHOD__ );
237 if ( !$row ) {
238 # No redirect from here, chain terminates
239 break;
240 } else {
241 $dest = $title = Title::makeTitle(
242 $row->rd_namespace,
243 $row->rd_title,
244 '',
245 $row->rd_interwiki ?? ''
246 );
247 }
248 }
249
250 return $dest;
251 }
252
260 private function getUser() {
261 if ( !self::$user ) {
262 $username = wfMessage( 'double-redirect-fixer' )->inContentLanguage()->text();
263 self::$user = User::newFromName( $username );
264 # User::newFromName() can return false on a badly configured wiki.
265 if ( self::$user && !self::$user->isRegistered() ) {
266 self::$user->addToDatabase();
267 }
268 }
269
270 return self::$user;
271 }
272}
getUser()
const EDIT_INTERNAL
Definition Defines.php:133
const EDIT_UPDATE
Definition Defines.php:127
const EDIT_SUPPRESS_RC
Definition Defines.php:129
const EDIT_MINOR
Definition Defines.php:128
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
$wgUser
Definition Setup.php:504
Job to fix double redirects after moving a page.
static getFinalDestination( $title)
Get the final destination of a redirect.
__construct(PageReference $page, array $params)
static fixRedirects( $reason, $redirTitle)
Insert jobs into the job queue to fix redirects to the given title.
Class to both describe a background job and handle jobs.
Definition Job.php:39
setLastError( $error)
Definition Job.php:469
Helper class for mapping value objects representing basic entities to cache keys.
Service locator for MediaWiki core services.
Value object representing a content slot associated with a page revision.
Represents a title within MediaWiki.
Definition Title.php:49
getNamespace()
Get the namespace index, i.e.
Definition Title.php:1066
getDBkey()
Get the main part with underscores.
Definition Title.php:1057
internal since 1.36
Definition User.php:70
static newFromName( $name, $validate='valid')
Definition User.php:598
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
Service for looking up page revisions.
const DB_PRIMARY
Definition defines.php:28
$content
Definition router.php:76