Debian Codegraph @

extensions/ReplaceText/src/Job.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * https://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @author Yaron Koren
20 * @author Ankit Garg
21 */
22 namespace MediaWiki\Extension\ReplaceText;
23
24 use Job as JobParent;
25 use MediaWiki\CommentStore\CommentStoreComment;
26 use MediaWiki\Content\ContentHandler;
27 use MediaWiki\Content\TextContent;
28 use MediaWiki\Context\RequestContext;
29 use MediaWiki\Page\MovePageFactory;
30 use MediaWiki\Page\WikiPageFactory;
31 use MediaWiki\Permissions\PermissionManager;
32 use MediaWiki\Title\Title;
33 use MediaWiki\User\UserFactory;
34 use MediaWiki\Watchlist\WatchlistManager;
35 use RecentChange;
36 use Wikimedia\ScopedCallback;
37
38 /**
39 * Background job to replace text in a given page
40 * - based on /includes/RefreshLinksJob.php
41 */
42 class Job extends JobParent {
43 private MovePageFactory $movePageFactory;
44 private PermissionManager $permissionManager;
45 private UserFactory $userFactory;
46 private WatchlistManager $watchlistManager;
47 private WikiPageFactory $wikiPageFactory;
48
49 /**
50 * Constructor.
51 * @param Title $title
52 * @param array|bool $params Cannot be === true
53 * @param MovePageFactory $movePageFactory
54 * @param PermissionManager $permissionManager
55 * @param UserFactory $userFactory
56 * @param WatchlistManager $watchlistManager
57 * @param WikiPageFactory $wikiPageFactory
58 */
59 function __construct( $title, $params,
60 MovePageFactory $movePageFactory,
61 PermissionManager $permissionManager,
62 UserFactory $userFactory,
63 WatchlistManager $watchlistManager,
64 WikiPageFactory $wikiPageFactory
65 ) {
66 parent::__construct( 'replaceText', $title, $params );
67 $this->movePageFactory = $movePageFactory;
68 $this->permissionManager = $permissionManager;
69 $this->userFactory = $userFactory;
70 $this->watchlistManager = $watchlistManager;
71 $this->wikiPageFactory = $wikiPageFactory;
72 }
73
74 /**
75 * Run a replaceText job
76 * @return bool success
77 */
78 function run() {
79 // T279090
80 $current_user = $this->userFactory->newFromId( $this->params['user_id'] );
81 if ( !$current_user->isRegistered() ) {
82 $this->error = 'replacetext: the user ID ' . $this->params['user_id'] .
83 ' does not belong to a registered user.';
84 return false;
85 }
86 if ( !$this->permissionManager->userCan(
87 'replacetext', $current_user, $this->title
88 ) ) {
89 $this->error = 'replacetext: permission no longer valid';
90 // T279090#6978214
91 return true;
92 }
93
94 if ( isset( $this->params['session'] ) ) {
95 $callback = RequestContext::importScopedSession( $this->params['session'] );
96 $this->addTeardownCallback( static function () use ( &$callback ) {
97 ScopedCallback::consume( $callback );
98 } );
99 }
100
101 if ( $this->title === null ) {
102 $this->error = 'replaceText: Invalid title';
103 return false;
104 }
105
106 if ( array_key_exists( 'move_page', $this->params ) ) {
107 $new_title = Search::getReplacedTitle(
108 $this->title,
109 $this->params['target_str'],
110 $this->params['replacement_str'],
111 $this->params['use_regex']
112 );
113
114 if ( $new_title === null ) {
115 $this->error = 'replaceText: Invalid new title - ' . $this->params['replacement_str'];
116 return false;
117 }
118
119 $reason = $this->params['edit_summary'];
120 $create_redirect = $this->params['create_redirect'];
121 $mvPage = $this->movePageFactory->newMovePage( $this->title, $new_title );
122 $mvStatus = $mvPage->move( $current_user, $reason, $create_redirect );
123 if ( !$mvStatus->isOK() ) {
124 $this->error = 'replaceText: error while moving: ' . $this->title->getPrefixedDBkey() .
125 '. Errors: ' . $mvStatus->getWikiText();
126 return false;
127 }
128
129 if ( $this->params['watch_page'] ) {
130 $this->watchlistManager->addWatch( $current_user, $new_title );
131 }
132 } else {
133 $wikiPage = $this->wikiPageFactory->newFromTitle( $this->title );
134 $latestRevision = $wikiPage->getRevisionRecord();
135
136 if ( $latestRevision === null ) {
137 $this->error =
138 'replaceText: No revision found for wiki page at "' . $this->title->getPrefixedDBkey() . '".';
139 return false;
140 }
141
142 if ( isset( $this->params['roles'] ) ) {
143 $slotRoles = $this->params['roles'];
144 } else {
145 $slotRoles = $latestRevision->getSlotRoles();
146 }
147
148 $revisionSlots = $latestRevision->getSlots();
149 $updater = $wikiPage->newPageUpdater( $current_user );
150 $hasMatches = false;
151
152 foreach ( $slotRoles as $role ) {
153 if ( !$revisionSlots->hasSlot( $role ) ) {
154 $this->error =
155 'replaceText: Slot "' . $role .
156 '" does not exist for wiki page "' . $this->title->getPrefixedDBkey() . '".';
157 return false;
158 }
159
160 $slotContent = $revisionSlots->getContent( $role );
161
162 if ( !( $slotContent instanceof TextContent ) ) {
163 // Sanity check: Does the slot actually contain TextContent?
164 $this->error =
165 'replaceText: Slot "' . $role .
166 '" does not hold regular wikitext for wiki page "' . $this->title->getPrefixedDBkey() . '".';
167 return false;
168 }
169
170 // Sometimes, especially if the system is
171 // overloaded, the getText() call can
172 // lead to this error message getting placed
173 // right within the text - if that happens,
174 // try calling getText() again until the
175 // error goes away.
176 $expansionDepthLimitError = '<span class="error">Expansion depth limit exceeded</span>';
177 $numTries = 0;
178 do {
179 $slot_text = $slotContent->getText();
180 $hasError = strpos( $slot_text, $expansionDepthLimitError ) > 0;
181 $numTries++;
182 } while ( $hasError && $numTries < 10 );
183
184 if ( $hasError ) {
185 $this->error =
186 'replaceText: Encountered "Expansion depth limit exceeded"' .
187 ' error for wiki page "' . $this->title->getPrefixedDBkey() . '".';
188 return false;
189 }
190
191 $target_str = $this->params['target_str'];
192 $replacement_str = $this->params['replacement_str'];
193 $num_matches = 0;
194
195 if ( $this->params['use_regex'] ) {
196 $new_text =
197 preg_replace( '/' . $target_str . '/Uu', $replacement_str, $slot_text, -1, $num_matches );
198 } else {
199 $new_text = str_replace( $target_str, $replacement_str, $slot_text, $num_matches );
200 }
201
202 // If there's at least one replacement, modify the slot.
203 if ( $num_matches > 0 ) {
204 $hasMatches = true;
205 $updater->setContent(
206 $role,
207 ContentHandler::makeContent( $new_text, $this->title, $slotContent->getModel() )
208 );
209 }
210 }
211
212 // If at least one slot is edited, modify the page,
213 // using the passed-in edit summary.
214 if ( $hasMatches ) {
215 $edit_summary = CommentStoreComment::newUnsavedComment( $this->params['edit_summary'] );
216 $flags = EDIT_MINOR;
217 if ( $this->permissionManager->userHasRight( $current_user, 'bot' ) ) {
218 $flags |= EDIT_FORCE_BOT;
219 }
220 if ( isset( $this->params['botEdit'] ) && $this->params['botEdit'] ) {
221 $flags |= EDIT_FORCE_BOT;
222 }
223 if ( $this->permissionManager->userHasRight( $current_user, 'patrol' ) ||
224 $this->permissionManager->userHasRight( $current_user, 'autopatrol' ) ) {
225 $updater->setRcPatrolStatus( RecentChange::PRC_PATROLLED );
226 }
227 $updater->saveRevision( $edit_summary, $flags );
228 }
229 }
230 return true;
231 }
232 }