Commit graph

76 commits

Author SHA1 Message Date
Umherirrender 0b870852d8 Use namespaced classes
Changes to the use statements done automatically via script
Addition of missing use statement done manually

Change-Id: Idc09b4f3e1ffa40f234b34cd8c0c515bd58ce67b
2024-06-09 11:27:57 +02:00
Wandji69 de999997e7 Use ObjectCacheFactory instead of the deprecated ObjectCache methods
Bug: T363770
Change-Id: I6c7967ef092513b1a57bacc4d68b9ee3a50822e8
2024-05-27 12:48:04 +00:00
Timo Tijhof 9c5b7cef9e Improve localisation on Special:Gadgets, minor code cleanups
For each item, either display human-readable and translated text,
or display a technical non-translatable identifier as `<code>`,
with optional localised text in the title attribute.

* Re-format "rights" as a sentence instead of a bullet list. It was
  the only one using a bullet list, which made it feel a bit long.

* Re-format "actions" as `<code>` since they are not localised.

* Re-format "contentModels" as `<code>`, and add the localised display
  name in a title attribute, same as we do with "rights" already.
  ContentHandler::getLocalizedName() is also used already on
  Special:ChangeContentModel.

* Fix "contentModels" to set `needLineBreakAfter = true`, otherwise
  if a gadget also sets "supportsUrlLoad", then that sentence is
  appended to the previous line.

Update phrasing and sorting to be consistent everywhere, and adopt
native PHP types where possible.

In most cases, I made things alphabetical, with the exception of
Special:Gadgets user interface output, and Gadget class methods,
which both follow the order of most recently added feature last
(rights, skins, actions, namespaces, contentmodels, categories).

Highlights:

* Fix namespace IDs type. These can be strings when they are parsed
  from the gadget definition text, not always integers.

* Add explicit default for 'category'. In theory not needed because
  MediaWikiGadgetsDefinitionRepo has a `$section = '';` default, and
  MediaWikiGadgetsJsonRepo uses GadgetDefinitionContentHandler where
  `category: ''` is part of both the initial page content, as well
  as merged via getDefaultMetadata.

  This default benefits simpler test cases, and static analysis,
  since the Gadget class constructor does not (yet) require it.
  Without this, getCategory() could TypeError due to returning null.

Bug: T63007
Change-Id: I3b2c72a6424d520431d03761d23a5a222518ce3d
2024-03-25 15:37:40 -07:00
jenkins-bot d6e1510af0 Merge "Add support for categories in definitions" 2024-03-16 22:54:42 +00:00
Sophivorus e2cb01ddcd Add support for categories in definitions
Adds a "categories" option to the gadget definition, which takes a
list of category names. Enabled gadgets will only load when the
viewed page is in one of the required categories.

Bug: T204201
Bug: T63007
Change-Id: I0ced9507bdab6cacd0baf2a331859099f35b73e5
2024-03-16 22:31:03 +00:00
Siddharth VP 9d05af9df8 Replace uses of deprecated wfGetDB()
Bug: T357642
Change-Id: I868133bca622ad2af851e874b450fe0be53017d2
2024-03-04 18:39:15 +05:30
Siddharth VP a629d7f71d Introduce MultiGadgetRepo to facilitate repo migration
MultiGadgetRepo is a wrapper around two or more GadgetRepos so that
they can be used at the same, facilitating migration from one repo to
another.

If a gadget appears in both repos, the definition in the first repo
takes precedence, and a warning is shown on Special:Gadgets.

This can be enabled to wrap GadgetDefinitionNamespaceRepo and
MediaWikiGadgetsDefinitionRepo by setting $wgGadgetsRepo to
'json+definition'. In this configuration, once a new JSON definition
exists for the same name, it is used instead of the old one, and the
old one can then safely be removed at a later time in the safe
knowledge that it is no longer used.

Adapted from If3cc5e22e9812d0fd1a9e8e269ea74a7f667dadd

