Commit graph

338 commits

Author SHA1 Message Date
Umherirrender
e662614f95 Use explicit nullable type on parameter arguments
Implicitly marking parameter $... as nullable is deprecated in php8.4,
the explicit nullable type must be used instead

Created with autofix from Ide15839e98a6229c22584d1c1c88c690982e1d7a

Break one long line in SpecialPage.php

Bug: T376276
Change-Id: I807257b2ba1ab2744ab74d9572c9c3d3ac2a968e
2024-10-16 20:58:33 +02:00
James D. Forrester
53b67ae0a6 Add namespace to remaining parts of Wikimedia\ObjectCache
Bug: T353458
Change-Id: I3b736346550953e3b2977c14dc3eb10edc07cf97
2024-09-27 16:19:10 -04:00
James D. Forrester
984076f98e Add namespace to includes/api classes
Bug: T353458
Change-Id: I3ea6b08c5018ba03ba45c5766e1f46e12f6b8597
2024-09-25 19:31:14 +00:00
Bartosz Dziewoński
df4cbf5ac6 Replace gettype() with get_debug_type() in debug/log/test output
get_debug_type() does the same thing but better (spelling type names
in the same way as in type declarations, and including names of
object classes and resource types). It was added in PHP 8, but the
symfony/polyfill-php80 package provides it while we still support 7.4.

Also remove uses of get_class() and get_resource_type() where the new
method already provides the same information.

For reference:
https://www.php.net/manual/en/function.get-debug-type.php
https://www.php.net/manual/en/function.gettype.php

In this commit I'm only changing code where it looks like the result
is used only for some king of debug, log, or test output. This
probably won't break anything important, but I'm not sure whether
anything might depend on the exact values.

Change-Id: I7c1f0a8f669228643e86f8e511c0e26a2edb2948
2024-07-31 19:33:57 +02:00
Umherirrender
9879723ef3 Use namespaced classes (1)
Changes to the use statements done automatically via script
Addition of missing use statement done manually

Change-Id: Ic4d4dd61de5ab896fb6173eb579c81f164a1e4a3
2024-06-16 20:18:23 +02:00
Lucas Werkmeister
7e77ad91d5 DeprecationHelper: Don’t crash during destruction
Apparently it’s sometimes possible for the DeprecationHelper’s magic
methods to be called while the object is being destructed and has
already had its properties unset; in that case, if we try to access
$this->dynamicPropertiesAccessDeprecated, we’ll get a warning. Fall back
to a simple property access in that case.

Bug: T363492
Change-Id: Idc233f7c65a48b7d8f13fd97e365ef907ec7fc40
2024-05-13 17:11:59 +02:00
Hakan Özdemir
6c5684d41f Add namespace and deprecation alias to DeprecationHelper
This patch introduces a namespace declaration for the
MediaWiki\Debug to DeprecationHelper and establishes a class
alias marked as deprecated since version 1.43.

Bug: T353458
Change-Id: I808c6aa7836445feaa7b401624d79d01f2c70322
2024-05-05 14:16:35 +02:00
Amir Sarabadani
214674d6b1 namespace MWDebug
Bug: T353458
Change-Id: I99d728bd111ff882220cd175ff09d4da20b81eae
2024-05-03 22:59:47 +02:00
Tim Starling
917f0a5996 Replace all instances of "per default" with "by default"
According to the dictionary, "per" (or more conventionally "as per")
means "according to". Refer OED "per" sense II.3.a. For example:

"No value was passed, so return null, as per default".

In this sentence, we are not specifying the default, we are referring
to the default. This correct usage of "per default" was used nowhere
in MediaWiki core as far as I can see.

Instead we have "per default" being used to mean "by default", that is,
giving the value to use when no explicit value was specified.

In OED, the phrase "by default" is blessed with its own section just
for computing usage:

"P.1.e. Computing. As an option or setting adopted automatically by a
computer program whenever an alternative is not specified by the user
or programmer. Cf. sense I.7a."

There are highly similar pre-computing usages of the same phrase,
whereas the phrase "per default" is not mentioned.

As a matter of style, I think "per default" should not be used even
when it is strictly correct, since the common incorrect usage makes it
ambiguous and misleading.

