Commit graph

627 commits

Author SHA1 Message Date
James D. Forrester
abdc94a3da Swap out uses of User->isLoggedIn() with ->isRegistered()
Bug: T270450
Change-Id: I90ff94a553ebae9a4ef97c25c333f067f19c327d
2020-12-22 03:13:37 +00:00
DannyS712
f4e21014ce Deprecate a bunch of global functions
* wfAcceptToPrefs
* wfClearOutputBuffers
* wfConfiguredReadOnlyReason
* wfDebugMem
* wfGetPrecompiledData
* wfNegotiateType

Bug: T264976
Bug: T264979
Bug: T264981
Bug: T264983
Bug: T264984
Bug: T264985
Change-Id: Ia05bc84e4d1be7c8a02472f32e2c009e4bb32032
2020-12-18 21:34:50 +00:00
Umherirrender
693ff9bba8 Use consts in ApiMain and ApiQuery class
Change-Id: I99d78eddc807bc44477f1583371189568394ad49
2020-11-22 20:10:28 +01:00
DannyS712
50a392b232 Inject UserCache into ApiUnblock
Bug: T248686
Bug: T259960
Change-Id: Ifa6ec8636ca8f15c898576ab7fcebda08357f0d0
2020-11-19 18:46:09 +00:00
Umherirrender
27816323e5 Use SpecialPageFactory to create special pages in ApiFeedRecentChanges
This makes it easier to change the constructor of both special pages

Change-Id: Id485ffa9fc1c1a9818ab487aed6ef886a7eb35a5
2020-11-03 18:11:46 +00:00
jenkins-bot
8de75456e5 Merge "Remove documentation that literally repeats the code" 2020-10-27 21:47:39 +00:00
Thiemo Kreuz
1fc8d79ac6 Remove documentation that literally repeats the code
For example, documenting the method getUser() with "get the User
object" does not add any information that's not already there.
But I have to read the text first to understand that it doesn't
document anything that's not already obvious from the code.

Some of this is from a time when we had a PHPCS sniff that was
complaining when a line like `@param User $user` doesn't end
with some descriptive text. Some users started adding text like
`@param User $user The User` back then. Let's please remove
this.

Change-Id: I0ea8d051bc732466c73940de9259f87ffb86ce7a
2020-10-27 19:20:26 +00:00
jenkins-bot
a98a9b8b83 Merge "Inject services into ApiResetPassword" 2020-10-27 18:19:15 +00:00
DannyS712
1ad50c566b Inject services into ApiResetPassword
Bug: T259960
Change-Id: I3d3f35cba2511b1a1fb94b1b0699f700e1955edb
2020-10-27 17:14:13 +00:00
DannyS712
4b37f95d14 Inject services into ApiStashEdit
Bug: T259960
Change-Id: I8fede2c0df26a101f880b4038fe90557fd1818f3
2020-10-27 17:07:54 +00:00
jenkins-bot
7d96bdcabb Merge "Inject services into ApiChangeContentModel" 2020-10-26 18:21:33 +00:00
DannyS712
ea99445378 Inject services into ApiChangeContentModel
Bug: T259960
Change-Id: Ic1b38181da4da07526ccf6019ee8adf78f0b175c
2020-10-25 22:38:36 +00:00
DannyS712
bd746f236c Inject TalkPageNotificationManager into ApiClearHasMsg
Bug: T259960
Change-Id: Ia5b8f5b852a453c0e6a431e848c5785c730663f2
2020-10-25 22:32:57 +00:00
DannyS712
627104459f Inject dependencies into ApiSetNotificationTimestamp
Bug: T259960
Change-Id: I8123dea3269a26c1fda8d1f301903aeb4534db0a
2020-10-21 15:12:27 +00:00
DannyS712
1db27ef095 Inject services into ApiValidatePassword
Bug: T259960
Bug: T265311
Change-Id: I4fb9c222b08285464e1dc8e3af1e73a5493e4a64
2020-10-13 19:04:09 +00:00
Ammar Abdulhamid
3565eebd14 Add WatchlistExpiry support to ApiBlock
Bug: T248515
Change-Id: I253bcb5a74a65cbd9834b5eabcd1ed7a1993a5d9
2020-10-01 15:42:15 +01:00
DannyS712
149e99f072 Revert "ApiEditPage: Show existing watchlist expiry if status is not being changed."
This reverts commit 07e547f47c.