Bug: T140323
Co-authored-by: Kunal Mehta <legoktm@debian.org>
Change-Id: Ibad53629e63ec8713d7a3b84a19838b94600588e
2024-03-02 18:22:04 +00:00
Timo Tijhof 9b6d34ddf2 tests: Widen @covers annotations
https://gerrit.wikimedia.org/r/q/owner:Krinkle+is:merged+message:%2522Widen%2522

Change-Id: I1d2a75a7f64500b81f95d895ce065e80e336fba7
2024-02-13 16:11:35 +00:00
Timo Tijhof fce6fdfb20 Goodbye Gadget/Gadget_definition namespaces!
== What ==

* Remove the empty Gadget and Gadget_definition namespaces.
* Remove the "gadgets-definition-edit" user right.
* Remove need for custom namespace permissions that previously
  had to extend editsitejs to apply to NS_GADGET.

== Why ==

Simplify the (unused) "GadgetDefinitionNamespaceRepo" backend for
Gadgets 2.0 by making it less radically different from the status quo.

The experimental 2.0 branch will now make use of the "gadget definition"
content model via "MediaWiki:Gadgets/<id>.json" pages, instead of
through a dedicated namespace.

When I first worked the Gadgets 2.0 branch, content models
were not a thing in MediaWiki, and interface-admin wasn't a thing yet
either. Now that we have per-page permissions and per-page content
models, we don't really need a separate namespace.

This follows the principle of least surprise, and fits well with other
interface admin and site configuration tools such as:
- Citoid, MediaWiki:Citoid-template-type-map.json,
- VisualEditor, MediaWiki:Visualeditor-template-tools-definition.json,
- AbuseFilter, MediaWiki:BlockedExternalDomains.json,
- the upcoming "Community Config" initiative.

If/when we develop the SpecialPage GUI for editing gadget definitions,
this can save its data to these pages the same as it would in
any other namespace. Similar to how Special:BlockedExternalDomains
operates on MediaWiki:BlockedExternalDomains.json.

See also bf1d6b3e93 (I6ffd5e9467), which recently removed the
gadgets-edit user right in favour of the editsite{css,js,json} rights.

Change-Id: I5b04ab251552e839087d0a8a6923d205adc7f771
2024-02-13 13:30:26 +00:00
James D. Forrester 27f913cc96 Drop RL targets definitions from module class, and validation / error code
Bug: T328497
Change-Id: If6d742ea719e4c8db9d915e8297d915d2150ef50
2024-01-24 12:11:20 -05:00
Timo Tijhof 1448500c2a Avoid global RequestContext in GadgetRep::validationWarnings()
This already returns safe Message objects, which allow Special:Gadgets
to localise it accordingly.

Avoid formatting the list ahead of time with a hardcoded user language,
and instead use Message::listParam() so that the caller controls
the Language object. This makes it safe to use in jobs, CLI, tests,
load.php and elsewhere instead of previously where RequestContext would
cause an exception or use something the caller doesn't control.

Bug: T298334
Change-Id: I77dd3c7fef86b4c8c41837bfb276b019f30baeb0
2024-01-09 21:05:18 +00:00
jenkins-bot cc4efec689 Merge "Avoid persisting default gadget configs in definitions" 2024-01-09 17:27:16 +00:00
jenkins-bot 791a330bca Merge "tests: Add test case to assert ES6 is valid by default" 2024-01-08 15:07:47 +00:00
jenkins-bot 08de291eef Merge "tests: Avoid matching source in validateScriptFile error message" 2024-01-08 15:06:54 +00:00
Timo Tijhof 9828552389 tests: Avoid matching source in validateScriptFile error message
Follows-up 3e871f2015 (If45856c563), which was meant to make
change Ie309e761f8b20640f7c0 in MediaWiki core pass, but didn't, and
I failed to realize this before the change landed.

The current error message is quite generic, and does not mention
"quux". The new validator (Peast) gives a better error message, and
thus would contain the word quux:

> Parse error: Unexpected: quux on line 1

My previous change was loosely checking this as a proxy for whether
the source code is included (valid) or stripped (invalid), but this
was a poor proxy. I did that because I assumed, incorrectly, that
this code would become minified. Since that's not true, we can simply
assert the code in its entirety for the valid case, and for the
invalid case only check for the presence of a warning, and leave it to
core to be responsible for omitting the invalid code in that case.

