Commit graph

391 commits

Author SHA1 Message Date
Máté Szabó
c4e9f987f1 rest: Return a 400 for invalid render IDs
Why:

- The REST API takes an optional renderid param when converting HTML
  back to source wikitext, which is user-provided and may be invalid.
- Invalid render IDs cause an InvalidArgumentException to be thrown that
  causes a 500 response.

What:

- Introduce a new error message for invalid render IDs in the REST API.
- Return a 400 with this new error message for HTML reverse-parses with
  an invalid render ID.

Bug: T385568
Change-Id: I062419fe8952329a39781a49cdca2e94c3996447
(cherry picked from commit cd1d42a5066e4bcb9b9d4ed9b4f7714fd428fea3)
2025-02-04 14:54:50 +00:00
jenkins-bot
b60d69a6ac Merge "REST: Add items wrapper for restbase compat to some response bodies" 2024-10-21 18:40:15 +00:00
Umherirrender
839613ff77 tests: Use namespaced classes
Changes to the use statements and some additions
are done automatically via script
This also updates @covers tag for the namespaced classes

Change-Id: I859ba6d05018c99710b744e2becab432410d3bca
2024-10-21 18:53:02 +02:00
Bartosz Dziewoński
9d56257d8c Make Message and MessageValue compatible
Fix two problems that made it difficult to convert between Message
and MessageValue, or to write code that could accept both of them,
as exemplified by the StatusValue class:

* Implement a common interface
* Use the same internal format for message parameters

While these changes should be compatible with most of existing code,
where the authors were courteous enough to simply call methods such
as Message::numParam() and not look inside the values they return,
it is potentially a breaking change for anything that depended on
the formatted params being arrays or accessed their keys.
Example patches: https://gerrit.wikimedia.org/r/q/topic:message-param

Notable changes:

* Message and MessageValue now both implement MessageSpecifier
  (only Message implemented it before).

* Message::numParam() and other static methods for encoding params
  now return MessageParam objects, instead of special arrays.
  Use these MessageParam objects internally in Message.

* Narrow down the return type of MessageSpecifier::getParams() (it
  was just `array`, allowing any type in the array). Narrow down the
  types for Message::params() and MessageValue::params() to match.

* Deprecate MediaWiki\Message\Converter. As a replacement add
  MessageValue::newFromSpecifier(), which is analogous to
  Message::newFromSpecifier(), but without weird legacy edge cases.

* Make StatusValue::getMessages() return MessageValues. Remove code
  that converted between Message and MessageValue, no longer needed.

* Update many type declarations and comments to use MessageSpecifier
  instead of MessageValue, as well as a couple of tests that depended
  on implementation details.

Bug: T358779
Change-Id: I625a48a6ecd3fad5c2ed76b23343a0fef91e1b83
2024-10-19 15:00:07 +02:00
jenkins-bot
53bd99f5dc Merge "Use statslib for metrics emitted by HtmlInputTransformHelper, HtmlToContentTransform" 2024-10-17 23:46:56 +00:00
Yiannis Giannelos
331c181598 Use statslib for metrics emitted by HtmlInputTransformHelper, HtmlToContentTransform
Bug: T359475
Change-Id: I7d4ca748c106dfd560dae31294decfb2b181e2db
2024-10-17 21:28:04 +02:00
jenkins-bot
1d0377f1ba Merge "Adding token validation to the edit handler" 2024-10-17 14:51:30 +00:00
Umherirrender
e662614f95 Use explicit nullable type on parameter arguments
Implicitly marking parameter $... as nullable is deprecated in php8.4,
the explicit nullable type must be used instead

Created with autofix from Ide15839e98a6229c22584d1c1c88c690982e1d7a

Break one long line in SpecialPage.php

Bug: T376276
Change-Id: I807257b2ba1ab2744ab74d9572c9c3d3ac2a968e
2024-10-16 20:58:33 +02:00
bpirkle
330e1633c2 REST: Add items wrapper for restbase compat to some response bodies
Bug: T374136
Change-Id: I995916f53eb9408923456cce8b1f3be391fbf942
2024-10-16 12:58:10 -05:00
James D. Forrester
a5387c7c20 Namespace all remaining classes in includes/parser
Bug: T353458
Change-Id: If02cc9b1ff78e26c1cf8c91ee4695845eb133829
2024-10-15 23:54:32 +01:00
Umherirrender
97e55f4795 tests: Remove global state from Rest Handler tests
Functions provided by the HandlerTestTrait and used in other rest tests