Reason for revert: LiquidThreads extends the ApiEditPage class,
even though it shouldn't, and thus fails when the dependencies
are not injected.

Bug: T261030
Bug: T264200
Change-Id: Ib14f8a04bb6c723aa502a47ef9ccde6fe96a0ac7
2020-09-30 15:29:59 +00:00
Ammar Abdulhamid
07e547f47c ApiEditPage: Show existing watchlist expiry if status is not being changed.
Bug: T261030
Change-Id: I795db12aefeffb1cfbbe2ab00fbb19444df7d37b
2020-09-29 16:20:20 +01:00
Martin Urbanec
a656d03597 Introduce backend class for blocking users
Rather than having to do DatabaseBlock calls directly,
and then ManualLogEntry calls to facilitate logging,
let's create a BlockUser service, capable of blocking users
and logging, optionally with permission checking.

This should make blocking users easier for developers,
for instance, AbuseFilter or CheckUser can easily
benefit from this commit.

Bug: T189073
Change-Id: Ifdced735b694b85116cb0e43dadbfa8e4cdb8cab
2020-09-22 14:14:01 +01:00
David Barratt
c36b320454
Handle CORS preflight request and prevent anon users from unsafe methods
Creates an OPTIONS handler that handles any OPTIONS requests that are
not already handled by a handler. CORS has no mechanism to ensure the
user is authenticated, so the Router will reject cross-origin requests
from anon users.

This change allows authenticated users to make cross-origin
requests if they authenticate with OAuth or if
$wgRestAllowCrossOriginCookieAuth is enabled.

Bug: T232176
Bug: T262712
Change-Id: I128b4bdbec4f6bea35142153c951fd7b79617106
2020-09-21 19:29:40 -04:00
Martin Urbanec
dd4f247e98 Do not call SpecialBlock::canBlockEmail in ApiBlock
Bug: T263327
Change-Id: Ic63178ba1ac2756b5b305d4fa8ea53ab40f9b011
2020-09-19 20:12:24 +02:00
Martin Urbanec
d64efdeacb Introduce backend class for unblocking users
Create UnblockUserFactory service, which returns an UnblockUser.
UnblockUser::unblock replaces SpecialUnblock::processUnblock, which
is now deprecated.

Update SpecialUnblock and ApiUnblock to use UnblockUser, removing
the API module's dependency on the special page.

Update SpecialUnblock::processUIUnblock to use UnblockUser, and mark
it for internal use only. It now returns a Status object.

Also add BlockPermissionChecker::checkBasePermissions, which checks
for the 'block' right and the 'hideuser' right if necessary.

Bug: T250020
Change-Id: Ide31da469297f4582ad0e3f7f1a7c40d542923f8
2020-08-25 13:34:36 +01:00
daniel
272db6afde Replace "@stable for calling" by "@stable to call"
For compliance with the new version of the table interface policy
(T255803).

This patch was created by an automated search & replace operation
on the includes/ directory.

Bug: T257789
Change-Id: If560596f5e1e0a3da91afc36e656e7c27f040968
2020-07-13 08:55:28 +00:00
daniel
128565505c Mark additional classes as newable for now.
The following glasses are marked as newable per the
Stable Interface Policy, even though logically, they should not be
newable. This is done for classes that are currently instantiated
by extensions, and lack an alternative.

A better way for obtaining an instance of these classes should be
created in the future. At that point, direct instantiation should
be deprecated and replaced.

