MediaWiki  1.27.2
DoubleRedirectJob.php
Go to the documentation of this file.
1 <?php
29 class DoubleRedirectJob extends Job {
33  private $reason;
34 
38  private $redirTitle;
39 
41  private static $user;
42 
48  parent::__construct( 'fixDoubleRedirect', $title, $params );
49  $this->reason = $params['reason'];
50  $this->redirTitle = Title::newFromText( $params['redirTitle'] );
51  }
52 
61  public static function fixRedirects( $reason, $redirTitle, $destTitle = false ) {
62  # Need to use the master to get the redirect table updated in the same transaction
63  $dbw = wfGetDB( DB_MASTER );
64  $res = $dbw->select(
65  [ 'redirect', 'page' ],
66  [ 'page_namespace', 'page_title' ],
67  [
68  'page_id = rd_from',
69  'rd_namespace' => $redirTitle->getNamespace(),
70  'rd_title' => $redirTitle->getDBkey()
71  ], __METHOD__ );
72  if ( !$res->numRows() ) {
73  return;
74  }
75  $jobs = [];
76  foreach ( $res as $row ) {
77  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
78  if ( !$title ) {
79  continue;
80  }
81 
82  $jobs[] = new self( $title, [
83  'reason' => $reason,
84  'redirTitle' => $redirTitle->getPrefixedDBkey() ] );
85  # Avoid excessive memory usage
86  if ( count( $jobs ) > 10000 ) {
87  JobQueueGroup::singleton()->push( $jobs );
88  $jobs = [];
89  }
90  }
91  JobQueueGroup::singleton()->push( $jobs );
92  }
93 
97  function run() {
98  if ( !$this->redirTitle ) {
99  $this->setLastError( 'Invalid title' );
100 
101  return false;
102  }
103 
104  $targetRev = Revision::newFromTitle( $this->title, false, Revision::READ_LATEST );
105  if ( !$targetRev ) {
106  wfDebug( __METHOD__ . ": target redirect already deleted, ignoring\n" );
107 
108  return true;
109  }
110  $content = $targetRev->getContent();
111  $currentDest = $content ? $content->getRedirectTarget() : null;
112  if ( !$currentDest || !$currentDest->equals( $this->redirTitle ) ) {
113  wfDebug( __METHOD__ . ": Redirect has changed since the job was queued\n" );
114 
115  return true;
116  }
117 
118  // Check for a suppression tag (used e.g. in periodically archived discussions)
119  $mw = MagicWord::get( 'staticredirect' );
120  if ( $content->matchMagicWord( $mw ) ) {
121  wfDebug( __METHOD__ . ": skipping: suppressed with __STATICREDIRECT__\n" );
122 
123  return true;
124  }
125 
126  // Find the current final destination
127  $newTitle = self::getFinalDestination( $this->redirTitle );
128  if ( !$newTitle ) {
129  wfDebug( __METHOD__ .
130  ": skipping: single redirect, circular redirect or invalid redirect destination\n" );
131 
132  return true;
133  }
134  if ( $newTitle->equals( $this->redirTitle ) ) {
135  // The redirect is already right, no need to change it
136  // This can happen if the page was moved back (say after vandalism)
137  wfDebug( __METHOD__ . " : skipping, already good\n" );
138  }
139 
140  // Preserve fragment (bug 14904)
141  $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
142  $currentDest->getFragment(), $newTitle->getInterwiki() );
143 
144  // Fix the text
145  $newContent = $content->updateRedirect( $newTitle );
146 
147  if ( $newContent->equals( $content ) ) {
148  $this->setLastError( 'Content unchanged???' );
149 
150  return false;
151  }
152 
153  $user = $this->getUser();
154  if ( !$user ) {
155  $this->setLastError( 'Invalid user' );
156 
157  return false;
158  }
159 
160  // Save it
161  global $wgUser;
162  $oldUser = $wgUser;
163  $wgUser = $user;
164  $article = WikiPage::factory( $this->title );
165 
166  // Messages: double-redirect-fixed-move, double-redirect-fixed-maintenance
167  $reason = wfMessage( 'double-redirect-fixed-' . $this->reason,
168  $this->redirTitle->getPrefixedText(), $newTitle->getPrefixedText()
169  )->inContentLanguage()->text();
170  $article->doEditContent( $newContent, $reason, EDIT_UPDATE | EDIT_SUPPRESS_RC, false, $user );
171  $wgUser = $oldUser;
172 
173  return true;
174  }
175 
183  public static function getFinalDestination( $title ) {
184  $dbw = wfGetDB( DB_MASTER );
185 
186  // Circular redirect check
187  $seenTitles = [];
188  $dest = false;
189 
190  while ( true ) {
191  $titleText = $title->getPrefixedDBkey();
192  if ( isset( $seenTitles[$titleText] ) ) {
193  wfDebug( __METHOD__, "Circular redirect detected, aborting\n" );
194 
195  return false;
196  }
197  $seenTitles[$titleText] = true;
198 
199  if ( $title->isExternal() ) {
200  // If the target is interwiki, we have to break early (bug 40352).
201  // Otherwise it will look up a row in the local page table
202  // with the namespace/page of the interwiki target which can cause
203  // unexpected results (e.g. X -> foo:Bar -> Bar -> .. )
204  break;
205  }
206 
207  $row = $dbw->selectRow(
208  [ 'redirect', 'page' ],
209  [ 'rd_namespace', 'rd_title', 'rd_interwiki' ],
210  [
211  'rd_from=page_id',
212  'page_namespace' => $title->getNamespace(),
213  'page_title' => $title->getDBkey()
214  ], __METHOD__ );
215  if ( !$row ) {
216  # No redirect from here, chain terminates
217  break;
218  } else {
219  $dest = $title = Title::makeTitle(
220  $row->rd_namespace,
221  $row->rd_title,
222  '',
223  $row->rd_interwiki
224  );
225  }
226  }
227 
228  return $dest;
229  }
230 
238  function getUser() {
239  if ( !self::$user ) {
240  $username = wfMessage( 'double-redirect-fixer' )->inContentLanguage()->text();
242  # User::newFromName() can return false on a badly configured wiki.
243  if ( self::$user && !self::$user->isLoggedIn() ) {
244  self::$user->addToDatabase();
245  }
246  }
247 
248  return self::$user;
249  }
250 }
static newFromName($name, $validate= 'valid')
Static factory method for creation from username.
Definition: User.php:568
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:99
static fixRedirects($reason, $redirTitle, $destTitle=false)
Insert jobs into the job queue to fix redirects to the given title.
wfGetDB($db, $groups=[], $wiki=false)
Get a Database object.
the array() calling protocol came about after MediaWiki 1.4rc1.
Class to both describe a background job and handle jobs.
Definition: Job.php:31
Using a hook running we can avoid having all this option specific stuff in our mainline code Using the function array $article
Definition: hooks.txt:78
getUser()
Get a user object for doing edits, from a request-lifetime cache False will be returned if the user n...
const EDIT_UPDATE
Definition: Defines.php:180
static newFromText($text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:277
Represents a title within MediaWiki.
Definition: Title.php:34
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
static newFromTitle(LinkTarget $linkTarget, $id=0, $flags=0)
Load either the current, or a specified, revision that's attached to a given link target...
Definition: Revision.php:117
wfDebug($text, $dest= 'all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
isExternal()
Is this Title interwiki?
Definition: Title.php:810
Title $redirTitle
The title which has changed, redirects pointing to this title are fixed.
getDBkey()
Get the main part with underscores.
Definition: Title.php:911
$res
Definition: database.txt:21
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock()-offset Set to overwrite offset parameter in $wgRequest set to ''to unsetoffset-wrap String Wrap the message in html(usually something like"&lt
const EDIT_SUPPRESS_RC
Definition: Defines.php:182
title
getNamespace()
Get the namespace index, i.e.
Definition: Title.php:934
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
Definition: distributors.txt:9
static & get($id)
Factory: creates an object representing an ID.
Definition: MagicWord.php:257
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition: hooks.txt:242
static singleton($wiki=false)
__construct(Title $title, array $params)
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
this hook is for auditing only or null if authentication failed before getting that far $username
Definition: hooks.txt:762
static getFinalDestination($title)
Get the final destination of a redirect.
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist e g Watchlist removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set and then return false from the hook function Ensure you consume the ChangeTagAfterDelete hook to carry out custom deletion actions as context called by AbstractContent::getParserOutput May be used to override the normal model specific rendering of page content $content
Definition: hooks.txt:1004
setLastError($error)
Definition: Job.php:391
string $reason
Reason for the change, 'maintenance' or 'move'.
array $params
Array of job parameters.
Definition: Job.php:36
const DB_MASTER
Definition: Defines.php:47
Title $title
Definition: Job.php:42
static & makeTitle($ns, $title, $fragment= '', $interwiki= '')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:524
getPrefixedDBkey()
Get the prefixed database key form.
Definition: Title.php:1437
$wgUser
Definition: Setup.php:794
Job to fix double redirects after moving a page.