mirror of
https://gerrit.wikimedia.org/r/mediawiki/extensions/ReplaceText
synced 2024-11-15 10:59:28 +00:00
New class to hold MediaWiki job that does actual replacement
This commit is contained in:
parent
c690bf4627
commit
c1d278baf2
Notes:
Yaron Koren
2009-07-20 18:49:29 +00:00
54
ReplaceTextJob.php
Normal file
54
ReplaceTextJob.php
Normal file
|
@ -0,0 +1,54 @@
|
|||
<?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;
|
||||
}
|
||||
|
||||
$article = new Article($this->title);
|
||||
if ( !$article ) {
|
||||
$this->error = 'replaceText: Article not found "' . $this->title->getPrefixedDBkey() . '"';
|
||||
wfProfileOut( __METHOD__ );
|
||||
return false;
|
||||
}
|
||||
|
||||
wfProfileIn( __METHOD__.'-replace' );
|
||||
$article_text = $article->fetchContent();
|
||||
$target_str = $this->params['target_str'];
|
||||
$replacement_str = $this->params['replacement_str'];
|
||||
$num_matches;
|
||||
$new_text = str_replace($target_str, $replacement_str, $article_text, $num_matches);
|
||||
// if there's at least one replacement, modify the page,
|
||||
// using the passed-in edit summary
|
||||
if ($num_matches > 0) {
|
||||
global $wgUser;
|
||||
$wgUser = User::newFromId($this->params['user_id']);
|
||||
$edit_summary = $this->params['edit_summary'];
|
||||
$article->doEdit($new_text, $edit_summary);
|
||||
}
|
||||
wfProfileOut( __METHOD__.'-replace' );
|
||||
wfProfileOut( __METHOD__ );
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in a new issue