Commit graph

312 commits

Author SHA1 Message Date
Tim Starling
317b460500 Fix even more PHPStorm inspections (#3)
* Inappropriate @inheritDoc usage. Arguably all @inheritDoc is
  inappropriate but these are the ones PHPStorm flags as misleading
  due to the method not being inherited.
* Doc comment type does not match actual argument/return type.
* I replaced "@return void|never" with "@return void" since never means
  never, it doesn't make sense for it to be conditional. If a method
  can return (even if that is unlikely) then @return contains the type
  that it returns. "@return never" means that there is no such type
  because the method never returns.
* Incomplete/partial/broken doc tags

Change-Id: Ide86bd6d2b44387f37d234c2b059d6fbc42ec962
2023-03-25 00:30:15 +00:00
Kevin Israel
753d266eab MaintenanceRunner: Pull some checks out of initInternal()
* Move the PHP_SAPI check to the earliest convenient point in
  Maintenance.php, well before we attempt to access $argv.

* Remove the register_argc_argv check altogether; we don't allow "cgi"
  SAPI to be used for running scripts from the command line any more.
  Any value from php.ini is overridden by "cli" and "phpdbg" SAPIs:

  https://www.php.net/manual/en/features.commandline.differences.php
  https://github.com/php/php-src/blob/php-8.2.1/sapi/phpdbg/phpdbg.c#L991

  And using "-d register_argc_argv=0" or similar seems unlikely.

* Don't check if $IP is null (reverts b0647c30bc). This specific
  check is not useful any more, as $IP is now initialized before the
  Maintenance object is constructed, and addOption() and similar methods
  happen to generate PHP errors in the described situation. If desired,
  a check serving the same purpose could be added in a separate commit.

Change-Id: I8032ab18cb26d92ee01ab9851ea1d45b3f9eadbf
2023-01-11 10:18:50 -05:00
daniel
dcef1674a5 Allow main settings file to be selected via env variable.
This allows a file other than LocalSettings.php to be used as the primary
settings file by setting the MW_CONFIG_FILE environment variable.
This also allows the primary settings file to use YAML or JSON format.

Using static configuration files should be the default in the future.
However, YAML files in the document root could easily be exposed to the
public. Better not to encourage that, and require them to be enabled
explicitly and loaded from a different place.

Bug: T294750
Change-Id: I7747f83481cb05a6d05f819be652259951183819
2022-02-06 21:13:00 +01:00
daniel
8a33830b7c Make sure RUN_MAINTENANCE_IF_MAIN is defined in tests.
This moves the definition of the RUN_MAINTENANCE_IF_MAIN constant
in Maintenance.php before the check on MEDIAWIKI, so the constant will
be defined even if MEDIAWIKI is also defined. This allows maintenance
scripts to be successfully loaded in a context in which MEDIAWIKI is
already defined, but RUN_MAINTENANCE_IF_MAIN is not. Without this
change, such an attempt would fail with a warning that the constant is
undefined at the bottom of the maintenance script.

A notable use case for loading a maintenance script inline is calling
RunJobs in a test case. This would work fine if the test is called via
MediaWiki's phpunit wrapper, since that wrapper is itself written as a
maintenance script, and thus declared RUN_MAINTENANCE_IF_MAIN.

However, integration tests may also be invoked without the wrapper,
using the phpunit.xml.dist config file instead. This is the standard way
to invoke PHPUnit from within an IDE like PhpStorm, allowing for debugging
of unit tests.

So, without this patch, integration tests that call RunJobs fail due to
an undeclared constant when run via IDE integration. With this patch,
they work properly.

Change-Id: I52889e092cc9f20bf892c297635b87a972dafa41
2021-01-19 19:09:31 +00:00
Tim Starling
20d06b34bb Safer autoloading with respect to file-scope code
Many files were in the autoloader despite having potentially harmful
file-scope code.

* Exclude all CommandLineInc maintenance scripts from the autoloader.
* Introduce  "NO_AUTOLOAD" tag which excludes the file containing it
  from the autoloader. Use it on CommandLineInc.php and a few
  suspicious-looking files without classes in case they are refactored
  to add classes in the future.
* Add a test which parses all non-PSR4 class files and confirms that
  they do not contain dangerous file-scope code. It's slow (15s) but
  its results were enlightening.
* Several maintenance scripts define constants in the file scope,
  intending to modify the behaviour of MediaWiki. Either move the
  define() to a later setup function, or protect with NO_AUTOLOAD.
* Use require_once consistently with Maintenance.php and
  doMaintenance.php, per the original convention which is supposed to
  allow one maintenance script to use the class of another maintenance
  script. Using require breaks autoloading of these maintenance class
  files.
* When Maintenance.php is included, check if MediaWiki has already
  started, and if so, return early. Revert the fix for T250003 which
  is incompatible with this safety measure. Hopefully it was superseded
  by splitting out the class file.
* In runScript.php add a redundant PHP_SAPI check since it does some
  things in file-scope code before any other check will be run.
* Change the if(false) class_alias(...) to something more hackish and
  more compatible with the new test.
* Some site-related scripts found Maintenance.php in a non-standard way.
  Use the standard way.
* fileOpPerfTest.php called error_reporting(). Probably debugging code
  left in; removed.
* Moved mediawiki.compress.7z registration from the class file to the
  caller.

Change-Id: I1b1be90343a5ab678df6f1b1bdd03319dcf6537f
2021-01-11 11:59:36 +11:00
Reedy
c7eb28aac9 Fix various MediaWiki.WhiteSpace.SpaceBeforeSingleLineComment.NewLineComment
Change-Id: I50c7c93f1534e966224f98a835ca01f93eb9416d
2020-05-21 01:06:05 +00:00
Daimona Eaytoy
870743489b maintenance: Move FakeMaintenance and LoggedUpdateMaintenance to their own files
Originally submitted as commit 2cdeb26d0c.

This patch is fully backwards compatible. The Maintenance.php entry
point is now a true entry point (i.e. no classes defined), and it requires
all the *Maintenance classes.

Bug: T246780
Change-Id: I75b22f54b19fe560ab71349189598245af1c5693
2020-04-21 16:42:01 +00:00
Daimona Eaytoy
57554fa31d maintenance: Add @return-taint none to getArg and getOption
In a generic context, $argv would be considered tainted by taint-check,
which makes sense for certain applications. In the context of MW
maintenance scripts, however, we can assume that arguments are trusted,
because maintenance scripts can only be executed by sysadmins, who are
generally trusted (and even if they aren't, they have other ways to
compromise a wiki).

Bug: T216348
Change-Id: I8e171f5dcd5b9232d45e85d775cd794f2ebb2dfc
2020-04-18 20:37:57 +02:00
Timo Tijhof
5bfa31187b maintenance: Move $IP and MEDIAWIKI assignment to doMaintenance.php
Per 4eb1e679d5, these are now documented as the bare minimum
entry point responsibilities. For WebStart these are already
consolidated, but for the CLI use case they were a bit scattered.
Consolidate these in the CLI entry point (doMaintenance.php)
for clarity.

* $IP was previously assigned in Maintenance::__construct,
  which is called from doMaintenance.php.
  It still executes at the same logical time, one line above
  'new $maintClass()'.

* MEDIAWIKI was (for CLI) previously defined in Maintenance::setup()
  which is called from doMaintenance.php.
  This is now done a few lines earlier, and in the entry point file
  instead.

For both of these, they are still available and set in all the same
scenarios as before.

Bug: T246076
Bug: T250003
Change-Id: I2a6f5e997a36502535eb383a95deac0ce74d83fb
2020-04-14 19:33:13 +00:00
ArtBaltai
9ff7146049 Maintenance: Don't redefine MW_ENTRY_POINT if it's already set
Maintenance.php may be included after other entry points in certain
configurations (like IDE bootstrapping). So if MW_ENTRY_POINT is already
set, don't try and define() it as another value.

Bug: T250003
Change-Id: Ide011ad11d8dd29fe786a745c6e67259e46b9bfb
2020-04-14 01:54:05 +00:00
Marius Hoch
27fe734fb2 Maintenance: Don't create DBLoadBalancerFactory to destroy it
This is the right way to do this, per the documentation
of ServiceContainer::peekService.

Change-Id: I47c622faacea2ab49e16903e3b5aa9d1215d5bfb
2020-03-26 20:32:04 +01:00
Petr Pchelko
b3ab937939 Deprecate Revision::loadFromPageId
Bug: T246284
Depends-On: Id4ec83cc0ac0685857064486697a7daed979ea69
Change-Id: I5de750322e30052700f509d2908456ebd5be95ef
2020-03-25 12:10:48 -07:00
daniel
495323c063 Remove compat code for pre-MCR schema.
NOTE: This was manually tested to ensure it does not
break updates, see T242764.

Bug: T198557
Change-Id: I1d9f5465018bae10124514bc38169e23e0e613e6
2020-03-24 19:45:47 +01:00
Timo Tijhof
c75a4ac16f Remove $wgProfileLimit setting
Deprecated since MediaWiki 1.25.

Bug: T247777
Bug: T228895
Change-Id: I638d9a219df7b5c661548354fd3d219ada340a86
2020-03-19 23:03:20 +00:00
Timo Tijhof
624c629300 maintenance: Remove --mwdebug option
I added this in fdb7d36903 when DevelopmentSettings.php was fairly
basic and might have even been harmless to include in production
(unverified at this point).

The use case was easy injection by WMF CI and Travis CI,
but we never ended up using that. Both for CI and for local
development we have converged on the debuggable/non-magical way
of injecting it, which is explicitly placing it in LocalSettings.php
at the point where the developer is in control of it.

It also means it consistently applies no matter which script you
run, including on the web, which makes sense as the site settings
shouldn't change unexpectedly.

An alternative might be to make this configurable and only
available in a special development mode, and not in prod. However
that creates a bit of a paradox. If we require admins to place
something in LocalSettings to make this available, might as well
make that line of code be "require DevelopmentSettings.php",
equally easy to copy/paste and comment out.

Change-Id: Iffc58599581bf20b5880b7c57259e8b37ddf9dcb
2020-03-11 17:57:01 +00:00
Daimona Eaytoy
7def03fe80 Revert "Split FakeMaintenance and LoggedUpdateMaintenance to their own files"
This reverts commit 2cdeb26d0c.

Reason for revert: broke roughly all scripts extending LoggedUpdateMaintenance.

Bug: T246754
Change-Id: Icfe3da924c364b4b4f9286871aeb0aa9f1bbff1a
2020-03-03 14:14:31 +00:00
Reedy
2cdeb26d0c Split FakeMaintenance and LoggedUpdateMaintenance to their own files
Change-Id: I4c95f910993fffa060c2860847b8cfff1612ccf5
2020-02-28 21:51:24 +00:00
James D. Forrester
0958a0bce4 Coding style: Auto-fix MediaWiki.Usage.IsNull.IsNull
Change-Id: I90cfe8366c0245c9c67e598d17800684897a4e27
2020-01-10 14:17:13 -08:00
James D. Forrester
4f2d1efdda Coding style: Auto-fix MediaWiki.Classes.UnsortedUseStatements.UnsortedUse
Change-Id: I94a0ae83c65e8ee419bbd1ae1e86ab21ed4d8210
2020-01-10 09:32:25 -08:00
jenkins-bot
748110478c Merge "Show the PHPUnit help in phpunit.php --help" 2019-12-02 17:59:29 +00:00
Umherirrender
c7ad21c25f Improve param docs
Change-Id: I746a69f6ed01c3ff000da125457df62b02d13b34
2019-11-28 19:08:59 +01:00
Tim Starling
15e058d46e Show the PHPUnit help in phpunit.php --help
Split Maintenance::showHelp() from Maintenance::maybeHelp(), and
override it in phpunit.php. Expose PHPUnit's protected method
Command::showHelp() in our subclass and call it, so that running
"phpunit.php  --help" causes the MediaWiki options to be shown, followed
by the PHPUnit options.

Change-Id: I4687d484e322a465af0492789645819cd8a7b67c
2019-11-25 17:19:20 +11:00
Daimona Eaytoy
b1a5367ec8 Fix new phan errors, part 7
Bug: T231636
Change-Id: Ia5e0abee7163c5a1abd0bb53b89603cc2e7a9b5c
2019-10-21 22:10:20 +00:00
Umherirrender
a7fb6361a6 Add type hint for $maintClass
Change-Id: I0ba7fd7eb6145ab12625796c842bb2b664d362ab
2019-10-04 19:59:59 +02:00
TheSandDoctor
d6f4189088 Maintenance.php: Fix mwdebug error
Resolve error with showJobs.php --mwdebug option. Previously, it displayed an error if not followed by "=1".
This was resolved by changing
$this->addOption( 'mwdebug', 'Enable built-in MediaWiki development settings', false, true );
to
$this->addOption( 'mwdebug', 'Enable built-in MediaWiki development settings', false, false );

Bug: T233257
Change-Id: I322fa539a302c2726fffd2420f7f56aec476b32b
2019-09-23 10:24:19 -07:00
Brad Jorsch
d2725a8e11 maintenance/Maintenance.php: Fix MW_ENTRY_POINT definition (followup I24099f4c)
Quotes were omitted. This currently works in PHP, but raises a warning.

Change-Id: I4c9ce4b5ea9fb9f30215f646cc8fa60db0ed7d1c
2019-09-09 11:56:57 -04:00
jenkins-bot
1880c8401a Merge "Add MW_REST_API and MW_ENTRY_POINT" 2019-09-09 15:29:21 +00:00
Daimona Eaytoy
b5cbb5ab3f Upgrade phan config to 0.7.1
This allows us to remove many suppressions for phan false positives.

Bug: T231636
Depends-On: I82a279e1f7b0fdefd3bb712e46c7d0665429d065
Change-Id: I5c251e9584a1ae9fb1577afcafb5001e0dcd41c7
2019-09-04 08:20:53 +00:00
Daimona Eaytoy
43cc14657d Unsuppress phan issues, part 8
And also clean up the config.

Bug: T231636
Depends-On: Ie6233561de78457cae5e4e44e220feec2d1272d8
Change-Id: I82a279e1f7b0fdefd3bb712e46c7d0665429d065
2019-09-04 01:02:06 +00:00
Daimona Eaytoy
c659bc6308 Unsuppress another phan issue (part 7)
Bug: T231636
Depends-On: I2cd24e73726394e3200a570c45d5e86b6849bfa9
Depends-On: I4fa3e6aad872434ca397325ed7a83f94973661d0
Change-Id: Ie6233561de78457cae5e4e44e220feec2d1272d8
2019-09-03 17:19:21 +00:00
Tim Starling
b7ce7aacb0 Add MW_REST_API and MW_ENTRY_POINT
Define the global constant MW_REST_API in rest.php, by analogy with
MW_API. Also generalize this by adding MW_ENTRY_POINT, which contains
the entry script name, "cli" or "unknown". This allows tests such as

  if ( MW_ENTRY_POINT !== 'index' )

which is probably what is really intended by defined('MW_API') in many
cases.

Change-Id: I24099f4cdd170de17afd6e1bbad67c9b204071fc
2019-09-03 11:43:18 +10:00
Timo Tijhof
8a87ec2778 profiler: Centralise output responsibility from ProfilerOutputText to Profiler
Make it Profiler.php's responsibility to enforce this, based on the
existing signal from ProfilerOutput::logsToOutput().

The ProfilerOutputText class should not have to double-check this
a second time.

Long-term, I'd like even this check in Profiler::logDataPageOutputOnly
to be removed, because really the external caller of that should
know whether it is safe to output stuff or not rather than stashing
its own state inside Profiler::$allowOutput and then implicitly
reading it back out again later on. But, that's for another time.

Also:
* Remove use of deprecated Profiler::setTemplated while at it.
* Make 'visible' parameter explicit, as for other parameters.

Change-Id: Iaa3fc4ea25a059b90235d769db60c04b8f152f05
2019-08-28 16:07:18 +00:00
jenkins-bot
553c6c4197 Merge "Convert maintenance scripts to LoadBalancer::getMaintenanceConnectionRef()" 2019-08-02 22:52:23 +00:00
Aaron Schulz
4a55e578ff Convert maintenance scripts to LoadBalancer::getMaintenanceConnectionRef()
Change-Id: I8944a052f51a1941ad3b76a40fc9c46d1363c426
2019-08-02 22:29:43 +00:00
David Causse
bc0de57e75 Simplify SearchUpdate constructor and hard deprecate some param types
Change-Id: I5677041169402014f1afc1a9012460c760ca24b6
2019-08-01 17:24:42 +02:00
daniel
275d6e2ad4 Tell users how they can force a maintenance script to run again.
Subclasses of LoggedUpdateMaintenance tell the user that "Update
'{$key}' already logged as completed". This adds "Use --force to
run it again."

Bug: T200653
Change-Id: I8cf3c5383c0fecdc92b58048138332855b5602a6
2019-07-05 13:23:21 +00:00
jenkins-bot
f444e41fb5 Merge "Fix documentation of Maintenance::updateSearchIndex" 2019-06-10 20:18:53 +00:00
Umherirrender
9d82a218ac Fix documentation of Maintenance::updateSearchIndex
Document callback as callable type and the results part as
IResultWrapper to match the implementation, which is passing return of
Database::query to this function

Change-Id: I63ee8d4907590a21ef34d0b761b8fabfe0ff2569
2019-06-07 19:22:19 +02:00
Umherirrender
5f43b1706c Pass options as array to IDatabase::insert
The documentation only allows arrays there

Change-Id: I00c6e47a817a70bed9a443aebc675ef4c3d6b1e5
2019-06-07 19:12:35 +02:00
Kunal Mehta
01541db36b Remove usage of deprecated SkinFactory::getDefaultInstance()
Change-Id: I3bd50f8ca5baabd34dbc0e3bbc2f97e94650a17a
2019-06-02 21:08:07 -04:00
jenkins-bot
1663892723 Merge "Set MW_INSTALL_PATH in Maintenance.php" 2019-04-12 20:34:45 +00:00
Aryeh Gregor
b46a32c85f Set MW_INSTALL_PATH in Maintenance.php
Otherwise, if running update.php, there are require_once failures from
extensions (at least Echo and Flow) if they aren't inside extensions/ --
e.g., if they're just symlinked from there.

Change-Id: Iaf4231abae1621627f01171f955b5bb7a0fa77d8
2019-04-11 11:39:45 +03:00
Fomafix
14d9e80ba4 Fix order of @var parameter in PHP
Replace

 @var $foo type

by

 @var type $foo

Change-Id: Iec8b90ffea4028a1005daef422b7c4ec38c2715f
2019-04-08 18:37:56 +02:00
Derick Alangi
84292b7728 Replace deprecated function wfEscapeShellArg with Shell::escape()
Change-Id: I4046d593d1450cfffc489ca2abadba1084a540e4
2019-04-07 20:17:39 +01:00
jenkins-bot
b0a3dd3653 Merge "maintenance: Deprecate Maintenance::hasArg/getArg with no param" 2019-03-31 16:36:23 +00:00
jenkins-bot
a6facc8a0a Merge "Allow extensions to add params to the update.php maintenance script" 2019-03-29 16:33:55 +00:00
Thiemo Kreuz
06c1a5976c maintenance: Deprecate Maintenance::hasArg/getArg with no param
Benefit of keeping the parameter optional:
- In maintenance scripts that really only have one parameter, it's a
  little more convenient to be able to ask for *the* parameter via an
  empty getArg().

Disadvantages:
- It's unclear what getArg() means when there is no indication *which*
  argument the code asks for. This might as well return the last
  argument, or an array of all arguments.
- In scripts with two or more arguments, it's confusing to see
  getArg( 1 ) next to an empty getArg().
- The methods are more complex and a bit more complicated to use with
  the extra feature of this parameter being optional. Users need to
  look up what the default is to be able to use it safely.

Change-Id: I22a43bfdfc0f0c9ffdb468c13aba73b888d1f15e
2019-03-29 14:37:46 +01:00
Bill Pirkle
4d5edb65f3 Allow extensions to add params to the update.php maintenance script
T110209 caused maintenance scripts to fail on unknown parameters,
which is normally desirable.  However, some extensions (notably
SemanticMediaWiki) need to support additional params and had no
way to add them to the list of expected parameters.  It will
now be possible them to update.php via a new hook:
MaintenanceUpdateAddParams.

Bug: T213893
Change-Id: Ia40949ccb2f32090f21e0f3f7e5b9c4aef322330
2019-03-26 21:12:49 -05:00
Thiemo Kreuz
e5831cfd5a maintenance: Use the ?? feature together with explode()
This gets rid of a few if(), count() and such that make the code appear
quite complicated, even if it isn't.

Change-Id: Iade6589eba2a9496b28042bfc777b92258b3332a
2019-03-26 11:45:42 +01:00
Aaron Schulz
7d33a793e4 Fix fatal due to lock name mismatch in Maintenance::unlockSearchindex
Change-Id: Id18c0c8eb20c215ba7421e087b721ac7dc57227a
2019-03-19 21:29:14 -07:00