Reduce session leaking from global state within the rest test

Bug: T376970
Change-Id: Ie0dc52441c6a6ca408812c5d0cc6d791834780e5
2024-10-11 23:18:22 +02:00
C. Scott Ananian
8a650d5d48 ParsoidParser: ensure magic variable expansion uses pageLanguageOverride
This patch adds tests for the caching fix in
Ie76020dc4fa3545f827e1674051530b479f01f31, but these tests also revealed
that the recursive invocation of the legacy parser to expand magic
variables like {{PAGELANGUAGE}} wasn't using the pageLanguageOverride,
aka ParserOptions::getTargetLanguage().

The page language override is used when parsing new context which
doesn't currently exist in the database and therefore doesn't have a
page language set by its title (which doesn't yet exist).

Bug: T376783
Follows-Up: Ie76020dc4fa3545f827e1674051530b479f01f31
Change-Id: If6fe7cf00be6e78ef46181b17f01138383e95e46
2024-10-09 12:28:23 -04:00
Yiannis Giannelos
fec9daae4e Use statslib for metrics emitted by HtmlOutputRendererHelper
Bug: T359476
Change-Id: If8bc5a7f7383dcb67dadf1f4c02d1b69d7c8fa53
2024-10-04 14:52:30 -04:00
James D. Forrester
cc28acc455 Add namespace to remaining parts of Wikimedia\Mime and Wikimedia\Stats
Bug: T353458
Change-Id: If0137003ab625017d322d57870448a02569668c3
2024-09-27 16:19:10 -04:00
Wendy Quarshie
0c22b25b31 Adding token validation to the edit handler
Change-Id: I86fa7d613aa79e07ce20ea7936becf02f25832ac
2024-09-26 15:08:57 +00:00
James D. Forrester
984076f98e Add namespace to includes/api classes
Bug: T353458
Change-Id: I3ea6b08c5018ba03ba45c5766e1f46e12f6b8597
2024-09-25 19:31:14 +00:00
daniel
84fe1b9ccd REST: Introduce discovery endpoint
The discovery endpoint provides basic information about accessing the
wiki's APIs, as well as a directory of available modules.

Bug: T365753
Change-Id: I161aa68566da91867b650e13c8aadc87cd0c428c
2024-09-20 17:02:59 +00:00
daniel
d7ed4b14bb REST: validate JSON in tests
This applies JSON Schema validation in phpunit tests where appropriate:

1) In ModuleSpecHandlerTest, the generated OpenApi specs are validated
against the OpenAPI 3 schema.

2) In RestStructureTest, module definition files are validated against
   the mwapi schema.

This patch introduces a new trait to make it easy for phpunit test cases
to perform validation.

This patch also fixes some issues with the docs/rest/mwapi-1.0.json
schema and the includes/Rest/content.v1.json module definition.

Change-Id: I966cddb337c9373ed3a369496548a8d8c538ae84
2024-09-20 15:42:32 +00:00
jenkins-bot
634373a3cf Merge "REST: add restbase compat mode for some endpoints" 2024-09-16 19:21:57 +00:00
daniel
602f0ab595 REST: add restbase compat mode for some endpoints
In order to replace the /api/rest_v1/page/title/{title} endpoints, we
need to have something in MediaWiki that generates a compatible
responser. The v1/page/{title}/bare and v1/revision/{id}/bare endpoints
are functionally equivalent, so the easiest approach seemed to be to
add a compatibility mode to them. The compatibility mode is triggered
using the x-restbase-compat header, which can be set via the gateway
when routing the request from /api/rest_v1/page/title/.

Bug: T374136
Change-Id: I4af7ff5325660ae30faebb24753b9dc1c3acb2b3
2024-09-15 16:41:21 -05:00
Umherirrender
503bff0b04 Rest: Add missing documentation to class properties
Add doc-typehints to class properties found by the PropertyDocumentation
sniff to improve the documentation.

Once the sniff is enabled it avoids that new code is missing type
declarations. This is focused on documentation and does not change code.