- includes/ApiMain.php - needs factory
- includes/media/BitmapMetadataHandler.php - should become a stateless service or use handler pattern
- includes/GitInfo.php - should become a stateless service
- includes/logging/LogPage.php - should become a stateless service or use command pattern
- includes/logging/ManualLogEntry.php - should become a stateless service or use command pattern
- includes/poolcounter/PoolCounterWorkViaCallback.php - needs a factory
- includes/context/RequestContext.php - needs to be narrowed down, and should use a factory
- includes/search/SearchHighlighter.php - should have a factory
- includes/TitleArrayFromResult.php - should perhaps be part of TitleFactory
- includes/user/User.php - should at least get a factory method for anons
- includes/diff/Diff.php: needs a TextDiffGenerator service or a factory
- includes/EditPage.php: needs a factory

Bug: T247862
Change-Id: I033158e693c98630ee167d9528fc8c9936f978d4
2020-07-08 18:06:12 +02:00
jenkins-bot
8b2f44b6e7 Merge "phan: Enable redundant_condition_detection" 2020-07-02 00:28:10 +00:00
Umherirrender
bc5cb7ae64 phan: Enable redundant_condition_detection
Remove duplicate casts
Suppress false positives

Bug: T248438
Change-Id: I2f89664a4bcd3b39b15e7cf850adda2f0c90ae6f
2020-07-01 20:13:07 +00:00
Tim Starling
c75eef91bf Introduce $wgForceHTTPS
Add $wgForceHTTPS. When set to true:

* It makes the HTTP to HTTPS redirect unconditional and suppresses the
  forceHTTPS cookie.
* It makes session cookies be secure.
* In the Action API, it triggers the existing deprecation warning and
  avoids more expensive user/session checks.
* In login and signup, it suppresses the old hidden form fields for
  protocol switching.
* It hides the prefershttps user preference.

Other changes:

* Factor out the HTTPS redirect in MediaWiki::main() into
  maybeDoHttpsRedirect() and shouldDoHttpRedirect(). Improve
  documentation.
* User::requiresHTTPS() reflects $wgForceHTTPS whereas the Session
  concept of "force HTTPS" does not. The documentation of
  User::requiresHTTPS() says that it includes configuration, and
  retaining this definition was beneficial for some callers. Whereas
  Session::shouldForceHTTPS() was used fairly narrowly as the value
  of the forceHTTPS cookie, and injecting configuration into it is not
  so easy or beneficial, so I left it as it was, except for clarifying
  the documentation.
* Deprecate the following hooks: BeforeHttpsRedirect, UserRequiresHTTPS,
  CanIPUseHTTPS. No known extension uses them, and they're not compatible
  with the long-term goal of ending support for mixed-protocol wikis.
  BeforeHttpsRedirect was documented as unstable from its inception.
  CanIPUseHTTPS was a WMF config hack now superseded by GFOC's SNI
  sniffing.
* For tests which failed with $wgForceHTTPS=true, I mostly split the
  tests, testing each configuration value separately.
* Add ArrayUtils::cartesianProduct() as a helper for generating
  combinations of boolean options in the session tests.

Bug: T256095

Change-Id: Iefb5ba55af35350dfc7c050f9fb8f4e8a79751cb
2020-06-30 15:38:11 +10:00
DannyS712
69e63b4c07 Reduce further direct references to $wgUser
Should be a no-op

Bug: T243708
Change-Id: Id2df2009eada1e6d2b4ce0b03485d9bc4e8f18d7
2020-06-05 11:06:21 +00:00
Tim Starling
47a1619027 Remove terminating line breaks from debug messages
A terminating line break has not been required in wfDebug() since 2014,
however no migration was done. Some of these line breaks found their way
into LoggerInterface::debug() calls, where they mess up the formatting
of the debug log.

So, remove terminating line breaks from wfDebug() and
LoggerInterface::debug() calls.

Also:
* Fix the stripping of leading line breaks from the log header emitted
  by Setup.php. This feature, accidentally broken in 2014, allows
  requests to be distinguished in the log file.
