Commit graph

69 commits

Author SHA1 Message Date
Thiemo Kreuz da8969812c Move getRawParams helper method to ApiTemplateData class
This method is not used anywhere else:
https://codesearch.wmcloud.org/search/?q=getRawParams

I tried to make the code a bit more readable. Notable:
* Make use of the return value we get from the preg_… function.
* {{3,} means "the character '{' 3 or more times". {{{+ does the
  same. Note the { doesn't need to be escaped when it's not
  followed by a number.
* At the end, it doesn't make any difference when we scan for
  optional closing brackets. The moment we find at least 3 we are
  done.

The test is intentionally not moved. This is something for a later
patch.

Bug: T301337
Change-Id: I55e31ceecea2ae7c35bcfbc2d641b35f751820db
2022-02-09 12:37:33 +00:00
Thiemo Kreuz 001494f443 Move last remaining HTML formating code out of blob class
Effectively a no-op. This patch doesn't change what the code does.
Tests are in place to prove this.

As before, the tests are intentionally not moved but left in place.
This is for later patches to clean up.

Change-Id: If130e0d006a36d8c755288f8a4e4e9a4c42a6295
2022-02-03 09:33:03 +01:00
Thiemo Kreuz 94573e1152 Inject localizer service into HTML formatter class
Change-Id: I7f27b0c42e15ab939749c9dbe9eb51754580ca87
2022-02-01 15:59:32 +01:00
Thiemo Kreuz 15aa40aa1d Rename all $paramObj to $param
The word "param" is not really that ambiguous in this context. The
only other meaning it could have is "parameter name". Such places
already use $paramName.

This makes the following patches easier to review.

Change-Id: I1e6210d1ca7d58726a0fc3b3396d75e0e28c16d8
2022-02-01 15:47:53 +01:00
Thiemo Kreuz 8c24751491 Split validation and HTML formatting into separate services
No functional change was made to the code. It was only moved from one
place to another. Note there are a lot of tests that cover this code.
The tests haven't been touched on purpose. Splitting these as well
is something for a later patch.

Bug: T260980
Change-Id: I9fa0fa87768f2560b83a1b5f3d39211ea9d6cfad
2022-02-01 15:47:52 +01:00
Thiemo Kreuz c6822649d5 Replace effectively unused $paramNames array
All this effectively does is counting the number of properties in the
$data->params object.

Change-Id: I92fff1f70b4f583c27c0e0ba7e4d7239e7790ee3
2022-02-01 15:45:33 +01:00
Thiemo Kreuz 5f749c6418 Allow aliases to be integers in addition to strings
Parameter names in a template can be numeric. While it makes a lot of
sense to force a specific format in the TemplateData JSON (i.e. only
strings), it's inconvenient and confusing if numbers are rejected for
being "invalid".

Effects of this patch:
* The incoming JSON is allowed to contain numbers in the aliases
  array.
* However, the API normalizes these and forces all aliases to be
  strings, as it was always documented.
* The editor component accepts anything in the aliases array, but
  forces all aliases to be strings. Again, as documented.
* Note that it was never possible to use numeric keys in the `params`
  list. This patch is only about aliases.

At the moment this is a somewhat "hidden" feature. We might or might
not update the documentation to officially allow numeric aliases.

Bug: T298795
Change-Id: I32ea296b4520e7f21b03a1f6390db4f43b613bdd
2022-01-10 13:33:27 +01:00
Thiemo Kreuz e83e3f2661 Add missing validation for aliases and suggestedvalues
This is not new. This was always documented to not accept anything
but strings.

Let's apply the same fix to aliases while we are here.

Bug: T297386
Change-Id: I57e18779f28802816d5adb66cc4067df4e58b26e
2021-12-09 14:40:36 +01:00
Reedy 166812da07 Namespace extension
Change-Id: I779e97e512ec0c4f74fb6a4b706772fb1428e40f
2021-11-25 22:53:34 +00:00
jenkins-bot 30204d74f5 Merge "Small clean-up of "format"-related HTML output" 2021-10-08 22:22:17 +00:00
jenkins-bot 810125500b Merge "Fix and update type hints" 2021-10-07 07:51:56 +00:00
jenkins-bot da2077d5bc Merge "Fix parameter auto-detection picking up syntax elements" 2021-10-06 23:27:08 +00:00
Thiemo Kreuz 37a5285c29 Small clean-up of "format"-related HTML output
I hope this is a little easier to read.

Change-Id: I1ca1cb3d9565f083ad16c3f6aed98f93e3675a3a
2021-10-03 10:07:19 +00:00
Thiemo Kreuz 3ea3de8655 Fix and update type hints
The $this->data property stores whatever json_decode() returned,
which could be anything. The validation happens later.

Change-Id: I0d04831b8253005734c1c6c97b48061be35e5d88
2021-10-02 10:50:45 +00:00
Thiemo Kreuz 3060559d1d Fix parameter auto-detection picking up syntax elements
See T290322 for a detailed description.

Bug: T290322
Change-Id: Id9935482fb466e7a1f6e55f042b13fe5851412d0
2021-09-03 13:18:42 +02:00
Thiemo Kreuz eb12e48b14 Add test cases for (almost) all possible parsing errors
"Almost" because I found at least one that appears to be
unreachable (the very first check for null). But changing this
code is out of scope of this patch.

This also updates some of the error messages to explain the
location of the error better. It appears like the incomplete
paths are copy-paste mistakes.

I also found one duplicate test case and removed it.

Change-Id: Ic0ee9d04f5cd1060ade385ef308e70d221dd2f18
2021-08-30 15:17:21 +00:00
jenkins-bot b745f0961a Merge "Make parameter order when sorting by status independent of language" 2021-08-02 12:51:37 +00:00
Thiemo Kreuz c2508a78ad Make parameter order when sorting by status independent of language
At the moment, when the user clicks the "Status" column to
sort by status, the statuses are ordered alphabetically,
which gives widely varying results depending on the language.
But there is an inherent order for these, even hard-coded in
the code: When a parameter is deprecated, nothing else matters.
Otherwise it's required → suggested → optional. Doesn't it
make much more sense to order the column this way? Especially
because there are never more than these 4 hard-coded values.

This is one of the (few remaining) issues mentioned on
https://de.wikipedia.org/wiki/Vorlage:TemplateData#Vorlagendokumentationsseite_verbessern_%E2%80%93_MediaWiki_ungen%C3%BCgend

This patch also makes it so that a CSS class name is always
added to all status fields, not only to the required ones.
This allows for per-wiki or per-user styling.

Change-Id: Id3f1ffafe09a3817972a4ee4bd4a3ded7be6f039
2021-07-30 13:42:00 +00:00
jenkins-bot 654e1ba037 Merge "Render docs with HTML5 <section>/<header> markup" 2021-07-30 13:40:14 +00:00
libraryupgrader a09e939fe7 build: Updating dependencies
composer:
* mediawiki/mediawiki-codesniffer: 36.0.0 → 37.0.0

npm:
* postcss: 7.0.35 → 7.0.36
  * https://npmjs.com/advisories/1693 (CVE-2021-23368)
* glob-parent: 5.1.0 → 5.1.2
  * https://npmjs.com/advisories/1751 (CVE-2020-28469)
* trim-newlines: 3.0.0 → 3.0.1
  * https://npmjs.com/advisories/1753 (CVE-2021-33623)

Change-Id: I23a441a089501a97f329b7b6d37bc658481e682f
2021-07-24 02:43:18 +00:00
Thiemo Kreuz 4b110c6c85 Render docs with HTML5 <section>/<header> markup
Change-Id: I91083561f7c4bcd3ee7184aee114a735de153fc4
2021-07-12 12:13:34 +02:00
Thiemo Kreuz f6e1004720 Various minor JSDoc/PHPDoc documentation fixes
Change-Id: I537bf4086058a7e18c9118f7433090c4bfaf98e7
2021-07-12 11:47:55 +02:00
Adam Wight 7b32bcefb4 Add suggested values parameter
Parameters may include a `suggestedvalues` property, which is rendered
in the UI for some parameter types.

TemplateData editor UI elements are implemented behind the
TemplateDataSuggestedValuesEditor feature flag.

Bug: T271897
Change-Id: I14012c79b3fa0d48c58fd8999584cc03ec03575e
2021-04-09 12:05:37 +02:00
jenkins-bot f26a5d37ec Merge "More strict validation of InterfaceTexts" 2021-02-13 17:23:44 +00:00
Thiemo Kreuz 294786223d Remove inconsistent capitalization of parameter names
Example:

On
https://en.wikipedia.beta.wmflabs.org/wiki/Template:Anschutz
the two parameters "state" and "capitalization_test" don't have
a label in the <templatedata> JSON structure. Instead the
internal parameter name is shown. But it's capitalized for an
unknown reason. I guess this is done to make the table look
"nice". But it causes confusion – see the ticket.

This capitalization is there since the very first commits from
2013, see I16d3f9e.

Compare with VisualEditor: Edit the template on
https://en.wikipedia.beta.wmflabs.org/wiki/Conflict-title-0.8542952978413387-I%C3%B1t%C3%ABrn%C3%A2ti%C3%B4n%C3%A0liz%C3%A6ti%C3%B8n
or use the old wikitext editor and TemplateWizard to insert
the template. In both cases the parameter names are not
capitalized.

Another argument why this capitalization is misplaced: When
there is no <templatedata> JSON blob, the TemplateData editor
auto-detects the parameters and semi-automatically creates a
minimal JSON blob. This is the moment where labels should be
created and stored so the user can edit them. But this doesn't
happen (for good reasons).

The user can't do anything about the current capitalization.
The only way to change it is to add a label that does nothing
but repeat the parameter name, just to undo the capitalization.
That should not be the way this works, I would like to argue.

Bug: T174771
Change-Id: Ia8133d3f0d6b79fe89c63bb0392a334c0a185a65
2020-12-23 11:59:12 +00:00
WMDE-Fisch 49bc2e8735 Replace deprecated Language::getFallbacksIncludingSiteLanguage
Change-Id: I24a95ce973169ab3f76abe39ca9780c3bf4ec5a4
2020-12-16 12:07:34 +01:00
Thiemo Kreuz f6de5d3493 Fix parameter extraction from wikitext source
This fixes a series of issues:
* The JS implementation had a trim() in one place that was
  missing in PHP.
* The actual parameter name in the paramNames/$params array was
  trimmed, but the "normalized" name (this is only for duplicate
  detection) was not trimmed.
* It was possible for an empty parameter to show up.

This resulted in very strange behavior, e.g. {{{ 1}}}{{{1 }}}
was detected as "1" and "10" (?), i.e. it would try to renumber
the duplicate in a strange way (string "1" plus a counter that
starts with 0).

Change-Id: I0a6371f3633b03b5b21809ecd06ea4c72d7d914d
2020-09-22 18:27:23 +02:00
jenkins-bot 077cb83cc9 Merge "Add and update soft PHPDoc type hints" 2020-09-21 01:00:27 +00:00
jenkins-bot 6d6ef56ea2 Merge "Prefer Html::rawElement instead of open/closeElement" 2020-09-21 00:53:23 +00:00
Thiemo Kreuz 9f878897cf Turn static variables into private constants
These are not really variables. They are never modified, and not
meant to be modified. Using "static" was a common workaround
when we had no private constants in PHP.

Change-Id: Ie1234ce8833986431be95f8537282fa174978063
2020-09-14 14:17:25 +02:00
Thiemo Kreuz eeffc4bf38 Prefer Html::rawElement instead of open/closeElement
Html::open/closeElement are hard to read and possibly error
prone. We can easily avoid it in this case here.

Change-Id: I2251cb63e58bc132ced0bb684e3f0e3be35ab1aa
2020-09-09 16:54:59 +02:00
jenkins-bot 6dd0b66fea Merge "Make TemplateDataBlob::normaliseInterfaceText() private" 2020-09-08 22:45:15 +00:00
jenkins-bot 8e56813b6e Merge "Use more canonical (object)[] instead of new stdClass" 2020-09-08 22:45:13 +00:00
jenkins-bot 3319103531 Merge "Make properties in TemplateDataBlob private" 2020-09-08 22:17:40 +00:00
jenkins-bot 81c7cc823e Merge "Make subclass constructors properly call each other" 2020-09-08 22:17:39 +00:00
Thiemo Kreuz f2306a2eab Make properties in TemplateDataBlob private
No subclass is directly using these. They don't need to be
protected. There are getters, if needed.

Change-Id: I27dcb8bee37b9559242451774c52240b490a18af
2020-09-04 15:28:15 +00:00
Thiemo Kreuz a4813c26f3 Add and update soft PHPDoc type hints
In detail:
* Callers don't need to know that the return value can be a
  TemplateDataCompressedBlob. All relevant stuff is declared in
  the base class.
* It's not relevant which internal method returned the status.
  It's just the status of the object after it was constructed.
* "stdClass" is more specific. "object" includes more stuff
  which can't be returned here.
* Avoid duplication and use @inheritDoc instead.

Change-Id: I68878a5b26ecd566fbea88b513ee697b45769659
2020-09-04 06:10:24 +00:00
Thiemo Kreuz 53d6c2fcd9 Make subclass constructors properly call each other
In detail:
* Mark both as protected and make them call each other.
* Avoid duplication.
* Remove unused "null" default value.

Change-Id: I272a68bb3cc0c544ef306b16c2998458c2eb1a2d
2020-09-04 06:09:35 +00:00
Thiemo Kreuz 62c608f172 Add strict type hints in unambiguous places
Notes:
* In PHP, when a function parameter does have a strict type,
  all it does is forcefully casting the value to this type. It
  doesn't cause warnings.
* Violating a strict return type causes a warning.
* I'm intentionally not touching places where the result from
  json_decode() is passed through. In theory this could be
  anything. Let's update these later, after more refactoring
  is done.

Change-Id: I9a203356f70cf9edd434f7dc4ca130c2b7605ab4
2020-09-03 12:12:19 +02:00
Thiemo Kreuz a2415bb32a Turn static property into private constant
We can do this in PHP now.

Change-Id: I54ae244651134424d5a2b1ce27d685f8165692da
2020-08-26 13:00:26 +02:00
jenkins-bot 4a45fd6cbf Merge "Remove unused variables" 2020-08-20 15:07:00 +00:00
Thiemo Kreuz 1d798c0e43 Remove redundant null check after isset()
isset() already checks if a thing a) exists, and b) is not null.

Change-Id: I5291ee30c8897dda1a5c095e196d5b5477a1877b
2020-08-20 12:20:58 +00:00
Thiemo Kreuz 10971b6124 Make TemplateDataBlob::normaliseInterfaceText() private
Not used anywhere else:
https://codesearch.wmcloud.org/search/?q=normaliseInterfaceText
Doesn't even need to be static, as it is never called from a
non-static context.

Change-Id: I0981392e18cab08fb19501f57d805caf8f57dd4a
2020-08-19 15:40:55 +02:00
Thiemo Kreuz c355a8fac4 More strict validation of InterfaceTexts
This fixes parts of a TODO in the code. Specifically:
* When an InterfaceText is an object, it can not be any object,
  but must be an stdClass.
* It can't be empty.
* Language codes must be non-empty strings.
* Values must be strings.

I'm intentionally not adding more validation for the language
codes, as this needs discussion (what values should be allowed?),
and can potentially break existing pages.

Same for empty strings. This can easily happen when users
manually create a <templatedata> tag and copy-paste pieces that
are meant to be filled in later. Empty strings are not really
invalid (again, this needs discussion), but might be something a
client want's to ignore.

Change-Id: I0facaa08cffe5a5a038423a58d55bc90a40b2d75
2020-08-19 15:29:55 +02:00
Thiemo Kreuz 62ac9da173 Use more canonical (object)[] instead of new stdClass
Both styles create the exact same object. Casting an array to an
object creates an stdClass object as well. The main benefit of this
syntax is that there is much less repetition. Everything is one
token instead of individual lines, where each line might contain a
typo.

Change-Id: I8fb09e9d33e5a1d91d4b32a71f658b31c629987b
2020-08-19 14:23:29 +02:00
Thiemo Kreuz 4a0d7139ca Remove unused variables
Change-Id: I3f307113be730d770af796e91bc28bb149c45ad5
2020-08-19 14:11:02 +02:00
Siddharth VP 512911b92e Make getRawParams() ignore parameters in comments and nowiki tags
This is used by the includeMissingTitles option of API action=templatedata.
Parameter syntax existing within nowiki tags or comments will not be valid
for the template.

Bug: T237195
Change-Id: Ibbfa3e21488f2a37fc494862e929baf50607d4c9
2020-08-17 00:33:06 +00:00
adham-khatean 55a423a5ba Remove Sortable class from tables that have one row or less
Removing the sorting arrows from tables that have one row or less in
templates. By using variable $sorting that changes according to the
coung of elements in the array $data->params, to determine whether the
table should have "sortable" class or not.

Bug: T126150
Change-Id: I414c2375d4eb4da5d78f92f6b4e99b55e314ce4d
2020-06-22 09:13:17 +01:00
libraryupgrader d2cee1239d build: Updating mediawiki/mediawiki-phan-config to 0.10.2
Additional changes:
* Removed phan-taint-check-plugin from extra, now inherited from mediawiki-phan-config.

Change-Id: I1f52b9bd1dbbdf15359d16efd5fc35eaf8b8ea76
2020-06-02 02:54:09 +00:00
DannyS712 720aff85b1 PHP coding conventions: reorder doxygen tags
See https://www.mediawiki.org/wiki/Manual:Coding_conventions/PHP#Doxygen_tags

Change-Id: Ied27b09fd717fd469170dc842091732195b4046d
2020-03-26 15:48:33 +00:00