Change-Id: Idf17719c875466810313f0fbbf16bc67f3e40059
2024-09-07 21:49:56 +02:00
C. Scott Ananian
a0503debb0 Provide previous parse results to parser when rendering
This patch lays the groundwork for incremental/selective parsing in
Parsoid by ensuring that we can pass previous cached parses through
the parse pipeline to Parsoid.  We do this by adding a new render
hint type, `previous-output`, and ensuring it is passed along.

Because revisions can contain a ParserOutput which is the combination
of separate ParserOutput objects for each of their slots, RenderedRevision
also contains a method to unsplit the combined ParserOutput to reconstruct
an original ParserOutput for use in incremental parsing.  Currently this
is mostly a stub, but illustrates how slot combination and splitting can
work, assuming those transformations are reversible.

Extra calls to ParserCache::getDirty() are added to some code paths
in order to ensure that any previously-cached ParserOutput is available
for selective update.  In order to mitigate any performance concerns,
these are only done for the Parsoid parser at the moment.  Future
patches will add additional metrics to quantify the cost/benefit ratio
of the additional cache lookups on these paths.

Bug: T363421
Bug: T371713
Change-Id: I440884f1d7e09c1ff9806f848b7b53a636367690
2024-08-23 17:41:55 -04:00
jenkins-bot
aef156e925 Merge "Migrate ParserOutputAccess metrics to statslib" 2024-08-23 21:39:21 +00:00
Yiannis Giannelos
fa3e92db5e Migrate ParserOutputAccess metrics to statslib
Bug: T359372
Change-Id: I5450251dff31d55a9a46b7fe885c27cd6ba083bf
2024-08-23 21:06:34 +00:00
daniel
057dda8588 REST: include version in generated OpenAPI spec
The generated OpenAPI spec should contain information from the info
section of module spec files.

Bug: T366834
Change-Id: I488f550adbf32205bdcdcb59b1e7f5273643bf69
2024-08-19 20:47:40 +02:00
thiemowmde
ba36bf46a1 tests: Make use of ??= and ?? operators in various tests
The main benefit is that this syntax avoids repeating the variable
name. It does the same as before.

Change-Id: I2040be889c749f4e2416ca9948ba261345858af2
2024-08-15 11:44:46 +02:00
James D. Forrester
bc662aec9b Move Language and friends into Language namespace
Bug: T353458
Change-Id: Id3202c0c4f4a2043bf97b7caee081acab684155c
2024-08-10 13:36:30 +02:00
Ebrahim Byagowi
697e19e461 Add MediaWiki\Registration namespace to registration classes
Bug: T353458
Change-Id: Ifa3b6a6e0353bb4ce21a3f4456f1fc696c8d377c
2024-08-10 10:08:22 +00:00
jenkins-bot
fb5f6862c0 Merge "Add content.v1 REST module with relevant content endpoints" 2024-08-09 17:01:15 +00:00
bpirkle
3f614a0898 Add content.v1 REST module with relevant content endpoints
T366837 added the ability to group REST endpoints into modules.
This change now introduces the first use of a REST module.
The existing endpoints remain accessible at their original paths,
and mocha tests are run against both old and new paths.

Bug: T370430
Change-Id: I83a5cdbdd359e4d3e5f70dd3930783616b556738
2024-08-07 16:51:09 -05:00
Ebrahim Byagowi
4c270a72ac Add namespace to WikitextContent
It adds MediaWiki\Content namespace to WikitextContent
and two classes related.

Change-Id: Ib74e4c5b3edac6aa0e35d3b2093ce1d0b794cb6d
2024-08-06 17:42:51 +03:30
C. Scott Ananian
16de2c0851 [ParsoidParser] Remove unneeded code to set render ID
Since I72c5e6f86b7f081ab5ce7a56f5365d2f75067a78 it is part of the
contract of ContentRenderer::getParserOutput() that the render ID (and
other cache parameters) will be set when it returns.
(ContentHandler::getParserOutput() can set them even earlier if it has
custom content-based overrides.)  We had a lot of temporary
backward-compatibility code "later" in the parse process to try to close
the barn door if some code path "forgot" to set them, but these are
unnecessary now.

This patch removes that backward-compatibility code in ParsoidParser;
there is similar remaining code in ParserCache etc. which can be
addressed in follow ups.

