MediaWiki  1.34.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  $current_user = User::newFromId( $this->params['user_id'] );
59  $this->title,
60  $this->params['target_str'],
61  $this->params['replacement_str'],
62  $this->params['use_regex']
63  );
64 
65  if ( is_null( $new_title ) ) {
66  $this->error = "replaceText: Invalid new title - " . $this->params['replacement_str'];
67  return false;
68  }
69 
70  $reason = $this->params['edit_summary'];
71  $create_redirect = $this->params['create_redirect'];
72  $mvPage = new MovePage( $this->title, $new_title );
73  $mvStatus = $mvPage->move( $current_user, $reason, $create_redirect );
74  if ( !$mvStatus->isOK() ) {
75  $this->error = "replaceText: error while moving: " . $this->title->getPrefixedDBkey() .
76  ". Errors: " . $mvStatus->getWikiText();
77  return false;
78  }
79 
80  if ( $this->params['watch_page'] ) {
81  WatchAction::doWatch( $new_title, $current_user );
82  }
83  } else {
84  if ( $this->title->getContentModel() !== CONTENT_MODEL_WIKITEXT ) {
85  $this->error = 'replaceText: Wiki page "' .
86  $this->title->getPrefixedDBkey() . '" does not hold regular wikitext.';
87  return false;
88  }
89  $wikiPage = new WikiPage( $this->title );
90  // Is this check necessary?
91  if ( !$wikiPage ) {
92  $this->error =
93  'replaceText: Wiki page not found for "' . $this->title->getPrefixedDBkey() . '."';
94  return false;
95  }
96  $wikiPageContent = $wikiPage->getContent();
97  if ( is_null( $wikiPageContent ) ) {
98  $this->error =
99  'replaceText: No contents found for wiki page at "' . $this->title->getPrefixedDBkey() . '."';
100  return false;
101  }
102  $article_text = $wikiPageContent->getNativeData();
103 
104  $target_str = $this->params['target_str'];
105  $replacement_str = $this->params['replacement_str'];
106  $num_matches = 0;
107 
108  if ( $this->params['use_regex'] ) {
109  $new_text =
110  preg_replace( '/' . $target_str . '/Uu', $replacement_str, $article_text, -1, $num_matches );
111  } else {
112  $new_text = str_replace( $target_str, $replacement_str, $article_text, $num_matches );
113  }
114 
115  // If there's at least one replacement, modify the page,
116  // using the passed-in edit summary.
117  if ( $num_matches > 0 ) {
118  // Change global $wgUser variable to the one
119  // specified by the job only for the extent of
120  // this replacement.
121  global $wgUser;
122  $actual_user = $wgUser;
123  $wgUser = User::newFromId( $this->params['user_id'] );
124  $edit_summary = $this->params['edit_summary'];
125  $flags = EDIT_MINOR;
126  if ( $wgUser->isAllowed( 'bot' ) ) {
127  $flags |= EDIT_FORCE_BOT;
128  }
129  if ( isset( $this->params['doAnnounce'] ) &&
130  !$this->params['doAnnounce'] ) {
131  $flags |= EDIT_SUPPRESS_RC;
132  # fixme log this action
133  }
134  $new_content = new WikitextContent( $new_text );
135  $wikiPage->doEditContent( $new_content, $edit_summary, $flags );
136  $wgUser = $actual_user;
137  }
138  }
139  return true;
140  }
141 }
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:539
EDIT_FORCE_BOT
const EDIT_FORCE_BOT
Definition: Defines.php:136
Job\$title
Title $title
Definition: Job.php:41
WikiPage
Class representing a MediaWiki article and history.
Definition: WikiPage.php:47
Job\addTeardownCallback
addTeardownCallback( $callback)
Definition: Job.php:352
CONTENT_MODEL_WIKITEXT
const CONTENT_MODEL_WIKITEXT
Definition: Defines.php:215
Job\$params
array $params
Array of job parameters.
Definition: Job.php: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:116
MovePage
Handles the backend logic of moving a page from one title to another.
Definition: MovePage.php:36
ReplaceTextJob
Background job to replace text in a given page.
Definition: ReplaceTextJob.php:29
WikitextContent
Content object for wiki text pages.
Definition: WikitextContent.php:36
RequestContext\importScopedSession
static importScopedSession(array $params)
Import an client IP address, HTTP headers, user ID, and session ID.
Definition: RequestContext.php:503
ReplaceTextSearch\getReplacedTitle
static getReplacedTitle(Title $title, $search, $replacement, $regex)
Do a replacement on a title.
Definition: ReplaceTextSearch.php:178
EDIT_MINOR
const EDIT_MINOR
Definition: Defines.php:134
EDIT_SUPPRESS_RC
const EDIT_SUPPRESS_RC
Definition: Defines.php:135