Commit graph

23 commits

Author SHA1 Message Date
Dreamy Jazz
e7393b3cc7 Exclude boilerplate maintenance code from code coverage reports
Why:
* Maintenance scripts in core have bolierplate code that is
  added before and after the class to allow directly running
  the maintenance script.
* Running the maintenance script directly has been deprecated
  since 1.40, so this boilerplate code is only to support a now
  deprecated method of running maintenance scripts.
* This code cannot also be marked as covered, due to PHPUnit
  not recognising code coverage for files.
* Therefore, it is best to ignore this boilerplate code in code
  coverage reports as it cannot be marked as covered and also
  is for deprecated code.

What:
* Wrap the boilerplate code (requiring Maintenance.php and then
  later defining the maintenance script class and running if the
  maintenance script was called directly) with @codeCoverageIgnore
  comments.
* Some files use a different boilerplate code, however, these
  should also be marked as ignored for coverage for the same
  reason that coverage is not properly reported for files.

Bug: T371167
Change-Id: I32f5c6362dfb354149a48ce9c28da9a7fc494f7c
2024-08-27 13:22:29 +01:00
Irina Balaban
dc989f680f fix: use objectcachefactory methods instead of deprecated objectcache methods
Bug: T363770
Change-Id: Ie732f6925ec2b1316a60bebbe3c27f963c9dacb1
2024-05-05 12:40:30 +03:00
jenkins-bot
104590040e Merge "psysh: Reset ObjectCache instances when custom logging is used" 2024-02-02 12:16:23 +00:00
Bartosz Dziewoński
4deac8b075 Fix type hint in shell.php
Follow-up to ee91e0f2eb.

Change-Id: I6ff8fc310436cd378d813c44033d15256159d814
2024-01-22 14:20:05 +01:00
jenkins-bot
2c85098361 Merge "Allow shell.php to run without LocalSettings.php" 2024-01-20 17:23:11 +00:00
daniel
ee91e0f2eb Allow shell.php to run without LocalSettings.php
The shell.php environment may be useful for testing and debugging even
without a configured MediaWiki instance. Allow it to run without
LocalSettings.php.

Change-Id: I738679384ee8065826f05148829cd04aa9f52efd
2024-01-20 17:02:52 +00:00
Amir Sarabadani
d9370003fb maintenance: Introduce getReplicaDB() and getPrimaryDB()
And start using them instead of wfGetDB(), LB/LBF connection methods or
worse, $this->getDB().

$this->getDB() reuses the database object regardless of whether you're
calling a replica or primary, leading to returning a replica on a
primary and other way around.

Bug: T330641
Change-Id: I9e2cf85ca277022284fc26b9f37db57bd12aaa81
2024-01-18 15:12:04 +01:00
Derick Alangi
74033c50cd maintenance: Begin using Maintenance::getServiceContainer()
Maintenance class provides a method for getting a fresh reference
of the MW services container instance. Let's make use of these in
maintenance scripts now that we have it.

NOTE: There are still some static methods like in refreshLinks.php
that makes use of services that we can't use this method for now.

Change-Id: Idba744057577896fc97c9ecf4724db27542bf01c
2023-09-04 10:39:58 +00:00
Umherirrender
e04d3a28f6 Replace internal Hooks::runner
The Hooks class contains deprecated functions and the whole class is
going to get removed, so remove the convenience function and inline the
code.

Bug: T335536
Change-Id: I8ef3468a64a0199996f26ef293543fcacdf2797f
2023-05-11 06:17:38 +00:00
Gergő Tisza
ecc9b023a8 psysh: Reset ObjectCache instances when custom logging is used
This is necessary for the loggers in ObjectCache instances to be
updated. It will clear in-process caches; hopefully that is not
too disruptive.

Change-Id: I9e425942e43251d36a5308cf4819d9de985002de
2023-02-09 11:36:03 +00: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
Gergő Tisza
969f701fce
psysh: Add MW_NO_SESSION mode for testing related features
Add a --no-session option to shell.php for easier manual
testing/debugging of code for which that flag is relevant.

Exempt from autoloading to avoid breaking ScopeStructureTest.

Change-Id: I97548c8fe76a833837b131a5cd5bd0006a5f7326
2023-01-18 00:49:15 -08:00
James D. Forrester
df5eb22f83 Replace uses of DB_MASTER with DB_PRIMARY
Just an auto-replace from codesniffer for now.

Change-Id: I5240dc9ac5929d291b0ef1c743ea2bfd3f428266
2021-04-29 09:24:31 -07:00
addshore
d873fa15a2 Allow hooking into shell and eval maint scripts for convenience
First intended use in I11bc2b4b43a1eb0578be50a47485e233e15d8dba