(For compatibility we do have to temporarily copy the render ID code
inside ParsoidOutputAccess::parseUncachable, but that class is
deprecated and will be removed.)

The HtmlOutputRendererHelper path which used to call
ParsoidParser::parseFakeRevision() is now replaced with a codepath that
goes through RevisionRenderer.  In order to maintain the same behavior
of the ParsoidHandler, we have also added 'useParsoid' handling to the
JsonContentHandler.  This support can perhaps be deprecated eventually.

Bug: T350538
Change-Id: I0853624cf785f72fd956c6c2336f979f4402a68f
2024-07-19 16:09:32 -04:00
C. Scott Ananian
c23420603c Deprecate Html*Helper::init()
Instead of creating a half-initialized helper and later calling ::init,
provide all the information necessary for the helper in the constructor.
This is facilitated by the fact that there already exists a factory
class, PageRestHelperFactory, which holds all the services required.

This affects:
* HtmlOutputRendererHelper::init()
* HtmlMessageOutputHelper::init()
* HtmlInputTransformHelper::init()

Change-Id: I1e1213597c6be012f2bc024c2b370c968ff3b472
2024-07-19 15:03:37 +00:00
C. Scott Ananian
fc0af94d32 Hard deprecate ParsoidOutputAccess
This class was @unstable and should be replaced by ParserOutputAccess.

Bug: T367074
Depends-On: I543a6e9da4fc473a2ac54ac635286453f2aff96a
Change-Id: Ie51b9b7a8b42a6faafeb28378c188347f274a9c5
2024-07-19 03:09:35 -04:00
C. Scott Ananian
1a1ac6a82d [HtmlOutputRendererHelper] Remove use of ParsoidOutputAccess::parseUncacheable
This removes the last use of ParsoidOutputAccess in core, allowing it
to be deprecated and eventually removed.

Bug: T367074
Bug: T317018
Change-Id: Ica2c880e2e7c2b126aaea66a3e4be460b3f2234f
2024-07-19 03:09:32 -04:00
jenkins-bot
82d91ed739 Merge "Use MainConfigNames constants in tests where possible" 2024-07-11 12:46:04 +00:00
jenkins-bot
d9f64a6287 Merge "REST: Generate request body specs from param settings." 2024-07-10 19:27:24 +00:00
daniel
e6d0adcc68 REST: Generate request body specs from param settings.
Bug: T323786
Change-Id: I49da2a07efa32759480070c9d93c31d46f211d26
2024-07-10 20:23:56 +02:00
thiemowmde
4bd95cd96b Use MainConfigNames constants in tests where possible
I believe this makes the code less brittle, and also makes it a bit
more obvious what these strings are meant to represent.

Change-Id: Ia39b5c80af4b495931d0a68fd091b783645dd709
2024-07-10 10:11:22 +00:00
Ebrahim Byagowi
fab78547ad Add namespace to the root classes of ObjectCache
And deprecated aliases for the the no namespaced classes.

ReplicatedBagOStuff that already is deprecated isn't moved.

Bug: T353458
Change-Id: Ie01962517e5b53e59b9721e9996d4f1ea95abb51
2024-07-10 00:14:54 +03:30
jenkins-bot
7df99f2dae Merge "REST: Make module definition files more like OpenAPI specs" 2024-06-24 20:36:14 +00:00
daniel
79c61e80dc REST: Make module definition files more like OpenAPI specs
This splits RouteFileModule into two classes, ExtraRoutesModule and
SpecBasedModule.

ExtraRoutesModule has no module prefix and supports
only "flat" route definition files and additional routes from
extension.json.

SpecBasedModule represents a single module defined in a definition
file similar to an OpenAPI spec. The idea is that a full OpenAPI spec
can be generated by filling in any missing information based on
information provided by the Handler implementation. In particular, the
definition of parameters and request body schemas will be generated.

A JSON schema for the new file format is added under docs/rest/.

Support for the intermediate format introduced in Iebcde4645d4 is
removed. It was not included in a release and was not being used outside
core tests.

Bug: T366837
Change-Id: I4ce306b0997f80b78a3d901e38bbfa8445bed604
2024-06-24 16:42:59 +02:00
Bartosz Dziewoński
279fd16bab tests: Avoid deprecated Status / StatusValue methods
Use more specific and less fragile assertions whenever possible;
most tests don't need to examine the internals of the status so
closely. Use StatusValue::getMessages() when necessary.

