Commit graph

94 commits

Author SHA1 Message Date
Kunal Mehta
3bf222c703 Hooks: remove some unnecessary code
PHP automatically creates the array when you try to append an item to
it. Really.

Change-Id: I73b1efe749a9311feeb0d14e1c76b5bbbc0265bd
2018-05-26 23:39:07 -07:00
Kunal Mehta
cac3ce5668 Complete test coverage of Hooks class
Change-Id: I9e720c44e6d6c9c5d726a8a147e07ee9b638913f
2018-05-26 23:38:28 -07:00
Timo Tijhof
08f609d45d Hooks: Introduce Hooks::runWithoutAbort() alongside Hooks::run()
When used, any hook that tries to abort the event by returning something
other than true or null, will result in a run-time exception.

To make it easier to introduce this opt-in flag, still allow explicit
'true' returns from existing callers.

Also factor out the common code between these methods into a new
private method callHook().

Bug: T173615
Change-Id: I94c7ab656bd1a046410681a810c0a3fd4f72a2e5
2017-09-04 18:55:42 +00:00
Brad Jorsch
9d3d29177b Cleanup after Iba0138a6
Iba0138a6 removed the custom error handler in Hooks::run(), but didn't
remove the comment describing it and left behind an unused variable.

Change-Id: I9b043fa8bd79625e63d35f95f2fa3ac1b214275c
2017-01-03 14:58:06 -05:00
Tim Starling
6117fb244f Refactor parser tests
Merge the PHPUnit parser test runner with the old parserTests.inc,
taking the good bits of both. Reviewed, pared down and documented the
setup code. parserTests.php is now a frontend to a fully featured
parser test system, with lots of developer options, whereas PHPUnit
provides a simpler interface with increased isolation between test
cases.

Performance of both frontends is much improved, perhaps 2x faster for
parserTests.php and 10x faster for PHPUnit.

General:

* Split out the pre-Setup.php global variable configuration from
  phpunit.php into a new class called TestSetup, also called it from
  parserTests.php.
* Factored out the setup of TestsAutoLoader into a static method in
  Maintenance.
* In Setup.php improved "caches" debug output.

PHPUnit frontend:

* Delete the entire contents of NewParserTest and replace it with a
  small wrapper around ParserTestRunner. It doesn't inherit from
  MediaWikiTestCase anymore since integrating the setup code was an
  unnecessary complication.
* Rename MediaWikiParserTest to ParserTestTopLevelSuite and made it an
  instantiable TestSuite class instead of just a static method. Got rid
  of the eval(), just construct TestCase objects directly with a
  specified name, it works just as well.
* Introduce ParserTestFileSuite for per-file setup.
* Remove parser-related options from phpunit.php, since we don't
  support them anymore. Note that --filter now works just as well as
  --regex used to.
* Add CoreParserTestSuite, equivalent to ExtensionsParserTestSuite,
  for clarity.
* Make it possible to call MediaWikiTestCase::setupTestDB() more than
  once, as is implied by the documentation.

parserTests.php frontend:

* Made parserTests.php into a Maintenance subclass, moved CLI-specific
  code to it.
* Renamed ParserTest to ParserTestRunner, this is now the generic
  backend.
* Add --upload-dir option which sets up an FSFileBackend, similar
  to the old default behaviour

Test file reading and interpretation:

* Rename TestFileIterator to TestFileReader, and make it read and buffer
  an entire file, instead of iterating.
* The previous code had an associative array representation of test
  specifications. Used this form more widely to pass around test data.
* Remove the idea of !!hooks copying hooks from $wgParser, this is
  unnecessary now that all extensions use ParserFirstCallInit. Resurrect
  an old interpretation of the feature which was accidentally broken: if
  a named hook does not exist, skip all tests in the file.
* Got rid of the "subtest" idea for tidy variants, instead use a
  human-readable description that appears in the output.
* When all tests in a file are filtered or skipped, don't create the
  articles in them. This greatly speeds up execution time when --regex
  matches a small number of tests. It may possibly break extensions, but
  they would have been randomly broken anyway since there is no
  guarantee of test file execution order.
