Commit graph

86 commits

Author SHA1 Message Date
WMDE-Fisch
21c5baff50 Block: Drop AbstractBlock::getPermissionsError
Bug: T362636
Change-Id: I6ddfa089aee47451819a92eaae100c1cc8486d20
2024-05-04 18:22:03 +03:00
James D. Forrester
eeb5a740b3 Namespace Message, move to appropriate directory
Bug: T353458
Change-Id: I088cbc53fbcdb974e5b05b45a62e91709dacc024
2024-02-14 15:10:36 -05:00
James D. Forrester
4bae64d1c7 Namespace includes/context
Bug: T353458
Change-Id: I4dbef138fd0110c14c70214282519189d70c94fb
2024-02-08 11:07:01 -05:00
jenkins-bot
d023c7fb99 Merge "Move creation of BlockErrorFormatter into FormatterFactory" 2024-02-02 22:29:47 +00:00
thiemowmde
ab1e3a66e7 Make use of the [ ... ] operator instead of array_merge
The array spread operator is documented to behave identical to
array_merge. The syntax is just much shorter and easier to read in
situations like this, in my opinion.

Change-Id: I3b016e896e552af53d87d5e72436dc4e29070ce1
2024-01-31 15:47:44 +01:00
daniel
3d55397207 Move creation of BlockErrorFormatter into FormatterFactory
The idea is that all formatters that need the user language or
other request specific context should be instantiated by
FormatterFactory.

Change-Id: I8334cc89dcf0f293298b82e004116be50a90f0d1
2024-01-26 13:03:44 -05:00
Umherirrender
9b0591448e Use UserFactory::newAnonymous
Bug: T325686
Change-Id: Ia7ce7df94c233a4534625d250229806fb21d8017
2024-01-22 20:39:05 +00:00
jenkins-bot
440b56cbb2 Merge "Drop User::blockedFor() and AbstractBlock::getReason(), deprecated in 1.35" 2024-01-12 17:37:41 +00:00
Alexander Vorwerk
fd77427f47 Make BlockUtils a wiki-aware service
Bug: T353620
Bug: T354298
Change-Id: I46699071bbdbbde650061dece6b67a0c58444523
2024-01-03 20:05:40 +01:00
James D. Forrester
de07700e86 Drop User::blockedFor() and AbstractBlock::getReason(), deprecated in 1.35
Bug: T277511
Change-Id: I35c16f92470e65126db8961ee3599256c96ec554
2023-12-11 16:20:16 +00:00
Tim Starling
d6727856fd Support new block schema
Support migration stages when reading and writing blocks.

I tried to set it up for an easy next stage, in which support for the
old schema is removed. I tried to avoid factoring out of shared code
between the two schemas, so that the old schema cases can simply be
deleted without the need to revert unnecessary abstractions.

However, I added HideUserUtils to factor out ipb_deleted queries. Code
review showed that this was already quite complex, with multiple
approaches to the problem, so it benefits from refactoring even without
the schema abstraction.

HideUserUtils is a service rather than a standalone class to support
unit tests, since unit tests do not allow global config access. When
the migration stage config is removed, it will be a service with no
constructor parameters -- an unnecessary abstraction which should
ideally be resolved at that time.

When interpreting result rows, it is possible to share code by using
field aliases. But when constructing WHERE conditions, the actual field
names need to be used, so the migration is more intrusive in
ApiQueryBlocks and SpecialBlockList, where complex conditions are used.

Bug: T346293
Bug: T51504
Bug: T349883
Change-Id: I408acf7a57b0100fe18c455fc13141277a598925
2023-11-29 13:31:42 +11:00
jenkins-bot
b011c84294 Merge "block,Permissions: Blocks should not apply to implicit rights" 2023-11-06 17:31:40 +00:00
daniel
898d11f9b7 block,Permissions: Blocks should not apply to implicit rights
Implicit rights represent actions granted to all users.
They can be rate limited, but not revoked.