Change-Id: Ibcccc65ead864d082677b472b34ff32ff41c60ae
2024-04-29 10:47:54 +10:00
Gergő Tisza
e2bad05089 debug: Add MW class as helper for eval.php and shell.php
This class defines convenient shortcuts for common commands, like
creating a User object. To use on shell.php and similar tools.

Change-Id: I7982d163521e7ae8f2be22613ac6a6fb6af1d7cd
2024-03-13 19:07:27 -07:00
jenkins-bot
16fb214dbf Merge "Add LegacyLogger when $wgShowDebug is true." 2024-03-07 19:37:44 +00:00
Sébastien Beyou
2700b39041 Add LegacyLogger when $wgShowDebug is true.
When the logging system is Monolog and when $wgShowDebug
or $wgDebugToolbar is true, the LegacyLogger should be
added as a PsrHandler to display the debug data.

Bug: T114532
Change-Id: I7548201ca33e475e894f92ab1022bdae2a7daecc
2024-02-15 02:26:54 +01:00
Sébastien Beyou
4717d9d7a5 Debugger: Display debug log when $wgShowDebug = true
Bug: T330623
Change-Id: I3934fd2d05ff3874a24492e8e058ca4d46a1bb4d
2024-02-14 20:24:54 +01:00
jenkins-bot
24e083146d Merge "debug: Improve type safety in MonologSpi" 2024-02-10 14:49:02 +00:00
James D. Forrester
4bae64d1c7 Namespace includes/context
Bug: T353458
Change-Id: I4dbef138fd0110c14c70214282519189d70c94fb
2024-02-08 11:07:01 -05:00
thiemowmde
ac94837efc debug: Improve type safety in MonologSpi
Utilizing array shapes with the `array{…}` syntax. See e.g.
https://phpstan.org/writing-php-code/phpdoc-types#array-shapes

Change-Id: Iab860ec25ae77770b8af57c2e68d2f11eee11f20
2024-02-04 14:36:18 +01:00
jenkins-bot
fb09ec21ff Merge "Use the instanceof and ::class features instead of strings" 2024-02-02 16:01:15 +00:00
thiemowmde
7e70b8042c Use the instanceof and ::class features instead of strings
This makes it much easier for IDEs and tools like Phan to understand
what's going on. Note this syntax is perfectly valid even if a class
is undefined. Language features like `use`, `instanceof`, and
`class_exists` work perfectly fine. We do this a lot in existing
code.

Change-Id: I4d397621ebcc6a7e842150f7641c1b23d082b730
2024-02-02 14:50:15 +00:00
Bartosz Dziewoński
290789f080 DeprecationHelper: Don't copy the info into every instance
Store the deprecation info in a static property instead of an
instance property. This was causing noticeable memory use for
the Title class.

Because the deprecated properties are defined in the constructor,
we now have to ignore repeated calls.

Bug: T351732
Change-Id: If9504321c66d9033e4090088cbf21d4dd2bd463f
2024-01-31 17:53:37 +01:00
Doğu Abaris
0a9d969f12 Replace deprecated WebRequest::getRequestId() with Telemetry
The WikiProcessor::__invoke() method was using the deprecated
WebRequest::getRequestId() to obtain the request ID. This patch
updates the method to use Telemetry::getInstance()->getRequestId()
instead, as per the deprecation notice in MediaWiki 1.41.

Change-Id: I542d76af00613023db219e55ab57ac9bd807c6c3
2024-01-19 10:32:45 +00:00
Kosta Harlan
5734b8c1b9
LegacyLogger: Emit to stderr if MW_LOG_STDERR is set
Why:

- Developers could reasonably expect that `composer serve` would provide
  logs in the terminal used for serving the app

What:

- Set an environment variable, MW_LOG_STDERR, and check this in
  LegacyLogger which is already loaded in DevelopmentSettings for
  installs using the local development quickstart guidelines. If the env
  variable is set, and LegacyLogger is already in use for writing to log
  files, then use error_log() to also write to stderr

Bug: T347347
Change-Id: I7ff8ec1664d5ee9ca0487b3ed363b43fe235b738
2023-12-21 09:38:46 +01:00
Bartosz Dziewoński
f61827d9e0 Replace $wgCommandLineMode checks with MW_ENTRY_POINT in developer code
This code should only be executed in development environments, never
in production, so it should be safe to mess with it.