* Avoid using the global variable $self.
* Move the logging of the client IP back to Setup.php. It was moved to
  WebRequest in the hopes that it would not always be needed, however
  $wgRequest->getIP() is now called unconditionally a few lines up in
  Setup.php. This means that it is put in its proper place after the
  "start request" message.
* Wrap the log header code in a closure so that variables like $name do
  not leak into global scope.
* In Linker.php, remove a few instances of an unnecessary second
  parameter to wfDebug().

Change-Id: I96651d3044a95b9d210b51cb8368edc76bebbb9e
2020-06-03 12:01:16 +10:00
Tim Starling
68c433bd23 Hooks::run() call site migration
Migrate all callers of Hooks::run() to use the new
HookContainer/HookRunner system.

General principles:
* Use DI if it is already used. We're not changing the way state is
  managed in this patch.
* HookContainer is always injected, not HookRunner. HookContainer
  is a service, it's a more generic interface, it is the only
  thing that provides isRegistered() which is needed in some cases,
  and a HookRunner can be efficiently constructed from it
  (confirmed by benchmark). Because HookContainer is needed
  for object construction, it is also needed by all factories.
* "Ask your friendly local base class". Big hierarchies like
  SpecialPage and ApiBase have getHookContainer() and getHookRunner()
  methods in the base class, and classes that extend that base class
  are not expected to know or care where the base class gets its
  HookContainer from.
* ProtectedHookAccessorTrait provides protected getHookContainer() and
  getHookRunner() methods, getting them from the global service
  container. The point of this is to ease migration to DI by ensuring
  that call sites ask their local friendly base class rather than
  getting a HookRunner from the service container directly.
* Private $this->hookRunner. In some smaller classes where accessor
  methods did not seem warranted, there is a private HookRunner property
  which is accessed directly. Very rarely (two cases), there is a
  protected property, for consistency with code that conventionally
  assumes protected=private, but in cases where the class might actually
  be overridden, a protected accessor is preferred over a protected
  property.
* The last resort: Hooks::runner(). Mostly for static, file-scope and
  global code. In a few cases it was used for objects with broken
  construction schemes, out of horror or laziness.

Constructors with new required arguments:
* AuthManager
* BadFileLookup
* BlockManager
* ClassicInterwikiLookup
* ContentHandlerFactory
* ContentSecurityPolicy
* DefaultOptionsManager
* DerivedPageDataUpdater
* FullSearchResultWidget
* HtmlCacheUpdater
* LanguageFactory
* LanguageNameUtils
* LinkRenderer
* LinkRendererFactory
* LocalisationCache
* MagicWordFactory
* MessageCache
* NamespaceInfo
* PageEditStash
* PageHandlerFactory
* PageUpdater
* ParserFactory
* PermissionManager
* RevisionStore
* RevisionStoreFactory
* SearchEngineConfig
* SearchEngineFactory
* SearchFormWidget
* SearchNearMatcher
* SessionBackend
* SpecialPageFactory
* UserNameUtils
* UserOptionsManager
* WatchedItemQueryService
* WatchedItemStore

Constructors with new optional arguments:
* DefaultPreferencesFactory
* Language
* LinkHolderArray
* MovePage
* Parser
* ParserCache
* PasswordReset
* Router

setHookContainer() now required after construction:
* AuthenticationProvider
* ResourceLoaderModule
* SearchEngine

Change-Id: Id442b0dbe43aba84bd5cf801d86dedc768b082c7
2020-05-30 14:23:28 +00:00
Aaron Schulz
438c94cd7c exception: cleanup "caught_by" and use Throwable in more places
Make sure that CAUGHT_BY_HANDLER is only for errors caught by the
handler from MWExceptionHandler::installHandler().

Add CAUGHT_BY_ENTRYPOINT constant for entrypoint try/catch logic,
e.g. MediaWiki::run()/ApiMain::executeActionWithErrorHandling().

Use Throwable to catch more types of errors given that PHP 7.2
is already required.