Change-Id: I14e58d5f125236f9a95823c25a78a9ebab28fd17
2021-03-12 14:18:09 +00:00
James D. Forrester
4f2d1efdda Coding style: Auto-fix MediaWiki.Classes.UnsortedUseStatements.UnsortedUse
Change-Id: I94a0ae83c65e8ee419bbd1ae1e86ab21ed4d8210
2020-01-10 09:32:25 -08:00
Aaron Schulz
4a55e578ff Convert maintenance scripts to LoadBalancer::getMaintenanceConnectionRef()
Change-Id: I8944a052f51a1941ad3b76a40fc9c46d1363c426
2019-08-02 22:29:43 +00:00
Gergő Tisza
3f2db72083 shell.php: prevent permission error
Avoid bobthecow/psysh#443[1] (PsySH using XDG_RUNTIME_DIR as temp
directory, which is inherited by the new user when doing sudo -E
but is only writable by the old user) by using the MediaWiki temp
dir.

[1]: https://github.com/bobthecow/psysh/issues/443

Change-Id: Ica9344cdf23323d264f66df03b9d82ccc0582e45
2018-07-05 07:41:14 +00:00
Umherirrender
ad776c7d5f Use ::class to resolve class names in maintenance scripts
This helps to find renamed or misspelled classes earlier.
Phan will check the class names

Change-Id: I1d4567f47f93eb1436cb98558388e48d35258666
2018-01-23 17:40:16 +00:00
Bryan Davis
9e34eeff23 Maintenance: add fatalError() method
Deprecate the second argument to Maintenance::error() in favor of a new
Maintenance::fatalError() method. This is intended to make it easier to
review flow control in maintenance scripts.

Change-Id: I75699008638f7e99b11210c7bb9e2e131fca7c9e
2017-11-21 21:34:16 -07:00
Tim Starling
dd8a939ea6 Make "eval.php -d 2" work again
Instead of iterating through load balancer info, just set the flag in
the master and replica connections, since that seems more robust.

Deprecate LoadBalancer::setServerInfo() and
LoadBalancer::getServerInfo(), no remaining callers in core or
in-tree extensions, I think I added the function just for this feature.

Do a service reset, since Logger instances are injected into
LoadBalancer by ServiceWiring (via MWLBFactory::applyDefaultConfig()).
Similarly CryptRand, MimeAnalyzer.

Fix the usage text for shell.php

Change-Id: I3e85a6e8cfa1243a0371cfb3ce1c18665e8c711d
2017-05-03 09:49:44 -04:00
Brion Vibber
4632c4f618 Fix help text for --d on shell.php
1 is stderr, not 0

Change-Id: Iaee4ffd5767f8ae693e8ef19371ce38fe6bd1817
2017-04-27 18:22:31 -07:00
Tim Starling
a59ed5f3de Add ConsoleLogger, use it for eval.php -d
eval.php previously set $wgDebugLogFile to /dev/stdout. This had the
following problems:

* It doesn't work if the maintenance script is executed via sudo, since
  /dev/stdout is typically owned by the original user, so MW can't open
  it. Using php://stdout worked on HHVM but not PHP.
* Setting $wgDebugLogFile has no effect if the wiki uses MonologSpi.
* Setting $wgDebugLogFile has no effect on channels configured with
  $wgDebugLogGroups.
* stderr is a more appropriate place to send logging output.
* Writing to configuration variables is discouraged.

So, add ConsoleSpi, which is a very simple logging service provider
which sends all messages to stderr. This should be suitable for
debugging with eval.php or shell.php in WMF production or beta.

Change-Id: Ib0d6ce45e0cbecd58263fc4e360c63d4149acb3a
2017-04-28 10:05:05 +10:00
Antoine Musso
7671b2b71e Drop in replacement of eval.php based on psysh
eval.php is meant to eval() commands in MediaWiki global scope. We had
at least a couple attempts to move it to a regular Maintenance script.

As noted on the revert commit b475e930 r54839, using a Maintenance
script drop us in the callee function scope instead of the global scope
which is a hard requirement.

http://psysh.org/ is a Read-Eval-Print-Loop with color highlight, modern
code and more.

Add maintenance/shell.php script as MediaWikiShell class.

Passing command from stdin is supported.
Execution is forked for graceful handling of PHP fatal errors.
Colors!!
Support the undocumented '-d' arguments from eval.php:
  0 set $wgDebugLogFile to stdout. Eval.php used /dev/stdout, make it
    php://stdout in the new script.
  1 additionally set DBO_DEBUG on all the database load balancer servers

PHP globals have to be whitelisted which is not supported out of the box
by Psy. Upon request, the author of PsySh, Justin Hileman, kindly
provided a pass to inject globals (with the exceptions of super
globals). He agreed for code reuse:
    https://github.com/bobthecow/psysh/issues/353
The code was added to maintenance/CodeCleanerGlobalsPass.inc

Note that this is not a perfect simulation of global scope (but pretty
close): variables declared in the shell which did not exist before
won't be global unless the 'global' keword is used.

Example usage:

$ php maintenance/shell.php

>>> $wgFullyInitialised
=> true

>>> $hashar = User::newFromName( 'hashar' )
=> User {#274
     +mId: null,
     +mName: "Hashar",
     ...

>>> ls --long --all $h
<descriptive output of all object properties/methods>

Bug: T117661
Signed-off-by: Justin Hileman <justin@justinhileman.info>
Signed-off-by: Gergő Tisza <tgr.huwiki@gmail.com>
Change-Id: I3d6d42e138d3cc4a0aaafdd7f5f97cb17d8b8cb3
2017-02-13 08:34:38 +00:00