Bug: T313841
Change-Id: I8cab7704b244db1432f4266b2e06e03452ffc98f
2023-12-14 15:24:48 +01:00
Daimona Eaytoy
44719ee5d2 Guard against deleted files in MWDebug::getFilesIncluded
When an included file has since been deleted, filesize will raise a PHP
warning and return false, thus causing logspam (possibly) and test
failures (surely).

Add a check for deleted files and report their size as 0 (in case
anything expects the value to be numeric), but keep including them in
the generated debug information in case this turns out to be useful.

Bug: T351986
Change-Id: Ie91c9f2882f12fd8ea822529528dd1dd9efe89db
2023-11-27 02:30:27 +01:00
James D. Forrester
67217d08df Namespace remaining files under includes/deferred
Bug: T166010
Change-Id: Ibd40734b96fd2900e3ce12239d09becfb4150059
2023-11-22 10:08:53 -05:00
Tim Starling
5d37420943 rdbms: Log query runtime in milliseconds with microsecond resolution
For readability, and as reassurance since I kept seeing "0s" and
wondering if it was rounding to the nearest second.

Change-Id: If19f2c2155a1f94b38f80154aef497738c654d75
2023-11-21 17:27:42 +11:00
Timo Tijhof
2bea123495 MWDebug: Add support for native PHP warnings to "Console"
== Change ==

Remove integration from MWDebug methods in favour of capturing
messages from the 'error' channel. This way we fix the following
that we were all blind to:

* PHP built-in errors for native notices and deprecation warnings.
* trigger_error calls from our own code such as in standolone libs,
  where we can't use wfLogWarning or wfDeprecated, and instead use
  E_USER_WARNING and E_USER_DEPRECATED instead.
* trigger_error calls from third-party Composer libraries, which
  similarly can't and don't know about these at all.

I removed support for including a backtrace. XDebug does a better job
of this nowadays, and the information is also available in
mw-error.log already in a more readable manner. Cramming it into a
100px scrollable area made the "Console" tab much less readable in
my opinion. To make it work, we'd need to duplicate the pretty
string formatting code of wfBacktrace() and make it re-usable when
given only a trace array (rather than capture a new backtrace). I
considered re-using MWExceptionHandler::prettyPrintTrace but that is
far more verbose than the string format in wfBacktrace. Creating yet
another untested error pretty print function seemed not worthwhile
as imho in the majority of cases the $caller suffices, and when not,
the toolbar suffices as a nudge to take a look in mw-debug.log, or
check XDebug display_errors, or engage an IDE.

== Test plan ==

1. Add the following to MediaWiki.php#main() as example:
   ```
   wfLogWarning('Hello');
   wfDeprecated('Something');
   trigger_error('Goodbye');
   $x = []; $a = $x['a'];
  ```
2. Before this change, your native display_errors=1 or XDebug shows
   all four. With $wgDebugToolbar enabled, the "Console" tab
   showed only the wfLogWarning and wfDeprecated entry.
3. After this change, all four are included

Change-Id: I62d864823ec8ab9b940aae0e0f47b47a728ba861
2023-10-19 17:13:12 -07:00
Ariel T. Glenn
e0dc348cc3 typo fixes in inline docs for logger spi module
Change-Id: I992ad350cc3af83f567036b251aa1a488835e657
2023-10-11 11:20:29 +03:00
Timo Tijhof
19b97fd575 debug: Optimize createLogger() and set UTC timezone by default
Remove the need for wmf-config to perform dynamic 'calls'. It
currently uses this to make createLogger() call setTimezone().

The reasons for this are not WMF-specific, and thus can be done here.
In addition, Monolog\Logger supports a timezome argument so that we
don't need to construct two objects for every logger (one in
Monolog\Logger::__construct, and then another to replace it when
calling setTimezone), and also remove the setTimezone call and
skip the dynamic call for it.

The same call overhead can also be removed for the processors
and handlers array. Instead of calling push on each item, we can
pass these to the constructor directly. This does mean the order
will now be reversed but this is imho less surprising than before
(i.e. call from first to last, instead of reversed), and to my
knowledge none of our processors or handlers depend on side-effect
from previous ones.

