MediaWiki  master
DoubleRedirectJob.php
Go to the documentation of this file.
1 <?php
30 
36 class 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  foreach ( $res as $row ) {
89  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
90  if ( !$title ) {
91  continue;
92  }
93 
94  $jobs[] = new self( $title, [
95  'reason' => $reason,
96  'redirTitle' => MediaWikiServices::getInstance()
97  ->getTitleFormatter()
98  ->getPrefixedDBkey( $redirTitle ),
99  ] );
100  # Avoid excessive memory usage
101  if ( count( $jobs ) > self::MAX_DR_JOBS_COUNTER ) {
102  JobQueueGroup::singleton()->push( $jobs );
103  $jobs = [];
104  }
105  }
106  JobQueueGroup::singleton()->push( $jobs );
107  }
108 
112  public function run() {
113  if ( !$this->redirTitle ) {
114  $this->setLastError( 'Invalid title' );
115 
116  return false;
117  }
118 
119  $services = MediaWikiServices::getInstance();
120  $targetRev = $services->getRevisionLookup()
121  ->getRevisionByTitle( $this->title, 0, RevisionLookup::READ_LATEST );
122  if ( !$targetRev ) {
123  wfDebug( __METHOD__ . ": target redirect already deleted, ignoring" );
124 
125  return true;
126  }
127  $content = $targetRev->getContent( SlotRecord::MAIN );
128  $currentDest = $content ? $content->getRedirectTarget() : null;
129  if ( !$currentDest || !$currentDest->equals( $this->redirTitle ) ) {
130  wfDebug( __METHOD__ . ": Redirect has changed since the job was queued" );
131 
132  return true;
133  }
134 
135  // Check for a suppression tag (used e.g. in periodically archived discussions)
136  $mw = $services->getMagicWordFactory()->get( 'staticredirect' );
137  if ( $content->matchMagicWord( $mw ) ) {
138  wfDebug( __METHOD__ . ": skipping: suppressed with __STATICREDIRECT__" );
139 
140  return true;
141  }
142 
143  // Find the current final destination
144  $newTitle = self::getFinalDestination( $this->redirTitle );
145  if ( !$newTitle ) {
146  wfDebug( __METHOD__ .
147  ": skipping: single redirect, circular redirect or invalid redirect destination" );
148 
149  return true;
150  }
151  if ( $newTitle->equals( $this->redirTitle ) ) {
152  // The redirect is already right, no need to change it
153  // This can happen if the page was moved back (say after vandalism)
154  wfDebug( __METHOD__ . " : skipping, already good" );
155  }
156 
157  // Preserve fragment (T16904)
158  $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
159  $currentDest->getFragment(), $newTitle->getInterwiki() );
160 
161  // Fix the text
162  $newContent = $content->updateRedirect( $newTitle );
163 
164  if ( $newContent->equals( $content ) ) {
165  $this->setLastError( 'Content unchanged???' );
166 
167  return false;
168  }
169 
170  $user = $this->getUser();
171  if ( !$user ) {
172  $this->setLastError( 'Invalid user' );
173 
174  return false;
175  }
176 
177  // Save it
178  // phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgUser
179  global $wgUser;
180  $oldUser = $wgUser;
181  $wgUser = $user;
182  $article = $services->getWikiPageFactory()->newFromTitle( $this->title );
183 
184  // Messages: double-redirect-fixed-move, double-redirect-fixed-maintenance
185  $reason = wfMessage( 'double-redirect-fixed-' . $this->params['reason'],
186  $this->redirTitle->getPrefixedText(), $newTitle->getPrefixedText()
187  )->inContentLanguage()->text();
189  $article->doUserEditContent( $newContent, $user, $reason, $flags );
190  $wgUser = $oldUser;
191 
192  return true;
193  }
194 
203  public static function getFinalDestination( $title ) {
204  $dbw = wfGetDB( DB_PRIMARY );
205 
206  // Circular redirect check
207  $seenTitles = [];
208  $dest = false;
209 
210  while ( true ) {
211  $titleText = CacheKeyHelper::getKeyForPage( $title );
212  if ( isset( $seenTitles[$titleText] ) ) {
213  wfDebug( __METHOD__, "Circular redirect detected, aborting" );
214 
215  return false;
216  }
217  $seenTitles[$titleText] = true;
218 
219  if ( $title->isExternal() ) {
220  // If the target is interwiki, we have to break early (T42352).
221  // Otherwise it will look up a row in the local page table
222  // with the namespace/page of the interwiki target which can cause
223  // unexpected results (e.g. X -> foo:Bar -> Bar -> .. )
224  break;
225  }
226 
227  $row = $dbw->selectRow(
228  [ 'redirect', 'page' ],
229  [ 'rd_namespace', 'rd_title', 'rd_interwiki' ],
230  [
231  'rd_from=page_id',
232  'page_namespace' => $title->getNamespace(),
233  'page_title' => $title->getDBkey()
234  ], __METHOD__ );
235  if ( !$row ) {
236  # No redirect from here, chain terminates
237  break;
238  } else {
239  $dest = $title = Title::makeTitle(
240  $row->rd_namespace,
241  $row->rd_title,
242  '',
243  $row->rd_interwiki ?? ''
244  );
245  }
246  }
247 
248  return $dest;
249  }
250 
258  private function getUser() {
259  if ( !self::$user ) {
260  $username = wfMessage( 'double-redirect-fixer' )->inContentLanguage()->text();
261  self::$user = User::newFromName( $username );
262  # User::newFromName() can return false on a badly configured wiki.
263  if ( self::$user && !self::$user->isRegistered() ) {
264  self::$user->addToDatabase();
265  }
266  }
267 
268  return self::$user;
269  }
270 }
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:377
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:204
DoubleRedirectJob\fixRedirects
static fixRedirects( $reason, $redirTitle)
Insert jobs into the job queue to fix redirects to the given title.
Definition: DoubleRedirectJob.php:73
DoubleRedirectJob\getFinalDestination
static getFinalDestination( $title)
Get the final destination of a redirect.
Definition: DoubleRedirectJob.php:203
Job\$title
Title $title
Definition: Job.php:49
DoubleRedirectJob\getUser
getUser()
Get a user object for doing edits, from a request-lifetime cache False will be returned if the user n...
Definition: DoubleRedirectJob.php:258
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:595
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1167
Title\isExternal
isExternal()
Is this Title interwiki?
Definition: Title.php:956
$res
$res
Definition: testCompression.php:57
Job\$params
array $params
Array of job parameters.
Definition: Job.php:43
DoubleRedirectJob\__construct
__construct(PageReference $page, array $params)
Definition: DoubleRedirectJob.php:61
Job\setLastError
setLastError( $error)
Definition: Job.php:467
Page\PageReference
Interface for objects (potentially) representing a page that can be viewable and linked to on a wiki.
Definition: PageReference.php:49
MediaWiki\Revision\RevisionLookup
Service for looking up page revisions.
Definition: RevisionLookup.php:38
DoubleRedirectJob
Job to fix double redirects after moving a page.
Definition: DoubleRedirectJob.php:36
Job
Class to both describe a background job and handle jobs.
Definition: Job.php:38
Title\getDBkey
getDBkey()
Get the main part with underscores.
Definition: Title.php:1057
Title\getNamespace
getNamespace()
Get the namespace index, i.e.
Definition: Title.php:1066
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2186
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:648
wfDebug
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
Definition: GlobalFunctions.php:894
DoubleRedirectJob\$redirTitle
Title $redirTitle
The title which has changed, redirects pointing to this title are fixed.
Definition: DoubleRedirectJob.php:37
$content
$content
Definition: router.php:76
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
EDIT_SUPPRESS_RC
const EDIT_SUPPRESS_RC
Definition: Defines.php:128
DoubleRedirectJob\$user
static User $user
Definition: DoubleRedirectJob.php:50
EDIT_UPDATE
const EDIT_UPDATE
Definition: Defines.php:126
Title
Represents a title within MediaWiki.
Definition: Title.php:47
JobQueueGroup\singleton
static singleton( $domain=false)
Definition: JobQueueGroup.php:114
MediaWiki\Cache\CacheKeyHelper
Helper class for mapping value objects representing basic entities to cache keys.
Definition: CacheKeyHelper.php:43
MediaWiki\Linker\LinkTarget
Definition: LinkTarget.php:26
DoubleRedirectJob\run
run()
Definition: DoubleRedirectJob.php:112
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:67
MediaWiki\Revision\SlotRecord
Value object representing a content slot associated with a page revision.
Definition: SlotRecord.php:40
EDIT_INTERNAL
const EDIT_INTERNAL
Definition: Defines.php:132