Change-Id: If3b2b4a75013baeaa0d9b92cd10dfb06e5534153
2024-01-06 22:22:25 +00:00
Umherirrender 1afd0a6e02 Use namespaced classes
This requires 1.42 for some new names

Changes to the use statements done automatically via script
Addition of missing use statement done manually

Change-Id: Iacbea33299995c537a7ef77b524614ad02c6a559
2024-01-06 17:45:07 +01:00
jenkins-bot 95e524ffe7 Merge "Fix parsing logic when comments or hidden characters are present" 2024-01-06 16:22:33 +00:00
Siddharth VP 18204a4b0d Fix parsing logic when comments or hidden characters are present
0c61a5a468 subtly affected the
gadget definition parsing, causing issues with definitions containing
HTML comments or certain kinds of whitespace characters. They would get
saved to the pages array with a trailing whitespace but
Gadget::getScripts() would only return pages strictly ending in `.js`.

Trimming the title before in MediaWikiGadgetsDefinitionRepo fixes the
issue.

Bug: T354385
Change-Id: Iacee432756006060217981a534434fd455285793
2024-01-06 11:09:29 +05:30
Timo Tijhof a516fa48c3 tests: Add test case to assert ES6 is valid by default
Bug: T75714
Depends-On: Ie309e761f8b20640f7c0e85def0a3d1ccc8a658e
Change-Id: I1b3518ae4599bae93e36aa2bd1c216fc7cde1d90
2024-01-05 21:51:33 +00:00
Timo Tijhof 3e871f2015 tests: Improve validateScriptFile coverage and tolerate valid ES6
Remove the test that expects ES6 to fail by default, instead assert
that invalid syntax fails (as it should). Also add various other
test cases, including a commented-out test for valid ES6 by default,
to be enabled after Ie309e761f8b20640f7c0 in MediaWiki core.

Change-Id: If45856c563518255189687e3b1f620973349fb27
2024-01-05 21:48:58 +00:00
Siddharth VP 05cc93b9be Avoid persisting default gadget configs in definitions
Bug: T331222
Change-Id: I022a66267fd01efdfdc86cf0b21d3a0aaf7e5950
2023-12-16 14:31:09 +05:30
Siddharth VP 0c61a5a468 Specify all source pages in a single array in JSON definitions
As this repo is not used anywhere in production, there is no backward
compatibility. Any scripts, styles, datas specified in existing
definition pages will get ignored.

Gadget::getJSON() no longer checks isPackaged() – that is now left
for callers - only relevant one is in GadgetRLModule. That was done
because validationWarnings() method needs the JSON page list even for
non-package gadgets.

Bug: T353174
Change-Id: Ic820bc4632981369c72f822fb8b9b7195930eaa6
2023-12-16 14:31:09 +05:30
Timo Tijhof a631066d62 tests: Switch test from newEmpty to new HashBagOStuff
Follows-up I24af0c2fa75. I forgot that newEmpty uses the "always empty"
EmptyBag, as opposed to an "initially empty" HashBag.

Change-Id: I62075bf685223b31c161508f99f95ee46280450b
2023-12-13 03:08:38 +00:00
Siddharth VP c665a90423 Add tests for cache invalidations in MWGadgetDefinitionRepo
Change-Id: I24af0c2fa7524b5d6b5cd2cb151d3617c99a6ab0
2023-12-12 07:35:47 +00:00
Siddharth VP ce486be160 Further expand test coverage
* Separate out tests for MediaWikiGadgetsDefinitionRepo and
Hooks::onGetPreferences. Removed hack in fetchStructuredList() for
overriding page content for tests.
* Add tests for ES6 gadgets. For this, ResourceLoaderModuleTest is
converted back to an integration test.
* Removed createGadgetDefinitionPage() test helper method. Now using
MediaWikiIntegrationTestCase::editPage().
* Fixed GDNamespaceRepoTest failure when GDNamespaceRepo is the active
repo, by clearing WAN process cache.