Bug: T350202
Bug: T350117
Bug: T350347
Change-Id: I22bf5e60c38900f881d43577351761827066a25c
2023-11-06 17:06:47 +00:00
Tim Starling
d06ac1a3cd Move DatabaseBlock read query methods to DatabaseBlockStore
* Move to DatabaseBlockStore the DatabaseBlock methods newFromID,
  getQueryInfo, getRangeCond, newFromRow, isExemptedFromAutoblocks,
  doAutoblock, updateTimestamp, getAutoblockExpiry, newFromTarget,
  newListFromTarget.
* Split DatabaseBlock::getBlocksForIPList. Now
  BlockManager::getBlocksForIPList() is responsible for XFF header
  validation and trusted proxy handling. DatabaseBlockStore::
  newListFromIPs() just does the queries and constructs the Block
  objects.
* In DatabaseBlockStore::newFromRow() and doAutoblock(), use the
  DatabaseBlock constructor instead of calling many setter methods. Add
  constructor options decodedExpiry, decodedTimestamp, id,
  parentBlockId and restrictions to support this.
* Move isExemptedFromAutoblocks() to its own service. Remove the cache
  since in my testing with production eval.php, the WAN cache fetch is
  10 times slower than just using the message cache, contradicting the
  comment written in 2008.
* Fix AuthManagerTest which was previously passing an unrecognised
  "restrictions" option to DatabaseBlock. Now that the option actually
  works, we have to use the right type.

Bug: T255433
Change-Id: I5049e60be1681f67fcca133e569e315792dc42dd
2023-11-03 11:00:57 +11:00
Tim Starling
66f85fa125 AuthManager: deny auto-creation for globally blocked users
* In AuthManager::autoCreateUser(), check the permissions of the
  performer instead of relying on the secondary providers. This means
  that auto-creation will be denied when the anonymous user is globally
  IP-blocked.
* Remove create account block check from
  CheckBlocksSecondaryAuthenticationProvider. testUserForCreation() is
  supposed to only do target name checks, but it's not actually
  possible to block a non-existent local name. So we don't need this
  code.
* Add a $performer parameter to autoCreateUser() so that
  Special:CreateLocalAccount can have elevated permissions when it
  creates an account with IP block exemption.
* When a performer is passed, don't use the session as a cache.
* Since we are passing autocreateaccount as the action to
  PermissionManager instead of createaccount, some special cases need to
  be tweaked. Previously AuthManager checked for either
  autocreateaccount or createaccount rights. Now PermissionManager does
  that when the action is autocreateaccount.

By removing redundant checks from testUserForCreation(), the number of
ipblocks queries during a normal Special:CreateAccount post request is
reduced from 8 to 6.

The CentralAuth change I7e7a7fc8bcd86285f857063a38de02b41b5175d0 should
be merged immediately after this one.

Bug: T234371
Bug: T345683
Change-Id: If2937c7d717d2adc249f608d4585122b02a43fff
2023-10-20 10:07:50 +11:00
Tim Starling
9e7c9c13b9 Multiblocks preparation in User::getBlock(), PermissionManager and BlockManager
Functional changes:

* On account creation, if both a user block and an IP block apply,
  return a CompositeBlock instead of picking a winner.
* On other actions, instead of returning the whole CompositeBlock if
  some part of it matches the specified page and action, filter the
  individual parts of the CompositeBlock by whether or not they match.

Refactoring:

* Add BlockManager::getBlock(), which is the same as getUserBlock()
  except that it expects the caller to do IP exemption checking.
  Deprecate getUserBlock().
* Migrate some callers of User::getBlock() to use BlockManager.
* Have PermissionManager use BlockManager instead of
  User::getBlock().
* Add $request parameter to PermissionManager::getApplicableBlock(),
  to fix the weird bundling of the request with the User.