Change-Id: Ib496e26572c98d771a772972676c02c05b872e31
2020-05-18 16:20:56 -07:00
Reedy
b80a9f4f6a Fix even more PSR12.Properties.ConstantVisibility.NotFound
Change-Id: I5e04824d6fa6a4c36ce489850bb0ed7b4ac588f9
2020-05-16 00:51:14 +01:00
Gergő Tisza
0ed077d3fc
Allow whitelisting custom headers in action API CORS logic
Bug: T249107
Change-Id: I622e4b956cd839c9d4375e1ef8f695d635a0d767
2020-04-03 22:30:11 +02:00
DannyS712
0789d1568d Add a ContentModelChange helper, and an api module that uses it
Bug: T107174
Change-Id: I6eb38c4aec23619d7ec42ef12092edf9ff25c6fa
2020-02-27 19:02:29 +00:00
Bartosz Dziewoński
68e495d8b0 ApiMain: Add support for assert=anon
In VisualEditor, when the user is saving their edit, we want to ensure
that they understand how their edit will be attributed. Therefore, if
the user gets logged out or logs in in another tab, we want to display
a message about it before saving.

We've been detecting this situation by manually managing tokens and
handling the 'badtoken' error, but our code would be much simpler and
it would be possible to share it with other extensions if we could
instead rely on automatic retrying in mw.Api#postWithToken and use
'assertuser' and 'assert' parameters. 'assert=anon' is the only
feature we're missing.

Bug: T245327
Change-Id: I485f99e1f5f493262b0c9af22370da01adf1e09c
2020-02-19 22:37:31 +01:00
jenkins-bot
4fbf949afa Merge "Fix some PHPStorm inspection warnings in includes/api" 2020-02-18 19:32:58 +00:00
Tim Starling
a06e3d06b1 Fix some PHPStorm inspection warnings in includes/api
Notably:
* In ApiManageTags, I used a switch instead of a dynamic function name,
  so that the call graph will be correct.
* In ApiImageRotate, checkTitleUserPermissions() has always returned
  void, this was an error introduced in 4e6810e4a2

Change-Id: Iea22616b8e7e2e0cc804619a54f8690898b2cb82
2020-02-18 14:17:37 -05:00
Aaron Schulz
6b12696452 Move UIDGenerator code to a service and put it under /libs
All MediaWiki dependencies have been removed or injected.

Change-Id: I01c9e96edd6b03496c1595670967ffa5a4069c9d
2020-02-18 00:20:40 +00:00
Ricordisamoa
1b3bc281ac Clean up redundant Exception|Throwable union type
PHP 7.0 makes many error conditions throw instances of the new Error class
which does not extend the known Exception.
The Throwable interface provides a concise and type-safe way of handling
either, e.g. for logging purposes, but HHVM did not support it, requiring
tedious fallback checks.

This commit replaces occurrences of Exception in code paths equally
covered by Throwable, like Exception|Throwable parameter and return types
(also nullable), instanceof guards, duplicated `catch` blocks, as well as
related comments and documentation blocks, with the exception of $previous
parameter descriptions consistent with the manual at
https://www.php.net/manual/en/exception.construct.php

Proper type declarations have been added or reinstated where possible.

Change-Id: I5d3920d3cc66936a350314e2f19c4f6faeffd7c0
2020-02-12 20:28:40 +00:00
jenkins-bot
c57205f452 Merge "API: Remove unused "non-whitelisted CORS origin" log" 2020-02-07 23:15:14 +00:00
Brad Jorsch
c2b1525908 API: Use ParamValidator library
This brings significant modularization to the Action API's parameter
validation, and allows the Action API and MW REST API to share
validation code.

Note there are several changes in this patch that may affect other code;
see the entries in RELEASE-NOTES-1.35 for details.

