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