Bug: T99581
Bug: T116550
Change-Id: Ib300c01c886dc8916413db65078a8356fd40a5c1
2023-10-05 14:16:52 +00:00
Timo Tijhof
9cb8bb611b debug: Improve docs, fix ingroup tags, clean up tests
* Remove redundant file-level description and ensure the class desc
  and ingroup tag are on the class block instead.
  Ref https://gerrit.wikimedia.org/r/q/owner:Krinkle+message:ingroup

* Widen `@covers` annotations in unit tests.
  Ref https://gerrit.wikimedia.org/r/q/owner:krinkle+is:merged+message:covers

* Create "Debug" documentation group, covering the debug/ directory.
  This will show up on doc.wikimedia.org under "Modules", where
  each class is listed, and the class page will also link back to
  the group as part of its breadcrumb navigation.

  Test with `php maintenance/mwdocgen.php --file docs/,includes/debug/`
  and then view /w/docs/html/

* Improve docs of various classes and explain relationships better.
  In particular, reformat to ensure each class has a oneline
  description that captures its essential function.

Change-Id: I5d1143a9244b7fd888e1dc31f0fd7965272aa900
2023-10-05 11:54:30 +00:00
Timo Tijhof
1c9a6c1cfe debug: Reference PSR LoggerInterface instead of Monolog where possible
* Remove conditional test since monolog is, at least as a library,
  unconditionally available in both test and production context.

* Reduce direct usage of Monolog by preferring the PSR interface
  where possible.

Change-Id: Id529cfd54e98697cf3254d2a0b8b1e247fe1b7f3
2023-09-25 21:04:25 +00:00
James D. Forrester
468e69bccc Namespace Sanitizer under \MediaWiki\Parser
Bug: T166010
Change-Id: Id13dcbf7a0372017495958dbc4f601f40c122508
2023-09-21 05:39:23 +00:00
Amir Sarabadani
5bd33d46ef Reorg: Move WebRequest to includes\Request
This has been approved as part of RFC T166010

Bug: T321882
Change-Id: I6bbdbbe6ea48cc1f50bc568bb8780fc7c5361a6f
2023-09-11 21:44:34 +01:00
Amir Sarabadani
d8e542abf9 Reorg: Move three output related classes to includes/Output/
And namesapce them:
 - StreamFile
 - OutputHandler
 - OutputPage

Bug: T321882
Change-Id: Iedf8d88c595e580f2d8f0734c92aa5c45618ba33
2023-09-05 19:36:42 +01:00
Amir Sarabadani
cb7351809e Reorg: Move GitInfo to utils/
Bug: T321882
Change-Id: Ibd8aff5f57555d5b2a3b9cd121c8919a5a96d313
2023-08-21 18:35:32 +02:00
Timo Tijhof
b28faecb2b ResourceLoader: Remove support for CSP nonce
The "nonce" CSP feature has been never been properly tested and is not
on track to be enabled at WMF or in MW by default. The use of
nonce-protected eval is not particularly meaningful. It is trivially
bypassed by mw.loader.implement, mw.loader.store, or importScript();
all of which allow any code to be executed directly, or to be stored
and loaded from a first-party origin.

The "nonce" feature is not required for the T208188 roadmap, and with
change I51b8535b2b21a3 there is now also a (small) performance reason
to keep this disabled long-term.

Until and unless there is a plan for enabling this particular aspect
of CSP, we might as well remove it so that we don't waste time our
time building abstraction layers and satisfying dependency injection
through many years for something that might never be used.

Note that this does not remove CSP support from ResourceLoader, and
does not take away from the future of enabling CSP in MediaWiki to
e.g. strictly block third-party scripts, or to require domains to
opt-in by site config or user pref (T208188).

Change-Id: I5a176c41a06a484a11e64bdacdc42b40811fe72e
2023-08-09 02:30:39 +01:00
thiemowmde
ef84619cd3 Consistently use LogicException for test-only methods
"BadMethodCallException" sounds like it would fit, but it does
have a very different meaning, described as "exception thrown if
a callback refers to an undefined method or if some arguments are
missing". This is not what's going on here. These are methods that
should only be called from unit tests.