Change-Id: I26b84576a91f6cb0ebae64c5fc1408666d767911
2023-12-09 09:29:15 +05:30
Siddharth VP d12f0f366a Expand test coverage
* Add tests for onBeforePageDisplay hook. A simple gadget repo
  implementation, StaticGadgetRepo, is introduced for this.
* Add integrations tests for various gadget load conditions.
* Add test for onUserGetDefaultOptions hook.
* Add tests for GadgetDefinitionNamespaceRepo.
* Convert ResourceLoaderModuleTest to a unit test.

Change-Id: I275380c2bfcaa44770b3946a0a468eaaabef70c0
2023-12-06 09:18:09 +05:30
Siddharth VP 9d11216d4b Inject services into Hooks
Change-Id: I042d6f33f9ecd2573f25e737bb0ee9a43869eeac
2023-12-06 09:18:09 +05:30
Siddharth VP 22752225a1 Make GadgetRepo a service
Turn GadgetRepo into a service, deprecate GadgetRepo::singleton()

Change-Id: I0ab14ca936eec1eead91d4eb0970f898628c8388
2023-12-05 17:38:41 -06:00
Siddharth VP adda8a6649 Show gadget definition validation warnings in Special:Gadgets
Bug: T298334
Change-Id: Id241125b9b8abfb971dfa600ab0df9c6047f2b32
2023-11-30 21:59:04 +00:00
Siddharth VP 72cbb64069 Add support for content models in definitions
Adds a contentModels option to gadget definitions to restrict gadgets to
run in pages of the given content models only.

Use-cases include gadgets for tabular data[1] (for Tabular
.JsonConfig model), for editing map data[2] (for Map.JsonConfig model),
and for showing links in code pages[3] (restricted to javascript, css,
json, and scribunto models), as well as common gadgets like ProveIt[4]
which can be restricted to wikitext model.

[1]: https://commons.wikimedia.org/wiki/MediaWiki:Gadget-TabularImportExport.js
[2]: https://fr.wikipedia.org/wiki/MediaWiki:Gadget-KartoEditor.js
[3]: https://en.wiktionary.org/wiki/MediaWiki:Gadget-CodeLinks.js
[4]: https://commons.wikimedia.org/wiki/Help:Gadget-ProveIt

Bug: T204201
Bug: T63007
Change-Id: I7dcfc21d674ead8d710e7f77d13bf18bcad15079
2023-09-21 22:27:22 +05:30
brandonxlf ec69cea0e3 Add support for namespaces in definitions
Adds a "namespaces" option to the gadget definition, which takes a
list of namespace IDs. Enabled gadgets will only load when the
viewed page is in one of the required namespaces.

Bug: T204201
Bug: T63007
Change-Id: I7f797e35352b242ad78704074e98c6569a1adf91
2023-09-20 14:50:33 -07:00
Timo Tijhof 5aaa843a28 tests: Enforce parity between Gadget::serializeDefinition and toArray
Follows-up 087ab65e24 (Ieae6706537). To avoid silent mistakes such
as during development of T63007 (I7f797e35352b242).

Bug: T303194
Change-Id: If675f8d02ed3fee768af3d2b4912249319ae9ef4
2023-09-20 14:41:33 -07:00
jsn 929bfc6404 Preserve Gadget prefs when they can't be enabled
- Always define gadget enable/disable preferences
- Show a gadget's preference when it can be enabled

Bug: T341421
Change-Id: If32eecebccf2c9b557d7dde273b7faf7118a7ac3
2023-09-06 19:32:31 -05:00
Daimona Eaytoy b6e76eeb3a Add GadgetHooksTest to the Database group
The test uses black magic to override the behaviour of
MediaWikiGadgetsDefinitionRepo. However, this magic doesn't work well
with I2ac659a4, which disabled gadget loading in non-database tests.
This test is the only one that needs that code to be run, although it
won't actually make any queries because of said black magic.

Add the test to the Database group as a temporary (TM) workaround to
replacing the TestingAccessWrapper magic with something else.

