MediaWiki  1.33.0
ReplaceTextJob.php
Go to the documentation of this file.
1 <?php
23 use Wikimedia\ScopedCallback;
24 
29 class 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 }
ReplaceTextJob\run
run()
Run a replaceText job.
Definition: ReplaceTextJob.php:43
User\newFromId
static newFromId( $id)
Static factory method for creation from a given user ID.
Definition: User.php:609
Title\newFromText
static newFromText( $text, $defaultNamespace=NS_MAIN)
Create a new Title from text, such as what one would find in a link.
Definition: Title.php:306
EDIT_FORCE_BOT
const EDIT_FORCE_BOT
Definition: Defines.php:156
Job\$title
Title $title
Definition: Job.php:41
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:45
Job\addTeardownCallback
addTeardownCallback( $callback)
Definition: Job.php:355
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:235
Job\$params
array $params
Array of job parameters.
Definition: Job.php:35
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
ReplaceTextJob\__construct
__construct( $title, $params='')
Constructor.
Definition: ReplaceTextJob.php:35
Job
Class to both describe a background job and handle jobs.
Definition: Job.php:30
WatchAction\doWatch
static doWatch(Title $title, User $user, $checkRights=User::CHECK_USER_RIGHTS)
Watch a page.
Definition: WatchAction.php:114
ReplaceTextJob
Background job to replace text in a given page.
Definition: ReplaceTextJob.php:29
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:36
title
title
Definition: parserTests.txt:245
RequestContext\importScopedSession
static importScopedSession(array $params)
Import an client IP address, HTTP headers, user ID, and session ID.
Definition: RequestContext.php:502
error
do that in ParserLimitReportFormat instead use this to modify the parameters of the image all existing parser cache entries will be invalid To avoid you ll need to handle that somehow(e.g. with the RejectParserCacheValue hook) because MediaWiki won 't do it for you. & $defaults error
Definition: hooks.txt:2636
EDIT_MINOR
const EDIT_MINOR
Definition: Defines.php:154
EDIT_SUPPRESS_RC
const EDIT_SUPPRESS_RC
Definition: Defines.php:155