MediaWiki REL1_30
DoubleRedirectJob.php
Go to the documentation of this file.
1<?php
29class DoubleRedirectJob extends Job {
33 private $reason;
34
38 private $redirTitle;
39
41 private static $user;
42
47 function __construct( Title $title, array $params ) {
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 (T16904)
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();
171 $article->doEditContent( $newContent, $reason, $flags, false, $user );
172 $wgUser = $oldUser;
173
174 return true;
175 }
176
185 public static function getFinalDestination( $title ) {
186 $dbw = wfGetDB( DB_MASTER );
187
188 // Circular redirect check
189 $seenTitles = [];
190 $dest = false;
191
192 while ( true ) {
193 $titleText = $title->getPrefixedDBkey();
194 if ( isset( $seenTitles[$titleText] ) ) {
195 wfDebug( __METHOD__, "Circular redirect detected, aborting\n" );
196
197 return false;
198 }
199 $seenTitles[$titleText] = true;
200
201 if ( $title->isExternal() ) {
202 // If the target is interwiki, we have to break early (T42352).
203 // Otherwise it will look up a row in the local page table
204 // with the namespace/page of the interwiki target which can cause
205 // unexpected results (e.g. X -> foo:Bar -> Bar -> .. )
206 break;
207 }
208
209 $row = $dbw->selectRow(
210 [ 'redirect', 'page' ],
211 [ 'rd_namespace', 'rd_title', 'rd_interwiki' ],
212 [
213 'rd_from=page_id',
214 'page_namespace' => $title->getNamespace(),
215 'page_title' => $title->getDBkey()
216 ], __METHOD__ );
217 if ( !$row ) {
218 # No redirect from here, chain terminates
219 break;
220 } else {
221 $dest = $title = Title::makeTitle(
222 $row->rd_namespace,
223 $row->rd_title,
224 '',
225 $row->rd_interwiki
226 );
227 }
228 }
229
230 return $dest;
231 }
232
240 function getUser() {
241 if ( !self::$user ) {
242 $username = wfMessage( 'double-redirect-fixer' )->inContentLanguage()->text();
243 self::$user = User::newFromName( $username );
244 # User::newFromName() can return false on a badly configured wiki.
245 if ( self::$user && !self::$user->isLoggedIn() ) {
246 self::$user->addToDatabase();
247 }
248 }
249
250 return self::$user;
251 }
252}
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.
$wgUser
Definition Setup.php:817
Job to fix double redirects after moving a page.
static getFinalDestination( $title)
Get the final destination of a redirect.
getUser()
Get a user object for doing edits, from a request-lifetime cache False will be returned if the user n...
static fixRedirects( $reason, $redirTitle, $destTitle=false)
Insert jobs into the job queue to fix redirects to the given title.
__construct(Title $title, array $params)
Title $redirTitle
The title which has changed, redirects pointing to this title are fixed.
string $reason
Reason for the change, 'maintenance' or 'move'.
static singleton( $wiki=false)
Class to both describe a background job and handle jobs.
Definition Job.php:31
Title $title
Definition Job.php:42
setLastError( $error)
Definition Job.php:403
array $params
Array of job parameters.
Definition Job.php:36
static & get( $id)
Factory: creates an object representing an ID.
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:134
Represents a title within MediaWiki.
Definition Title.php:39
getNamespace()
Get the namespace index, i.e.
Definition Title.php:978
getPrefixedDBkey()
Get the prefixed database key form.
Definition Title.php:1538
getDBkey()
Get the main part with underscores.
Definition Title.php:955
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition WikiPage.php:121
$res
Definition database.txt:21
either a unescaped string or a HtmlArmor object 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 unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
it s the revision text itself In either if gzip is the revision text is gzipped $flags
Definition hooks.txt:2805
this hook is for auditing only or null if authentication failed before getting that far $username
Definition hooks.txt:783
const EDIT_INTERNAL
Definition Defines.php:160
const EDIT_UPDATE
Definition Defines.php:154
const EDIT_SUPPRESS_RC
Definition Defines.php:156
const DB_MASTER
Definition defines.php:26