MediaWiki REL1_30
Block.php
Go to the documentation of this file.
1<?php
26
27class Block {
29 public $mReason;
30
33
35 public $mAuto;
36
38 public $mExpiry;
39
41 public $mHideName;
42
45
47 private $mId;
48
50 private $mFromMaster;
51
53 private $mBlockEmail;
54
57
60
62 private $target;
63
66
68 private $type;
69
71 private $blocker;
72
74 private $isHardblock;
75
78
81
82 # TYPE constants
83 const TYPE_USER = 1;
84 const TYPE_IP = 2;
85 const TYPE_RANGE = 3;
86 const TYPE_AUTO = 4;
87 const TYPE_ID = 5;
88
115 function __construct( $options = [] ) {
116 $defaults = [
117 'address' => '',
118 'user' => null,
119 'by' => null,
120 'reason' => '',
121 'timestamp' => '',
122 'auto' => false,
123 'expiry' => '',
124 'anonOnly' => false,
125 'createAccount' => false,
126 'enableAutoblock' => false,
127 'hideName' => false,
128 'blockEmail' => false,
129 'allowUsertalk' => false,
130 'byText' => '',
131 'systemBlock' => null,
132 ];
133
134 if ( func_num_args() > 1 || !is_array( $options ) ) {
135 $options = array_combine(
136 array_slice( array_keys( $defaults ), 0, func_num_args() ),
137 func_get_args()
138 );
139 wfDeprecated( __METHOD__ . ' with multiple arguments', '1.26' );
140 }
141
142 $options += $defaults;
143
144 $this->setTarget( $options['address'] );
145
146 if ( $this->target instanceof User && $options['user'] ) {
147 # Needed for foreign users
148 $this->forcedTargetID = $options['user'];
149 }
150
151 if ( $options['by'] ) {
152 # Local user
153 $this->setBlocker( User::newFromId( $options['by'] ) );
154 } else {
155 # Foreign user
156 $this->setBlocker( $options['byText'] );
157 }
158
159 $this->mReason = $options['reason'];
160 $this->mTimestamp = wfTimestamp( TS_MW, $options['timestamp'] );
161 $this->mExpiry = wfGetDB( DB_REPLICA )->decodeExpiry( $options['expiry'] );
162
163 # Boolean settings
164 $this->mAuto = (bool)$options['auto'];
165 $this->mHideName = (bool)$options['hideName'];
166 $this->isHardblock( !$options['anonOnly'] );
167 $this->isAutoblocking( (bool)$options['enableAutoblock'] );
168
169 # Prevention measures
170 $this->prevents( 'sendemail', (bool)$options['blockEmail'] );
171 $this->prevents( 'editownusertalk', !$options['allowUsertalk'] );
172 $this->prevents( 'createaccount', (bool)$options['createAccount'] );
173
174 $this->mFromMaster = false;
175 $this->systemBlockType = $options['systemBlock'];
176 }
177
184 public static function newFromID( $id ) {
186 $res = $dbr->selectRow(
187 'ipblocks',
188 self::selectFields(),
189 [ 'ipb_id' => $id ],
190 __METHOD__
191 );
192 if ( $res ) {
193 return self::newFromRow( $res );
194 } else {
195 return null;
196 }
197 }
198
206 public static function selectFields() {
207 return [
208 'ipb_id',
209 'ipb_address',
210 'ipb_by',
211 'ipb_by_text',
212 'ipb_timestamp',
213 'ipb_auto',
214 'ipb_anon_only',
215 'ipb_create_account',
216 'ipb_enable_autoblock',
217 'ipb_expiry',
218 'ipb_deleted',
219 'ipb_block_email',
220 'ipb_allow_usertalk',
221 'ipb_parent_block_id',
222 ] + CommentStore::newKey( 'ipb_reason' )->getFields();
223 }
224
233 public function equals( Block $block ) {
234 return (
235 (string)$this->target == (string)$block->target
236 && $this->type == $block->type
237 && $this->mAuto == $block->mAuto
238 && $this->isHardblock() == $block->isHardblock()
239 && $this->prevents( 'createaccount' ) == $block->prevents( 'createaccount' )
240 && $this->mExpiry == $block->mExpiry
241 && $this->isAutoblocking() == $block->isAutoblocking()
242 && $this->mHideName == $block->mHideName
243 && $this->prevents( 'sendemail' ) == $block->prevents( 'sendemail' )
244 && $this->prevents( 'editownusertalk' ) == $block->prevents( 'editownusertalk' )
245 && $this->mReason == $block->mReason
246 );
247 }
248
259 protected function newLoad( $vagueTarget = null ) {
260 $db = wfGetDB( $this->mFromMaster ? DB_MASTER : DB_REPLICA );
261
262 if ( $this->type !== null ) {
263 $conds = [
264 'ipb_address' => [ (string)$this->target ],
265 ];
266 } else {
267 $conds = [ 'ipb_address' => [] ];
268 }
269
270 # Be aware that the != '' check is explicit, since empty values will be
271 # passed by some callers (T31116)
272 if ( $vagueTarget != '' ) {
273 list( $target, $type ) = self::parseTarget( $vagueTarget );
274 switch ( $type ) {
275 case self::TYPE_USER:
276 # Slightly weird, but who are we to argue?
277 $conds['ipb_address'][] = (string)$target;
278 break;
279
280 case self::TYPE_IP:
281 $conds['ipb_address'][] = (string)$target;
282 $conds[] = self::getRangeCond( IP::toHex( $target ) );
283 $conds = $db->makeList( $conds, LIST_OR );
284 break;
285
286 case self::TYPE_RANGE:
287 list( $start, $end ) = IP::parseRange( $target );
288 $conds['ipb_address'][] = (string)$target;
289 $conds[] = self::getRangeCond( $start, $end );
290 $conds = $db->makeList( $conds, LIST_OR );
291 break;
292
293 default:
294 throw new MWException( "Tried to load block with invalid type" );
295 }
296 }
297
298 $res = $db->select( 'ipblocks', self::selectFields(), $conds, __METHOD__ );
299
300 # This result could contain a block on the user, a block on the IP, and a russian-doll
301 # set of rangeblocks. We want to choose the most specific one, so keep a leader board.
302 $bestRow = null;
303
304 # Lower will be better
305 $bestBlockScore = 100;
306
307 # This is begging for $this = $bestBlock, but that's not allowed in PHP :(
308 $bestBlockPreventsEdit = null;
309
310 foreach ( $res as $row ) {
311 $block = self::newFromRow( $row );
312
313 # Don't use expired blocks
314 if ( $block->isExpired() ) {
315 continue;
316 }
317
318 # Don't use anon only blocks on users
319 if ( $this->type == self::TYPE_USER && !$block->isHardblock() ) {
320 continue;
321 }
322
323 if ( $block->getType() == self::TYPE_RANGE ) {
324 # This is the number of bits that are allowed to vary in the block, give
325 # or take some floating point errors
326 $end = Wikimedia\base_convert( $block->getRangeEnd(), 16, 10 );
327 $start = Wikimedia\base_convert( $block->getRangeStart(), 16, 10 );
328 $size = log( $end - $start + 1, 2 );
329
330 # This has the nice property that a /32 block is ranked equally with a
331 # single-IP block, which is exactly what it is...
332 $score = self::TYPE_RANGE - 1 + ( $size / 128 );
333
334 } else {
335 $score = $block->getType();
336 }
337
338 if ( $score < $bestBlockScore ) {
339 $bestBlockScore = $score;
340 $bestRow = $row;
341 $bestBlockPreventsEdit = $block->prevents( 'edit' );
342 }
343 }
344
345 if ( $bestRow !== null ) {
346 $this->initFromRow( $bestRow );
347 $this->prevents( 'edit', $bestBlockPreventsEdit );
348 return true;
349 } else {
350 return false;
351 }
352 }
353
360 public static function getRangeCond( $start, $end = null ) {
361 if ( $end === null ) {
362 $end = $start;
363 }
364 # Per T16634, we want to include relevant active rangeblocks; for
365 # rangeblocks, we want to include larger ranges which enclose the given
366 # range. We know that all blocks must be smaller than $wgBlockCIDRLimit,
367 # so we can improve performance by filtering on a LIKE clause
368 $chunk = self::getIpFragment( $start );
370 $like = $dbr->buildLike( $chunk, $dbr->anyString() );
371
372 # Fairly hard to make a malicious SQL statement out of hex characters,
373 # but stranger things have happened...
374 $safeStart = $dbr->addQuotes( $start );
375 $safeEnd = $dbr->addQuotes( $end );
376
377 return $dbr->makeList(
378 [
379 "ipb_range_start $like",
380 "ipb_range_start <= $safeStart",
381 "ipb_range_end >= $safeEnd",
382 ],
384 );
385 }
386
393 protected static function getIpFragment( $hex ) {
395 if ( substr( $hex, 0, 3 ) == 'v6-' ) {
396 return 'v6-' . substr( substr( $hex, 3 ), 0, floor( $wgBlockCIDRLimit['IPv6'] / 4 ) );
397 } else {
398 return substr( $hex, 0, floor( $wgBlockCIDRLimit['IPv4'] / 4 ) );
399 }
400 }
401
407 protected function initFromRow( $row ) {
408 $this->setTarget( $row->ipb_address );
409 if ( $row->ipb_by ) { // local user
410 $this->setBlocker( User::newFromId( $row->ipb_by ) );
411 } else { // foreign user
412 $this->setBlocker( $row->ipb_by_text );
413 }
414
415 $this->mTimestamp = wfTimestamp( TS_MW, $row->ipb_timestamp );
416 $this->mAuto = $row->ipb_auto;
417 $this->mHideName = $row->ipb_deleted;
418 $this->mId = (int)$row->ipb_id;
419 $this->mParentBlockId = $row->ipb_parent_block_id;
420
421 // I wish I didn't have to do this
422 $db = wfGetDB( DB_REPLICA );
423 $this->mExpiry = $db->decodeExpiry( $row->ipb_expiry );
424 $this->mReason = CommentStore::newKey( 'ipb_reason' )
425 // Legacy because $row probably came from self::selectFields()
426 ->getCommentLegacy( $db, $row )->text;
427
428 $this->isHardblock( !$row->ipb_anon_only );
429 $this->isAutoblocking( $row->ipb_enable_autoblock );
430
431 $this->prevents( 'createaccount', $row->ipb_create_account );
432 $this->prevents( 'sendemail', $row->ipb_block_email );
433 $this->prevents( 'editownusertalk', !$row->ipb_allow_usertalk );
434 }
435
441 public static function newFromRow( $row ) {
442 $block = new Block;
443 $block->initFromRow( $row );
444 return $block;
445 }
446
453 public function delete() {
454 if ( wfReadOnly() ) {
455 return false;
456 }
457
458 if ( !$this->getId() ) {
459 throw new MWException( "Block::delete() requires that the mId member be filled\n" );
460 }
461
462 $dbw = wfGetDB( DB_MASTER );
463 $dbw->delete( 'ipblocks', [ 'ipb_parent_block_id' => $this->getId() ], __METHOD__ );
464 $dbw->delete( 'ipblocks', [ 'ipb_id' => $this->getId() ], __METHOD__ );
465
466 return $dbw->affectedRows() > 0;
467 }
468
477 public function insert( $dbw = null ) {
479
480 if ( $this->getSystemBlockType() !== null ) {
481 throw new MWException( 'Cannot insert a system block into the database' );
482 }
483
484 wfDebug( "Block::insert; timestamp {$this->mTimestamp}\n" );
485
486 if ( $dbw === null ) {
487 $dbw = wfGetDB( DB_MASTER );
488 }
489
490 # Periodic purge via commit hooks
491 if ( mt_rand( 0, 9 ) == 0 ) {
493 }
494
495 $row = $this->getDatabaseArray( $dbw );
496
497 $dbw->insert( 'ipblocks', $row, __METHOD__, [ 'IGNORE' ] );
498 $affected = $dbw->affectedRows();
499 $this->mId = $dbw->insertId();
500
501 # Don't collide with expired blocks.
502 # Do this after trying to insert to avoid locking.
503 if ( !$affected ) {
504 # T96428: The ipb_address index uses a prefix on a field, so
505 # use a standard SELECT + DELETE to avoid annoying gap locks.
506 $ids = $dbw->selectFieldValues( 'ipblocks',
507 'ipb_id',
508 [
509 'ipb_address' => $row['ipb_address'],
510 'ipb_user' => $row['ipb_user'],
511 'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() )
512 ],
513 __METHOD__
514 );
515 if ( $ids ) {
516 $dbw->delete( 'ipblocks', [ 'ipb_id' => $ids ], __METHOD__ );
517 $dbw->insert( 'ipblocks', $row, __METHOD__, [ 'IGNORE' ] );
518 $affected = $dbw->affectedRows();
519 $this->mId = $dbw->insertId();
520 }
521 }
522
523 if ( $affected ) {
524 $auto_ipd_ids = $this->doRetroactiveAutoblock();
525
526 if ( $wgBlockDisablesLogin && $this->target instanceof User ) {
527 // Change user login token to force them to be logged out.
528 $this->target->setToken();
529 $this->target->saveSettings();
530 }
531
532 return [ 'id' => $this->mId, 'autoIds' => $auto_ipd_ids ];
533 }
534
535 return false;
536 }
537
545 public function update() {
546 wfDebug( "Block::update; timestamp {$this->mTimestamp}\n" );
547 $dbw = wfGetDB( DB_MASTER );
548
549 $dbw->startAtomic( __METHOD__ );
550
551 $dbw->update(
552 'ipblocks',
553 $this->getDatabaseArray( $dbw ),
554 [ 'ipb_id' => $this->getId() ],
555 __METHOD__
556 );
557
558 $affected = $dbw->affectedRows();
559
560 if ( $this->isAutoblocking() ) {
561 // update corresponding autoblock(s) (T50813)
562 $dbw->update(
563 'ipblocks',
564 $this->getAutoblockUpdateArray( $dbw ),
565 [ 'ipb_parent_block_id' => $this->getId() ],
566 __METHOD__
567 );
568 } else {
569 // autoblock no longer required, delete corresponding autoblock(s)
570 $dbw->delete(
571 'ipblocks',
572 [ 'ipb_parent_block_id' => $this->getId() ],
573 __METHOD__
574 );
575 }
576
577 $dbw->endAtomic( __METHOD__ );
578
579 if ( $affected ) {
580 $auto_ipd_ids = $this->doRetroactiveAutoblock();
581 return [ 'id' => $this->mId, 'autoIds' => $auto_ipd_ids ];
582 }
583
584 return false;
585 }
586
592 protected function getDatabaseArray( IDatabase $dbw ) {
593 $expiry = $dbw->encodeExpiry( $this->mExpiry );
594
595 if ( $this->forcedTargetID ) {
597 } else {
598 $uid = $this->target instanceof User ? $this->target->getId() : 0;
599 }
600
601 $a = [
602 'ipb_address' => (string)$this->target,
603 'ipb_user' => $uid,
604 'ipb_by' => $this->getBy(),
605 'ipb_by_text' => $this->getByName(),
606 'ipb_timestamp' => $dbw->timestamp( $this->mTimestamp ),
607 'ipb_auto' => $this->mAuto,
608 'ipb_anon_only' => !$this->isHardblock(),
609 'ipb_create_account' => $this->prevents( 'createaccount' ),
610 'ipb_enable_autoblock' => $this->isAutoblocking(),
611 'ipb_expiry' => $expiry,
612 'ipb_range_start' => $this->getRangeStart(),
613 'ipb_range_end' => $this->getRangeEnd(),
614 'ipb_deleted' => intval( $this->mHideName ), // typecast required for SQLite
615 'ipb_block_email' => $this->prevents( 'sendemail' ),
616 'ipb_allow_usertalk' => !$this->prevents( 'editownusertalk' ),
617 'ipb_parent_block_id' => $this->mParentBlockId
618 ] + CommentStore::newKey( 'ipb_reason' )->insert( $dbw, $this->mReason );
619
620 return $a;
621 }
622
627 protected function getAutoblockUpdateArray( IDatabase $dbw ) {
628 return [
629 'ipb_by' => $this->getBy(),
630 'ipb_by_text' => $this->getByName(),
631 'ipb_create_account' => $this->prevents( 'createaccount' ),
632 'ipb_deleted' => (int)$this->mHideName, // typecast required for SQLite
633 'ipb_allow_usertalk' => !$this->prevents( 'editownusertalk' ),
634 ] + CommentStore::newKey( 'ipb_reason' )->insert( $dbw, $this->mReason );
635 }
636
643 protected function doRetroactiveAutoblock() {
644 $blockIds = [];
645 # If autoblock is enabled, autoblock the LAST IP(s) used
646 if ( $this->isAutoblocking() && $this->getType() == self::TYPE_USER ) {
647 wfDebug( "Doing retroactive autoblocks for " . $this->getTarget() . "\n" );
648
649 $continue = Hooks::run(
650 'PerformRetroactiveAutoblock', [ $this, &$blockIds ] );
651
652 if ( $continue ) {
653 self::defaultRetroactiveAutoblock( $this, $blockIds );
654 }
655 }
656 return $blockIds;
657 }
658
666 protected static function defaultRetroactiveAutoblock( Block $block, array &$blockIds ) {
668
669 // No IPs are in recentchanges table, so nothing to select
670 if ( !$wgPutIPinRC ) {
671 return;
672 }
673
675
676 $options = [ 'ORDER BY' => 'rc_timestamp DESC' ];
677 $conds = [ 'rc_user_text' => (string)$block->getTarget() ];
678
679 // Just the last IP used.
680 $options['LIMIT'] = 1;
681
682 $res = $dbr->select( 'recentchanges', [ 'rc_ip' ], $conds,
683 __METHOD__, $options );
684
685 if ( !$res->numRows() ) {
686 # No results, don't autoblock anything
687 wfDebug( "No IP found to retroactively autoblock\n" );
688 } else {
689 foreach ( $res as $row ) {
690 if ( $row->rc_ip ) {
691 $id = $block->doAutoblock( $row->rc_ip );
692 if ( $id ) {
693 $blockIds[] = $id;
694 }
695 }
696 }
697 }
698 }
699
707 public static function isWhitelistedFromAutoblocks( $ip ) {
708 // Try to get the autoblock_whitelist from the cache, as it's faster
709 // than getting the msg raw and explode()'ing it.
710 $cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
711 $lines = $cache->getWithSetCallback(
712 $cache->makeKey( 'ipb', 'autoblock', 'whitelist' ),
713 $cache::TTL_DAY,
714 function ( $curValue, &$ttl, array &$setOpts ) {
715 $setOpts += Database::getCacheSetOptions( wfGetDB( DB_REPLICA ) );
716
717 return explode( "\n",
718 wfMessage( 'autoblock_whitelist' )->inContentLanguage()->plain() );
719 }
720 );
721
722 wfDebug( "Checking the autoblock whitelist..\n" );
723
724 foreach ( $lines as $line ) {
725 # List items only
726 if ( substr( $line, 0, 1 ) !== '*' ) {
727 continue;
728 }
729
730 $wlEntry = substr( $line, 1 );
731 $wlEntry = trim( $wlEntry );
732
733 wfDebug( "Checking $ip against $wlEntry..." );
734
735 # Is the IP in this range?
736 if ( IP::isInRange( $ip, $wlEntry ) ) {
737 wfDebug( " IP $ip matches $wlEntry, not autoblocking\n" );
738 return true;
739 } else {
740 wfDebug( " No match\n" );
741 }
742 }
743
744 return false;
745 }
746
753 public function doAutoblock( $autoblockIP ) {
754 # If autoblocks are disabled, go away.
755 if ( !$this->isAutoblocking() ) {
756 return false;
757 }
758
759 # Don't autoblock for system blocks
760 if ( $this->getSystemBlockType() !== null ) {
761 throw new MWException( 'Cannot autoblock from a system block' );
762 }
763
764 # Check for presence on the autoblock whitelist.
765 if ( self::isWhitelistedFromAutoblocks( $autoblockIP ) ) {
766 return false;
767 }
768
769 // Avoid PHP 7.1 warning of passing $this by reference
770 $block = $this;
771 # Allow hooks to cancel the autoblock.
772 if ( !Hooks::run( 'AbortAutoblock', [ $autoblockIP, &$block ] ) ) {
773 wfDebug( "Autoblock aborted by hook.\n" );
774 return false;
775 }
776
777 # It's okay to autoblock. Go ahead and insert/update the block...
778
779 # Do not add a *new* block if the IP is already blocked.
780 $ipblock = self::newFromTarget( $autoblockIP );
781 if ( $ipblock ) {
782 # Check if the block is an autoblock and would exceed the user block
783 # if renewed. If so, do nothing, otherwise prolong the block time...
784 if ( $ipblock->mAuto && // @todo Why not compare $ipblock->mExpiry?
785 $this->mExpiry > self::getAutoblockExpiry( $ipblock->mTimestamp )
786 ) {
787 # Reset block timestamp to now and its expiry to
788 # $wgAutoblockExpiry in the future
789 $ipblock->updateTimestamp();
790 }
791 return false;
792 }
793
794 # Make a new block object with the desired properties.
795 $autoblock = new Block;
796 wfDebug( "Autoblocking {$this->getTarget()}@" . $autoblockIP . "\n" );
797 $autoblock->setTarget( $autoblockIP );
798 $autoblock->setBlocker( $this->getBlocker() );
799 $autoblock->mReason = wfMessage( 'autoblocker', $this->getTarget(), $this->mReason )
800 ->inContentLanguage()->plain();
801 $timestamp = wfTimestampNow();
802 $autoblock->mTimestamp = $timestamp;
803 $autoblock->mAuto = 1;
804 $autoblock->prevents( 'createaccount', $this->prevents( 'createaccount' ) );
805 # Continue suppressing the name if needed
806 $autoblock->mHideName = $this->mHideName;
807 $autoblock->prevents( 'editownusertalk', $this->prevents( 'editownusertalk' ) );
808 $autoblock->mParentBlockId = $this->mId;
809
810 if ( $this->mExpiry == 'infinity' ) {
811 # Original block was indefinite, start an autoblock now
812 $autoblock->mExpiry = self::getAutoblockExpiry( $timestamp );
813 } else {
814 # If the user is already blocked with an expiry date, we don't
815 # want to pile on top of that.
816 $autoblock->mExpiry = min( $this->mExpiry, self::getAutoblockExpiry( $timestamp ) );
817 }
818
819 # Insert the block...
820 $status = $autoblock->insert();
821 return $status
822 ? $status['id']
823 : false;
824 }
825
830 public function deleteIfExpired() {
831 if ( $this->isExpired() ) {
832 wfDebug( "Block::deleteIfExpired() -- deleting\n" );
833 $this->delete();
834 $retVal = true;
835 } else {
836 wfDebug( "Block::deleteIfExpired() -- not expired\n" );
837 $retVal = false;
838 }
839
840 return $retVal;
841 }
842
847 public function isExpired() {
848 $timestamp = wfTimestampNow();
849 wfDebug( "Block::isExpired() checking current " . $timestamp . " vs $this->mExpiry\n" );
850
851 if ( !$this->mExpiry ) {
852 return false;
853 } else {
854 return $timestamp > $this->mExpiry;
855 }
856 }
857
862 public function isValid() {
863 return $this->getTarget() != null;
864 }
865
869 public function updateTimestamp() {
870 if ( $this->mAuto ) {
871 $this->mTimestamp = wfTimestamp();
872 $this->mExpiry = self::getAutoblockExpiry( $this->mTimestamp );
873
874 $dbw = wfGetDB( DB_MASTER );
875 $dbw->update( 'ipblocks',
876 [ /* SET */
877 'ipb_timestamp' => $dbw->timestamp( $this->mTimestamp ),
878 'ipb_expiry' => $dbw->timestamp( $this->mExpiry ),
879 ],
880 [ /* WHERE */
881 'ipb_id' => $this->getId(),
882 ],
883 __METHOD__
884 );
885 }
886 }
887
893 public function getRangeStart() {
894 switch ( $this->type ) {
895 case self::TYPE_USER:
896 return '';
897 case self::TYPE_IP:
898 return IP::toHex( $this->target );
899 case self::TYPE_RANGE:
900 list( $start, /*...*/ ) = IP::parseRange( $this->target );
901 return $start;
902 default:
903 throw new MWException( "Block with invalid type" );
904 }
905 }
906
912 public function getRangeEnd() {
913 switch ( $this->type ) {
914 case self::TYPE_USER:
915 return '';
916 case self::TYPE_IP:
917 return IP::toHex( $this->target );
918 case self::TYPE_RANGE:
919 list( /*...*/, $end ) = IP::parseRange( $this->target );
920 return $end;
921 default:
922 throw new MWException( "Block with invalid type" );
923 }
924 }
925
931 public function getBy() {
932 $blocker = $this->getBlocker();
933 return ( $blocker instanceof User )
934 ? $blocker->getId()
935 : 0;
936 }
937
943 public function getByName() {
944 $blocker = $this->getBlocker();
945 return ( $blocker instanceof User )
946 ? $blocker->getName()
947 : (string)$blocker; // username
948 }
949
954 public function getId() {
955 return $this->mId;
956 }
957
963 public function getSystemBlockType() {
965 }
966
973 public function fromMaster( $x = null ) {
974 return wfSetVar( $this->mFromMaster, $x );
975 }
976
982 public function isHardblock( $x = null ) {
983 wfSetVar( $this->isHardblock, $x );
984
985 # You can't *not* hardblock a user
986 return $this->getType() == self::TYPE_USER
987 ? true
989 }
990
995 public function isAutoblocking( $x = null ) {
996 wfSetVar( $this->isAutoblocking, $x );
997
998 # You can't put an autoblock on an IP or range as we don't have any history to
999 # look over to get more IPs from
1000 return $this->getType() == self::TYPE_USER
1001 ? $this->isAutoblocking
1002 : false;
1003 }
1004
1012 public function prevents( $action, $x = null ) {
1014 $res = null;
1015 switch ( $action ) {
1016 case 'edit':
1017 # For now... <evil laugh>
1018 $res = true;
1019 break;
1020 case 'createaccount':
1021 $res = wfSetVar( $this->mCreateAccount, $x );
1022 break;
1023 case 'sendemail':
1024 $res = wfSetVar( $this->mBlockEmail, $x );
1025 break;
1026 case 'editownusertalk':
1027 $res = wfSetVar( $this->mDisableUsertalk, $x );
1028 break;
1029 case 'read':
1030 $res = false;
1031 break;
1032 }
1033 if ( !$res && $wgBlockDisablesLogin ) {
1034 // If a block would disable login, then it should
1035 // prevent any action that all users cannot do
1036 $anon = new User;
1037 $res = $anon->isAllowed( $action ) ? $res : true;
1038 }
1039
1040 return $res;
1041 }
1042
1047 public function getRedactedName() {
1048 if ( $this->mAuto ) {
1049 return Html::rawElement(
1050 'span',
1051 [ 'class' => 'mw-autoblockid' ],
1052 wfMessage( 'autoblockid', $this->mId )
1053 );
1054 } else {
1055 return htmlspecialchars( $this->getTarget() );
1056 }
1057 }
1058
1065 public static function getAutoblockExpiry( $timestamp ) {
1067
1068 return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
1069 }
1070
1074 public static function purgeExpired() {
1075 if ( wfReadOnly() ) {
1076 return;
1077 }
1078
1079 DeferredUpdates::addUpdate( new AtomicSectionUpdate(
1080 wfGetDB( DB_MASTER ),
1081 __METHOD__,
1082 function ( IDatabase $dbw, $fname ) {
1083 $dbw->delete(
1084 'ipblocks',
1085 [ 'ipb_expiry < ' . $dbw->addQuotes( $dbw->timestamp() ) ],
1086 $fname
1087 );
1088 }
1089 ) );
1090 }
1091
1112 public static function newFromTarget( $specificTarget, $vagueTarget = null, $fromMaster = false ) {
1113 list( $target, $type ) = self::parseTarget( $specificTarget );
1114 if ( $type == self::TYPE_ID || $type == self::TYPE_AUTO ) {
1115 return self::newFromID( $target );
1116
1117 } elseif ( $target === null && $vagueTarget == '' ) {
1118 # We're not going to find anything useful here
1119 # Be aware that the == '' check is explicit, since empty values will be
1120 # passed by some callers (T31116)
1121 return null;
1122
1123 } elseif ( in_array(
1124 $type,
1125 [ self::TYPE_USER, self::TYPE_IP, self::TYPE_RANGE, null ] )
1126 ) {
1127 $block = new Block();
1128 $block->fromMaster( $fromMaster );
1129
1130 if ( $type !== null ) {
1131 $block->setTarget( $target );
1132 }
1133
1134 if ( $block->newLoad( $vagueTarget ) ) {
1135 return $block;
1136 }
1137 }
1138 return null;
1139 }
1140
1151 public static function getBlocksForIPList( array $ipChain, $isAnon, $fromMaster = false ) {
1152 if ( !count( $ipChain ) ) {
1153 return [];
1154 }
1155
1156 $conds = [];
1157 $proxyLookup = MediaWikiServices::getInstance()->getProxyLookup();
1158 foreach ( array_unique( $ipChain ) as $ipaddr ) {
1159 # Discard invalid IP addresses. Since XFF can be spoofed and we do not
1160 # necessarily trust the header given to us, make sure that we are only
1161 # checking for blocks on well-formatted IP addresses (IPv4 and IPv6).
1162 # Do not treat private IP spaces as special as it may be desirable for wikis
1163 # to block those IP ranges in order to stop misbehaving proxies that spoof XFF.
1164 if ( !IP::isValid( $ipaddr ) ) {
1165 continue;
1166 }
1167 # Don't check trusted IPs (includes local squids which will be in every request)
1168 if ( $proxyLookup->isTrustedProxy( $ipaddr ) ) {
1169 continue;
1170 }
1171 # Check both the original IP (to check against single blocks), as well as build
1172 # the clause to check for rangeblocks for the given IP.
1173 $conds['ipb_address'][] = $ipaddr;
1174 $conds[] = self::getRangeCond( IP::toHex( $ipaddr ) );
1175 }
1176
1177 if ( !count( $conds ) ) {
1178 return [];
1179 }
1180
1181 if ( $fromMaster ) {
1182 $db = wfGetDB( DB_MASTER );
1183 } else {
1184 $db = wfGetDB( DB_REPLICA );
1185 }
1186 $conds = $db->makeList( $conds, LIST_OR );
1187 if ( !$isAnon ) {
1188 $conds = [ $conds, 'ipb_anon_only' => 0 ];
1189 }
1190 $selectFields = array_merge(
1191 [ 'ipb_range_start', 'ipb_range_end' ],
1192 self::selectFields()
1193 );
1194 $rows = $db->select( 'ipblocks',
1195 $selectFields,
1196 $conds,
1197 __METHOD__
1198 );
1199
1200 $blocks = [];
1201 foreach ( $rows as $row ) {
1202 $block = self::newFromRow( $row );
1203 if ( !$block->isExpired() ) {
1204 $blocks[] = $block;
1205 }
1206 }
1207
1208 return $blocks;
1209 }
1210
1232 public static function chooseBlock( array $blocks, array $ipChain ) {
1233 if ( !count( $blocks ) ) {
1234 return null;
1235 } elseif ( count( $blocks ) == 1 ) {
1236 return $blocks[0];
1237 }
1238
1239 // Sort hard blocks before soft ones and secondarily sort blocks
1240 // that disable account creation before those that don't.
1241 usort( $blocks, function ( Block $a, Block $b ) {
1242 $aWeight = (int)$a->isHardblock() . (int)$a->prevents( 'createaccount' );
1243 $bWeight = (int)$b->isHardblock() . (int)$b->prevents( 'createaccount' );
1244 return strcmp( $bWeight, $aWeight ); // highest weight first
1245 } );
1246
1247 $blocksListExact = [
1248 'hard' => false,
1249 'disable_create' => false,
1250 'other' => false,
1251 'auto' => false
1252 ];
1253 $blocksListRange = [
1254 'hard' => false,
1255 'disable_create' => false,
1256 'other' => false,
1257 'auto' => false
1258 ];
1259 $ipChain = array_reverse( $ipChain );
1260
1262 foreach ( $blocks as $block ) {
1263 // Stop searching if we have already have a "better" block. This
1264 // is why the order of the blocks matters
1265 if ( !$block->isHardblock() && $blocksListExact['hard'] ) {
1266 break;
1267 } elseif ( !$block->prevents( 'createaccount' ) && $blocksListExact['disable_create'] ) {
1268 break;
1269 }
1270
1271 foreach ( $ipChain as $checkip ) {
1272 $checkipHex = IP::toHex( $checkip );
1273 if ( (string)$block->getTarget() === $checkip ) {
1274 if ( $block->isHardblock() ) {
1275 $blocksListExact['hard'] = $blocksListExact['hard'] ?: $block;
1276 } elseif ( $block->prevents( 'createaccount' ) ) {
1277 $blocksListExact['disable_create'] = $blocksListExact['disable_create'] ?: $block;
1278 } elseif ( $block->mAuto ) {
1279 $blocksListExact['auto'] = $blocksListExact['auto'] ?: $block;
1280 } else {
1281 $blocksListExact['other'] = $blocksListExact['other'] ?: $block;
1282 }
1283 // We found closest exact match in the ip list, so go to the next Block
1284 break;
1285 } elseif ( array_filter( $blocksListExact ) == []
1286 && $block->getRangeStart() <= $checkipHex
1287 && $block->getRangeEnd() >= $checkipHex
1288 ) {
1289 if ( $block->isHardblock() ) {
1290 $blocksListRange['hard'] = $blocksListRange['hard'] ?: $block;
1291 } elseif ( $block->prevents( 'createaccount' ) ) {
1292 $blocksListRange['disable_create'] = $blocksListRange['disable_create'] ?: $block;
1293 } elseif ( $block->mAuto ) {
1294 $blocksListRange['auto'] = $blocksListRange['auto'] ?: $block;
1295 } else {
1296 $blocksListRange['other'] = $blocksListRange['other'] ?: $block;
1297 }
1298 break;
1299 }
1300 }
1301 }
1302
1303 if ( array_filter( $blocksListExact ) == [] ) {
1304 $blocksList = &$blocksListRange;
1305 } else {
1306 $blocksList = &$blocksListExact;
1307 }
1308
1309 $chosenBlock = null;
1310 if ( $blocksList['hard'] ) {
1311 $chosenBlock = $blocksList['hard'];
1312 } elseif ( $blocksList['disable_create'] ) {
1313 $chosenBlock = $blocksList['disable_create'];
1314 } elseif ( $blocksList['other'] ) {
1315 $chosenBlock = $blocksList['other'];
1316 } elseif ( $blocksList['auto'] ) {
1317 $chosenBlock = $blocksList['auto'];
1318 } else {
1319 throw new MWException( "Proxy block found, but couldn't be classified." );
1320 }
1321
1322 return $chosenBlock;
1323 }
1324
1334 public static function parseTarget( $target ) {
1335 # We may have been through this before
1336 if ( $target instanceof User ) {
1337 if ( IP::isValid( $target->getName() ) ) {
1338 return [ $target, self::TYPE_IP ];
1339 } else {
1340 return [ $target, self::TYPE_USER ];
1341 }
1342 } elseif ( $target === null ) {
1343 return [ null, null ];
1344 }
1345
1346 $target = trim( $target );
1347
1348 if ( IP::isValid( $target ) ) {
1349 # We can still create a User if it's an IP address, but we need to turn
1350 # off validation checking (which would exclude IP addresses)
1351 return [
1352 User::newFromName( IP::sanitizeIP( $target ), false ),
1354 ];
1355
1356 } elseif ( IP::isValidRange( $target ) ) {
1357 # Can't create a User from an IP range
1358 return [ IP::sanitizeRange( $target ), self::TYPE_RANGE ];
1359 }
1360
1361 # Consider the possibility that this is not a username at all
1362 # but actually an old subpage (bug #29797)
1363 if ( strpos( $target, '/' ) !== false ) {
1364 # An old subpage, drill down to the user behind it
1365 $target = explode( '/', $target )[0];
1366 }
1367
1368 $userObj = User::newFromName( $target );
1369 if ( $userObj instanceof User ) {
1370 # Note that since numbers are valid usernames, a $target of "12345" will be
1371 # considered a User. If you want to pass a block ID, prepend a hash "#12345",
1372 # since hash characters are not valid in usernames or titles generally.
1373 return [ $userObj, self::TYPE_USER ];
1374
1375 } elseif ( preg_match( '/^#\d+$/', $target ) ) {
1376 # Autoblock reference in the form "#12345"
1377 return [ substr( $target, 1 ), self::TYPE_AUTO ];
1378
1379 } else {
1380 # WTF?
1381 return [ null, null ];
1382 }
1383 }
1384
1389 public function getType() {
1390 return $this->mAuto
1392 : $this->type;
1393 }
1394
1402 public function getTargetAndType() {
1403 return [ $this->getTarget(), $this->getType() ];
1404 }
1405
1412 public function getTarget() {
1413 return $this->target;
1414 }
1415
1421 public function getExpiry() {
1422 return $this->mExpiry;
1423 }
1424
1429 public function setTarget( $target ) {
1430 list( $this->target, $this->type ) = self::parseTarget( $target );
1431 }
1432
1437 public function getBlocker() {
1438 return $this->blocker;
1439 }
1440
1445 public function setBlocker( $user ) {
1446 $this->blocker = $user;
1447 }
1448
1457 public function setCookie( WebResponse $response ) {
1458 // Calculate the default expiry time.
1459 $maxExpiryTime = wfTimestamp( TS_MW, wfTimestamp() + ( 24 * 60 * 60 ) );
1460
1461 // Use the Block's expiry time only if it's less than the default.
1462 $expiryTime = $this->getExpiry();
1463 if ( $expiryTime === 'infinity' || $expiryTime > $maxExpiryTime ) {
1464 $expiryTime = $maxExpiryTime;
1465 }
1466
1467 // Set the cookie. Reformat the MediaWiki datetime as a Unix timestamp for the cookie.
1468 $expiryValue = DateTime::createFromFormat( 'YmdHis', $expiryTime )->format( 'U' );
1469 $cookieOptions = [ 'httpOnly' => false ];
1470 $cookieValue = $this->getCookieValue();
1471 $response->setCookie( 'BlockID', $cookieValue, $expiryValue, $cookieOptions );
1472 }
1473
1481 public static function clearCookie( WebResponse $response ) {
1482 $response->clearCookie( 'BlockID', [ 'httpOnly' => false ] );
1483 }
1484
1494 public function getCookieValue() {
1495 $config = RequestContext::getMain()->getConfig();
1496 $id = $this->getId();
1497 $secretKey = $config->get( 'SecretKey' );
1498 if ( !$secretKey ) {
1499 // If there's no secret key, don't append a HMAC.
1500 return $id;
1501 }
1502 $hmac = MWCryptHash::hmac( $id, $secretKey, false );
1503 $cookieValue = $id . '!' . $hmac;
1504 return $cookieValue;
1505 }
1506
1517 public static function getIdFromCookieValue( $cookieValue ) {
1518 // Extract the ID prefix from the cookie value (may be the whole value, if no bang found).
1519 $bangPos = strpos( $cookieValue, '!' );
1520 $id = ( $bangPos === false ) ? $cookieValue : substr( $cookieValue, 0, $bangPos );
1521 // Get the site-wide secret key.
1522 $config = RequestContext::getMain()->getConfig();
1523 $secretKey = $config->get( 'SecretKey' );
1524 if ( !$secretKey ) {
1525 // If there's no secret key, just use the ID as given.
1526 return $id;
1527 }
1528 $storedHmac = substr( $cookieValue, $bangPos + 1 );
1529 $calculatedHmac = MWCryptHash::hmac( $id, $secretKey, false );
1530 if ( $calculatedHmac === $storedHmac ) {
1531 return $id;
1532 } else {
1533 return null;
1534 }
1535 }
1536
1545 $blocker = $this->getBlocker();
1546 if ( $blocker instanceof User ) { // local user
1547 $blockerUserpage = $blocker->getUserPage();
1548 $link = "[[{$blockerUserpage->getPrefixedText()}|{$blockerUserpage->getText()}]]";
1549 } else { // foreign user
1550 $link = $blocker;
1551 }
1552
1553 $reason = $this->mReason;
1554 if ( $reason == '' ) {
1555 $reason = $context->msg( 'blockednoreason' )->text();
1556 }
1557
1558 /* $ip returns who *is* being blocked, $intended contains who was meant to be blocked.
1559 * This could be a username, an IP range, or a single IP. */
1560 $intended = $this->getTarget();
1561
1563
1564 $lang = $context->getLanguage();
1565 return [
1566 $systemBlockType !== null
1567 ? 'systemblockedtext'
1568 : ( $this->mAuto ? 'autoblockedtext' : 'blockedtext' ),
1569 $link,
1570 $reason,
1571 $context->getRequest()->getIP(),
1572 $this->getByName(),
1573 $systemBlockType !== null ? $systemBlockType : $this->getId(),
1574 $lang->formatExpiry( $this->mExpiry ),
1575 (string)$intended,
1576 $lang->userTimeAndDate( $this->mTimestamp, $context->getUser() ),
1577 ];
1578 }
1579}
Apache License January AND DISTRIBUTION Definitions License shall mean the terms and conditions for use
$wgAutoblockExpiry
Number of seconds before autoblock entries expire.
$wgBlockCIDRLimit
Limits on the possible sizes of range blocks.
$wgPutIPinRC
Log IP addresses in the recentchanges table; can be accessed only by extensions (e....
$wgBlockDisablesLogin
If true, blocked users will not be allowed to login.
wfDebug( $text, $dest='all', array $context=[])
Sends a line to the debug log if enabled or, optionally, to a comment in output.
wfTimestampNow()
Convenience function; returns MediaWiki timestamp for the present time.
wfReadOnly()
Check whether the wiki is in read-only mode.
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfSetVar(&$dest, $source, $force=false)
Sets dest to source and returns the original value of dest If source is NULL, it just returns the val...
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
wfDeprecated( $function, $version=false, $component=false, $callerOffset=2)
Throws a warning that $function is deprecated.
if(!defined( 'MEDIAWIKI')) $fname
This file is not a valid entry point, perform no further processing unless MEDIAWIKI is defined.
Definition Setup.php:36
$line
Definition cdb.php:58
Deferrable Update for closure/callback updates via IDatabase::doAtomicSection()
bool $mCreateAccount
Definition Block.php:59
int $forcedTargetID
Hack for foreign blocking (CentralAuth)
Definition Block.php:65
static selectFields()
Return the list of ipblocks fields that should be selected to create a new block.
Definition Block.php:206
setCookie(WebResponse $response)
Set the 'BlockID' cookie to this block's ID and expiry time.
Definition Block.php:1457
getPermissionsError(IContextSource $context)
Get the key and parameters for the corresponding error message.
Definition Block.php:1544
static clearCookie(WebResponse $response)
Unset the 'BlockID' cookie.
Definition Block.php:1481
bool $isAutoblocking
Definition Block.php:77
insert( $dbw=null)
Insert a block into the block table.
Definition Block.php:477
static getRangeCond( $start, $end=null)
Get a set of SQL conditions which will select rangeblocks encompassing a given range.
Definition Block.php:360
newLoad( $vagueTarget=null)
Load a block from the database which affects the already-set $this->target: 1) A block directly on th...
Definition Block.php:259
update()
Update a block in the DB with new parameters.
Definition Block.php:545
prevents( $action, $x=null)
Get/set whether the Block prevents a given action.
Definition Block.php:1012
static newFromRow( $row)
Create a new Block object from a database row.
Definition Block.php:441
getDatabaseArray(IDatabase $dbw)
Get an array suitable for passing to $dbw->insert() or $dbw->update()
Definition Block.php:592
isHardblock( $x=null)
Get/set whether the Block is a hardblock (affects logged-in users on a given IP/range)
Definition Block.php:982
isValid()
Is the block address valid (i.e.
Definition Block.php:862
User string $target
Definition Block.php:62
bool $isHardblock
Definition Block.php:74
setBlocker( $user)
Set the user who implemented (or will implement) this block.
Definition Block.php:1445
getRedactedName()
Get the block name, but with autoblocked IPs hidden as per standard privacy policy.
Definition Block.php:1047
isExpired()
Has the block expired?
Definition Block.php:847
static newFromID( $id)
Load a blocked user from their block id.
Definition Block.php:184
static getAutoblockExpiry( $timestamp)
Get a timestamp of the expiry for autoblocks.
Definition Block.php:1065
int $mId
Definition Block.php:47
static getBlocksForIPList(array $ipChain, $isAnon, $fromMaster=false)
Get all blocks that match any IP from an array of IP addresses.
Definition Block.php:1151
getRangeEnd()
Get the IP address at the end of the range in Hex form.
Definition Block.php:912
getAutoblockUpdateArray(IDatabase $dbw)
Definition Block.php:627
static getIpFragment( $hex)
Get the component of an IP address which is certain to be the same between an IP address and a rangeb...
Definition Block.php:393
static parseTarget( $target)
From an existing Block, get the target and the type of target.
Definition Block.php:1334
bool $mBlockEmail
Definition Block.php:53
string null $systemBlockType
Definition Block.php:80
static chooseBlock(array $blocks, array $ipChain)
From a list of multiple blocks, find the most exact and strongest Block.
Definition Block.php:1232
bool $mDisableUsertalk
Definition Block.php:56
getSystemBlockType()
Get the system block type, if any.
Definition Block.php:963
initFromRow( $row)
Given a database row from the ipblocks table, initialize member variables.
Definition Block.php:407
static isWhitelistedFromAutoblocks( $ip)
Checks whether a given IP is on the autoblock whitelist.
Definition Block.php:707
getId()
Get the block ID.
Definition Block.php:954
getExpiry()
Definition Block.php:1421
getType()
Get the type of target for this particular block.
Definition Block.php:1389
const TYPE_ID
Definition Block.php:87
__construct( $options=[])
Create a new block with specified parameters on a user, IP or IP range.
Definition Block.php:115
getBy()
Get the user id of the blocking sysop.
Definition Block.php:931
setTarget( $target)
Set the target for this block, and update $this->type accordingly.
Definition Block.php:1429
const TYPE_RANGE
Definition Block.php:85
string $mExpiry
Definition Block.php:38
updateTimestamp()
Update the timestamp on autoblocks.
Definition Block.php:869
fromMaster( $x=null)
Get/set a flag determining whether the master is used for reads.
Definition Block.php:973
doAutoblock( $autoblockIP)
Autoblocks the given IP, referring to this Block.
Definition Block.php:753
bool $mHideName
Definition Block.php:41
deleteIfExpired()
Check if a block has expired.
Definition Block.php:830
User $blocker
Definition Block.php:71
doRetroactiveAutoblock()
Retroactively autoblocks the last IP used by the user (if it is a user) blocked by this Block.
Definition Block.php:643
static getIdFromCookieValue( $cookieValue)
Get the stored ID from the 'BlockID' cookie.
Definition Block.php:1517
bool $mFromMaster
Definition Block.php:50
int $mParentBlockId
Definition Block.php:44
getTargetAndType()
Get the target and target type for this particular Block.
Definition Block.php:1402
getTarget()
Get the target for this particular Block.
Definition Block.php:1412
const TYPE_USER
Definition Block.php:83
static purgeExpired()
Purge expired blocks from the ipblocks table.
Definition Block.php:1074
getRangeStart()
Get the IP address at the start of the range in Hex form.
Definition Block.php:893
getCookieValue()
Get the BlockID cookie's value for this block.
Definition Block.php:1494
getByName()
Get the username of the blocking sysop.
Definition Block.php:943
equals(Block $block)
Check if two blocks are effectively equal.
Definition Block.php:233
isAutoblocking( $x=null)
Definition Block.php:995
string $mReason
Definition Block.php:29
const TYPE_AUTO
Definition Block.php:86
static defaultRetroactiveAutoblock(Block $block, array &$blockIds)
Retroactively autoblocks the last IP used by the user (if it is a user) blocked by this Block.
Definition Block.php:666
getBlocker()
Get the user who implemented this block.
Definition Block.php:1437
bool $mAuto
Definition Block.php:35
static newFromTarget( $specificTarget, $vagueTarget=null, $fromMaster=false)
Given a target and the target's type, get an existing Block object if possible.
Definition Block.php:1112
int $type
Block::TYPE_ constant.
Definition Block.php:68
string $mTimestamp
Definition Block.php:32
const TYPE_IP
Definition Block.php:84
static newKey( $key)
Static constructor for easier chaining.
static hmac( $data, $key, $raw=true)
Generate an acceptably unstable one-way-hmac of some text making use of the best hash algorithm that ...
MediaWiki exception.
MediaWikiServices is the service locator for the application scope of MediaWiki.
static getMain()
Static methods.
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:51
getName()
Get the user name, or the IP of an anonymous user.
Definition User.php:2249
isAllowed( $action='')
Internal mechanics of testing a permission.
Definition User.php:3565
getId()
Get the user's ID.
Definition User.php:2224
getUserPage()
Get this user's personal page title.
Definition User.php:4313
Allow programs to request this object from WebRequest::response() and handle all outputting (or lack ...
setCookie( $name, $value, $expire=0, $options=[])
Set the browser cookie.
Relational database abstraction object.
Definition Database.php:45
$res
Definition database.txt:21
deferred txt A few of the database updates required by various functions here can be deferred until after the result page is displayed to the user For updating the view updating the linked to tables after a etc PHP does not yet have any way to tell the server to actually return and disconnect while still running these but it might have such a feature in the future We handle these by creating a deferred update object and putting those objects on a global list
Definition deferred.txt:11
when a variable name is used in a function
Definition design.txt:94
when a variable name is used in a it is silently declared as a new local masking the global
Definition design.txt:95
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
const LIST_OR
Definition Defines.php:47
const LIST_AND
Definition Defines.php:44
Status::newGood()` to allow deletion, and then `return false` from the hook function. Ensure you consume the 'ChangeTagAfterDelete' hook to carry out custom deletion actions. $tag:name of the tag $user:user initiating the action & $status:Status object. See above. 'ChangeTagsListActive':Allows you to nominate which of the tags your extension uses are in active use. & $tags:list of all active tags. Append to this array. 'ChangeTagsAfterUpdateTags':Called after tags have been updated with the ChangeTags::updateTags function. Params:$addedTags:tags effectively added in the update $removedTags:tags effectively removed in the update $prevTags:tags that were present prior to the update $rc_id:recentchanges table id $rev_id:revision table id $log_id:logging table id $params:tag params $rc:RecentChange being tagged when the tagging accompanies the action or null $user:User who performed the tagging when the tagging is subsequent to the action or null 'ChangeTagsAllowedAdd':Called when checking if a user can add tags to a change. & $allowedTags:List of all the tags the user is allowed to add. Any tags the user wants to add( $addTags) that are not in this array will cause it to fail. You may add or remove tags to this array as required. $addTags:List of tags user intends to add. $user:User who is adding the tags. 'ChangeUserGroups':Called before user groups are changed. $performer:The User who will perform the change $user:The User whose groups will be changed & $add:The groups that will be added & $remove:The groups that will be removed 'Collation::factory':Called if $wgCategoryCollation is an unknown collation. $collationName:Name of the collation in question & $collationObject:Null. Replace with a subclass of the Collation class that implements the collation given in $collationName. 'ConfirmEmailComplete':Called after a user 's email has been confirmed successfully. $user:user(object) whose email is being confirmed 'ContentAlterParserOutput':Modify parser output for a given content object. Called by Content::getParserOutput after parsing has finished. Can be used for changes that depend on the result of the parsing but have to be done before LinksUpdate is called(such as adding tracking categories based on the rendered HTML). $content:The Content to render $title:Title of the page, as context $parserOutput:ParserOutput to manipulate 'ContentGetParserOutput':Customize parser output for a given content object, called by AbstractContent::getParserOutput. May be used to override the normal model-specific rendering of page content. $content:The Content to render $title:Title of the page, as context $revId:The revision ID, as context $options:ParserOptions for rendering. To avoid confusing the parser cache, the output can only depend on parameters provided to this hook function, not on global state. $generateHtml:boolean, indicating whether full HTML should be generated. If false, generation of HTML may be skipped, but other information should still be present in the ParserOutput object. & $output:ParserOutput, to manipulate or replace 'ContentHandlerDefaultModelFor':Called when the default content model is determined for a given title. May be used to assign a different model for that title. $title:the Title in question & $model:the model name. Use with CONTENT_MODEL_XXX constants. 'ContentHandlerForModelID':Called when a ContentHandler is requested for a given content model name, but no entry for that model exists in $wgContentHandlers. Note:if your extension implements additional models via this hook, please use GetContentModels hook to make them known to core. $modeName:the requested content model name & $handler:set this to a ContentHandler object, if desired. 'ContentModelCanBeUsedOn':Called to determine whether that content model can be used on a given page. This is especially useful to prevent some content models to be used in some special location. $contentModel:ID of the content model in question $title:the Title in question. & $ok:Output parameter, whether it is OK to use $contentModel on $title. Handler functions that modify $ok should generally return false to prevent further hooks from further modifying $ok. 'ContribsPager::getQueryInfo':Before the contributions query is about to run & $pager:Pager object for contributions & $queryInfo:The query for the contribs Pager 'ContribsPager::reallyDoQuery':Called before really executing the query for My Contributions & $data:an array of results of all contribs queries $pager:The ContribsPager object hooked into $offset:Index offset, inclusive $limit:Exact query limit $descending:Query direction, false for ascending, true for descending 'ContributionsLineEnding':Called before a contributions HTML line is finished $page:SpecialPage object for contributions & $ret:the HTML line $row:the DB row for this line & $classes:the classes to add to the surrounding< li > & $attribs:associative array of other HTML attributes for the< li > element. Currently only data attributes reserved to MediaWiki are allowed(see Sanitizer::isReservedDataAttribute). 'ContributionsToolLinks':Change tool links above Special:Contributions $id:User identifier $title:User page title & $tools:Array of tool links $specialPage:SpecialPage instance for context and services. Can be either SpecialContributions or DeletedContributionsPage. Extensions should type hint against a generic SpecialPage though. 'ConvertContent':Called by AbstractContent::convert when a conversion to another content model is requested. Handler functions that modify $result should generally return false to disable further attempts at conversion. $content:The Content object to be converted. $toModel:The ID of the content model to convert to. $lossy:boolean indicating whether lossy conversion is allowed. & $result:Output parameter, in case the handler function wants to provide a converted Content object. Note that $result->getContentModel() must return $toModel. 'CustomEditor':When invoking the page editor Return true to allow the normal editor to be used, or false if implementing a custom editor, e.g. for a special namespace, etc. $article:Article being edited $user:User performing the edit 'DatabaseOraclePostInit':Called after initialising an Oracle database $db:the DatabaseOracle object 'DeletedContribsPager::reallyDoQuery':Called before really executing the query for Special:DeletedContributions Similar to ContribsPager::reallyDoQuery & $data:an array of results of all contribs queries $pager:The DeletedContribsPager object hooked into $offset:Index offset, inclusive $limit:Exact query limit $descending:Query direction, false for ascending, true for descending 'DeletedContributionsLineEnding':Called before a DeletedContributions HTML line is finished. Similar to ContributionsLineEnding $page:SpecialPage object for DeletedContributions & $ret:the HTML line $row:the DB row for this line & $classes:the classes to add to the surrounding< li > & $attribs:associative array of other HTML attributes for the< li > element. Currently only data attributes reserved to MediaWiki are allowed(see Sanitizer::isReservedDataAttribute). 'DifferenceEngineAfterLoadNewText':called in DifferenceEngine::loadNewText() after the new revision 's content has been loaded into the class member variable $differenceEngine->mNewContent but before returning true from this function. $differenceEngine:DifferenceEngine object 'DifferenceEngineLoadTextAfterNewContentIsLoaded':called in DifferenceEngine::loadText() after the new revision 's content has been loaded into the class member variable $differenceEngine->mNewContent but before checking if the variable 's value is null. This hook can be used to inject content into said class member variable. $differenceEngine:DifferenceEngine object 'DifferenceEngineMarkPatrolledLink':Allows extensions to change the "mark as patrolled" link which is shown both on the diff header as well as on the bottom of a page, usually wrapped in a span element which has class="patrollink". $differenceEngine:DifferenceEngine object & $markAsPatrolledLink:The "mark as patrolled" link HTML(string) $rcid:Recent change ID(rc_id) for this change(int) 'DifferenceEngineMarkPatrolledRCID':Allows extensions to possibly change the rcid parameter. For example the rcid might be set to zero due to the user being the same as the performer of the change but an extension might still want to show it under certain conditions. & $rcid:rc_id(int) of the change or 0 $differenceEngine:DifferenceEngine object $change:RecentChange object $user:User object representing the current user 'DifferenceEngineNewHeader':Allows extensions to change the $newHeader variable, which contains information about the new revision, such as the revision 's author, whether the revision was marked as a minor edit or not, etc. $differenceEngine:DifferenceEngine object & $newHeader:The string containing the various #mw-diff-otitle[1-5] divs, which include things like revision author info, revision comment, RevisionDelete link and more $formattedRevisionTools:Array containing revision tools, some of which may have been injected with the DiffRevisionTools hook $nextlink:String containing the link to the next revision(if any) $status
Definition hooks.txt:1245
the array() calling protocol came about after MediaWiki 1.4rc1.
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 also a ContextSource after deleting those rows but within the same transaction $rows
Definition hooks.txt:2746
This code would result in ircNotify being run twice when an article is and once for brion Hooks can return three possible true was required This is the default since MediaWiki *some string
Definition hooks.txt:181
either a plain
Definition hooks.txt:2026
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped & $options
Definition hooks.txt:1971
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 also a ContextSource after deleting those rows but within the same transaction you ll probably need to make sure the header is varied on and they can depend only on the ResourceLoaderContext $context
Definition hooks.txt:2780
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:1976
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition hooks.txt:2989
this hook is for auditing only $response
Definition hooks.txt:781
processing should stop and the error should be shown to the user * false
Definition hooks.txt:187
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a local account $user
Definition hooks.txt:247
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:37
Interface for objects which can provide a MediaWiki context on request.
getLanguage()
Get the Language object.
Basic database interface for live and lazy-loaded relation database handles.
Definition IDatabase.php:40
delete( $table, $conds, $fname=__METHOD__)
DELETE query wrapper.
addQuotes( $s)
Adds quotes and backslashes.
timestamp( $ts=0)
Convert a timestamp in one of the formats accepted by wfTimestamp() to the format used for inserting ...
encodeExpiry( $expiry)
Encode an expiry time into the DBMS dependent format.
$cache
Definition mcc.php:33
$expiryTime
This document describes the state of Postgres support in and is fairly well maintained The main code is very well while extensions are very hit and miss it is probably the most supported database after MySQL Much of the work in making MediaWiki database agnostic came about through the work of creating Postgres as and are nearing end of but without copying over all the usage comments General notes on the but these can almost always be programmed around *Although Postgres has a true BOOLEAN type
Definition postgres.txt:36
const DB_REPLICA
Definition defines.php:25
const DB_MASTER
Definition defines.php:26
$lines
Definition router.php:61
if(!isset( $args[0])) $lang