Debian Codegraph @

includes/block/BlockUser.php

raw symbols SCIP
1 <?php
2
3 /**
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 * http://www.gnu.org/copyleft/gpl.html
18 *
19 * @file
20 */
21
22 namespace MediaWiki\Block;
23
24 use ChangeTags;
25 use InvalidArgumentException;
26 use ManualLogEntry;
27 use MediaWiki\Block\Restriction\AbstractRestriction;
28 use MediaWiki\Block\Restriction\ActionRestriction;
29 use MediaWiki\Block\Restriction\NamespaceRestriction;
30 use MediaWiki\Block\Restriction\PageRestriction;
31 use MediaWiki\Config\ServiceOptions;
32 use MediaWiki\Deferred\DeferredUpdates;
33 use MediaWiki\HookContainer\HookContainer;
34 use MediaWiki\HookContainer\HookRunner;
35 use MediaWiki\MainConfigNames;
36 use MediaWiki\Message\Message;
37 use MediaWiki\Permissions\Authority;
38 use MediaWiki\Status\Status;
39 use MediaWiki\Title\MalformedTitleException;
40 use MediaWiki\Title\Title;
41 use MediaWiki\Title\TitleFactory;
42 use MediaWiki\User\UserEditTracker;
43 use MediaWiki\User\UserFactory;
44 use MediaWiki\User\UserIdentity;
45 use Psr\Log\LoggerInterface;
46 use RevisionDeleteUser;
47 use Wikimedia\ParamValidator\TypeDef\ExpiryDef;
48
49 /**
50 * Handles the backend logic of blocking users
51 *
52 * @since 1.36
53 */
54 class BlockUser {
55 /**
56 * @var UserIdentity|string|null
57 *
58 * Target of the block
59 *
60 * This is null in case BlockUtils::parseBlockTarget failed to parse the target.
61 * Such case is detected in placeBlockUnsafe, by calling validateTarget from SpecialBlock.
62 */
63 private $target;
64
65 /**
66 * @var int
67 *
68 * One of AbstractBlock::TYPE_* constants
69 *
70 * This will be -1 if BlockUtils::parseBlockTarget failed to parse the target.
71 */
72 private $targetType;
73
74 /** @var Authority Performer of the block */
75 private $performer;
76
77 private ServiceOptions $options;
78 private BlockRestrictionStore $blockRestrictionStore;
79 private BlockPermissionChecker $blockPermissionChecker;
80 private BlockUtils $blockUtils;
81 private BlockActionInfo $blockActionInfo;
82 private HookRunner $hookRunner;
83 private DatabaseBlockStore $blockStore;
84 private UserFactory $userFactory;
85 private UserEditTracker $userEditTracker;
86 private LoggerInterface $logger;
87 private TitleFactory $titleFactory;
88
89 /**
90 * @internal For use by UserBlockCommandFactory
91 */
92 public const CONSTRUCTOR_OPTIONS = [
93 MainConfigNames::HideUserContribLimit,
94 MainConfigNames::BlockAllowsUTEdit,
95 ];
96
97 /**
98 * @var string
99 *
100 * Expiry of the to-be-placed block exactly as it was passed to the constructor.
101 */
102 private $rawExpiry;
103
104 /**
105 * @var string|bool
106 *
107 * Parsed expiry. This may be false in case of an error in parsing.
108 */
109 private $expiryTime;
110
111 /** @var string */
112 private $reason;
113
114 /** @var bool */
115 private $isCreateAccountBlocked = false;
116
117 /**
118 * @var bool|null
119 *
120 * This may be null when an invalid option was passed to the constructor.
121 * Such a case is caught in placeBlockUnsafe.
122 */
123 private $isUserTalkEditBlocked = null;
124
125 /** @var bool */
126 private $isEmailBlocked = false;
127
128 /** @var bool */
129 private $isHardBlock = true;
130
131 /** @var bool */
132 private $isAutoblocking = true;
133
134 /** @var bool */
135 private $isHideUser = false;
136
137 /**
138 * @var bool
139 *
140 * Flag that needs to be true when the to-be-created block allows all editing,
141 * but does not allow some other action.
142 *
143 * This flag is used only by isPartial(), and should not be used anywhere else,
144 * even within this class. If you want to determine whether the block will be partial,
145 * use $this->isPartial().
146 */
147 private $isPartialRaw;
148
149 /** @var AbstractRestriction[] */
150 private $blockRestrictions = [];
151
152 /** @var string[] */
153 private $tags = [];
154
155 /** @var int|null */
156 private $logDeletionFlags;
157
158 /**
159 * @param ServiceOptions $options
160 * @param BlockRestrictionStore $blockRestrictionStore
161 * @param BlockPermissionCheckerFactory $blockPermissionCheckerFactory
162 * @param BlockUtils $blockUtils
163 * @param BlockActionInfo $blockActionInfo
164 * @param HookContainer $hookContainer
165 * @param DatabaseBlockStore $databaseBlockStore
166 * @param UserFactory $userFactory
167 * @param UserEditTracker $userEditTracker
168 * @param LoggerInterface $logger
169 * @param TitleFactory $titleFactory
170 * @param string|UserIdentity $target Target of the block
171 * @param Authority $performer Performer of the block
172 * @param string $expiry Expiry of the block (timestamp or 'infinity')
173 * @param string $reason Reason of the block
174 * @param bool[] $blockOptions
175 * Valid options:
176 * - isCreateAccountBlocked : Are account creations prevented?
177 * - isEmailBlocked : Is emailing other users prevented?
178 * - isHardBlock : Are named (non-temporary) users prevented from editing?
179 * - isAutoblocking : Should this block spread to others to
180 * limit block evasion?
181 * - isUserTalkEditBlocked : Is editing blocked user's own talk page prevented?
182 * - isHideUser : Should blocked user's name be hidden (needs hideuser)?
183 * - isPartial : Is this block partial? This is ignored when
184 * blockRestrictions is not an empty array.
185 * @param AbstractRestriction[] $blockRestrictions
186 * @param string[] $tags Tags that should be assigned to the log entry
187 */
188 public function __construct(
189 ServiceOptions $options,
190 BlockRestrictionStore $blockRestrictionStore,
191 BlockPermissionCheckerFactory $blockPermissionCheckerFactory,
192 BlockUtils $blockUtils,
193 BlockActionInfo $blockActionInfo,
194 HookContainer $hookContainer,
195 DatabaseBlockStore $databaseBlockStore,
196 UserFactory $userFactory,
197 UserEditTracker $userEditTracker,
198 LoggerInterface $logger,
199 TitleFactory $titleFactory,
200 $target,
201 Authority $performer,
202 string $expiry,
203 string $reason,
204 array $blockOptions,
205 array $blockRestrictions,
206 array $tags
207 ) {
208 // Process dependencies
209 $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
210 $this->options = $options;
211 $this->blockRestrictionStore = $blockRestrictionStore;
212 $this->blockPermissionChecker = $blockPermissionCheckerFactory
213 ->newBlockPermissionChecker(
214 $target,
215 $performer
216 );
217 $this->blockUtils = $blockUtils;
218 $this->hookRunner = new HookRunner( $hookContainer );
219 $this->blockStore = $databaseBlockStore;
220 $this->userFactory = $userFactory;
221 $this->userEditTracker = $userEditTracker;
222 $this->logger = $logger;
223 $this->titleFactory = $titleFactory;
224 $this->blockActionInfo = $blockActionInfo;
225
226 // Process block target
227 [ $this->target, $rawTargetType ] = $this->blockUtils->parseBlockTarget( $target );
228 if ( $rawTargetType !== null ) { // Guard against invalid targets
229 $this->targetType = $rawTargetType;
230 } else {
231 $this->targetType = -1;
232 }
233
234 // Process other block parameters
235 $this->performer = $performer;
236 $this->rawExpiry = $expiry;
237 $this->expiryTime = self::parseExpiryInput( $this->rawExpiry );
238 $this->reason = $reason;
239 $this->blockRestrictions = $blockRestrictions;
240 $this->tags = $tags;
241
242 // Process blockOptions
243 foreach ( [
244 'isCreateAccountBlocked',
245 'isEmailBlocked',
246 'isHardBlock',
247 'isAutoblocking',
248 ] as $possibleBlockOption ) {
249 if ( isset( $blockOptions[ $possibleBlockOption ] ) ) {
250 $this->$possibleBlockOption = $blockOptions[ $possibleBlockOption ];
251 }
252 }
253
254 $this->isPartialRaw = !empty( $blockOptions['isPartial'] ) && !$blockRestrictions;
255
256 if (
257 !$this->isPartial() ||
258 in_array( NS_USER_TALK, $this->getNamespaceRestrictions() )
259 ) {
260
261 // It is possible to block user talk edit. User talk edit is:
262 // - always blocked if the config says so;
263 // - otherwise blocked/unblocked if the option was passed in;
264 // - otherwise defaults to not blocked.
265 if ( !$this->options->get( MainConfigNames::BlockAllowsUTEdit ) ) {
266 $this->isUserTalkEditBlocked = true;
267 } else {
268 $this->isUserTalkEditBlocked = $blockOptions['isUserTalkEditBlocked'] ?? false;
269 }
270
271 } else {
272
273 // It is not possible to block user talk edit. If the option
274 // was passed, an error will be thrown in ::placeBlockUnsafe.
275 // Otherwise, set to not blocked.
276 if ( !isset( $blockOptions['isUserTalkEditBlocked'] ) || !$blockOptions['isUserTalkEditBlocked'] ) {
277 $this->isUserTalkEditBlocked = false;
278 }
279
280 }
281
282 if (
283 isset( $blockOptions['isHideUser'] ) &&
284 $this->targetType === AbstractBlock::TYPE_USER
285 ) {
286 $this->isHideUser = $blockOptions['isHideUser'];
287 }
288 }
289
290 /**
291 * @unstable This method might be removed without prior notice (see T271101)
292 * @param int $flags One of LogPage::* constants
293 */
294 public function setLogDeletionFlags( int $flags ): void {
295 $this->logDeletionFlags = $flags;
296 }
297
298 /**
299 * Convert a submitted expiry time, which may be relative ("2 weeks", etc) or absolute
300 * ("24 May 2034", etc), into an absolute timestamp we can put into the database.
301 *
302 * @todo strtotime() only accepts English strings. This means the expiry input
303 * can only be specified in English.
304 * @see https://www.php.net/manual/en/function.strtotime.php
305 *
306 * @param string $expiry Whatever was typed into the form
307 *
308 * @return string|false Timestamp (format TS_MW) or 'infinity' or false on error.
309 */
310 public static function parseExpiryInput( string $expiry ) {
311 try {
312 return ExpiryDef::normalizeExpiry( $expiry, TS_MW );
313 } catch ( InvalidArgumentException $e ) {
314 return false;
315 }
316 }
317
318 /**
319 * Is the to-be-placed block partial?
320 *
321 * @return bool
322 */
323 private function isPartial(): bool {
324 return $this->blockRestrictions !== [] || $this->isPartialRaw;
325 }
326
327 /**
328 * Configure DatabaseBlock according to class properties
329 *
330 * @param DatabaseBlock|null $sourceBlock Copy any options from this block.
331 * Null to construct a new one.
332 *
333 * @return DatabaseBlock
334 */
335 private function configureBlock( $sourceBlock = null ): DatabaseBlock {
336 if ( $sourceBlock === null ) {
337 $block = new DatabaseBlock();
338 } else {
339 $block = clone $sourceBlock;
340 }
341
342 $isSitewide = !$this->isPartial();
343
344 $block->setTarget( $this->target );
345 $block->setBlocker( $this->performer->getUser() );
346 $block->setReason( $this->reason );
347 $block->setExpiry( $this->expiryTime );
348 $block->isCreateAccountBlocked( $this->isCreateAccountBlocked );
349 $block->isEmailBlocked( $this->isEmailBlocked );
350 $block->isHardblock( $this->isHardBlock );
351 $block->isAutoblocking( $this->isAutoblocking );
352 $block->isSitewide( $isSitewide );
353 $block->isUsertalkEditAllowed( !$this->isUserTalkEditBlocked );
354 $block->setHideName( $this->isHideUser );
355
356 $blockId = $block->getId();
357 if ( $blockId === null ) {
358 // Block wasn't inserted into the DB yet
359 $block->setRestrictions( $this->blockRestrictions );
360 } else {
361 // Block is in the DB, we need to set restrictions through a service
362 $block->setRestrictions(
363 $this->blockRestrictionStore->setBlockId(
364 $blockId,
365 $this->blockRestrictions
366 )
367 );
368 }
369
370 return $block;
371 }
372
373 /**
374 * Place a block, checking permissions
375 *
376 * @param bool $reblock Should this reblock?
377 *
378 * @return Status If the block is successful, the value of the returned
379 * Status is an instance of a newly placed block.
380 */
381 public function placeBlock( bool $reblock = false ): Status {
382 $priorBlock = $this->blockStore
383 ->newFromTarget( $this->target, null, /*fromPrimary=*/true );
384 $priorHideUser = $priorBlock instanceof DatabaseBlock && $priorBlock->getHideName();
385 if (
386 $this->blockPermissionChecker
387 ->checkBasePermissions(
388 $this->isHideUser || $priorHideUser
389 ) !== true
390 ) {
391 $this->logger->debug( 'placeBlock: checkBasePermissions failed' );
392 return Status::newFatal( $priorHideUser ? 'cant-see-hidden-user' : 'badaccess-group0' );
393 }
394
395 $blockCheckResult = $this->blockPermissionChecker->checkBlockPermissions();
396 if ( $blockCheckResult !== true ) {
397 $this->logger->debug( 'placeBlock: checkBlockPermissions failed' );
398 return Status::newFatal( $blockCheckResult );
399 }
400
401 if (
402 $this->isEmailBlocked &&
403 !$this->blockPermissionChecker->checkEmailPermissions()
404 ) {
405 // TODO: Maybe not ignore the error here?
406 $this->isEmailBlocked = false;
407 }
408
409 if ( $this->tags !== [] ) {
410 $status = ChangeTags::canAddTagsAccompanyingChange(
411 $this->tags,
412 $this->performer
413 );
414
415 if ( !$status->isOK() ) {
416 $this->logger->debug( 'placeBlock: ChangeTags::canAddTagsAccompanyingChange failed' );
417 return $status;
418 }
419 }
420
421 $status = Status::newGood();
422 foreach ( $this->getPageRestrictions() as $pageRestriction ) {
423 try {
424 $title = $this->titleFactory->newFromTextThrow( $pageRestriction );
425 if ( !$title->exists() ) {
426 $this->logger->debug( "placeBlock: nonexistent page restriction $title" );
427 $status->fatal( 'cant-block-nonexistent-page', $pageRestriction );
428 }
429 } catch ( MalformedTitleException $e ) {
430 $this->logger->debug( 'placeBlock: malformed page restriction title' );
431 $status->fatal( $e->getMessageObject() );
432 }
433 }
434 if ( !$status->isOK() ) {
435 return $status;
436 }
437
438 return $this->placeBlockUnsafe( $reblock );
439 }
440
441 /**
442 * Place a block without any sort of permissions checks.
443 *
444 * @param bool $reblock Should this reblock?
445 *
446 * @return Status If the block is successful, the value of the returned
447 * Status is an instance of a newly placed block.
448 */
449 public function placeBlockUnsafe( bool $reblock = false ): Status {
450 $status = $this->blockUtils->validateTarget( $this->target );
451
452 if ( !$status->isOK() ) {
453 $this->logger->debug( 'placeBlockUnsafe: invalid target' );
454 return $status;
455 }
456
457 if ( $this->isUserTalkEditBlocked === null ) {
458 $this->logger->debug( 'placeBlockUnsafe: partial block on user talk page' );
459 return Status::newFatal( 'ipb-prevent-user-talk-edit' );
460 }
461
462 if (
463 // There should be some expiry
464 strlen( $this->rawExpiry ) === 0 ||
465 // can't be a larger string as 50 (it should be a time format in any way)
466 strlen( $this->rawExpiry ) > 50 ||
467 // the time can't be parsed
468 !$this->expiryTime
469 ) {
470 $this->logger->debug( 'placeBlockUnsafe: invalid expiry' );
471 return Status::newFatal( 'ipb_expiry_invalid' );
472 }
473
474 if ( $this->expiryTime < wfTimestampNow() ) {
475 $this->logger->debug( 'placeBlockUnsafe: expiry in the past' );
476 return Status::newFatal( 'ipb_expiry_old' );
477 }
478
479 if ( $this->isHideUser ) {
480 if ( $this->isPartial() ) {
481 $this->logger->debug( 'placeBlockUnsafe: partial block cannot hide user' );
482 return Status::newFatal( 'ipb_hide_partial' );
483 }
484
485 if ( !wfIsInfinity( $this->rawExpiry ) ) {
486 $this->logger->debug( 'placeBlockUnsafe: temp user block has expiry' );
487 return Status::newFatal( 'ipb_expiry_temp' );
488 }
489
490 $hideUserContribLimit = $this->options->get( MainConfigNames::HideUserContribLimit );
491 if (
492 $hideUserContribLimit !== false &&
493 $this->userEditTracker->getUserEditCount( $this->target ) > $hideUserContribLimit
494 ) {
495 $this->logger->debug( 'placeBlockUnsafe: hide user with too many contribs' );
496 return Status::newFatal( 'ipb_hide_invalid', Message::numParam( $hideUserContribLimit ) );
497 }
498 }
499
500 if ( $this->isPartial() ) {
501 if (
502 $this->blockRestrictions === [] &&
503 !$this->isEmailBlocked &&
504 !$this->isCreateAccountBlocked &&
505 !$this->isUserTalkEditBlocked
506 ) {
507 $this->logger->debug( 'placeBlockUnsafe: empty partial block' );
508 return Status::newFatal( 'ipb-empty-block' );
509 }
510 }
511
512 return $this->placeBlockInternal( $reblock );
513 }
514
515 /**
516 * Places a block without any sort of permission or double checking, hooks can still
517 * abort the block through, as well as already existing block.
518 *
519 * @param bool $reblock Should this reblock?
520 *
521 * @return Status
522 */
523 private function placeBlockInternal( bool $reblock = true ): Status {
524 $block = $this->configureBlock();
525
526 $denyReason = [ 'hookaborted' ];
527 $legacyUser = $this->userFactory->newFromAuthority( $this->performer );
528 if ( !$this->hookRunner->onBlockIp( $block, $legacyUser, $denyReason ) ) {
529 $status = Status::newGood();
530 foreach ( $denyReason as $key ) {
531 $this->logger->debug( "placeBlockInternal: hook aborted with message \"$key\"" );
532 $status->fatal( $key );
533 }
534 return $status;
535 }
536
537 // Is there a conflicting block?
538 // xxx: there is an identical call at the beginning of ::placeBlock
539 $priorBlock = $this->blockStore
540 ->newFromTarget( $this->target, null, /*fromPrimary=*/true );
541
542 // T287798: we are blocking an IP that is currently autoblocked
543 // we can ignore the block because ipb_address_unique allows the IP address
544 // be both manually blocked and autoblocked
545 // this will work as long as DatabaseBlockStore::newLoad prefers manual IP blocks
546 // over autoblocks
547 if ( $priorBlock !== null
548 && $priorBlock->getType() === AbstractBlock::TYPE_AUTO
549 && $this->targetType === AbstractBlock::TYPE_IP
550 ) {
551 $priorBlock = null;
552 }
553
554 if ( $priorBlock !== null ) {
555 // Reblock only if the caller wants so
556 if ( !$reblock ) {
557 $this->logger->debug(
558 'placeBlockInternal: already blocked and reblock not requested' );
559 return Status::newFatal( 'ipb_already_blocked', $block->getTargetName() );
560 }
561
562 if ( $block->equals( $priorBlock ) ) {
563 // Block settings are equal => user is already blocked
564 $this->logger->debug( 'placeBlockInternal: already blocked, no change' );
565 return Status::newFatal( 'ipb_already_blocked', $block->getTargetName() );
566 }
567
568 $currentBlock = $this->configureBlock( $priorBlock );
569 $logEntry = $this->prepareLogEntry( true );
570 $this->blockStore->updateBlock( $currentBlock ); // TODO handle failure
571 $block = $currentBlock;
572 } else {
573 $logEntry = $this->prepareLogEntry( false );
574 // Try to insert block.
575 $insertStatus = $this->blockStore->insertBlock( $block );
576 if ( !$insertStatus ) {
577 $this->logger->warning( 'Block could not be inserted. No existing block was found.' );
578 return Status::newFatal( 'ipb-block-not-found', $block->getTargetName() );
579 }
580 }
581 // Relate log ID to block ID (T27763)
582 $logEntry->setRelations( [ 'ipb_id' => $block->getId() ] );
583
584 // Set *_deleted fields if requested
585 if ( $this->isHideUser ) {
586 // This should only be the case of $this->target is a user, so we can
587 // safely call ->getId()
588 RevisionDeleteUser::suppressUserName( $this->target->getName(), $this->target->getId() );
589 }
590
591 DeferredUpdates::addCallableUpdate( function () use ( $block, $legacyUser, $priorBlock ) {
592 $this->hookRunner->onBlockIpComplete( $block, $legacyUser, $priorBlock );
593 } );
594
595 // DatabaseBlock constructor sanitizes certain block options on insert
596 $this->isEmailBlocked = $block->isEmailBlocked();
597 $this->isAutoblocking = $block->isAutoblocking();
598
599 $this->log( $logEntry );
600
601 $this->logger->debug( 'placeBlockInternal: success' );
602 return Status::newGood( $block );
603 }
604
605 /**
606 * Build namespace restrictions array from $this->blockRestrictions
607 *
608 * Returns an array of namespace IDs.
609 *
610 * @return int[]
611 */
612 private function getNamespaceRestrictions(): array {
613 $namespaceRestrictions = [];
614 foreach ( $this->blockRestrictions as $restriction ) {
615 if ( $restriction instanceof NamespaceRestriction ) {
616 $namespaceRestrictions[] = $restriction->getValue();
617 }
618 }
619 return $namespaceRestrictions;
620 }
621
622 /**
623 * Build an array of page restrictions from $this->blockRestrictions
624 *
625 * Returns an array of stringified full page titles.
626 *
627 * @return string[]
628 */
629 private function getPageRestrictions(): array {
630 $pageRestrictions = [];
631 foreach ( $this->blockRestrictions as $restriction ) {
632 if ( $restriction instanceof PageRestriction ) {
633 $pageRestrictions[] = $restriction->getTitle()->getFullText();
634 }
635 }
636 return $pageRestrictions;
637 }
638
639 /**
640 * Build an array of actions from $this->blockRestrictions
641 *
642 * Returns an array of stringified actions.
643 *
644 * @return string[]
645 */
646 private function getActionRestrictions(): array {
647 $actionRestrictions = [];
648 foreach ( $this->blockRestrictions as $restriction ) {
649 if ( $restriction instanceof ActionRestriction ) {
650 $actionRestrictions[] = $this->blockActionInfo->getActionFromId( $restriction->getValue() );
651 }
652 }
653 return $actionRestrictions;
654 }
655
656 /**
657 * Prepare $logParams
658 *
659 * Helper method for $this->log()
660 *
661 * @return array
662 */
663 private function constructLogParams(): array {
664 $logExpiry = wfIsInfinity( $this->rawExpiry ) ? 'infinity' : $this->rawExpiry;
665 $logParams = [
666 '5::duration' => $logExpiry,
667 '6::flags' => $this->blockLogFlags(),
668 'sitewide' => !$this->isPartial()
669 ];
670
671 if ( $this->isPartial() ) {
672 $pageRestrictions = $this->getPageRestrictions();
673 $namespaceRestrictions = $this->getNamespaceRestrictions();
674 $actionRestrictions = $this->getActionRestrictions();
675
676 if ( count( $pageRestrictions ) > 0 ) {
677 $logParams['7::restrictions']['pages'] = $pageRestrictions;
678 }
679 if ( count( $namespaceRestrictions ) > 0 ) {
680 $logParams['7::restrictions']['namespaces'] = $namespaceRestrictions;
681 }
682 if ( count( $actionRestrictions ) ) {
683 $logParams['7::restrictions']['actions'] = $actionRestrictions;
684 }
685 }
686 return $logParams;
687 }
688
689 /**
690 * Create the log entry object to be inserted. Do read queries here before
691 * we start locking block_target rows.
692 *
693 * @param bool $isReblock
694 * @return ManualLogEntry
695 */
696 private function prepareLogEntry( bool $isReblock ) {
697 $logType = $this->isHideUser ? 'suppress' : 'block';
698 $logAction = $isReblock ? 'reblock' : 'block';
699 $title = Title::makeTitle( NS_USER, $this->target );
700 // Preload the page_id: needed for log_page in ManualLogEntry::insert()
701 $title->getArticleID();
702
703 $logEntry = new ManualLogEntry( $logType, $logAction );
704 $logEntry->setTarget( $title );
705 $logEntry->setComment( $this->reason );
706 $logEntry->setPerformer( $this->performer->getUser() );
707 $logEntry->setParameters( $this->constructLogParams() );
708 $logEntry->addTags( $this->tags );
709 if ( $this->logDeletionFlags !== null ) {
710 $logEntry->setDeleted( $this->logDeletionFlags );
711 }
712 return $logEntry;
713 }
714
715 /**
716 * Log the block to Special:Log
717 *
718 * @param ManualLogEntry $logEntry
719 */
720 private function log( ManualLogEntry $logEntry ) {
721 $logId = $logEntry->insert();
722 $logEntry->publish( $logId );
723 }
724
725 /**
726 * Return a comma-delimited list of flags to be passed to the log
727 * reader for this block, to provide more information in the logs.
728 *
729 * @return string
730 */
731 private function blockLogFlags(): string {
732 $flags = [];
733
734 if ( $this->targetType != AbstractBlock::TYPE_USER && !$this->isHardBlock ) {
735 // For grepping: message block-log-flags-anononly
736 $flags[] = 'anononly';
737 }
738
739 if ( $this->isCreateAccountBlocked ) {
740 // For grepping: message block-log-flags-nocreate
741 $flags[] = 'nocreate';
742 }
743
744 if ( $this->targetType == AbstractBlock::TYPE_USER && !$this->isAutoblocking ) {
745 // For grepping: message block-log-flags-noautoblock
746 $flags[] = 'noautoblock';
747 }
748
749 if ( $this->isEmailBlocked ) {
750 // For grepping: message block-log-flags-noemail
751 $flags[] = 'noemail';
752 }
753
754 if ( $this->options->get( MainConfigNames::BlockAllowsUTEdit ) && $this->isUserTalkEditBlocked ) {
755 // For grepping: message block-log-flags-nousertalk
756 $flags[] = 'nousertalk';
757 }
758
759 if ( $this->isHideUser ) {
760 // For grepping: message block-log-flags-hiddenname
761 $flags[] = 'hiddenname';
762 }
763
764 return implode( ',', $flags );
765 }
766 }