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
25 * Job to fix double redirects after moving a page
29 class DoubleRedirectJob
extends Job
{
30 var $reason, $redirTitle, $destTitleText;
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
);
47 array( 'redirect', 'page' ),
48 array( 'page_namespace', 'page_title' ),
51 'rd_namespace' => $redirTitle->getNamespace(),
52 'rd_title' => $redirTitle->getDBkey()
54 if ( !$res->numRows() ) {
58 foreach ( $res as $row ) {
59 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
64 $jobs[] = new self( $title, array(
66 'redirTitle' => $redirTitle->getPrefixedDBkey() ) );
67 # Avoid excessive memory usage
68 if ( count( $jobs ) > 10000 ) {
69 Job
::batchInsert( $jobs );
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'] );
80 $this->destTitleText
= !empty( $params['destTitle'] ) ?
$params['destTitle'] : '';
87 if ( !$this->redirTitle
) {
88 $this->setLastError( 'Invalid title' );
92 $targetRev = Revision
::newFromTitle( $this->title
);
94 wfDebug( __METHOD__
.": target redirect already deleted, ignoring\n" );
97 $text = $targetRev->getText();
98 $currentDest = Title
::newFromRedirect( $text );
99 if ( !$currentDest ||
!$currentDest->equals( $this->redirTitle
) ) {
100 wfDebug( __METHOD__
.": Redirect has changed since the job was queued\n" );
104 # Check for a suppression tag (used e.g. in periodically archived discussions)
105 $mw = MagicWord
::get( 'staticredirect' );
106 if ( $mw->match( $text ) ) {
107 wfDebug( __METHOD__
.": skipping: suppressed with __STATICREDIRECT__\n" );
111 # Find the current final destination
112 $newTitle = self
::getFinalDestination( $this->redirTitle
);
114 wfDebug( __METHOD__
.": skipping: single redirect, circular redirect or invalid redirect destination\n" );
117 if ( $newTitle->equals( $this->redirTitle
) ) {
118 # The redirect is already right, no need to change it
119 # This can happen if the page was moved back (say after vandalism)
120 wfDebug( __METHOD__
.": skipping, already good\n" );
123 # Preserve fragment (bug 14904)
124 $newTitle = Title
::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
125 $currentDest->getFragment() );
128 # Remember that redirect pages can have categories, templates, etc.,
129 # so the regex has to be fairly general
130 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
131 '[[' . $newTitle->getFullText() . ']]',
134 if ( $newText === $text ) {
135 $this->setLastError( 'Text unchanged???' );
142 $wgUser = $this->getUser();
143 $article = WikiPage
::factory( $this->title
);
144 $reason = wfMsgForContent( 'double-redirect-fixed-' . $this->reason
,
145 $this->redirTitle
->getPrefixedText(), $newTitle->getPrefixedText() );
146 $article->doEdit( $newText, $reason, EDIT_UPDATE | EDIT_SUPPRESS_RC
, false, $this->getUser() );
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
166 $titleText = $title->getPrefixedDBkey();
167 if ( isset( $seenTitles[$titleText] ) ) {
168 wfDebug( __METHOD__
, "Circular redirect detected, aborting\n" );
171 $seenTitles[$titleText] = true;
173 $row = $dbw->selectRow(
174 array( 'redirect', 'page' ),
175 array( 'rd_namespace', 'rd_title' ),
178 'page_namespace' => $title->getNamespace(),
179 'page_title' => $title->getDBkey()
182 # No redirect from here, chain terminates
185 $dest = $title = Title
::makeTitle( $row->rd_namespace
, $row->rd_title
);
192 * Get a user object for doing edits, from a request-lifetime cache
196 if ( !self
::$user ) {
197 self
::$user = User
::newFromName( wfMsgForContent( 'double-redirect-fixer' ), false );
198 # FIXME: newFromName could return false on a badly configured wiki.
199 if ( !self
::$user->isLoggedIn() ) {
200 self
::$user->addToDatabase();