Fix for r47482: put the RELEASE-NOTES entry in the "bug fixes" section and not the...
[mediawiki.git] / includes / DoubleRedirectJob.php
blob889beecfe7342326d4f12e5dc924caced546e065
1 <?php
3 class DoubleRedirectJob extends Job {
4 var $reason, $redirTitle, $destTitleText;
5 static $user;
7 /**
8 * Insert jobs into the job queue to fix redirects to the given title
9 * @param string $type The reason for the fix, see message double-redirect-fixed-<reason>
10 * @param Title $redirTitle The title which has changed, redirects pointing to this title are fixed
12 public static function fixRedirects( $reason, $redirTitle, $destTitle = false ) {
13 # Need to use the master to get the redirect table updated in the same transaction
14 $dbw = wfGetDB( DB_MASTER );
15 $res = $dbw->select(
16 array( 'redirect', 'page' ),
17 array( 'page_namespace', 'page_title' ),
18 array(
19 'page_id = rd_from',
20 'rd_namespace' => $redirTitle->getNamespace(),
21 'rd_title' => $redirTitle->getDBkey()
22 ), __METHOD__ );
23 if ( !$res->numRows() ) {
24 return;
26 $jobs = array();
27 foreach ( $res as $row ) {
28 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
29 if ( !$title ) {
30 continue;
33 $jobs[] = new self( $title, array(
34 'reason' => $reason,
35 'redirTitle' => $redirTitle->getPrefixedDBkey() ) );
36 # Avoid excessive memory usage
37 if ( count( $jobs ) > 10000 ) {
38 Job::batchInsert( $jobs );
39 $jobs = array();
42 Job::batchInsert( $jobs );
44 function __construct( $title, $params = false, $id = 0 ) {
45 parent::__construct( 'fixDoubleRedirect', $title, $params, $id );
46 $this->reason = $params['reason'];
47 $this->redirTitle = Title::newFromText( $params['redirTitle'] );
48 $this->destTitleText = !empty( $params['destTitle'] ) ? $params['destTitle'] : '';
51 function run() {
52 if ( !$this->redirTitle ) {
53 $this->setLastError( 'Invalid title' );
54 return false;
57 $targetRev = Revision::newFromTitle( $this->title );
58 if ( !$targetRev ) {
59 wfDebug( __METHOD__.": target redirect already deleted, ignoring\n" );
60 return true;
62 $text = $targetRev->getText();
63 $currentDest = Title::newFromRedirect( $text );
64 if ( !$currentDest || !$currentDest->equals( $this->redirTitle ) ) {
65 wfDebug( __METHOD__.": Redirect has changed since the job was queued\n" );
66 return true;
69 # Check for a suppression tag (used e.g. in periodically archived discussions)
70 $mw = MagicWord::get( 'staticredirect' );
71 if ( $mw->match( $text ) ) {
72 wfDebug( __METHOD__.": skipping: suppressed with __STATICREDIRECT__\n" );
73 return true;
76 # Find the current final destination
77 $newTitle = self::getFinalDestination( $this->redirTitle );
78 if ( !$newTitle ) {
79 wfDebug( __METHOD__.": skipping: single redirect, circular redirect or invalid redirect destination\n" );
80 return true;
82 if ( $newTitle->equals( $this->redirTitle ) ) {
83 # The redirect is already right, no need to change it
84 # This can happen if the page was moved back (say after vandalism)
85 wfDebug( __METHOD__.": skipping, already good\n" );
88 # Preserve fragment (bug 14904)
89 $newTitle = Title::makeTitle( $newTitle->getNamespace(), $newTitle->getDBkey(),
90 $currentDest->getFragment() );
92 # Fix the text
93 # Remember that redirect pages can have categories, templates, etc.,
94 # so the regex has to be fairly general
95 $newText = preg_replace( '/ \[ \[ [^\]]* \] \] /x',
96 '[[' . $newTitle->getFullText() . ']]',
97 $text, 1 );
99 if ( $newText === $text ) {
100 $this->setLastError( 'Text unchanged???' );
101 return false;
104 # Save it
105 global $wgUser;
106 $oldUser = $wgUser;
107 $wgUser = $this->getUser();
108 $article = new Article( $this->title );
109 $reason = wfMsgForContent( 'double-redirect-fixed-' . $this->reason,
110 $this->redirTitle->getPrefixedText(), $newTitle->getPrefixedText() );
111 $article->doEdit( $newText, $reason, EDIT_UPDATE | EDIT_SUPPRESS_RC );
112 $wgUser = $oldUser;
114 return true;
118 * Get the final destination of a redirect
119 * Returns false if the specified title is not a redirect, or if it is a circular redirect
121 public static function getFinalDestination( $title ) {
122 $dbw = wfGetDB( DB_MASTER );
124 $seenTitles = array(); # Circular redirect check
125 $dest = false;
127 while ( true ) {
128 $titleText = $title->getPrefixedDBkey();
129 if ( isset( $seenTitles[$titleText] ) ) {
130 wfDebug( __METHOD__, "Circular redirect detected, aborting\n" );
131 return false;
133 $seenTitles[$titleText] = true;
135 $row = $dbw->selectRow(
136 array( 'redirect', 'page' ),
137 array( 'rd_namespace', 'rd_title' ),
138 array(
139 'rd_from=page_id',
140 'page_namespace' => $title->getNamespace(),
141 'page_title' => $title->getDBkey()
142 ), __METHOD__ );
143 if ( !$row ) {
144 # No redirect from here, chain terminates
145 break;
146 } else {
147 $dest = $title = Title::makeTitle( $row->rd_namespace, $row->rd_title );
150 return $dest;
154 * Get a user object for doing edits, from a request-lifetime cache
156 function getUser() {
157 if ( !self::$user ) {
158 self::$user = User::newFromName( wfMsgForContent( 'double-redirect-fixer' ), false );
159 if ( !self::$user->isLoggedIn() ) {
160 self::$user->addToDatabase();
163 return self::$user;