* Remove integrated testing of OutputPage::addCategoryLinks() category
  link formatting, life is complicated enough already. It can go in
  OutputPageTest if that's a thing we really need.

Result recording and display:

* Make TestRecorder into a generic plugin interface for progress output
  etc., which needs to be abstracted for PHPUnit integration.
* Introduce MultiTestRecorder for recorder chaining, instead of using
  a long inheritance chain. All test recorders now directly inherit from
  TestRecorder.
* Move all console-related code to the new ParserTestPrinter.
* Introduce PhpunitTestRecorder, which is the recorder for the PHPUnit
  frontend. Most events are ignored since they are never emitted in the
  PHPUnit frontend, which does not call runTests().
* Put more information into ParserTestResult and use it more often.

Setup and teardown:

* Introduce a new API for setup/teardown where setup functions return a
  ScopedCallback object which automatically performs the corresponding
  teardown when it goes out of scope.
* Rename setUp() to staticSetup(), rewrite. There was a lot of cruft in
  here which was simply copied from Setup.php without review, and had
  nothing to do with parser tests.
* Rename setupGlobals() to perTestSetup(), mostly rewrite. For
  performance, give staticSetup() precedence in cases where they were
  both setting up the same thing.
* In support of merged setup code, allow Hooks::clear() to be called
  from parserTests.php.
* Remove wgFileExtensions -- it is only used by UploadBase which we
  don't call.
* Remove wgUseImageResize -- superseded by MockMediaHandlerFactory which
  I imported from NewParserTest.
* Import MockFileBackend from NewParserTest. But instead of
  customising the configuration globals, I injected services.
* Remove thumbnail deletion from upload teardown. This makes glob
  handling as in the old parserTests.php unnecessary.
* Remove math file from upload teardown, math is actually an extension
  now! Also, the relevant parser tests were removed from the Math
  extension two years ago in favour of unit tests.
* Make addArticle() private, and introduce addArticles() instead, which
  allows setup/teardown to be done once for each batch of articles
  instead of every time.
* Remove $wgNamespaceAliases and $wgNamespaceProtection setup. These were
  copied in from Setup.php in 2010, and are redundant since we do
  actually run Setup.php.
* Use NullLockManager, don't set up a temporary directory just for
  this alone.

Fuzz tests:

* Use the new TestSetup class.
* Updated for ParserTestRunner interface change.
* Remove some obsolete references to fuzz tests from the two frontends
  where they used to reside.

Bug: T41473
Change-Id: Ia8e17008cb9d9b62ce5645e15a41a3b402f4026a
2016-09-12 16:11:42 +10:00
Kunal Mehta
6e9b4f0e9c Convert all array() syntax to []
Per wikitech-l consensus:
 https://lists.wikimedia.org/pipermail/wikitech-l/2016-February/084821.html

Notes:
* Disabled CallTimePassByReference due to false positives (T127163)

Change-Id: I2c8ce713ce6600a0bb7bf67537c87044c7a45c4b
2016-02-17 01:33:00 -08:00
Ori Livneh
fdca49759b Follow-up for Iba0138a6d: remove now-unused MWHookException class
Change-Id: I9cf80f2b701d6db8a6cfe884e4fd15d4e0482dda
2015-12-04 13:37:18 -08:00
Ori Livneh
cdc50e9029 Don't install a custom error handler for hooks
Installing a custom error handler on every hook invocation has a high overhead,
and does not even correctly achieves what it sets out to achieve, which is to
flag hook function signature errors (and only hook function signature errors).
The "PHP way" is to simply increase the error reporting level for development
environments, which we do already.

Bug: T117553
Change-Id: Iba0138a6d0a0ddf839bc5a36e03cadb012e06f3c
2015-12-04 11:59:52 -08:00
Aaron Schulz
ca2840b5a5 Make hookErrorHandler() only care about serious signature errors
Previously, it would send all manor of warnings to the error
and error-json channels. This adds a lot of overhead due to
AbuseFilter parse/eval errors. By passing immediately instead
of after calling handleError(), that overhead is avoided. Since
it still passes (e.g. returns false), any default PHP warning
logging still applies.

