Commit graph

197 commits

Author SHA1 Message Date
jenkins-bot 47c360c47a Merge "Expand pre-save validations for JSON definition pages" 2024-01-09 06:07:27 +00:00
Siddharth VP 41af49e9b5 Expand pre-save validations for JSON definition pages
* Validate that pages have .js, .css or .json suffixes.
* Validate that namespaces are integers, disallowing doubles.
* Validate that type is either general or styles.
* Validate that targets are either desktop or mobile.
* Fix validation of skins: stop allowing `true` as it isn't actually
allowed - it causes an exception in Gadget::isSkinSupported().

Bug: T298334
Change-Id: Ic59c20172f496d281acd59419f5c760ad29ce317
2024-01-08 20:04:12 +00:00
Umherirrender dc84f149bc SpecialGadgetUsage: Fix query for postgres (and improve for mariadb)
The column user_properties.up_value is a text based type in all rdbms.
postgres does not autoconvert strings to numbers, so SUM( up_value )
gives an error when running on postgres.
The use of SUM( up_value ) is also problematic on mariadb/sqlite, as
up_value can be set to any number via the Action API's options module.
This value goes into the sum and produce a wrong result. Using a
non-number value allows to remove the value from the sum and that also
produce a wrong result.

Change the query to exclude the rows which disables a gadget and just
count the "active" ones. Typically there are only rows to disable a
gadget, when the gadget is marked as default, but default gadget are
displayed with the message 'gadgetusage-default' and does not needs to
be counted. In case a gadget was default in the past, there could always
such rows, so be safe and exclude them. That means it is safe to only
count for "active" gadget option usage.
This also allows to remove the up_value condition on the join to
querycachetwo as only rows to enable a gadget are now selected and get
joined to find the active users.

For the join condition use an assoc array, as for database condition the
database class is quoting the value of that array, if that is a string.
In postgres the " is used to quote columns or tables, but not values,
that gives a query error on postgres about the condition.
fixed when using the array style and not "raw" sql.

Define the condition only once as it should be the same for both
queries. Use the new IDatabase::expr function to write that sql piece.

Change-Id: I8dfc3fd5adc4c4bdabceaab20c4b37ffd48e6bee
2024-01-08 18:59:45 +01: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 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
jenkins-bot be19375262 Merge "Specify all source pages in a single array in JSON definitions" 2023-12-29 11:08:02 +00:00
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
Novem Linguae 868c3c10d5 Special:GadgetUsage "Gadget" column now wikilinks to Special:Gadgets
A little efficiency to make getting from Special:GadgetUsage
to the relevant gadget files easier.

Bug: T344255
Change-Id: I0e26d10992bc3f8073bcdc8dbac86726ee6f7c36
2023-12-15 05:29:06 -08:00
jenkins-bot a3a1ea918e Merge "Remove redundant data updates for GadgetDefinitionNamespaceRepo" 2023-12-14 02:23:23 +00:00
Siddharth VP 8695a4ee1a Remove redundant data updates for GadgetDefinitionNamespaceRepo
Remove DefinitionDeletionUpdate and DefinitionSecondaryDataUpdate –
both call $repo->handlePageUpdate() which is already being called from
onPageSaveComplete/onPageDeleteComplete hooks.

The chain that made this redundant:

1. Ibe2e26d123, change hooks from hardcoded to GadgetRepo singleton.
2. Ieccc1cae8c, migrate from create-only onPageContentInsertComplete
   hook to create-and-edit onPageSaveComplete hook.
3. Ib27fd34fbf, further consolidate handling of create and edit
   events with handlePageUpdate, and remove any hook-level
   conditionals. At this point, the DateUpdate classes became redundant.

Bug: T31272
Change-Id: I20c2759b219c80571237a73e8422f3128047eb87
2023-12-14 02:03:44 +00:00
Siddharth VP 34a1a297c2 Make repo configuration independent of ObjectFactory specs
Replace $wgGadgetsRepoClass with $wgGadgetsRepo.

