2008-05-12 20:34:54 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Background job to replace text in a given page
|
|
|
|
* - based on /includes/RefreshLinksJob.php
|
|
|
|
*
|
|
|
|
* @author Yaron Koren
|
|
|
|
*/
|
|
|
|
class ReplaceTextJob extends Job {
|
|
|
|
|
|
|
|
function __construct( $title, $params = '', $id = 0 ) {
|
|
|
|
parent::__construct( 'replaceText', $title, $params, $id );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run a replaceText job
|
|
|
|
* @return boolean success
|
|
|
|
*/
|
|
|
|
function run() {
|
|
|
|
wfProfileIn( __METHOD__ );
|
|
|
|
|
|
|
|
if ( is_null( $this->title ) ) {
|
|
|
|
$this->error = "replaceText: Invalid title";
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-27 09:02:48 +00:00
|
|
|
if ( array_key_exists( 'move_page', $this->params ) ) {
|
2008-05-12 20:34:54 +00:00
|
|
|
global $wgUser;
|
2008-08-06 16:11:56 +00:00
|
|
|
$actual_user = $wgUser;
|
2009-04-27 09:02:48 +00:00
|
|
|
$wgUser = User::newFromId( $this->params['user_id'] );
|
2008-11-05 20:33:38 +00:00
|
|
|
$cur_page_name = $this->title->getText();
|
2009-04-27 09:02:48 +00:00
|
|
|
$new_page_name = str_replace( $this->params['target_str'], $this->params['replacement_str'], $cur_page_name );
|
|
|
|
$new_title = Title::newFromText( $new_page_name, $this->title->getNamespace() );
|
2008-11-05 20:33:38 +00:00
|
|
|
$reason = $this->params['edit_summary'];
|
2009-01-22 23:41:02 +00:00
|
|
|
$create_redirect = $this->params['create_redirect'];
|
2009-04-27 09:02:48 +00:00
|
|
|
$this->title->moveTo( $new_title, true, $reason, $create_redirect );
|
2008-08-06 16:11:56 +00:00
|
|
|
$wgUser = $actual_user;
|
2008-11-05 20:33:38 +00:00
|
|
|
} else {
|
2009-04-27 09:02:48 +00:00
|
|
|
$article = new Article( $this->title );
|
2008-11-05 20:33:38 +00:00
|
|
|
if ( !$article ) {
|
|
|
|
$this->error = 'replaceText: Article not found "' . $this->title->getPrefixedDBkey() . '"';
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-04-27 09:02:48 +00:00
|
|
|
wfProfileIn( __METHOD__ . '-replace' );
|
2008-11-05 20:33:38 +00:00
|
|
|
$article_text = $article->fetchContent();
|
|
|
|
$target_str = $this->params['target_str'];
|
|
|
|
$replacement_str = $this->params['replacement_str'];
|
|
|
|
$num_matches;
|
2009-04-27 09:02:48 +00:00
|
|
|
$new_text = str_replace( $target_str, $replacement_str, $article_text, $num_matches );
|
2008-11-05 20:33:38 +00:00
|
|
|
// if there's at least one replacement, modify the page,
|
|
|
|
// using the passed-in edit summary
|
2009-04-27 09:02:48 +00:00
|
|
|
if ( $num_matches > 0 ) {
|
2008-11-05 20:33:38 +00:00
|
|
|
// change global $wgUser variable to the one
|
|
|
|
// specified by the job only for the extent of this
|
|
|
|
// replacement
|
|
|
|
global $wgUser;
|
|
|
|
$actual_user = $wgUser;
|
2009-04-27 09:02:48 +00:00
|
|
|
$wgUser = User::newFromId( $this->params['user_id'] );
|
2008-11-05 20:33:38 +00:00
|
|
|
$edit_summary = $this->params['edit_summary'];
|
|
|
|
$flags = EDIT_MINOR;
|
2009-04-27 09:02:48 +00:00
|
|
|
$article->doEdit( $new_text, $edit_summary, $flags );
|
2008-11-05 20:33:38 +00:00
|
|
|
$wgUser = $actual_user;
|
|
|
|
}
|
2008-05-12 20:34:54 +00:00
|
|
|
}
|
2009-04-27 09:02:48 +00:00
|
|
|
wfProfileOut( __METHOD__ . '-replace' );
|
2008-05-12 20:34:54 +00:00
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|