Change-Id: I18e60c09c2a48f2e26abab5d451bb52ea4ba7961
2015-10-21 17:49:31 -07:00
Bryan Davis
a6016137e8 Enhance stacktrace logging for fatals
Split fatal error handling out of MWExceptionHandler::handleError() and
move to MWExceptionHandler::handleFatalError() which has been updated to
work as a dual purpose error handler and shutdown function. Under HHVM
it will be called as an error handler and receive a stacktrace via an
undocumented extension of the error handler callback data. Under PHP5 it
will be called as a shutdown handler and attempt to gather data via
error_get_last().

Also update the error handler installed by Hooks::run() to delegate to
MWExceptionHandler::handleError() for most errors. This will allow us to
properly handle errors raised from within hooks.

Bug: T89169
Change-Id: I0f1c66f203b91fff9069520169ecc4a3b55c43d0
2015-09-08 07:03:36 +00:00
Aaron Schulz
a556484ba0 Revert "Count API and hook calls, with 1:1000 sampling"
This seems to use 3.67% of index.php real time per xenon.

This reverts commit c4cd239939.

Change-Id: I1eb8dfc72036493e430271f0db3f2f2f96b149c3
2015-08-21 20:22:05 +00:00
Gergő Tisza
c4cd239939 Count API and hook calls, with 1:1000 sampling
Re-adds I6f807adc9cbf71c5d7b83c7eec43965dce1d2a16 and
Ic04daf475b936b942833362c7a979dde671b3ef4 (reverted in
35ccd9c2fe) with 1:1000 sampling
to avoid swamping the statsd hosts.

Also fixes query module logging.

Bug: T102079
Bug: T106450
Change-Id: I8b9366407c0d1713790d08e69aaa518130f01977
2015-08-10 22:57:59 +00:00
Bryan Davis
35ccd9c2fe Revert Count API module instantiations and Hook runs
Reverts I6f807adc9cbf71c5d7b83c7eec43965dce1d2a16 and
Ic04daf475b936b942833362c7a979dde671b3ef4

When this hit group1 wikis statsite went nuts with errors like:
* "Failed value conversion! Input: :moduleManager:1"
* "Failed value conversion! Input: :getMessagesFileName:1"
* "Failed value conversion! Input: :get:1"

Change-Id: If0237cdd0d66634d75b2bab8bc4292c0f3ef75ef
2015-07-08 15:13:16 -06:00
Ori Livneh
bf27bbb181 Count API module instantiations and Hook runs
We previously had counts for hooks and modules as a side-effect of having them
profiled. We removed the profiling for performance reasons, which left us
without counts also. But the performance of counters is not a concern, and
their signal value not insubstantial. So introduce them here.

Fix getModulePath() to not crash while we're at it.

Change-Id: Ic04daf475b936b942833362c7a979dde671b3ef4
2015-07-02 16:45:02 -07:00
Aaron Schulz
0e298302d4 Avoid section profiling in Hooks::run due to high overhead
* This wastes 60-70ms and is not really needed with flame graphs anyway

Change-Id: Ifc20ee6fb6439128195b025c652fa29c6f9fc25e
2015-06-10 20:33:21 +00:00
Reedy
4d9143c7f5 Add lots of @throws
Change-Id: I09d0c13070f966fcf23d2638d8fc1328279a5995
2014-12-24 13:49:20 +00:00
Aaron Schulz
dcd639f9f9 Switched hook profiling to use scopedProfileIn
* Also made scopedProfileOut handle the case where the callback
  was null (e.g. when there are no frame methods for xhprof).

Change-Id: Ife242bda8e046990d0d8ac27d628975b7b4a14d7
2014-12-04 19:32:38 +00:00
Stephan Gambke
337698551e Add name of offensive hook function to error message
When a hook function is not callable an exception is thrown.

This patch adds the name of the offensive hook function to
the exception message to improve its informative value.

