991b2eb716fabb3a3d0a0d3905ffbd0a2843ca37
[lhc/web/wiklou.git] / includes / job / jobs / 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;
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 string $reason 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 bool $destTitle 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 JobQueueGroup::singleton()->push( $jobs );
70 $jobs = array();
71 }
72 }
73 JobQueueGroup::singleton()->push( $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 }
81
82 /**
83 * @return bool
84 */
85 function run() {
86 if ( !$this->redirTitle ) {
87 $this->setLastError( 'Invalid title' );
88
89 return false;
90 }
91
92 $targetRev = Revision::newFromTitle( $this->title, false, Revision::READ_LATEST );
93 if ( !$targetRev ) {
94 wfDebug( __METHOD__ . ": target redirect already deleted, ignoring\n" );
95
96 return true;
97 }
98 $content = $targetRev->getContent();
99 $currentDest = $content ? $content->getRedirectTarget() : null;
100 if ( !$currentDest || !$currentDest->equals( $this->redirTitle ) ) {
101 wfDebug( __METHOD__ . ": Redirect has changed since the job was queued\n" );
102
103 return true;
104 }
105
106 // Check for a suppression tag (used e.g. in periodically archived discussions)
107 $mw = MagicWord::get( 'staticredirect' );
108 if ( $content->matchMagicWord( $mw ) ) {
109 wfDebug( __METHOD__ . ": skipping: suppressed with __STATICREDIRECT__\n" );
110
111 return true;
112 }
113
114 // Find the current final destination
115 $newTitle = self::getFinalDestination( $this->redirTitle );
116 if ( !$newTitle ) {
117 wfDebug( __METHOD__ . ": skipping: single redirect, circular redirect or invalid redirect destination\n" );
118
119 return true;
120 }
121 if ( $newTitle->equals( $this->redirTitle ) ) {
122 // The redirect is already right, no need to change it
123 // This can happen if the page was moved back (say after vandalism)
124 wfDebug( __METHOD__ . " : skipping, already good\n" );
125 }
126
127 // Preserve fragment (bug 14904)
128 $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
129 $currentDest->getFragment(), $newTitle->getInterwiki() );
130
131 // Fix the text
132 $newContent = $content->updateRedirect( $newTitle );
133
134 if ( $newContent->equals( $content ) ) {
135 $this->setLastError( 'Content unchanged???' );
136
137 return false;
138 }
139
140 $user = $this->getUser();
141 if ( !$user ) {
142 $this->setLastError( 'Invalid user' );
143
144 return false;
145 }
146
147 // Save it
148 global $wgUser;
149 $oldUser = $wgUser;
150 $wgUser = $user;
151 $article = WikiPage::factory( $this->title );
152
153 // Messages: double-redirect-fixed-move, double-redirect-fixed-maintenance
154 $reason = wfMessage( 'double-redirect-fixed-' . $this->reason,
155 $this->redirTitle->getPrefixedText(), $newTitle->getPrefixedText()
156 )->inContentLanguage()->text();
157 $article->doEditContent( $newContent, $reason, EDIT_UPDATE | EDIT_SUPPRESS_RC, false, $user );
158 $wgUser = $oldUser;
159
160 return true;
161 }
162
163 /**
164 * Get the final destination of a redirect
165 *
166 * @param $title Title
167 *
168 * @return bool if the specified title is not a redirect, or if it is a circular redirect
169 */
170 public static function getFinalDestination( $title ) {
171 $dbw = wfGetDB( DB_MASTER );
172
173 // Circular redirect check
174 $seenTitles = array();
175 $dest = false;
176
177 while ( true ) {
178 $titleText = $title->getPrefixedDBkey();
179 if ( isset( $seenTitles[$titleText] ) ) {
180 wfDebug( __METHOD__, "Circular redirect detected, aborting\n" );
181
182 return false;
183 }
184 $seenTitles[$titleText] = true;
185
186 if ( $title->getInterwiki() ) {
187 // If the target is interwiki, we have to break early (bug 40352).
188 // Otherwise it will look up a row in the local page table
189 // with the namespace/page of the interwiki target which can cause
190 // unexpected results (e.g. X -> foo:Bar -> Bar -> .. )
191 break;
192 }
193
194 $row = $dbw->selectRow(
195 array( 'redirect', 'page' ),
196 array( 'rd_namespace', 'rd_title', 'rd_interwiki' ),
197 array(
198 'rd_from=page_id',
199 'page_namespace' => $title->getNamespace(),
200 'page_title' => $title->getDBkey()
201 ), __METHOD__ );
202 if ( !$row ) {
203 # No redirect from here, chain terminates
204 break;
205 } else {
206 $dest = $title = Title::makeTitle( $row->rd_namespace, $row->rd_title, '', $row->rd_interwiki );
207 }
208 }
209
210 return $dest;
211 }
212
213 /**
214 * Get a user object for doing edits, from a request-lifetime cache
215 * False will be returned if the user name specified in the
216 * 'double-redirect-fixer' message is invalid.
217 *
218 * @return User|bool
219 */
220 function getUser() {
221 if ( !self::$user ) {
222 self::$user = User::newFromName( wfMessage( 'double-redirect-fixer' )->inContentLanguage()->text() );
223 # User::newFromName() can return false on a badly configured wiki.
224 if ( self::$user && !self::$user->isLoggedIn() ) {
225 self::$user->addToDatabase();
226 }
227 }
228
229 return self::$user;
230 }
231 }