Skip to content

Commit

Permalink
Added support for new ContentHandler approach in MediaWiki 1.21
Browse files Browse the repository at this point in the history
Change-Id: Id96a973dbd0b7fb5c49667ac1615d171dd724ade
  • Loading branch information
yaronkoren committed Jan 8, 2013
1 parent db6e310 commit 38f8df0
Showing 1 changed file with 34 additions and 11 deletions.
45 changes: 34 additions & 11 deletions ReplaceTextJob.php
Expand Up @@ -50,15 +50,33 @@ function run() {
}
$wgUser = $actual_user;
} else {
$article = new Article( $this->title, 0 );
if ( !$article ) {
$this->error = 'replaceText: Article not found "' . $this->title->getPrefixedDBkey() . '"';
wfProfileOut( __METHOD__ );
return false;
// 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;
}
$article_text = $wikiPage->getContent()->getNativeData();
} 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();
}

wfProfileIn( __METHOD__ . '-replace' );
$article_text = $article->fetchContent();
$target_str = $this->params['target_str'];
$replacement_str = $this->params['replacement_str'];
// @todo FIXME eh?
Expand All @@ -70,12 +88,12 @@ function run() {
$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 there's at least one replacement, modify the page,
// using the passed-in edit summary.
if ( $num_matches > 0 ) {
// change global $wgUser variable to the one
// Change global $wgUser variable to the one
// specified by the job only for the extent of
// this replacement
// this replacement.
global $wgUser;
$actual_user = $wgUser;
$wgUser = User::newFromId( $this->params['user_id'] );
Expand All @@ -84,7 +102,12 @@ function run() {
if ( $wgUser->isAllowed( 'bot' ) ) {
$flags |= EDIT_FORCE_BOT;
}
$article->doEdit( $new_text, $edit_summary, $flags );
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 );
}
$wgUser = $actual_user;
}
wfProfileOut( __METHOD__ . '-replace' );
Expand Down

0 comments on commit 38f8df0

Please sign in to comment.