Change-Id: I3dc1e29d1c3c65a18c206e7aa50e5bf31a55cc21
2023-12-12 13:15:31 +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
jenkins-bot 9ad0cf1a5a Merge "Make GadgetRepo a service" 2023-12-06 00:48:51 +00:00
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 bf1d6b3e93 Remove gadgets-edit right, use core editsite{css,js,json} rights instead
With GadgetDefinitionNamespaceRepo, there should be separate rights for
editing gadget json as opposed to editing gadget css/js, so that admins
can be granted the former, while the latter remains restricted to
int-admins.

Reusing the MW core rights for site CSS/JS/JSON seems to make the most
sense here. Protection is now applied via GetUserPermissionsErrors hook
rather than as namespace protection.

For editing any non-CSS/JS/JSON pages in the namespace, editsitejs right
is required.

Bug: T298834
Change-Id: I6ffd5e9467774f1e79ccdce8b6b4739f07be2da8
2023-12-05 20:37:59 +05:30
Siddharth VP adda8a6649 Show gadget definition validation warnings in Special:Gadgets
Bug: T298334
Change-Id: Id241125b9b8abfb971dfa600ab0df9c6047f2b32
2023-11-30 21:59:04 +00:00
thiemowmde c4680366ac Various PHP code cleanups
… e.g. using modern syntax, or avoiding unnecessarily complex
language features. For example, we don't need to call count() when
all we care about is the boolean "is empty/not empty" information.

Change-Id: I13ae802f64627a79b29d1e57ad71486cb2fb977f
2023-11-22 09:53:48 +01:00
thiemowmde ba496f7f4a Remove meaningless trim() from MediaWikiGadgetsDefinitionRepo
This can't do anything for two reasons:
* There are no spaces in the string after the str_replace. There is
  nothing to trim.
* The regular expression above guarantees the string starts and ends
  with alphanumeric characters. There can't be anything to trim, not
  even underscores.

Change-Id: Iea40aafaa2457cdee9d05f105978627d38d9d5b8
2023-11-20 13:44:08 +01:00
Timo Tijhof d12bf68c25 docs: Make PHP file headers more consistent
* A fixed license header for the file.

* Move class descriptions, doc tags, author/copyright to the
  class block. Remove MW-core specific "ingroup" tags such as
  "SpecialPage" which has no meaning in an extension and are not
  used by Doxygen.
  Ref https://gerrit.wikimedia.org/r/q/message:ingroup

Change-Id: I13d9b0ff7a3150180196c9fa58f8a321b14edd22
2023-10-30 18:10:58 -04:00
Siddharth VP a7309d2b1f Namespace special page classes
Change-Id: I005f6674c9cc80c5c9e26ae84acfbceebb43734e
2023-10-30 21:13:41 +05:30
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
Dringsim e292854740 Hide gadgets on Special:Preferences when forcesafemode=true
See https://gerrit.wikimedia.org/r/c/mediawiki/core/+/940156/comments/bdd88839_f9bdd28a

Bug: T342347
Depends-On: I6f2d024c01cdb1d7305ff024996175eb2656d0a8
Change-Id: Ia72bb33a3fd559cd5a30a44f736b0d3040843323
2023-09-08 17:15:15 +00: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
gerritbot b9da596fe8 Replace some moved Title class uses, now MediaWiki\Title\Title
Bug: T321681
Change-Id: I9b9c59a564e81daf01bb99d48dc81c9357e94acc
2023-08-19 04:15:51 +00:00
Umherirrender 7f95440c4d Use HookHandlers for CodeEditor hook
This requires 1.41 for the interface (added in 4d6a470a)

Bug: T271014
Change-Id: Ic486f44bd9215ac7dad5bc70d7dca1e9aad470de
2023-08-16 05:30:40 +00:00
Umherirrender c800299653 i18n: Split apihelp for parameter list=gadgets/gadgetcategories prop=
Easier to translate
There is no visible change on Special:ApiHelp/query+gadgets

