Commit graph

20 commits

Author SHA1 Message Date
Brad Jorsch
1c57794e37 API: Overhaul ApiResult, make format=xml not throw, and add json formatversion
ApiResult was a mess: some methods could only be used with an array
reference instead of manipulating the stored data, methods that had both
array-ref and internal-data versions had names that didn't at all
correspond, some methods that worked on an array reference were
annoyingly non-static, and then the whole mess with setIndexedTagName.

ApiFormatXml is also entirely annoying to deal with, as it liked to
throw exceptions if certain metadata wasn't provided that no other
formatter required. Its legacy also means we have this silly convention
of using empty-string rather than boolean true, annoying restrictions on
keys (leading to things that should be hashes being arrays of key-value
object instead), '*' used as a key all over the place, and so on.

So, changes here:
* ApiResult is no longer an ApiBase or a ContextSource.
* Wherever sensible, ApiResult provides a static method working on an
  arrayref and a non-static method working on internal data.
* Metadata is now always added to ApiResult's internal data structure.
  Formatters are responsible for stripping it if necessary. "raw mode"
  is deprecated.
* New metadata to replace the '*' key, solve the array() => '[]' vs '{}'
  question, and so on.
* New class for formatting warnings and errors using i18n messages, and
  support for multiple errors and a more machine-readable format for
  warnings. For the moment, though, the actual output will not be changing
  yet (see T47843 for future plans).
* New formatversion parameter for format=json and format=php, to select
  between BC mode and the modern output.
* In BC mode, booleans will be converted to empty-string presence style;
  modules currently returning booleans will need to use
  ApiResult::META_BC_BOOLS to preserve their current output.

Actual changes to the API modules' output (e.g. actually returning
booleans for the new formatversion) beyond the use of
ApiResult::setContentValue() are left for a future change.

Bug: T76728
Bug: T57371
Bug: T33629
Change-Id: I7b37295e8862b188d1f3b0cd07f66ac34629678f
2015-04-10 16:57:15 -04:00
umherirrender
1c0235513e Use mergeMwGlobalArrayValue in ApiOptionsTest
Change-Id: Ic0a9ceab9e8fad7c408e32ba110a3543ef28b59e
2014-10-23 17:42:34 +00:00
Timo Tijhof
820bb47490 ApiOptionsTest: Simplify hook restoration
Change-Id: I2afc35b45caf9ddfd34fb0bba0839987b7fb2ed4
2014-07-01 18:32:05 +02:00
Siebrand Mazeland
80a8a8e85c Pass phpcs-strict on some test files (5/x)
Change-Id: I690645cd8a9b1165dcc8271b201c695ea9391226
2014-04-24 18:51:45 +02:00
jenkins-bot
11cb289cfc Merge "API: Handle "special" options in action=options" 2014-02-04 18:43:40 +00:00
addshore
cad7b8368d General Cleanup of some Tests
Style Fixes, Comment fixes

Change-Id: I675d3f098e81709d5dfd928af6ca54589d3d5fad
2013-11-25 14:17:35 +00:00
umherirrender
f153998317 Fixed spacing
- Removed double spaces
- Added space after if/switch/foreach
- Removed space on elseif
- Added space around parentheses
- Added newline at end of file
- Removed space before semicolon at end of line

Change-Id: Id40b87e04786c6111e6686d7f7eea1e588bdf37d
2013-11-19 19:03:54 +01:00
addshore
dc18b813e8 Cleanup Api phpunit Tests
- Splits multiple classes into individual files
- Adds @covers tags
- Fixes scope

Change-Id: I7d2816d3574fa53a2aaa8e2a84b7a7ecdd245252
2013-10-24 19:17:01 +01:00
Brad Jorsch
b393835bda API: Handle "special" options in action=options
There are certain preferences, such as realname and emailaddress, that
are handled specially in Special:Preferences and are not accessible by
User::getOptions or User::setOptions. But action=options was allowing
a 'dummy' version to be set in User::setOptions.