* Move the createaccount block database fetch from PermissionManager to
  BlockManager, where it can easily be cached.
* Fix code duplication between PermissionManager::isBlockedFrom() and
  PermissionManager::getApplicableBlock() by having the former call the
  latter.
* In CheckBlocksSecondaryAuthenticationProvider, use
  AuthManager::getRequest(). Previously it used the global request and
  also User::getRequest() via User::getBlock().

Bug: T345683
Change-Id: Icd75025665282a34d8854bda267a0eb3d759719c
2023-10-20 10:07:50 +11:00
Tim Starling
03a9bf2794 Add Block::toArray()
There are 7 callers of CompositeBlock::getOriginalBlocks() in code
search, and all are following a pattern like

  $blocks = $block instanceof CompositeBlock
    ? $block->getOriginalBlock() : [ $block ]

or can be reorganised to follow such a pattern, which suggests the need
for a public interface along these lines.

So, add Block::toArray() which returns the list of blocks in a
CompositeBlock, or wraps the single block in an array if the block is
not a CompositeBlock.

Also, remove the need for a phan-var override by moving the instanceof
DatabaseBlock check from shouldTrackBlockWithCookie() to its caller.

Bug: T345683
Change-Id: If769db9a831f63a79b57fb3bc9c7cece511c70c3
2023-09-21 10:27:21 +10:00
jenkins-bot
512502fcc9 Merge "Blocks cleanup" 2023-09-20 05:43:55 +00:00
Tim Starling
cdbd39bfdf Blocks cleanup
* Make BlockManager internal methods private, since nothing calls them
  anymore.
* In AbstractBlock and DatabaseBlock, remove deprecated public
  properties mExpiry, mHideName, mTimestamp, mAuto and mParentBlockId.
* In BlockRestrictionStore, remove all the "instanceof Restriction"
  checks. If someone passes in something that's not a Restriction, we
  should throw, not ignore it, because we don't know the caller's
  intention. Add a type declaration to $hasher in equals() so that it
  will throw.
* Remove the "m" prefix from all private and protected properties.
  AbstractBlock is not stable to override so this is not a stable
  interface break.
* In BlockRestrictionStore::restrictionsToRemove(), use an O(N)
  algorithm.
* In BlockRestrictionStore::rowToRestriction(), use a switch instead of
  a type map, so that the calls are statically analyzable.
* In BlockUser::__construct(), fix the initialisation order issue by
  inlining the relevant logic.
* Rename variable $actionRestriction.
* In Special:Block, fix call to deprecated method getTargetAndType(),
  and hard deprecate it. @deprecated has the effect of deprecating a
  method for both internal and external callers, there's no such thing
  as an external-only deprecation. So it's necessary to rename it if you
  want to keep it as a private method.

Bug: T345683
Change-Id: If4a4a18d7b5fec825417de81302266119c215fd3
2023-09-20 09:40:52 +10:00
James D. Forrester
1d0b7ae1e2 Namespace User under \MediaWiki\User
Bug: T166010
Change-Id: I7257302b485588af31384d4f7fc8e30551f161f1
2023-09-19 19:18:16 +00:00
Tim Starling
1ee50f1a77 Blocks documentation review
* Use a consistent single-line comment style.
* Start each sentence with a capital letter.
* Use imperative mood.
* Put a space between the adjective and the noun in "hardblock" and
  "rangeblock".
* Capitalise acronyms.
* Break long lines.
* Remove excessively obvious or off-topic comments.
* Indent the non-initial lines of a multi-line @ command.
* Don't try to apply @deprecated to a parameter. Doxygen and PHPStorm
  interpret this as method deprecation.