Change-Id: I376d7f5590099620a4c11ff9a4967d6d8f6560cc
2014-10-10 15:11:04 +00:00
umherirrender
1c68a1ee86 Cleanup some docs (includes/*.php)
- Swap "$variable type" to "type $variable"
- Added missing types
- Fixed spacing inside docs
- Makes beginning of @param/@return/@var/@throws in capital
- Changed some types to match the more common spelling

Change-Id: I783e4dbfe5f6f98b32b9a03ccf6439e13e132bcc
2014-07-24 19:42:24 +02:00
umherirrender
de39f3e019 Use some callable hints on @param docs
Callbacks can be given as a string or array, so the hint 'callable' is
used.

Change-Id: I3842606f74c8c3705dffc70bf13e31f44a37fa65
2014-07-03 21:20:35 +02:00
Siebrand Mazeland
e9eb00b203 Make phpcs-strict pass on includes/ (1/~10)
Change-Id: Ib51381a2261d064988ba2f39b71c0252f2458faf
2014-05-11 19:14:17 +00:00
aude
5963aa7ed7 Add ability to mark hooks as deprecated
Change-Id: I8fd29e97a2b5d02746b572a9c315fbe54faf36f2
2014-01-10 09:12:26 +01:00
daniel
40f6d8546c Make sure we always restore the error handler.
Change-Id: I27f5d11ea27f783eda71c2bfdba7e70695f5d53c
2013-11-15 16:57:34 +00:00
Ori Livneh
5ff7e6fb7a Allow explicit & implicit null returns from hook handlers
Most hook handlers are written with the intent of complementing or augmenting
core functionality rather than vetoing it, making it quite natural for a
developer to forget that the caller is waiting for permission to proceed. The
potential for confusion is magnified by the fact that DOM event handlers and
jQuery event handlers are not required to return an explicit value for the
handled event to continue propagating.

This change tolerates null return values (both implicit and explicit -- that
is, both 'return null' and no return statement at all) from hook handlers. To
abort processing, a hook function must return an explicit false or an error
string.

This change should not break any existing hook functions, as returning null is
currently an error.

Bug: 50134
Change-Id: I11deb2117ff9233c77868470f50e0d8f74053545
2013-06-25 12:10:12 -07:00
umherirrender
6f79eef473 Fixed spacing around parenthesis in includes
Change-Id: Ie8adc00f4ee8ecec4554e584c18d5d2073415397
2013-04-28 15:50:07 +00:00
MatmaRex
af125df519 Cleaned up Hooks code, comments, and documentation.
Essentially rewrote Hooks::run() to get rid of the ridiculous
four levels of indentation. Also made some slight adjustments
to fix rare edge cases (for example, moved set_error_handler
after wfProfileIn in case Profiler triggers an error).

Change-Id: Iafdd4ceedac067b49ac597359ac456f4617da9e8
2013-04-22 16:41:17 +02:00
umherirrender
ef2f507d23 Fixed spacing in files direct in includes folder
Added spaces before if, foreach
Added some braces for one line statements

Change-Id: Ibb8dd102db045522d12ff939075ba7420d95ab6b
2013-04-21 06:38:49 +00:00
umherirrender
978bb31c5e Add missing wfProfileOut before throwing an exception
Change-Id: I1d830da0597f19efd0b2ae48642389975e736e23
2013-04-08 18:37:24 +00:00
umherirrender
6c278b6d7e fix some spacing
* Removed spaces around array index
* Removed double spaces or added spaces to begin or end of function
  calls, method signature, conditions or foreachs
* Added braces to one-line ifs
* Changed multi line conditions to one line conditions
* Realigned some arrays

Change-Id: Ia04d2a99d663b07101013c2d53b3b2e872fd9cc3
2013-03-25 22:22:46 +00:00
Tyler Anthony Romeo
4dcc7961df Fixed @param tags to conform with Doxygen format.
Doxygen expects parameter types to come before the
parameter name in @param tags. Used a quick regex
to switch everything around where possible. This
only fixes cases where a primitve variable (or a
primitive followed by other types) is the variable
type. Other cases will need to be fixed manually.

Change-Id: Ic59fd20856eb0489d70f3469a56ebce0efb3db13
2013-03-11 13:15:01 -04:00
umherirrender
d63121016d fix some spacing
Added/removed spaces around logical/arithmetic operator
Reduced multiple empty lines to one empty line
Removed wrong tabs before comments at end of line
Removed too many spaces in assigments

Change-Id: I2bba4e72f9b5f88c53324d7b70e6042f1aad8f6b
2013-03-07 17:53:21 +01:00
daniel
5d270eb65d Profiling for hook calls.
This is similar to profiloing for db queries.

Change-Id: I14051f8aff30d249f4197270337e4080adfa5417
2013-02-01 10:51:00 +00:00
Reedy
d537d96868 Add numerous missing @throws to method documentation
Change-Id: Iba868e82a75fef7c7d011bc5be192bf059d037c0
2012-12-09 03:09:48 +00:00
daniel
503ce75e27 Fix behavior of Hooks class.
Until now, Hooks::run() would execute hooks registered via $wgHooks, but
Hooks:isRegistered() would not consider them and Hooks::getHandlers() would
not return them. That is inconsistent and misleading. This change aims to
make the methods of the Hooks class behave consistently, and allows them
to be used as a generic way of interacting with hooks.

Change-Id: I39bd5de2bc8ccbe8df729446363960af9d29b0be
2012-10-08 13:45:58 +02:00
Reedy
7d34b9c48b Add @since tags too Hooks
Update some method documentation

Change-Id: If4913aab7d0f8c14d49ca8320e31b1b354145c83
2012-10-05 19:29:41 +01:00
Sam Reed
c052fc3b7b Documentation followups from lastnight 2012-02-10 15:37:33 +00:00
Sam Reed
7b25f8231f Fixing some of the "@return true" or "@return false", need to be "@return bool" and then the metadata can say true if foo, false if bar
Other documentation improvements
2012-02-09 19:30:01 +00:00
Aaron Schulz
5a6559d8b1 Reverted r108743 per CR comment. This should at least be discussed first. 2012-01-12 19:41:18 +00:00
Sam Reed
a5c200a25b Kill "* @return void" 2012-01-12 19:12:24 +00:00
Aaron Schulz
1d77bc94c2 * Cleanup for r108175: made if easier for extensions to interact with each other in TitleReadWhitelist hook. A handler can set $whitelisted to true to whitelist the page or it can set it to false and return false (most restrictive wins).
* Added some hook doc comments.
2012-01-06 06:16:05 +00:00
Alexandre Emsenhuber
4aa1b32903 Throw a FatalError exception instead of calling OutputPage::showFatalError() (on usage of $wgOut less) 2011-11-09 10:59:17 +00:00
Sam Reed
bdf84cb20e Swap else if for elseif
Trimming trailing whitespace also

Doing in 3 commits, so hopefully reviewable in CR...
2011-06-17 16:03:52 +00:00
Alexandre Emsenhuber
6494121882 Now that Hooks.php contains a class, moved wfRunHooks() definition to GlobalFunctions.php and removed its inclusion from Setup.php 2011-05-28 16:11:40 +00:00
Antoine Musso
9787dea7a2 use string as callback when possible (since PHP 5.2.3) 2011-05-18 20:33:47 +00:00
Alexandre Emsenhuber
81e9b02ff5 * Drop 5.1 compat code
* Fix one more PHP version requirement
2011-04-03 09:44:50 +00:00
Jack Phoenix
3f0decb5cd Hooks.php: trim trailing spaces, tweak docs, etc. 2011-01-26 16:50:31 +00:00
X!
aa9b9527b9 Add new Hooks class, because $wgHooks globals are evil.
$wgHooks['EventName'][] = $callback; --> Hooks::register( 'EventName', $callback );
wfRunHooks( 'EventName', array() ); --> Hooks::run( 'EventName', array() );
Tests added to complement change. Backwards compatibility added.
2011-01-17 03:35:42 +00:00
Sam Reed
8a01281913 Remove some unreachable code (usually returns after throwing exceptions)
Fixup call of method after return in DatabaseMssql by using a temp
2010-08-24 22:03:18 +00:00
Alexandre Emsenhuber
5be509b0c1 * Standardised file description headers
* Added some descriptions
* Added @file where needed
2010-08-08 14:23:14 +00:00
Niklas Laxström
f09f99ac1f Follow-up r70109: use restore_error_handler() 2010-08-03 19:46:18 +00:00