Change-Id: I4c1b3d8e1eae9520228d7b6da9c41ada80f7c387
2013-10-10 11:02:04 -04:00
Brad Jorsch
08adf46b26 Add 'viewmyprivateinfo', 'editmyprivateinfo', and 'editmyoptions' rights
These are needed for OAuth grants.

Note that we don't bother with a 'viewmyoptions' right, since the
majority will be determinable from just observing the interface.

Note that the fact of having a confirmed email address cannot be
reliably hidden, and if the user has 'sendemail' they may be able to
determine the real name and email address by sending an email to another
account that they control.

Change-Id: I3f03dd010020e8d43cc2d3bca7b3ef7196d1c548
2013-07-03 13:40:04 -04:00
Siebrand Mazeland
791d0b2a98 Update code formatting
Change-Id: I16a9b42651f1cfb1a70dffbb67b7b83dfeb90d03
2013-04-26 14:21:20 +00:00
Tyler Anthony Romeo
e521f0680e (bug 43959) Add ability to reset certain option kinds in API.
Added the "resetkinds" option to action=options, so that when the
"reset" option is set, the user can control which kinds of options
are reset, rather than having to do all or none.

Also added documentation to the "change" parameter, since passing
it option keys without any "=value" after it will result in resetting
that specific option to its default value.

Change-Id: Id5bc1fffa0d487c0f152b79115205d2722f380d3
2013-01-18 13:41:44 -05:00
MatmaRex
733d19d0d0 (bug 40124) allow arbitrary user preferences prefixed with 'userjs-'
Before change I98df55f2 it was possible to set arbitrary preferences (ie.
with anything as the key) using the action=options API. That change
removed this ability by enforcing full validation of the preferences, also
introducing several regressions which were fixed by follow-ups.

Per the discussion on bug 40124, this changeset aims to restore this
ability, but in a slightly restricted way: arbitrary preferences' names
must start with userjs- prefix, to avoid any possibility of conflicting
with new MediaWiki versions or extensions.

The contents of these preferences is not escaped, sanitized nor validated
in any way; script authors are expected to sanitize them themselves to
prevent XSS attacks and other security vulnerabilities.

This commit also adds the User::getOptionsKinds() method (to determine
whether given preference keys are used by MediaWiki itself or an extension,
intended to be used via the API, or entirely unknown) and enhances the
User::resetOptions() method to allow for resetting only preferences of
chosen kinds.

These changes allow for fixing of Special:Preferences not to clear those
additional fields when saving user settings.

Change-Id: I5f9ba5b0dfe7c2ea5458d836f03429cf6d93969d
2013-01-13 19:08:56 +01:00
Timo Tijhof
ab7729589e ApiOptionsTest: Group medium (takes more than 2 seconds).
Various jobs are failing in Jenkins due to this test exceeding
the maximum execution time of 2 seconds.

https://integration.mediawiki.org/ci/job/mediawiki-core-phpunit-api/684/console

Follows-up 13f0f58, fb7c95f.

Change-Id: I41a9a571a50e538de514586618d58438c5dd6148
2012-12-28 18:23:02 +01:00
Brad Jorsch
13f0f58f76 (bug 42639) Fix API action=options for multiselect prefs
Preferences options using HTMLForm's "multiselect" type are stored in
the user preferences table as one key with a boolean value for each
option in the multiselect. The validation code added in change I98df55f2
does not take this into account, and therefore considers all of these
option keys invalid.

This changeset fixes that, and adds a unit test to verify correct
behavior.

Change-Id: I137c74a6045c7b39e2119a8edde2705738879bc9
2012-12-08 10:09:04 +01:00
Brad Jorsch
fb7c95f567 (bug 42638) Fix API action=options&reset=1 & unit tests
Change I98df55f2 broke action=options&reset=1, causing it to return an
error "No changes were requested" rather than resetting the options as
it should. Unfortunately, that change also broke the unit test that
would have caught this regression.

