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