2011-02-16 14:40:34 +00:00
|
|
|
<?php
|
2008-05-12 20:34:54 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Background job to replace text in a given page
|
|
|
|
* - based on /includes/RefreshLinksJob.php
|
|
|
|
*
|
|
|
|
* @author Yaron Koren
|
2011-02-16 03:04:27 +00:00
|
|
|
* @author Ankit Garg
|
2008-05-12 20:34:54 +00:00
|
|
|
*/
|
|
|
|
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();
|
2011-02-16 03:04:27 +00:00
|
|
|
if ( $this->params['use_regex'] ) {
|
|
|
|
$new_page_name = preg_replace( "/".$this->params['target_str']."/U", $this->params['replacement_str'], $cur_page_name );
|
|
|
|
} else {
|
|
|
|
$new_page_name = str_replace( $this->params['target_str'], $this->params['replacement_str'], $cur_page_name );
|
|
|
|
}
|
|
|
|
|
2009-04-27 09:02:48 +00:00
|
|
|
$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 );
|
2010-03-14 18:10:47 +00:00
|
|
|
if ( $this->params['watch_page'] ) {
|
2011-06-06 15:15:43 +00:00
|
|
|
if ( class_exists( 'WatchAction' ) ) {
|
|
|
|
// Class was added in MW 1.19
|
|
|
|
WatchAction::doWatch( $new_title, $wgUser );
|
2011-06-14 23:24:08 +00:00
|
|
|
} else {
|
2012-10-07 10:56:07 +00:00
|
|
|
Action::factory( 'watch', new WikiPage( $new_title ) )->execute();
|
2011-06-06 15:15:43 +00:00
|
|
|
}
|
2009-04-27 18:30:39 +00:00
|
|
|
}
|
2008-08-06 16:11:56 +00:00
|
|
|
$wgUser = $actual_user;
|
2008-11-05 20:33:38 +00:00
|
|
|
} else {
|
2013-01-08 21:56:16 +00:00
|
|
|
// WikiPage::getContent() replaced
|
|
|
|
// Article::fetchContent() starting in MW 1.21.
|
|
|
|
if ( method_exists( 'WikiPage', 'getContent' ) ) {
|
|
|
|
if ( $this->title->getContentModel() !== CONTENT_MODEL_WIKITEXT ) {
|
|
|
|
$this->error = 'replaceText: Wiki page "' . $this->title->getPrefixedDBkey() . '" does not hold regular wikitext.';
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$wikiPage = new WikiPage( $this->title );
|
|
|
|
// Is this check necessary?
|
|
|
|
if ( !$wikiPage ) {
|
|
|
|
$this->error = 'replaceText: Wiki page not found for "' . $this->title->getPrefixedDBkey() . '."';
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return false;
|
|
|
|
}
|
2013-08-19 22:20:44 +00:00
|
|
|
$wikiPageContent = $wikiPage->getContent();
|
|
|
|
if ( is_null( $wikiPageContent ) ) {
|
|
|
|
$this->error = 'replaceText: No contents found for wiki page at "' . $this->title->getPrefixedDBkey() . '."';
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$article_text = $wikiPageContent->getNativeData();
|
2013-01-08 21:56:16 +00:00
|
|
|
} else {
|
|
|
|
$article = new Article( $this->title, 0 );
|
|
|
|
if ( !$article ) {
|
|
|
|
$this->error = 'replaceText: Article not found for "' . $this->title->getPrefixedDBkey() . '"';
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$article_text = $article->fetchContent();
|
2008-11-05 20:33:38 +00:00
|
|
|
}
|
|
|
|
|
2009-04-27 09:02:48 +00:00
|
|
|
wfProfileIn( __METHOD__ . '-replace' );
|
2008-11-05 20:33:38 +00:00
|
|
|
$target_str = $this->params['target_str'];
|
|
|
|
$replacement_str = $this->params['replacement_str'];
|
2014-07-23 00:31:36 +00:00
|
|
|
$num_matches = 0;
|
2011-02-16 03:04:27 +00:00
|
|
|
|
|
|
|
if ( $this->params['use_regex'] ) {
|
2014-07-23 00:31:36 +00:00
|
|
|
$new_text = preg_replace( '/' . $target_str . '/U', $replacement_str, $article_text, -1, $num_matches );
|
2011-02-16 03:04:27 +00:00
|
|
|
} else {
|
|
|
|
$new_text = str_replace( $target_str, $replacement_str, $article_text, $num_matches );
|
|
|
|
}
|
|
|
|
|
2013-01-08 21:56:16 +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 ) {
|
2013-01-08 21:56:16 +00:00
|
|
|
// Change global $wgUser variable to the one
|
2009-05-19 15:22:27 +00:00
|
|
|
// specified by the job only for the extent of
|
2013-01-08 21:56:16 +00:00
|
|
|
// this replacement.
|
2008-11-05 20:33:38 +00:00
|
|
|
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'];
|
2009-08-14 17:21:55 +00:00
|
|
|
$flags = EDIT_MINOR;
|
2012-11-06 02:08:00 +00:00
|
|
|
if ( $wgUser->isAllowed( 'bot' ) ) {
|
2009-08-14 17:21:55 +00:00
|
|
|
$flags |= EDIT_FORCE_BOT;
|
2012-11-06 02:08:00 +00:00
|
|
|
}
|
2013-01-08 21:56:16 +00:00
|
|
|
if ( method_exists( 'WikiPage', 'getContent' ) ) {
|
|
|
|
$new_content = new WikitextContent( $new_text );
|
|
|
|
$wikiPage->doEditContent( $new_content, $edit_summary, $flags );
|
|
|
|
} else {
|
|
|
|
$article->doEdit( $new_text, $edit_summary, $flags );
|
|
|
|
}
|
2008-11-05 20:33:38 +00:00
|
|
|
$wgUser = $actual_user;
|
|
|
|
}
|
2009-12-31 18:08:24 +00:00
|
|
|
wfProfileOut( __METHOD__ . '-replace' );
|
2008-05-12 20:34:54 +00:00
|
|
|
}
|
|
|
|
wfProfileOut( __METHOD__ );
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|