MediaWiki REL1_32
ReplaceTextJob.php
Go to the documentation of this file.
1<?php
23use Wikimedia\ScopedCallback;
24
29class ReplaceTextJob extends Job {
35 function __construct( $title, $params = '' ) {
36 parent::__construct( 'replaceText', $title, $params );
37 }
38
43 function run() {
44 if ( isset( $this->params['session'] ) ) {
45 $callback = RequestContext::importScopedSession( $this->params['session'] );
46 $this->addTeardownCallback( function () use ( &$callback ) {
47 ScopedCallback::consume( $callback );
48 } );
49 }
50
51 if ( is_null( $this->title ) ) {
52 $this->error = "replaceText: Invalid title";
53 return false;
54 }
55
56 if ( array_key_exists( 'move_page', $this->params ) ) {
57 global $wgUser;
58 $actual_user = $wgUser;
59 $wgUser = User::newFromId( $this->params['user_id'] );
60 $cur_page_name = $this->title->getText();
61 if ( $this->params['use_regex'] ) {
62 $new_page_name = preg_replace(
63 "/" . $this->params['target_str'] . "/Uu", $this->params['replacement_str'], $cur_page_name
64 );
65 } else {
66 $new_page_name =
67 str_replace( $this->params['target_str'], $this->params['replacement_str'], $cur_page_name );
68 }
69
70 $new_title = Title::newFromText( $new_page_name, $this->title->getNamespace() );
71 $reason = $this->params['edit_summary'];
72 $create_redirect = $this->params['create_redirect'];
73 $this->title->moveTo( $new_title, true, $reason, $create_redirect );
74 if ( $this->params['watch_page'] ) {
75 WatchAction::doWatch( $new_title, $wgUser );
76 }
77 $wgUser = $actual_user;
78 } else {
79 if ( $this->title->getContentModel() !== CONTENT_MODEL_WIKITEXT ) {
80 $this->error = 'replaceText: Wiki page "' .
81 $this->title->getPrefixedDBkey() . '" does not hold regular wikitext.';
82 return false;
83 }
84 $wikiPage = new WikiPage( $this->title );
85 // Is this check necessary?
86 if ( !$wikiPage ) {
87 $this->error =
88 'replaceText: Wiki page not found for "' . $this->title->getPrefixedDBkey() . '."';
89 return false;
90 }
91 $wikiPageContent = $wikiPage->getContent();
92 if ( is_null( $wikiPageContent ) ) {
93 $this->error =
94 'replaceText: No contents found for wiki page at "' . $this->title->getPrefixedDBkey() . '."';
95 return false;
96 }
97 $article_text = $wikiPageContent->getNativeData();
98
99 $target_str = $this->params['target_str'];
100 $replacement_str = $this->params['replacement_str'];
101 $num_matches = 0;
102
103 if ( $this->params['use_regex'] ) {
104 $new_text =
105 preg_replace( '/' . $target_str . '/Uu', $replacement_str, $article_text, -1, $num_matches );
106 } else {
107 $new_text = str_replace( $target_str, $replacement_str, $article_text, $num_matches );
108 }
109
110 // If there's at least one replacement, modify the page,
111 // using the passed-in edit summary.
112 if ( $num_matches > 0 ) {
113 // Change global $wgUser variable to the one
114 // specified by the job only for the extent of
115 // this replacement.
116 global $wgUser;
117 $actual_user = $wgUser;
118 $wgUser = User::newFromId( $this->params['user_id'] );
119 $edit_summary = $this->params['edit_summary'];
120 $flags = EDIT_MINOR;
121 if ( $wgUser->isAllowed( 'bot' ) ) {
122 $flags |= EDIT_FORCE_BOT;
123 }
124 if ( isset( $this->params['doAnnounce'] ) &&
125 !$this->params['doAnnounce'] ) {
126 $flags |= EDIT_SUPPRESS_RC;
127 # fixme log this action
128 }
129 $new_content = new WikitextContent( $new_text );
130 $wikiPage->doEditContent( $new_content, $edit_summary, $flags );
131 $wgUser = $actual_user;
132 }
133 }
134 return true;
135 }
136}
Class to both describe a background job and handle jobs.
Definition Job.php:30
addTeardownCallback( $callback)
Definition Job.php:310
array $params
Array of job parameters.
Definition Job.php:35
Background job to replace text in a given page.
__construct( $title, $params='')
Constructor.
run()
Run a replaceText job.
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition User.php:615
static doWatch(Title $title, User $user, $checkRights=User::CHECK_USER_RIGHTS)
Watch a page.
Class representing a MediaWiki article and history.
Definition WikiPage.php:44
Content object for wiki text pages.
const EDIT_FORCE_BOT
Definition Defines.php:156
const EDIT_SUPPRESS_RC
Definition Defines.php:155
const CONTENT_MODEL_WIKITEXT
Definition Defines.php:235
const EDIT_MINOR
Definition Defines.php:154