Change-Id: I7d9fd612dd685a5728d3a7a9ced97fd5ac45f491
2023-08-06 22:55:00 +02:00
Daimona Eaytoy b1b7bfefa3 Avoid using TestUser in non-database test
TestUser needs a database connection to create the User, but this test
doesn't need a real user, so just use a mock instead.

Ideally we'd use Authority, but the hook still takes a User object.

Change-Id: If6cb7e3756036fc458a4308b06499c4bc0d3d9b0
2023-07-17 23:12:13 +02:00
Jon Robson 8af75aa6e5 Gadget maintains its own version of targets system
The ResourceLoader module associated with the gadget will have
mobile and desktop targets, but this allows gadgets to disable
themselves on the mobile site if necessary by not adding the gadget
to the page if it's been marked as such.

This also has the benefit of not showing the gadget on the preferences
page if it's not relevant.

Bug: T328610
Change-Id: I4f2b57d1d22f641ff7520358a46cd0e6b2103aa9
2023-06-29 16:20:29 -07:00
Siddharth VP 087ab65e24 Avoid caching serialized PHP object
Cache array representation of the Gadget object instead of the
php-serialized representation. Gadget::toArray() is an inverse of the
constructor which already constructs an object from an array of config
values.

Also, the static Gadget::newFromDefinitionContent method which accesses
the service container is replaced with the dependency-less
serializeDefinition() method.

Bug: T303194
Change-Id: Ieae6706537143d766777b2299c31726e2a1dfd29
2023-06-22 06:01:28 +00:00
Jon Robson 58463ce97c Gadgets should default to mobile and desktop targets
The targets system is being deprecated.

Bug: T127268
Bug: T340046
Change-Id: I69d11d52238ac852534cb9a4555208a0b0bb5fd7
2023-06-21 15:36:39 +00:00
Siddharth VP 7793a9475f Add support for ES6 gadgets, but with validation disabled
Gadgets can mark themselves as ES6-only by specifying the requiresES6
boolean attribute. Syntax validation is disabled for them (as the
validator doesn't support ES6 yet), and they are loaded together in a
separate request.

The minifier doesn't reject syntax errors, and thus these would
be passed through to web clients. Hence, communities using this feature
are encouraged to use ESLint or another linter to make sure only
valid ES <= 6 code is being used.

Because of the above, this feature is only made available for
non-default gadgets.

Bug: T75714
Change-Id: Ib98ac0700471554d5721d7ab858d4660e1e0e980
2022-10-20 22:03:18 +00:00
Fomafix b504a6f496 Use HookHandlers and implement hook interfaces
Bug: T271014
Change-Id: I50d42df04038d1214fd9835c3d3b0f602037ac62
2022-04-17 12:00:24 +00:00
Timo Tijhof db04e488b6 Rewrite and simplify MediaWikiGadgetsDefinitionRepo cache
* Remove most APCu logic, including the reverse checking of WANCache
  touch time to validate the APCu key
  The APCu expiry had a 7-15s blind TTL before this check took place.
  Keep the conservative part of this (blind TTL). This should be
  more than enough to debounce memcached traffic.

* Simplify WAN handling with plain key and delete (using the default
  holdoff tombstone, which should take care of edge cases like second
  DC warming up the WAN key shortly after a delete).

* Fix incorrect method doc about being by section. The returned
  list is not by section.

Change-Id: If14ff83a29367df58d1824615bcf0bcd2edc886f
2022-04-05 21:39:45 +01:00
Timo Tijhof 6f452d11b9 tests: Use setGroupPermissions() to fix flaky test
Locally these tests are failing for me on master every time.
In WMF CI, it seems they are passing right now, although they
too would start failing as of Ib27fd34fbfe7a7. My guess is that
my change there causes the service container to be initialised
earlier and thus causes the modified global to no longer be seen.

At the very least this should have been using setMwGlobals(), which
would have also avoided the issue by ensuring a service reset,
however use setGroupPermissions() since it's tailor made for this
purpose, and avoids potential problems later with default groups
not existing.

Change-Id: I8f02ffc996d6077ca6e7433229b1ea5d8fc6ce7c
2022-04-05 21:07:28 +01:00
Reedy 998f20994c GadgetHooksTest: Use namespaced GadgetRepo
Change-Id: I6a1ba1241bd0ada4bc024c6a3f48200d9c6ae6d9
2022-03-06 02:07:17 +00:00
Reedy 01f6a6c34e Namespace extension
Bug: T278278
Change-Id: I49b57e68079c998419e235b1727eea8c121c234b
2022-03-05 17:03:35 +00:00
Ammarpad 5d3a547c8b Restrict ?withgadget query parameter to opt-in gadgets.
?withgadget query parameters allows for ad-hoc loading of gadgets
(after passing all other basic checks). This was recently added in
I5b30d4e.

In T29766#7611796 Gergo raised concerns about how this can be
potentially abused.

This patch aims to restrict the feature by giving gadgets latitude
to either use it or not depending on the nature of the gadget.

The patch does so by adding `supportsUrlLoad` option that gadgets
(maybe those deemed safe) can use it to opt-in to the parameter.
By default gadgets don't support it, so it can be enabled for each
on a case-by-case basis.

Bug: T29766
Change-Id: Ie64174085e650579d76cc862774a4fe1b3d08396
2022-01-24 00:31:19 +00:00
Siddharth VP 7ead5dab61 Allow specifying page actions in definitions
Allow specifying page actions ('view', 'edit', 'history', etc) in
gadget definitions. If specified, the gadget is run only on the given
page action(s).

This is especially useful for default gadgets like RefToolbar[1] and
TextReactions[2] that only need to be loaded while editing.

[1]: https://en.wikipedia.org/wiki/WP:RefToolbar
[2]: https://en.wikipedia.org/wiki/WP:Text_reactions

Bug: T204201
Bug: T63007
Change-Id: Idde71b3f1f6c36cd21539a2312be8f12217a9acc
2022-01-01 00:41:08 +05:30
Siddharth VP 1f7b9d90df Support JSON files in gadgets
The parsed content of JSON files in the gadget is made available from the
gadget's JS files via require(). That is, MediaWiki:Gadget-data.json (or
Gadget:data.json) is available as `require('./data.json')`. This is
supported for both MediaWikiGadgetsDefinitionRepo and
GadgetDefinitionNamespaceRepo. The JSON parsing is done server-side.

