3 * Job to fix double redirects after moving a page.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 use MediaWiki\MediaWikiServices
;
27 * Job to fix double redirects after moving a page
31 class DoubleRedirectJob
extends Job
{
32 /** @var string Reason for the change, 'maintenance' or 'move'. Suffix fo
33 * message key 'double-redirect-fixed-'.
37 /** @var Title The title which has changed, redirects pointing to this
47 * @param array $params
49 function __construct( Title
$title, array $params ) {
50 parent
::__construct( 'fixDoubleRedirect', $title, $params );
51 $this->reason
= $params['reason'];
52 $this->redirTitle
= Title
::newFromText( $params['redirTitle'] );
56 * Insert jobs into the job queue to fix redirects to the given title
57 * @param string $reason The reason for the fix, see message
58 * "double-redirect-fixed-<reason>"
59 * @param Title $redirTitle The title which has changed, redirects
60 * pointing to this title are fixed
61 * @param bool $destTitle Not used
63 public static function fixRedirects( $reason, $redirTitle, $destTitle = false ) {
64 # Need to use the master to get the redirect table updated in the same transaction
65 $dbw = wfGetDB( DB_MASTER
);
67 [ 'redirect', 'page' ],
68 [ 'page_namespace', 'page_title' ],
71 'rd_namespace' => $redirTitle->getNamespace(),
72 'rd_title' => $redirTitle->getDBkey()
74 if ( !$res->numRows() ) {
78 foreach ( $res as $row ) {
79 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
84 $jobs[] = new self( $title, [
86 'redirTitle' => $redirTitle->getPrefixedDBkey() ] );
87 # Avoid excessive memory usage
88 if ( count( $jobs ) > 10000 ) {
89 JobQueueGroup
::singleton()->push( $jobs );
93 JobQueueGroup
::singleton()->push( $jobs );
100 if ( !$this->redirTitle
) {
101 $this->setLastError( 'Invalid title' );
106 $targetRev = Revision
::newFromTitle( $this->title
, false, Revision
::READ_LATEST
);
108 wfDebug( __METHOD__
. ": target redirect already deleted, ignoring\n" );
112 $content = $targetRev->getContent();
113 $currentDest = $content ?
$content->getRedirectTarget() : null;
114 if ( !$currentDest ||
!$currentDest->equals( $this->redirTitle
) ) {
115 wfDebug( __METHOD__
. ": Redirect has changed since the job was queued\n" );
120 // Check for a suppression tag (used e.g. in periodically archived discussions)
121 $mw = MediaWikiServices
::getInstance()->getMagicWordFactory()->get( 'staticredirect' );
122 if ( $content->matchMagicWord( $mw ) ) {
123 wfDebug( __METHOD__
. ": skipping: suppressed with __STATICREDIRECT__\n" );
128 // Find the current final destination
129 $newTitle = self
::getFinalDestination( $this->redirTitle
);
131 wfDebug( __METHOD__
.
132 ": skipping: single redirect, circular redirect or invalid redirect destination\n" );
136 if ( $newTitle->equals( $this->redirTitle
) ) {
137 // The redirect is already right, no need to change it
138 // This can happen if the page was moved back (say after vandalism)
139 wfDebug( __METHOD__
. " : skipping, already good\n" );
142 // Preserve fragment (T16904)
143 $newTitle = Title
::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
144 $currentDest->getFragment(), $newTitle->getInterwiki() );
147 $newContent = $content->updateRedirect( $newTitle );
149 if ( $newContent->equals( $content ) ) {
150 $this->setLastError( 'Content unchanged???' );
155 $user = $this->getUser();
157 $this->setLastError( 'Invalid user' );
166 $article = WikiPage
::factory( $this->title
);
168 // Messages: double-redirect-fixed-move, double-redirect-fixed-maintenance
169 $reason = wfMessage( 'double-redirect-fixed-' . $this->reason
,
170 $this->redirTitle
->getPrefixedText(), $newTitle->getPrefixedText()
171 )->inContentLanguage()->text();
172 $flags = EDIT_UPDATE | EDIT_SUPPRESS_RC | EDIT_INTERNAL
;
173 $article->doEditContent( $newContent, $reason, $flags, false, $user );
180 * Get the final destination of a redirect
182 * @param Title $title
184 * @return Title|bool The final Title after following all redirects, or false if
185 * the page is not a redirect or the redirect loops.
187 public static function getFinalDestination( $title ) {
188 $dbw = wfGetDB( DB_MASTER
);
190 // Circular redirect check
195 $titleText = $title->getPrefixedDBkey();
196 if ( isset( $seenTitles[$titleText] ) ) {
197 wfDebug( __METHOD__
, "Circular redirect detected, aborting\n" );
201 $seenTitles[$titleText] = true;
203 if ( $title->isExternal() ) {
204 // If the target is interwiki, we have to break early (T42352).
205 // Otherwise it will look up a row in the local page table
206 // with the namespace/page of the interwiki target which can cause
207 // unexpected results (e.g. X -> foo:Bar -> Bar -> .. )
211 $row = $dbw->selectRow(
212 [ 'redirect', 'page' ],
213 [ 'rd_namespace', 'rd_title', 'rd_interwiki' ],
216 'page_namespace' => $title->getNamespace(),
217 'page_title' => $title->getDBkey()
220 # No redirect from here, chain terminates
223 $dest = $title = Title
::makeTitle(
236 * Get a user object for doing edits, from a request-lifetime cache
237 * False will be returned if the user name specified in the
238 * 'double-redirect-fixer' message is invalid.
243 if ( !self
::$user ) {
244 $username = wfMessage( 'double-redirect-fixer' )->inContentLanguage()->text();
245 self
::$user = User
::newFromName( $username );
246 # User::newFromName() can return false on a badly configured wiki.
247 if ( self
::$user && !self
::$user->isLoggedIn() ) {
248 self
::$user->addToDatabase();