This appears to be a common mistake, often copy-pasted.

Change-Id: Ib39e28f596a883481d5f526460a5c871c75f5313
2023-07-22 16:21:42 +00:00
Amir Sarabadani
4bb2886562 Reorg: Migrate WikiMap to WikiMap/ out of includes
And WikiReference

Bug: T321882
Change-Id: I60cf4b9ef02b9d58118caa39172677ddfe03d787
2023-02-27 05:19:46 +01:00
jenkins-bot
a5c22957a5 Merge "psysh: Dump selected log channels" 2023-02-23 00:03:23 +00:00
Amir Sarabadani
7d8768e931 Reorg: Move HTML-related classes out of includes/ to Html/
Bug: T321882
Change-Id: I5dc1f7e9c303cd3f5b9dd7010d6bb470d8400a18
2023-02-16 20:40:01 +01:00
Gergő Tisza
1c53f5902e psysh: Dump selected log channels
* Extend ConsoleLogger to support filtering by log level and
  forwarding to another logger.
* Extend ConsoleSpi to support filtering by channel/level and
  forwarding to another provider.
* Allow configuring which channels shell.php should log to STDERR
  and at what levels.
* Deprecate shell.php -d, use more meaningful parameter names.
* Change logging behavior so that logging to STDERR doesn't
  prevent normal logging, which seemed like confusing behavior
  (either we should always disable normal logging for shell.php
  or never, it shouldn't depend on whether we log to console).
  Keep old behavior for -d, just in case.

Change-Id: I5d29c95995bf83784c96dd40df9c96e22b9ec73e
2023-02-09 11:35:59 +00:00
Timo Tijhof
4ef0891994 rdbms: Consolidate logger channels into one
Notable changes:

* In SqlBagOStuff::getConnectionFromServerInfo, only two loggers were
  injected. The rest implicitly got a NullLogger due to being absent.
  These are now effectively unsilenced.

* Database::__construct() required almost all parameters, even the
  loggers. I've wanted to move some of DatabaseFactory into the ctor
  here for a while. In order to make this change not a breaking
  change, the new 'logger' parameter is optional with NullLogger as
  default. This allowed some of the test cases, which were simply
  passing NullLogger, to be fixed by passing nothing instead of
  passing the new option name.

  The Database class is behind a dozen layers of indirection for
  real use, so this will still be injected just fine (DBF, LB, LBF,
  MWLBF, etc.).

* In LegacyLogger, the handling for $wgDBerrorLog was previously
  limited to DBConnection and DBQuery. This now includes errors
  from other (generally, newer) parts of Rdbms as well, which were
  previously missing.

  This only affects sites (typically CI and dev setup) where
  $wgDBerrorLog is used, as opposed to the more common
  $wgDebugLogGroups by-channel configuration.

* TransactionProfiler gets its logger injected in a rather odd way,
  via entrypoints (MediaWiki.php, ApiMain.php, and MaintenanceRunner)
  as opposed to service wiring. This is kept as-is for now.

* In LBFactoryTest, in particular testInvalidSelectDBIndependent2,
  there are cases that intentionally produce failures of which
  the result is then observed. In CI we assert that dberror.log is
  empty so instead of adding the missing logger fields to that
  LBFactory instance, the only one set (replLogger) is removed.
  The alternative is to set 'logger' now, which would naturally
  cause CI failures due to unexpected entries coming through to
  non-mocked error log.

Bug: T320873
Change-Id: I7ca996618e41b93f488cb5c4de82000bb36e0dd3
2023-01-03 22:46:38 +00:00
Umherirrender
45e6a2b0f9 Use str_starts_with/str_ends_with/str_contains
Use the new function in conditions to avoid creating substrings or to
search the whole string

Change-Id: Ibad6b1b447a4f62cceb34359231f88ebb967a90b
2022-12-12 19:54:24 +01:00
Reedy
0cb2c3c106 Fix casing of class and function name usages
Bug: T253628
Change-Id: I5c64f436d3cf757390b751ce3e34bfc7872bc176
2022-12-04 19:09:30 +00:00
Tim Starling
7b3e7c017a Respond to some messages from Phan on PHP 8.1
* ForkController, OrderedStreamingForkController: indeed pcntl_fork()
  can't return false.
* RL\Image: Specify type instead of using suppression, since the issue
  name changes.
* VueComponentParser: Accept complaint about nullable nodeValue.
* Disable PHP 8.0 polyfill stubs when running on PHP 8.0+ to avoid
  duplicate interface errors.
* Add Socket stub and use it in LegacyHandler instead of multiple
  existing suppressions.
* MemcachedPeclBagOStuff: accept complaint recommending !$result over
  $result === false when the type is boolean.
* MemcachedPeclBagOStuff: fix probable bug, ignoring errors from
  Memcached::getMulti(). Phan noticed that $res=false was unreachable,
  but it should probably be reachable.
* DatabaseMysqli: accept complaint that $this->conn->errno is already
  known to be an int. It was probably a hack for some previous version
  of Phan.
* BcryptPassword, MWOldPassword, MWSaltedPassword: accept complaint that
  the !is_string() checks are unnecessary, after code review of PHP.
* Pbkdf2PasswordUsingHashExtension: note that contrary to Phan's
  suggestion, this check is necessary.
* DefaultPreferencesFactory: remove an existing hack for
  array_diff_key(), no longer necessary on 7.4 and causes an error on
  8.1. Use coalesce instead of cast for the remaining
  array_intersect_key() hack since it better shows that we are casting
  away null.
* FullSearchResultWidget: fix likely bug involving strict comparison
  between a float and an int.
* SpecialWatchlist: accept complaint that $selectedHours is
  unconditionally a float, being the return value of round(), and thus
  the cast is unnecessary.
* Add stub for AllowDynamicProperties, resolving an error in User.php.
* Xml: accept complaint that $encMonth is already known to be an int.

Six errors remain. These need suppressions or otherwise conflict with
PHP 7.4 support.

Bug: T322278
Change-Id: Ie375bbc8ccf22330b9a169e8da98f2bbe26ec8b9
2022-11-03 16:25:43 +11:00
Zabe
f6b9381d7f Revert "Reorg: Move some of request related classes to MediaWiki/Request"
This reverts commit 2bdc0b2b72.

Reason for revert: T166010#8349431

Bug: T166010
Change-Id: Idcd3025647aec99532f5d69b9c1718c531761283
2022-10-27 13:14:16 +00:00
Amir Sarabadani
2bdc0b2b72 Reorg: Move some of request related classes to MediaWiki/Request
Moving:
 - DerivativeRequest
 - FauxRequest
 - FauxRequestUpload
 - PathRouter
 - WebRequest
 - WebRequestUpload

Bug: T166010
Change-Id: I5ea70120d745f2876ae31d039f3f8a51e49e9ad8
2022-10-26 16:49:10 +02: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
Matěj Suchánek
1865180ae7 Do minor code cleanup
Remove dead code and fix typos. Should cause no change in behavior.

Change-Id: I5d293b842bc93a28b8bcd799a31b5e6e30fe692e
2022-06-24 13:52:42 +02:00
Tim Starling
3e2653f83b ResourceLoader namespace (attempt 2)
Move ResourceLoader classes to their own namespace. Strip the
"ResourceLoader" prefix from all except ResourceLoader itself.

Move the tests by analogy.

I used a namespace alias "RL" in some callers since RL\Module is less
ambiguous at the call site than just "Module".

I did not address DependencyStore which continues to have a non-standard
location and namespace.

Revert of a241d83e0a.

Bug: T308718
Change-Id: Id08a220e1d6085e2b33f3f6c9d0e3935a4204659
2022-05-24 15:41:46 +00:00
Lucas Werkmeister (WMDE)
a241d83e0a Revert "ResourceLoader namespace"
This reverts commit e08ea8ccb9.

Reason for revert: Breaks Phan in extensions, and as far as I’m aware,
this change isn’t urgently needed for anything, so the simplest fix is
to revert it again for now. After PHP 7.4 it should be safer to try this
again (we hopefully won’t need the two “hack” classes by then).

Bug: T308443
Change-Id: Iff3318cbf97a67f821f78e60da62a583f63e389e
2022-05-16 14:43:33 +00:00