Change-Id: Ie4b3ebc03abb0e352e82394ced6ab9e733c83fb4
2024-06-15 10:14:37 +02:00
Umherirrender
f27c2433bb tests: Use namespaced classes (2)
Changes to the use statements done automatically via script
Addition of missing use statement done manually

Change-Id: I4ff4d0c10820dc2a3b8419b4115fadf81a76f7a2
2024-06-13 23:21:02 +02:00
Bartosz Dziewoński
2bd7bc5cc8 Use real type hints for services etc. in includes/Rest/
Mostly used find-and-replace:

Find:
/\*[\*\s]+@var (I?[A-Z](\w+)(?:Interface)?)[\s\*]+/\s*(private|protected|public) (\$[a-z]\w+;\n)((?=\s*/\*[\*\s]+@var (I?[A-Z](\w+)(?:Interface)?))\n|)
Replace with:
\3 \1 \4

More could be done, but to keep this patch reasonably sized, I only
changed the most obvious and unambiguously correct cases.

In some cases, I also removed redundant doc comments on the
constructor, and re-ordered the properties to match the constructor.

Change-Id: Ifa710fdf4d8d44a2d7244798b787a1b2a58c35a7
2024-06-11 22:40:44 +02:00
C. Scott Ananian
64d343353e [HtmlOutputRendererHelper] Use ParserOutputAccess
One more step in gradually replacing uses of ParsoidOutputAccess with
ParserOutputAccess.  This is mostly just inlining the code from
ParsoidOutputAccess (which already uses ParserOutputAccess internally)
and then removing dead code.

Change-Id: I87d6568d9dc71bc11f406ea3c31ce86e01667e05
2024-06-05 10:37:36 -04:00
Arlo Breault
009edac867 Don't ignore offsetType attribute on lint API paths
The default used to be 'ucs2' when linting but
Ic9b7cc0fcf365e772b7d080d76a065e3fd585f80 stopped setting the offsetType
in the environment options, which changed the output to Parsoid's
default, 'byte'.

For the parsing paths, pagebundles are now post-processed and
non-pagebundles were left as a TODO.

However, for the lint paths, it was always ok to continue setting the
environment option because Parsoid is called directly and doesn't go
through the ParserOutputAccess.  The above patch was trying to limit
Parsoid specific options to that.

Bug: T365284
Change-Id: I8389c2f53b399b39a9f1d908a38aecb3abcb15ef
2024-06-03 19:59:28 -04:00
Arlo Breault
80b3b4494d Fix setting offsetType in options passed to HtmlInputTransformHelper
Ic9b7cc0fcf365e772b7d080d76a065e3fd585f80 stopped setting it as an
envOptions so pull it from the attribs when constructing.

The tests continued to set the offsetType in the envOptions, despite the
ParsoidHandler no longer doing that, so they failed to catch the error.

HtmlInputTransformHelper still considers it an envOptions though and a
cleanup could happen there too.  Although, offsetType still is an
envOptions to Parsoid, so maybe it's fine as is.

Change-Id: I661a170fbb2c61308e32a43a9f6b0ce8d2ffb797
2024-06-03 19:53:21 -04:00
C. Scott Ananian
1c0089772d [HtmlInputTransformHelper] Use ParserOutputAccess
One step in gradually replacing uses of ParsoidOutputAccess with
ParserOutputAccess.  This is mostly just inlining the code from
ParsoidOutputAccess (which already uses ParserOutputAccess internally)
and then removing dead code.

Change-Id: I9807149c30f4bfe06fd7ad12f96c13816c22aa9c
2024-05-22 10:58:38 -04:00
C. Scott Ananian
a565e388f9 Move ParsoidOutputAccess::supportsContentModel() into Parsoid SiteConfig
The `supportsContentModel` method is really querying Parsoid for the
set of content models it supports, so it makes sense to put it in the
Parsoid-specific SiteConfig service.

This is part of the work to deprecate and remove ParsoidOutputAccess.

Change-Id: I81eb2df8cef93ede95361a4e03185b3d58e5b84b
2024-05-22 10:57:37 -04:00