Also split for list=gadgetcategories&gcprop=
Visible on Special:ApiHelp/query+gadgetcategories

Bug: T285545
Change-Id: Ic40326cd747ffe2153cff7d10e0083bd5e51345e
2023-08-05 01:47:04 +02:00
Daimona Eaytoy 76522d5c66 Avoid loading gadgets if the storage backend is disabled in tests
This method is possibly the main cause of DB access in non-database
tests, because it's called from the UserGetDefaultOptions hook handler,
which is triggered by most tests using UserOptionsLookup in any way.

Trying to obtain a DB connection will throw an exception in I96ecf9ff,
so just skip the lookup altogether.

Bug: T155147
Change-Id: I2ac659a472655b2a95e64dcbd3f709e465839b33
2023-07-29 14:42:56 +02:00
Ed Sanders d09c3fac78 Use strict equality with in_array
Change-Id: I09ebdf0dae540195d5bfcebf32b1e6dd22ff11c5
2023-07-24 18:41:00 +01:00
Ed Sanders 9b6cfa97a0 SpecialGadgets: Remove incorrect comment and test for $requiredSkins being true
$requiredSkins is always an array

Change-Id: I91bade025a9581c33b99f986e9497299483b8b59
2023-07-24 13:36:59 +01:00
Siddharth VP 6d1ffbac15 Fix field name in schema.json
Change-Id: I7a89afe2876a1530cceb4884c8fdce348f8d6620
2023-07-08 21:13:26 +05:30
Ed Sanders f5466fca49 Use strict equality
Change-Id: Iccb15b405a6959238ba4f924ca4ec907e29a9ebf
2023-07-01 14:27:10 +01: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
Daimona Eaytoy 2912cd3477 Remove mention to deprecated MWException
The class is deprecated and RL will likely stop using it. And even then, this method shouldn't care. The exception is unchecked, so whatever its type is, it's just an implementation detail.

Bug: T328220
Change-Id: I3c87a376ce1d36f4a1d81a435bbc015199c75913
2023-06-07 15:58:45 +00:00
lens0021 c93bce31be
Add missing targets option
Bug: T303681
Change-Id: Id0d5f4e8fc1477e3322c0bc24b17b83ff9d0c6b3
2023-03-06 16:46:37 +09:00
jenkins-bot f1fbefffb4 Merge "Add PreferencesGetIconHook" 2022-10-25 21:31:31 +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
suecarmol 12eada5338 Add PreferencesGetIconHook
Added PreferencesGetIconHook to add an icon on the Special:Preferences section.

Bug: T317419
Depends-On: I6d5730d47e7595b1705787995fe5db2fe734d7f5
Change-Id: Ie2505622680573dfe9e50b48976c986cf2512097
2022-10-14 19:02:29 -05:00
libraryupgrader eb99f12bce build: Updating mediawiki/mediawiki-phan-config to 0.12.0
Change-Id: Ic857959b35f31be8a963e1889b6d79b54dbd3855
2022-10-09 12:06:20 +00:00
Gergő Tisza 1083062529
Purge in-process cache in MWGadgetsDefinitionRepo::handlePageUpdate
This is needed for registering gadgets for parser tests.

Change-Id: If7bf0cf63cc352566a58c20c4cf49e57c0085bb9
2022-09-01 21:41:04 -07:00
Siddharth VP 08cf009f36 Migrate usage of Database::selectFieldValues to SelectQueryBuilder
Bug: T312440
Change-Id: I4a54426667cd4c6070b0ff7953e6478dc25b96c8
2022-08-27 17:17:27 +05:30
Fomafix dda2f5a129 Remove default values for the constructor of SpecialPage
Change-Id: Ibdc2d3906f3020798cd885d6fba7e5149d1b5cdf
2022-08-08 15:44:05 +00:00
Reedy 67b8bf23d0 SpecialGadgets: Don't call explode() on null
Bug: T311557
Change-Id: I05455a309bee4a4f59dc5351484dfffc682a329a
2022-06-29 00:51:24 +00:00