This changeset fixes the bug and the unit tests.

Change-Id: I7fe63640d54efab4572538e9d08f5b75c61243a4
2012-12-02 23:22:18 -05:00
Catrope
fe45ba8752 (bug 42202) Validate preference values in action=options
Previously, there was no validation whatsoever and the module would
happily write any preference you asked it to. This, combined with the
fact that the code using the 'editfont' preference didn't perform any
validation or escaping, led to a CSS injection vulnerability.

Using Preferences::getPreferences breaks some existing test cases
because a MockUser doesn't have groups for preferences.

Change-Id: I98df55f2b16ac1b6fce578798b6f58b5dad96775
2012-11-29 16:42:56 -08:00
Timo Tijhof
181c7cdc8e Clean and repair many phpunit tests (+ fix implied configuration)
This commit depends on the introduction of
MediaWikiTestCase::setMwGlobals in change Iccf6ea81f4.

Various tests already set their globals, but forgot to restore
them afterwards, or forgot to call the parent setUp, tearDown...

Either way they won't have to anymore with setMwGlobals.

Consistent use of function characteristics:
* protected function setUp
* protected function tearDown
* public static function (provide..)

(Matching the function signature with PHPUnit/Framework/TestCase.php)

Replaces:
 * public function (setUp|tearDown)\(
 * protected function $1(

 * \tfunction (setUp|tearDown)\(
 * \tprotected function $1(

 * \tfunction (data|provide)\(
 * \tpublic static function $1\(

Also renamed a few "data#", "provider#" and "provides#" functions
to "provide#" for consistency. This also removes confusion where
the /media tests had a few private methods called dataFile(),
which were sometimes expected to be data providers.

Fixes:

TimestampTest often failed due to a previous test setting a
different language (it tests "1 hour ago" so need to make sure
it is set to English).

MWNamespaceTest became a lot cleaner now that it executes with
a known context. Though the now-redundant code that was removed
didn't work anyway because wgContentNamespaces isn't keyed by
namespace id, it had them was values...

FileBackendTest:
* Fixed: "PHP Fatal: Using $this when not in object context"

HttpTest
* Added comment about:
  "PHP Fatal: Call to protected MWHttpRequest::__construct()"
  (too much unrelated code to fix in this commit)

ExternalStoreTest
* Add an assertTrue as well, without it the test is useless
  because regardless of whether wgExternalStores is true or false
  it only uses it if it is an array.

Change-Id: I9d2b148e57bada64afeb7d5a99bec0e58f8e1561
2012-10-09 03:01:51 +02:00
Tim Starling
343cbf7463 Add an API log
During incident response, it was not possible to tell what API modules
were being requested and by whom, since the action parameter is often
posted. This change logs the API parameters whether they are posted or
sent in the query string.

I did try to get the API parameters from the module, but that turns out
to be difficult. Modules create submodules (generators, page sets) as
local variables, which are created in a procedural style and destroyed
before logging is done, so there is no easy way to query them for
parameter lists after execution completes.

In ApiOptionsTest, use a real ApiMain object like all the other API test
cases, rather than a mock object. Otherwise the test fails.

Change-Id: Idc786007fe61811d1874f29b5ce4762dd97b1847
2012-09-24 21:22:09 +00:00
Szymon Świerkosz
a6cd69d83a (bug 18195) Allow changing preferences via API
I have created an API module for changing the preferences.
It allows resetting preferences (reset argument) and bulk changes
of preferences (change argument) in a format:
name1=value1|name2=value2

The change argument has a limitation imposed by the current API
implementation as it cannot accept | in values. There is
available a pair of arguments optionname and optionvalue, the
latter accepts values with |.

I have created optionstoken parameter in meta=userinfo to provide
a token. There is already preferencestoken there, but I would
like to have a consistent naming.

Change-Id: I0d6c654a7354ba77e65e338423952a6a78c1150f
2012-04-20 16:56:27 +10:00