Bug: T142080
Bug: T232672
Bug: T21195
Bug: T34675
Bug: T154774
Change-Id: I1462edc1701278760fa695308007006868b249fc
Depends-On: I10011be060fe6d27c7527312ad41218786b3f40d
2020-02-04 13:36:14 -05:00
Brad Jorsch
7a8fa0d9fd API: Remove unused "non-whitelisted CORS origin" log
This log message was added at the request of Wikimedia's Security team
in 2016, but as far as I can tell was never enabled on Wikimedia sites.

Rather than adding more logic to avoid logspam when browsers include an
Origin header for same-orign requests, let's just get rid of it
entirely.

Bug: T243908
Change-Id: I799b17a5f4891f9e739d6b62551a5736b8a6664e
2020-01-29 09:28:35 -08:00
James D. Forrester
0958a0bce4 Coding style: Auto-fix MediaWiki.Usage.IsNull.IsNull
Change-Id: I90cfe8366c0245c9c67e598d17800684897a4e27
2020-01-10 14:17:13 -08:00
Petr Pchelko
e4b7fc3570 Hard deprecate User::isEveryoneAllowed
The method was soft-deprecated in 1.34. It's not used in any WMF
installed extensions or the tarball, so it can be hard deprecated.

Bug: T220191
Change-Id: I2f48d62a8dd3592918a6197168d31a1e08bd2a3e
2019-10-30 11:17:19 -07:00
Aryeh Gregor
0de9c47b50 Remove Language::factory and getParentLanguage use
Change-Id: I11f8801ef47ec1a1f63d840116e69667e6f3ae3c
2019-10-27 12:34:28 +02:00
David Barratt
20fb3632cd
Check and add block cookie to every uncached API request
The cleanest way to add the block cookie is to check for the users block when
the Caching headers are being added to the request. This prevents side effects
when checking for blocks and also prevents extensions from having to be aware
of block cookies.

Bug: T196575
Change-Id: I4f8de725e4be616e41d842f09f6144518237bbd5
2019-10-10 15:55:20 -04:00
James D. Forrester
d3473aca47 HHVM removal: Drop HHVM code path for Exception not Throwable in API code
Change-Id: I8c7e9bc35e4f415fa04199049313253a576846cf
2019-10-02 17:06:56 -07:00
Brad Jorsch
995aad376a API: Use ConvertibleTimestamp::setFakeTime for testing curtimestamp
Mainly to avoid spurious test failures when CI is being extremely slow.

Bug: T233752
Change-Id: Ie2cdd84dc076a852fbdce52f661ef893f9a2d45b
2019-09-26 12:35:00 -04:00
mainframe98
eb8823743c Use ObjectFactory to create API modules
This will allow constructing API modules that need services.

This overhauls some of the internals of the ApiModuleManager,
but the public interface remains unchanged.
The $class parameter of addModule, (now called $spec)
also allows passing an array with the spec of the module.
Note that this spec requires the attribute 'class' to be present,
even when 'factory' is specified. This is the same as before,
where $class was always required.

In a perfect DI world ObjectFactory would be injected into
ApiMain::__construct and ApiMain would pass that to its instance
of ApiModuleManager, but that is currently not possible, so for now
it is injected in ApiModuleManager by having ApiMain::__construct
call the service locator.

Bug: T222388
Change-Id: Iee04afc27283547dd68d6db93f44ac2e0ebf1258
2019-09-09 18:50:16 +00:00
Daimona Eaytoy
23daef5c18 Remove dead properties
*LogPage::timestamp was introduced with r4919 back in 2004, and is unused
since then.
*ApiMain::mCommit was introduced in r33133, then removed in r33381 but that
line was forgotten.
*SpecialRecentChangesLinked::mResultEmpty was introduced in r36682, then 
removed at some point with this leftover.
*SpecialStatistics::hook, introduced in r54511 and unused since then.
*MergeMessageFileList::hasError introduced in Id4b16083435ef7f4fce31861c72889e664d07236
and removed in I3d9cf1d614dacaa91fb2092019ccf1d14d61ccab with a leftover

Change-Id: Ie15c148a3217ee8da62874840f3ef7739893f69e
2019-09-07 16:34:55 +00:00