Bug: T345683
Change-Id: I7df126f7a031dde241dd46f66e5e83722f9b383c
2023-09-06 13:48:07 +10:00
jenkins-bot
65ca23c095 Merge "Ensure that soft blocks block temporary users" 2023-08-17 10:32:07 +00:00
James D. Forrester
984c540494 Follow-up 857d9d80f: AbstractBlock: Hard-deprecate getReason()
This is unfortunately by necessity a breaking change to the soon-to-
be-removed mBlockreason from string to CommentStoreComment. Oh well.

Change-Id: Iac918def95fb0600f2d805d2d95732b280d8fd81
2023-08-16 09:59:51 +08:00
Thalia
3eff3be2a4 Ensure that soft blocks block temporary users
Why:

* A soft block against an IP address does not block logged-in
  users, but it should block temporary users, otherwise anonymous
  users can circumvent it by creating a temporary account.

What:

* Return a block for a temporary user if a soft block is found
  against their IP address, from DatabaseBlock::newLoad
* Return soft blocks for temporary users in
  BlockManager::getXffBlocks
* Update documentation to make clear that soft blocks apply to
  temporary users, including where they are referred to as
  anon-only blocks

Bug: T343714
Change-Id: I5a2928eb3370bd47544b164b08014ef62297ce36
2023-08-15 14:26:44 +01:00
Dayllan Maza
a822b39d33 Blocks: Misc changes
* Removed un-used property
* Simplified return value and swift statement

Change-Id: I57db9d2f160bec5d9ae8afd6805e7ce07daf63c1
2023-08-02 23:23:09 -04:00
Alexander Vorwerk
6c9aa2242f Throw for mismatched wiki in AbstractBlock::getId()
Bug: T274817
Change-Id: I5b2c27ac3639ef92a55ff7b3cbc011a489c5705a
2023-05-29 23:42:30 +02:00
Alexander Vorwerk
ed3f707556 AbstractBlock: Hard-deprecate accessing block IDs cross-wiki
Bug: T274817
Change-Id: I5c1ee557db9be3ebce8592d06dac520df2dd1cad
2023-03-10 16:09:43 -05:00
James D. Forrester
ad06527fb4 Reorg: Namespace the Title class
This is moderately messy.

Process was principally:

* xargs rg --files-with-matches '^use Title;' | grep 'php$' | \
  xargs -P 1 -n 1 sed -i -z 's/use Title;/use MediaWiki\\Title\\Title;/1'
* rg --files-without-match 'MediaWiki\\Title\\Title;' . | grep 'php$' | \
  xargs rg --files-with-matches 'Title\b' | \
  xargs -P 1 -n 1 sed -i -z 's/\nuse /\nuse MediaWiki\\Title\\Title;\nuse /1'
* composer fix

Then manual fix-ups for a few files that don't have any use statements.

Bug: T166010
Follows-Up: Ia5d8cb759dc3bc9e9bbe217d0fb109e2f8c4101a
Change-Id: If8fc9d0d95fc1a114021e282a706fc3e7da3524b
2023-03-02 08:46:53 -05:00
Umherirrender
2e84ae2d96 block: Fix typo in AbstractBlock::setTimestamp
Change-Id: I254b39351e13f4e468935c3e39e2c897dee83065
2023-02-16 00:14:04 +01:00
Alexander Vorwerk
f6bd18d6c2 Split a base class out of CommentStore
so that extensions (i.e. CheckUser) can implement their own comment
store without having a lot of code duplication

basically the comment store version of I3a6486532f2ef36

Bug: T233004
Change-Id: Ib40f99e00a514d41776ce521baf113e46d37e9cd
2023-01-01 22:34:36 +00:00
csimiyu
1a12b163ad Hard deprecate AbstractBlock::getPermissionsError
Bug: T317206
Change-Id: Ia5920bc2c711311cf6efd94331c539e70e91703a
2022-11-23 12:24:16 +03:00
Tim Starling
0077c5da15 Use short array destructuring instead of list()
Introduced in PHP 7.1. Because it's shorter and looks nice.

I used regex replacement.

