Merge branch 'master' of ssh://gerrit.wikimedia.org:29418/mediawiki/core into Wikidata
[lhc/web/wiklou.git] / includes / job / DoubleRedirectJob.php
1 <?php
2 /**
3 * Job to fix double redirects after moving a page.
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 * @ingroup JobQueue
22 */
23
24 /**
25 * Job to fix double redirects after moving a page
26 *
27 * @ingroup JobQueue
28 */
29 class DoubleRedirectJob extends Job {
30 var $reason, $redirTitle, $destTitleText;
31
32 /**
33 * @var User
34 */
35 static $user;
36
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
42 */
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;
56 }
57 $jobs = array();
58 foreach ( $res as $row ) {
59 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
60 if ( !$title ) {
61 continue;
62 }
63
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();
71 }
72 }
73 Job::batchInsert( $jobs );
74 }
75
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'] : '';
81 }
82
83 /**
84 * @return bool
85 */
86 function run() {
87 if ( !$this->redirTitle ) {
88 $this->setLastError( 'Invalid title' );
89 return false;
90 }
91
92 $targetRev = Revision::newFromTitle( $this->title );
93 if ( !$targetRev ) {
94 wfDebug( __METHOD__.": target redirect already deleted, ignoring\n" );
95 return true;
96 }
97 $content = $targetRev->getContent();
98 $currentDest = $content->getRedirectTarget();
99 if ( !$currentDest || !$currentDest->equals( $this->redirTitle ) ) {
100 wfDebug( __METHOD__.": Redirect has changed since the job was queued\n" );
101 return true;
102 }
103
104 # Check for a suppression tag (used e.g. in periodically archived discussions)
105 $text = ContentHandler::getContentText( $content );
106 $mw = MagicWord::get( 'staticredirect' );
107 if ( $mw->match( $text ) ) { #FIXME: add support for this to ContentHandler/Content
108 wfDebug( __METHOD__.": skipping: suppressed with __STATICREDIRECT__\n" );
109 return true;
110 }
111
112 # Find the current final destination
113 $newTitle = self::getFinalDestination( $this->redirTitle );
114 if ( !$newTitle ) {
115 wfDebug( __METHOD__.": skipping: single redirect, circular redirect or invalid redirect destination\n" );
116 return true;
117 }
118 if ( $newTitle->equals( $this->redirTitle ) ) {
119 # The redirect is already right, no need to change it
120 # This can happen if the page was moved back (say after vandalism)
121 wfDebug( __METHOD__.": skipping, already good\n" );
122 }
123
124 # Preserve fragment (bug 14904)
125 $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
126 $currentDest->getFragment() );
127
128 # Fix the text
129 # Remember that redirect pages can have categories, templates, etc.,
130 # so the regex has to be fairly general
131 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
132 '[[' . $newTitle->getFullText() . ']]',
133 $text, 1 ); #FIXME: need a way to do this via ContentHandler!
134
135 if ( $newText === $text ) {
136 $this->setLastError( 'Text unchanged???' );
137 return false;
138 }
139
140 # Save it
141 global $wgUser;
142 $oldUser = $wgUser;
143 $wgUser = $this->getUser();
144 $article = WikiPage::factory( $this->title );
145 $reason = wfMsgForContent( 'double-redirect-fixed-' . $this->reason,
146 $this->redirTitle->getPrefixedText(), $newTitle->getPrefixedText() );
147 $article->doEdit( $newText, $reason, EDIT_UPDATE | EDIT_SUPPRESS_RC, false, $this->getUser() );
148 $wgUser = $oldUser;
149
150 return true;
151 }
152
153 /**
154 * Get the final destination of a redirect
155 *
156 * @param $title Title
157 *
158 * @return bool if the specified title is not a redirect, or if it is a circular redirect
159 */
160 public static function getFinalDestination( $title ) {
161 $dbw = wfGetDB( DB_MASTER );
162
163 $seenTitles = array(); # Circular redirect check
164 $dest = false;
165
166 while ( true ) {
167 $titleText = $title->getPrefixedDBkey();
168 if ( isset( $seenTitles[$titleText] ) ) {
169 wfDebug( __METHOD__, "Circular redirect detected, aborting\n" );
170 return false;
171 }
172 $seenTitles[$titleText] = true;
173
174 $row = $dbw->selectRow(
175 array( 'redirect', 'page' ),
176 array( 'rd_namespace', 'rd_title' ),
177 array(
178 'rd_from=page_id',
179 'page_namespace' => $title->getNamespace(),
180 'page_title' => $title->getDBkey()
181 ), __METHOD__ );
182 if ( !$row ) {
183 # No redirect from here, chain terminates
184 break;
185 } else {
186 $dest = $title = Title::makeTitle( $row->rd_namespace, $row->rd_title );
187 }
188 }
189 return $dest;
190 }
191
192 /**
193 * Get a user object for doing edits, from a request-lifetime cache
194 * @return User
195 */
196 function getUser() {
197 if ( !self::$user ) {
198 self::$user = User::newFromName( wfMsgForContent( 'double-redirect-fixer' ), false );
199 # FIXME: newFromName could return false on a badly configured wiki.
200 if ( !self::$user->isLoggedIn() ) {
201 self::$user->addToDatabase();
202 }
203 }
204 return self::$user;
205 }
206 }
207