JSON can only be used in "package" gadgets - in which the JS files can
also be invoked via require().

Also added a test for GadgetResourceLoaderModule.

Bug: T198758
Depends-On: Ib4556d09c4d393269e32771aab00f59a5f630e1b
Depends-On: Id4589a597ccfc4266b3e63d10f75b146aa7a287a
Change-Id: I21acb46cdd244a39b5cc6963aa763f0113bd1e38
2021-12-27 17:33:21 +00:00
Bartosz Dziewoński ea786ce9dc Re-apply "Make each gadget a separate preference, instead of one huge multiselect"
Previously attempted in 82281d82d0,
reverted in 7c4ac597e2.

This gives each section and each gadget's entry an `id` attribute,
which can be used for linking, as requested in T126962. Existing user
options still match the new preferences.

It also probably makes future improvements easier. No one understands
multiselects with subsections.

Bug: T126962
Change-Id: Ie96fd94c994d05ab8507920fa560c7ed9c1f9b69
2021-11-05 23:15:45 +01:00
Krinkle 7c4ac597e2 Revert "Make each gadget a separate preference, instead of one huge multiselect"
This reverts commit 82281d82d0.

Bug: T126962
Bug: T292777
Change-Id: I7c858b8c4bc12626ea1d53374ea4d75862f684ff
2021-10-07 19:52:18 +00:00
Bartosz Dziewoński 82281d82d0 Make each gadget a separate preference, instead of one huge multiselect
This gives each section and each gadget's entry an `id` attribute,
which can be used for linking, as requested in T126962. Existing user
options still match the new preferences.

It also probably makes future improvements easier. No one understands
multiselects with subsections.

Bug: T126962
Change-Id: Ifaca96e288c475017636c2408712d6a20aa77da9
2021-09-30 18:39:41 +02:00