Change-Id: I0555e199d126cd44501f859cb4589f8bd49694da
2022-10-21 15:33:37 +11:00
Daimona Eaytoy
350e9b88c1 Fixes for the phan upgrade, part 1
Mainly, document some parameters as non-empty-array so that phan knows
the list of arguments won't be empty when unpacking.

In EditPage, account for hooks potentially unsetting the copyright
notice.

Also rewrite some code in LogPager, so it's hopefully easier for phan to
understand what's going on.

Change-Id: Ic0638571554424098d0743db32dd46723a08e103
2022-10-08 13:08:47 +00:00
Gergő Tisza
e83dcb3fb3 Fix and document AbstractBlock::getPermissionsError()
This method is supposed to return a single message in array form
(['key', 'param1', ...]), as can be seen e.g. in includes/Block.php
in MediaWiki 1.31 of the includes/block classes in 1.34.

Allow for the key to be a Message object, which means passing the
array contents as a functions parameter list will work with most
message-related functions, is already used by the GlobalBlocking
extension, and allows more complex messages not possible with plain
arrays.

Change-Id: Ic19dc1dde25794f9bb3b3b5d8abba8ddf8948886
2022-08-23 19:30:16 +00:00
Matěj Suchánek
012011d941 Hard deprecate access to public properties on AbstractBlock/DatabaseBlock
Change-Id: I3cca210627d94d72e09beb52f8b015181f14ed7c
2022-06-23 12:43:58 +02:00
Umherirrender
de47d93928 Get the MainConfig from MediaWikiServices instead of RequestContext
The getConfig of a ContextSource should only be used, if the
ContextSource is available. Getting the global context just for the
config looks harder to fix/inject as using the MainConfig from
MediaWikiServices

Change-Id: Iaf14bfc7bd68cc315672e1c256887faf87e22542
2022-05-24 21:54:21 +00:00
Aryeh Gregor
7b791474a5 Use MainConfigNames instead of string literals, #4
Now largely automated:

VARS=$(grep -o "'[A-Za-z0-9_]*'" includes/MainConfigNames.php | \
  tr "\n" '|' | sed "s/|$/\n/;s/'//g")
sed -i -E "s/'($VARS)'/MainConfigNames::\1/g" \
  $(grep -ERIl "'($VARS)'" includes/)

Then git add -p with lots of error-prone manual checking. Then
semi-manually add all the necessary "use" lines:

vim $(grep -L 'use MediaWiki\\MainConfigNames;' \
  $(git diff --cached --name-only --diff-filter=M HEAD^))

I didn't bother fixing lines that were over 100 characters unless they
were over 120 and triggered phpcs.

Bug: T305805
Change-Id: I74e0ab511abecb276717ad4276a124760a268147
2022-04-26 19:03:37 +03:00
Alexander Vorwerk
b6793e47de Make Block objects aware of which wiki they belong to
Bug: T274817
Depends-On: I1c46c712a3afefce56238108cb2e78382dd41956
Change-Id: I8ae8133f7e232cc75aae6b72fcd7feaeb313cba7
2022-01-17 21:49:20 +01:00
Petr Pchelko
25bb5b296a Cleanup hard-deprecated code in blocks.
Change-Id: I1b3f4a0f072197c6b3dc6c9a80fcb2946aeb6360
2021-10-26 06:44:05 -07:00
jenkins-bot
96f08fa709 Merge "Convert AbstractBlock::$target to UserIdentity" 2021-07-27 15:30:20 +00:00
libraryupgrader
5357695270 build: Updating dependencies
composer:
* mediawiki/mediawiki-codesniffer: 36.0.0 → 37.0.0
  The following sniffs now pass and were enabled:
  * Generic.ControlStructures.InlineControlStructure
  * MediaWiki.PHPUnit.AssertCount.NotUsed

npm:
* svgo: 2.3.0 → 2.3.1
  * https://npmjs.com/advisories/1754 (CVE-2021-33587)

