MediaWiki  master
DoubleRedirectJob.php
Go to the documentation of this file.
1 <?php
30 
36 class DoubleRedirectJob extends Job {
37 
41  private $redirTitle;
42 
44  private static $user;
45 
55  public function __construct( PageReference $page, array $params ) {
56  parent::__construct( 'fixDoubleRedirect', $page, $params );
57  $this->redirTitle = Title::newFromText( $params['redirTitle'] );
58  }
59 
67  public static function fixRedirects( $reason, $redirTitle ) {
68  # Need to use the primary DB to get the redirect table updated in the same transaction
69  $dbw = wfGetDB( DB_PRIMARY );
70  $res = $dbw->select(
71  [ 'redirect', 'page' ],
72  [ 'page_namespace', 'page_title' ],
73  [
74  'page_id = rd_from',
75  'rd_namespace' => $redirTitle->getNamespace(),
76  'rd_title' => $redirTitle->getDBkey()
77  ], __METHOD__ );
78  if ( !$res->numRows() ) {
79  return;
80  }
81  $jobs = [];
82  foreach ( $res as $row ) {
83  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
84  if ( !$title ) {
85  continue;
86  }
87 
88  $jobs[] = new self( $title, [
89  'reason' => $reason,
90  'redirTitle' => MediaWikiServices::getInstance()
91  ->getTitleFormatter()
92  ->getPrefixedDBkey( $redirTitle ),
93  ] );
94  # Avoid excessive memory usage
95  if ( count( $jobs ) > 10000 ) {
96  JobQueueGroup::singleton()->push( $jobs );
97  $jobs = [];
98  }
99  }
100  JobQueueGroup::singleton()->push( $jobs );
101  }
102 
106  public function run() {
107  if ( !$this->redirTitle ) {
108  $this->setLastError( 'Invalid title' );
109 
110  return false;
111  }
112 
113  $services = MediaWikiServices::getInstance();
114  $targetRev = $services->getRevisionLookup()
115  ->getRevisionByTitle( $this->title, 0, RevisionLookup::READ_LATEST );
116  if ( !$targetRev ) {
117  wfDebug( __METHOD__ . ": target redirect already deleted, ignoring" );
118 
119  return true;
120  }
121  $content = $targetRev->getContent( SlotRecord::MAIN );
122  $currentDest = $content ? $content->getRedirectTarget() : null;
123  if ( !$currentDest || !$currentDest->equals( $this->redirTitle ) ) {
124  wfDebug( __METHOD__ . ": Redirect has changed since the job was queued" );
125 
126  return true;
127  }
128 
129  // Check for a suppression tag (used e.g. in periodically archived discussions)
130  $mw = $services->getMagicWordFactory()->get( 'staticredirect' );
131  if ( $content->matchMagicWord( $mw ) ) {
132  wfDebug( __METHOD__ . ": skipping: suppressed with __STATICREDIRECT__" );
133 
134  return true;
135  }
136 
137  // Find the current final destination
138  $newTitle = self::getFinalDestination( $this->redirTitle );
139  if ( !$newTitle ) {
140  wfDebug( __METHOD__ .
141  ": skipping: single redirect, circular redirect or invalid redirect destination" );
142 
143  return true;
144  }
145  if ( $newTitle->equals( $this->redirTitle ) ) {
146  // The redirect is already right, no need to change it
147  // This can happen if the page was moved back (say after vandalism)
148  wfDebug( __METHOD__ . " : skipping, already good" );
149  }
150 
151  // Preserve fragment (T16904)
152  $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
153  $currentDest->getFragment(), $newTitle->getInterwiki() );
154 
155  // Fix the text
156  $newContent = $content->updateRedirect( $newTitle );
157 
158  if ( $newContent->equals( $content ) ) {
159  $this->setLastError( 'Content unchanged???' );
160 
161  return false;
162  }
163 
164  $user = $this->getUser();
165  if ( !$user ) {
166  $this->setLastError( 'Invalid user' );
167 
168  return false;
169  }
170 
171  // Save it
172  // phpcs:ignore MediaWiki.Usage.DeprecatedGlobalVariables.Deprecated$wgUser
173  global $wgUser;
174  $oldUser = $wgUser;
175  $wgUser = $user;
176  $article = $services->getWikiPageFactory()->newFromTitle( $this->title );
177 
178  // Messages: double-redirect-fixed-move, double-redirect-fixed-maintenance
179  $reason = wfMessage( 'double-redirect-fixed-' . $this->params['reason'],
180  $this->redirTitle->getPrefixedText(), $newTitle->getPrefixedText()
181  )->inContentLanguage()->text();
183  $article->doUserEditContent( $newContent, $user, $reason, $flags );
184  $wgUser = $oldUser;
185 
186  return true;
187  }
188 
197  public static function getFinalDestination( $title ) {
198  $dbw = wfGetDB( DB_PRIMARY );
199 
200  // Circular redirect check
201  $seenTitles = [];
202  $dest = false;
203 
204  while ( true ) {
205  $titleText = CacheKeyHelper::getKeyForPage( $title );
206  if ( isset( $seenTitles[$titleText] ) ) {
207  wfDebug( __METHOD__, "Circular redirect detected, aborting" );
208 
209  return false;
210  }
211  $seenTitles[$titleText] = true;
212 
213  if ( $title->isExternal() ) {
214  // If the target is interwiki, we have to break early (T42352).
215  // Otherwise it will look up a row in the local page table
216  // with the namespace/page of the interwiki target which can cause
217  // unexpected results (e.g. X -> foo:Bar -> Bar -> .. )
218  break;
219  }
220 
221  $row = $dbw->selectRow(
222  [ 'redirect', 'page' ],
223  [ 'rd_namespace', 'rd_title', 'rd_interwiki' ],
224  [
225  'rd_from=page_id',
226  'page_namespace' => $title->getNamespace(),
227  'page_title' => $title->getDBkey()
228  ], __METHOD__ );
229  if ( !$row ) {
230  # No redirect from here, chain terminates
231  break;
232  } else {
233  $dest = $title = Title::makeTitle(
234  $row->rd_namespace,
235  $row->rd_title,
236  '',
237  $row->rd_interwiki
238  );
239  }
240  }
241 
242  return $dest;
243  }
244 
252  private function getUser() {
253  if ( !self::$user ) {
254  $username = wfMessage( 'double-redirect-fixer' )->inContentLanguage()->text();
255  self::$user = User::newFromName( $username );
256  # User::newFromName() can return false on a badly configured wiki.
257  if ( self::$user && !self::$user->isRegistered() ) {
258  self::$user->addToDatabase();
259  }
260  }
261 
262  return self::$user;
263  }
264 }
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:383
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:193
DoubleRedirectJob\fixRedirects
static fixRedirects( $reason, $redirTitle)
Insert jobs into the job queue to fix redirects to the given title.
Definition: DoubleRedirectJob.php:67
DoubleRedirectJob\getFinalDestination
static getFinalDestination( $title)
Get the final destination of a redirect.
Definition: DoubleRedirectJob.php:197
Job\$title
Title $title
Definition: Job.php:48
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:252
User\newFromName
static newFromName( $name, $validate='valid')
Definition: User.php:606
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1186
Title\isExternal
isExternal()
Is this Title interwiki?
Definition: Title.php:959
$res
$res
Definition: testCompression.php:57
Job\$params
array $params
Array of job parameters.
Definition: Job.php:42
DoubleRedirectJob\__construct
__construct(PageReference $page, array $params)
Definition: DoubleRedirectJob.php:55
Job\setLastError
setLastError( $error)
Definition: Job.php:466
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:37
Title\getDBkey
getDBkey()
Get the main part with underscores.
Definition: Title.php:1060
Title\getNamespace
getNamespace()
Get the namespace index, i.e.
Definition: Title.php:1069
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2202
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:651
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:41
$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:44
EDIT_UPDATE
const EDIT_UPDATE
Definition: Defines.php:126
Title
Represents a title within MediaWiki.
Definition: Title.php:48
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:106
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:68
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