| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 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 |
|
| 40 |
|
| 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 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 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 |
|
| 76 |
|
| 77 |
|
| 78 |
function run() { |
| 79 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 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 |
|
| 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 |
|
| 213 |
|
| 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 |
} |