Change-Id: I2a9bbee2fecbf7259876d335f565ece4b3622426
2021-07-22 03:36:05 +00:00
Alexander Vorwerk
96896bb0db Convert AbstractBlock::$target to UserIdentity
Change-Id: Ida1ad099e9716b55b64011f882fd5ca79ba9dd22
2021-07-21 22:40:17 +02:00
jenkins-bot
7a79548ac7 Merge "Convert BlockUtils::parseBlockTarget to UserIdentity" 2021-07-20 21:54:33 +00:00
Alexander Vorwerk
825ac0232c Convert BlockUtils::parseBlockTarget to UserIdentity
Bug: T286490
Change-Id: Ice96180690828bcf2efd60faf6ad10d64307713d
2021-07-20 22:12:26 +02:00
Alexander Vorwerk
bae3a25e6a Remove AbstractBlock::parseTarget()
deprecated since 1.36 and unused

Change-Id: I9d67d7f4294d175b536a518b2c89cd620773f7b9
2021-07-16 01:00:08 +02:00
vladshapik
e991dff925 Hard-deprecate AbstractBlock::getTargetAndType() and getTarget()
Replace all uses of AbstractBlock::getTarget with
Block ::getTargetName and ::getTargetUserIdentity.
Create AbstractBlockTest and two test cases for
AbstractBlock::getTarget and ::getTargetAndType.
It tests triggering of the deprecation warning.

Bug: T282247
Depends-On: I0543f363af66c57f5763b91320d87a69f23f9466
Change-Id: Iaeca824cac30172178de72f3cf7b7ae4cdd6f880
2021-06-22 16:59:00 +03:00
DannyS712
e6c6a525cb Add return typehints to Block interface and classes implementing it
Change-Id: I422895aca72cb34ceaee9e741b2b988786ebbfdc
2021-05-14 21:16:59 +00:00
daniel
753b1bcaff Introduce Block interface and replace AbstractBlock.
In order to allow Authority to know about user blocks,
we need a narrow interface to represent such blocks.

This deprecates some methods on AbstractBlocks in favor
of new methods on the Block interface that avoid binding to
the User class.

Bug: T271494
Change-Id: I7bb950533970984a014de0434518fbbefb695131
2021-05-11 11:36:11 +02:00
Thalia
c67f181dd4 Introduce infrastructure for partial blocks for actions
This adds a new type of block restriction for actions, which extends
AbstractRestriction. Like page and namespace restrictions, action
restrictions are stored in the ipblocks_restrictions table.

Blockable actions are defined in a BlockActionInfo service, with a
method for getting all the blockable actions, getAllBlockActions.

Action blocks are checked for in PermissionManager::checkUserBlock
using DatabaseBlock::appliesToRight. To make this work, this patch
also removes the 'edit' case from AbstractBlock::appliesToRight,
which always returned true. This was incorrect, as blocks do not
always apply to edit, so cases that called appliesToRight('edit')
were fixed before this commit. appliesToRight('edit') now returns
null (i.e. unsure), which is correct because it is not possible to
determine whether a block applies to editing a particular page
without knowing what that page is, and appliesToRight doesn't know
that page.

There are some flags on sitewide blocks that predate partial blocks,
which block particular actions: 'createaccount' and 'sendemail'.
These are still handled in AbstractBlock::appliesToRight, and are
still checked for separately in the peripheral components.

The feature flag $wgEnablePartialActionBlocks must set to true to
enable partial action blocks.

Bug: T279556
Bug: T6995
Change-Id: I17962bb7c4247a12c722e7bc6bcaf8c36efd8600
2021-04-27 21:53:13 +01:00
Reedy
fb771021ea Use some more neutral language
Bug: T277987
Change-Id: Ieceb01f7a61693a0f03cc331213cb8f93163b8e9
2021-04-18 16:49:36 +01:00