Add 2 new options to importImages.php
[mediawiki.git] / includes / job / DoubleRedirectJob.php
blobf9c4b0fff29a6ffbf70f1147ff3c23bd963a941e
1 <?php
2 /**
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
20 * @file
21 * @ingroup JobQueue
24 /**
25 * Job to fix double redirects after moving a page
27 * @ingroup JobQueue
29 class DoubleRedirectJob extends Job {
30 var $reason, $redirTitle;
32 /**
33 * @var User
35 static $user;
37 /**
38 * Insert jobs into the job queue to fix redirects to the given title
39 * @param $reason String: the reason for the fix, see message "double-redirect-fixed-<reason>"
40 * @param $redirTitle Title: the title which has changed, redirects pointing to this title are fixed
41 * @param $destTitle bool Not used
43 public static function fixRedirects( $reason, $redirTitle, $destTitle = false ) {
44 # Need to use the master to get the redirect table updated in the same transaction
45 $dbw = wfGetDB( DB_MASTER );
46 $res = $dbw->select(
47 array( 'redirect', 'page' ),
48 array( 'page_namespace', 'page_title' ),
49 array(
50 'page_id = rd_from',
51 'rd_namespace' => $redirTitle->getNamespace(),
52 'rd_title' => $redirTitle->getDBkey()
53 ), __METHOD__ );
54 if ( !$res->numRows() ) {
55 return;
57 $jobs = array();
58 foreach ( $res as $row ) {
59 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
60 if ( !$title ) {
61 continue;
64 $jobs[] = new self( $title, array(
65 'reason' => $reason,
66 'redirTitle' => $redirTitle->getPrefixedDBkey() ) );
67 # Avoid excessive memory usage
68 if ( count( $jobs ) > 10000 ) {
69 Job::batchInsert( $jobs );
70 $jobs = array();
73 Job::batchInsert( $jobs );
76 function __construct( $title, $params = false, $id = 0 ) {
77 parent::__construct( 'fixDoubleRedirect', $title, $params, $id );
78 $this->reason = $params['reason'];
79 $this->redirTitle = Title::newFromText( $params['redirTitle'] );
82 /**
83 * @return bool
85 function run() {
86 if ( !$this->redirTitle ) {
87 $this->setLastError( 'Invalid title' );
88 return false;
91 $targetRev = Revision::newFromTitle( $this->title, false, Revision::READ_LATEST );
92 if ( !$targetRev ) {
93 wfDebug( __METHOD__.": target redirect already deleted, ignoring\n" );
94 return true;
96 $text = $targetRev->getText();
97 $currentDest = Title::newFromRedirect( $text );
98 if ( !$currentDest || !$currentDest->equals( $this->redirTitle ) ) {
99 wfDebug( __METHOD__.": Redirect has changed since the job was queued\n" );
100 return true;
103 # Check for a suppression tag (used e.g. in periodically archived discussions)
104 $mw = MagicWord::get( 'staticredirect' );
105 if ( $mw->match( $text ) ) {
106 wfDebug( __METHOD__.": skipping: suppressed with __STATICREDIRECT__\n" );
107 return true;
110 # Find the current final destination
111 $newTitle = self::getFinalDestination( $this->redirTitle );
112 if ( !$newTitle ) {
113 wfDebug( __METHOD__.": skipping: single redirect, circular redirect or invalid redirect destination\n" );
114 return true;
116 if ( $newTitle->equals( $this->redirTitle ) ) {
117 # The redirect is already right, no need to change it
118 # This can happen if the page was moved back (say after vandalism)
119 wfDebug( __METHOD__.": skipping, already good\n" );
122 # Preserve fragment (bug 14904)
123 $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
124 $currentDest->getFragment(), $newTitle->getInterwiki() );
126 # Fix the text
127 # Remember that redirect pages can have categories, templates, etc.,
128 # so the regex has to be fairly general
129 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
130 '[[' . $newTitle->getFullText() . ']]',
131 $text, 1 );
133 if ( $newText === $text ) {
134 $this->setLastError( 'Text unchanged???' );
135 return false;
138 # Save it
139 global $wgUser;
140 $oldUser = $wgUser;
141 $wgUser = $this->getUser();
142 $article = WikiPage::factory( $this->title );
143 $reason = wfMessage( 'double-redirect-fixed-' . $this->reason,
144 $this->redirTitle->getPrefixedText(), $newTitle->getPrefixedText()
145 )->inContentLanguage()->text();
146 $article->doEdit( $newText, $reason, EDIT_UPDATE | EDIT_SUPPRESS_RC, false, $this->getUser() );
147 $wgUser = $oldUser;
149 return true;
153 * Get the final destination of a redirect
155 * @param $title Title
157 * @return bool if the specified title is not a redirect, or if it is a circular redirect
159 public static function getFinalDestination( $title ) {
160 $dbw = wfGetDB( DB_MASTER );
162 $seenTitles = array(); # Circular redirect check
163 $dest = false;
165 while ( true ) {
166 $titleText = $title->getPrefixedDBkey();
167 if ( isset( $seenTitles[$titleText] ) ) {
168 wfDebug( __METHOD__, "Circular redirect detected, aborting\n" );
169 return false;
171 $seenTitles[$titleText] = true;
173 if ( $title->getInterwiki() ) {
174 // If the target is interwiki, we have to break early (bug 40352).
175 // Otherwise it will look up a row in the local page table
176 // with the namespace/page of the interwiki target which can cause
177 // unexpected results (e.g. X -> foo:Bar -> Bar -> .. )
178 break;
181 $row = $dbw->selectRow(
182 array( 'redirect', 'page' ),
183 array( 'rd_namespace', 'rd_title', 'rd_interwiki' ),
184 array(
185 'rd_from=page_id',
186 'page_namespace' => $title->getNamespace(),
187 'page_title' => $title->getDBkey()
188 ), __METHOD__ );
189 if ( !$row ) {
190 # No redirect from here, chain terminates
191 break;
192 } else {
193 $dest = $title = Title::makeTitle( $row->rd_namespace, $row->rd_title, '', $row->rd_interwiki );
196 return $dest;
200 * Get a user object for doing edits, from a request-lifetime cache
201 * @return User
203 function getUser() {
204 if ( !self::$user ) {
205 self::$user = User::newFromName( wfMessage( 'double-redirect-fixer' )->inContentLanguage()->text(), false );
206 # FIXME: newFromName could return false on a badly configured wiki.
207 if ( !self::$user->isLoggedIn() ) {
208 self::$user->addToDatabase();
211 return self::$user;