Follow-up r92887: clear throttle count once the password is accepted as normal
[lhc/web/wiklou.git] / includes / job / DoubleRedirectJob.php
1 <?php
2 /**
3 * Job to fix double redirects after moving a page
4 *
5 * @file
6 * @ingroup JobQueue
7 */
8
9 /**
10 * Job to fix double redirects after moving a page
11 *
12 * @ingroup JobQueue
13 */
14 class DoubleRedirectJob extends Job {
15 var $reason, $redirTitle, $destTitleText;
16
17 /**
18 * @var User
19 */
20 static $user;
21
22 /**
23 * Insert jobs into the job queue to fix redirects to the given title
24 * @param $reason String: the reason for the fix, see message double-redirect-fixed-<reason>
25 * @param $redirTitle Title: the title which has changed, redirects pointing to this title are fixed
26 * @param $destTitle bool Not used
27 */
28 public static function fixRedirects( $reason, $redirTitle, $destTitle = false ) {
29 # Need to use the master to get the redirect table updated in the same transaction
30 $dbw = wfGetDB( DB_MASTER );
31 $res = $dbw->select(
32 array( 'redirect', 'page' ),
33 array( 'page_namespace', 'page_title' ),
34 array(
35 'page_id = rd_from',
36 'rd_namespace' => $redirTitle->getNamespace(),
37 'rd_title' => $redirTitle->getDBkey()
38 ), __METHOD__ );
39 if ( !$res->numRows() ) {
40 return;
41 }
42 $jobs = array();
43 foreach ( $res as $row ) {
44 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
45 if ( !$title ) {
46 continue;
47 }
48
49 $jobs[] = new self( $title, array(
50 'reason' => $reason,
51 'redirTitle' => $redirTitle->getPrefixedDBkey() ) );
52 # Avoid excessive memory usage
53 if ( count( $jobs ) > 10000 ) {
54 Job::batchInsert( $jobs );
55 $jobs = array();
56 }
57 }
58 Job::batchInsert( $jobs );
59 }
60
61 function __construct( $title, $params = false, $id = 0 ) {
62 parent::__construct( 'fixDoubleRedirect', $title, $params, $id );
63 $this->reason = $params['reason'];
64 $this->redirTitle = Title::newFromText( $params['redirTitle'] );
65 $this->destTitleText = !empty( $params['destTitle'] ) ? $params['destTitle'] : '';
66 }
67
68 function run() {
69 if ( !$this->redirTitle ) {
70 $this->setLastError( 'Invalid title' );
71 return false;
72 }
73
74 $targetRev = Revision::newFromTitle( $this->title );
75 if ( !$targetRev ) {
76 wfDebug( __METHOD__.": target redirect already deleted, ignoring\n" );
77 return true;
78 }
79 $text = $targetRev->getText();
80 $currentDest = Title::newFromRedirect( $text );
81 if ( !$currentDest || !$currentDest->equals( $this->redirTitle ) ) {
82 wfDebug( __METHOD__.": Redirect has changed since the job was queued\n" );
83 return true;
84 }
85
86 # Check for a suppression tag (used e.g. in periodically archived discussions)
87 $mw = MagicWord::get( 'staticredirect' );
88 if ( $mw->match( $text ) ) {
89 wfDebug( __METHOD__.": skipping: suppressed with __STATICREDIRECT__\n" );
90 return true;
91 }
92
93 # Find the current final destination
94 $newTitle = self::getFinalDestination( $this->redirTitle );
95 if ( !$newTitle ) {
96 wfDebug( __METHOD__.": skipping: single redirect, circular redirect or invalid redirect destination\n" );
97 return true;
98 }
99 if ( $newTitle->equals( $this->redirTitle ) ) {
100 # The redirect is already right, no need to change it
101 # This can happen if the page was moved back (say after vandalism)
102 wfDebug( __METHOD__.": skipping, already good\n" );
103 }
104
105 # Preserve fragment (bug 14904)
106 $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
107 $currentDest->getFragment() );
108
109 # Fix the text
110 # Remember that redirect pages can have categories, templates, etc.,
111 # so the regex has to be fairly general
112 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
113 '[[' . $newTitle->getFullText() . ']]',
114 $text, 1 );
115
116 if ( $newText === $text ) {
117 $this->setLastError( 'Text unchanged???' );
118 return false;
119 }
120
121 # Save it
122 global $wgUser;
123 $oldUser = $wgUser;
124 $wgUser = $this->getUser();
125 $article = new Article( $this->title );
126 $reason = wfMsgForContent( 'double-redirect-fixed-' . $this->reason,
127 $this->redirTitle->getPrefixedText(), $newTitle->getPrefixedText() );
128 $article->doEdit( $newText, $reason, EDIT_UPDATE | EDIT_SUPPRESS_RC );
129 $wgUser = $oldUser;
130
131 return true;
132 }
133
134 /**
135 * Get the final destination of a redirect
136 *
137 * @param $title Title
138 *
139 * @return false if the specified title is not a redirect, or if it is a circular redirect
140 */
141 public static function getFinalDestination( $title ) {
142 $dbw = wfGetDB( DB_MASTER );
143
144 $seenTitles = array(); # Circular redirect check
145 $dest = false;
146
147 while ( true ) {
148 $titleText = $title->getPrefixedDBkey();
149 if ( isset( $seenTitles[$titleText] ) ) {
150 wfDebug( __METHOD__, "Circular redirect detected, aborting\n" );
151 return false;
152 }
153 $seenTitles[$titleText] = true;
154
155 $row = $dbw->selectRow(
156 array( 'redirect', 'page' ),
157 array( 'rd_namespace', 'rd_title' ),
158 array(
159 'rd_from=page_id',
160 'page_namespace' => $title->getNamespace(),
161 'page_title' => $title->getDBkey()
162 ), __METHOD__ );
163 if ( !$row ) {
164 # No redirect from here, chain terminates
165 break;
166 } else {
167 $dest = $title = Title::makeTitle( $row->rd_namespace, $row->rd_title );
168 }
169 }
170 return $dest;
171 }
172
173 /**
174 * Get a user object for doing edits, from a request-lifetime cache
175 */
176 function getUser() {
177 if ( !self::$user ) {
178 self::$user = User::newFromName( wfMsgForContent( 'double-redirect-fixer' ), false );
179 if ( !self::$user->isLoggedIn() ) {
180 self::$user->addToDatabase();
181 }
182 }
183 return self::$user;
184 }
185 }
186