Commit graph

60 commits

Author SHA1 Message Date
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
Bartosz Dziewoński
df4cbf5ac6 Replace gettype() with get_debug_type() in debug/log/test output
get_debug_type() does the same thing but better (spelling type names
in the same way as in type declarations, and including names of
object classes and resource types). It was added in PHP 8, but the
symfony/polyfill-php80 package provides it while we still support 7.4.

Also remove uses of get_class() and get_resource_type() where the new
method already provides the same information.

For reference:
https://www.php.net/manual/en/function.get-debug-type.php
https://www.php.net/manual/en/function.gettype.php

In this commit I'm only changing code where it looks like the result
is used only for some king of debug, log, or test output. This
probably won't break anything important, but I'm not sure whether
anything might depend on the exact values.

Change-Id: I7c1f0a8f669228643e86f8e511c0e26a2edb2948
2024-07-31 19:33:57 +02:00
Bartosz Dziewoński
c9f73efd5a Namespace MessageSpecifier under Wikimedia\Message\
In change I625a48a6ecd3fad5c2ed76b23343a0fef91e1b83 I am planning to
make Wikimedia\Message\MessageValue use it, and we try to pretend that
it is a library separate from MediaWiki, so it makes sense to move
MessageSpecifier to the same namespace under Wikimedia\.

Bug: T353458
Change-Id: I9ff4ff7beb098b60c92f564591937c7d789c6684
2024-07-28 14:21:32 +02:00
Bartosz Dziewoński
159dad8abb language: Improve behavior of RawMessage used as MessageSpecifier
We have some code that expects that if you disassemble a
MessageSpecifier using its getKey() and getParams() methods,
then assemble a new MessageSpecifier using the return values,
you will get (at least approximately) the same message.

This was not the case with RawMessage, even though it implements
the MessageSpecifier interface, because its "keys" are not real
message keys, and this operation would mess it up.

Override the MessageSpecifier methods on RawMessage so that they're
compatible with how everyone expects a MessageSpecifier to work.

Add some tests for OutputPage, Message\Converter and Status
to verify some scenarios that would previously have failed.

Depends-On: I41991989515b4791bc1746f26bd404bf4f17dbdb
Depends-On: I612361dd20ff8aad4c0069f1c5af78e3e13b9692
Change-Id: Iddd14efa8b9536277c372257d5a7be135f26a540
2024-07-16 15:06:26 +01:00
Bartosz Dziewoński
92b189901e StatusValue: Reduce code duplication in addError()
Change-Id: I24dad3686657522480bc97b2b51565abf9531811
2024-06-13 21:40:44 +02:00
Bartosz Dziewoński
ccd423225f Add "implements Stringable" to every class with "function __toString()"
In PHP 8, but not in PHP 7.4, every class with a __toString() function
implicitly implements the Stringable interface. Therefore, the
behavior of checks like "instanceof Stringable" differs between these
PHP versions when such classes are involved. Make every such class
implement the interface so that the behavior will be consistent.

The PHP 7.4 fallback for the Stringable interface is provided by
symfony/polyfill-php80.

Change-Id: I3f0330c2555c7d3bf99b654ed3c0b0303e257ea1
2024-06-13 00:23:39 +00:00
Bartosz Dziewoński
d38c9a1ad1 StatusValue: Fix filtering out duplicate Message objects with context
Normally two Message objects can be compared with `==`, and they will
compare equal if they have the same key, parameters and internal state
such as the selected language.

However, if you call `setContext()` on a Message, it will never
compare equal to any other Message object, because a unique closure
is stored in the `userLangCallback` property.

Most implementations of MessageLocalizer::msg() use `setContext()`,
so filtering out duplicate Message objects in StatusValue usually
didn't work. Luckily most uses of StatusValue use string message keys.

Change-Id: Id22075bf73d37fd139c2ef7d80c69eeb462a7b68
2024-06-06 02:16:35 +02:00
Bartosz Dziewoński
c66441d05c StatusValue: Document and deprecate broken ways to search/replace messages
StatusValue::replaceMessage() doesn't really work if given a
MessageSpecifier, and doesn't work at all if given a MessageValue.

StatusValue::hasMessage() and ::hasMessagesExcept() ignore the message
parameters when searching if given a MessageSpecifier or MessageValue.

Therefore, deprecate passing anything other than strings representing
message keys to these methods.

Change-Id: Icb4effc6c5319c12ba4ebeab447a0cef54f8e255
2024-05-03 17:04:27 +02:00
Bartosz Dziewoński
3840f2181d StatusValue: Deprecate combining MessageSpecifier and parameters array
The parameters have always been ignored, so this is usually a mistake.

Depends-On: Ic12241bd3029bc1b0e7a0023689a2be35ccd30a8
Change-Id: I0675e557bb93a1c990fa923c50b9f6ee8a9836c8
2024-05-03 17:04:27 +02:00
Bartosz Dziewoński
f818da9351 StatusValue: Add a getter for MessageSpecifier list
An array of MessageSpecifier objects is easier to deal with than the
"legacy error array" format, which is an array of whatever wfMessage()
accepts, which can be a bunch of different things.

Deprecate some existing getters.

Change-Id: Ibc4ce11594cf36ce7b2495d2636ee080d3443b04
2024-04-10 00:13:04 +02:00
Umherirrender
723134eade Add explicit parentheses around mixed boolean operator
Mixing different binary boolean operators within an expression
without using parentheses to clarify precedence is not allowed (T358966)

Change-Id: I24ca752d5dac7c948fdbcabf721f6f0aef8a466f
2024-03-23 01:58:59 +01:00
Lucas Werkmeister
f0a5796328 Make StatusValue stable to extend
Custom Status or StatusValue subclasses can be useful to make the
contained value easier to work with – see e.g. PageUpdateStatus with its
custom factory newEmpty() and the accessors getNewRevision(),
wasPageCreated() etc. Let’s bless this practice by marking StatusValue
as stable to extend; Status is left untouched, because we generally want
to encourage use of StatusValue, not Status, these days. (Note that both
classes are already extended in some extensions, namely MediaModeration,
ReportIncident and SecurePoll.) Several protected fields by the class
which are not meant for other subclasses are marked @internal.

Bug: T358492
Change-Id: I6690fbbb13624ce650d45ab2a872ba38f1dd90a2
2024-02-27 11:19:57 +01:00
thiemowmde
a61078f653 Minimize code duplication in StatusValue methods
Use modern array destructuring and such to make the code – so I
hope – much more readable.

Also fix a mistake in the Status test setup. This caused the mocked
Message::getParams() to return an array with string keys. This is
impossible in non-mocked scenarios. There is an array_values() call
in the Message constructor that doesn't allow string keys.

Change-Id: Ief09feb06c60d3ffbfa95fa8ef56b4a9829da680
2023-11-30 20:26:52 +00:00
Ammarpad
42e166dd83 StatusValue: Allow passing arbitrary data to augment result
This allows extensions and hooks to pass around additional data
about the operation result arbitrarily to supplement value and errors.

When two StatusValue instances are to be merged, it's responsibility
of the caller to ensure either only one has this extra data or none,
but never both (since the type is unrestricted). If necessary, the
caller should merge them before invoking StatusValue::merge.

Bug: T326479
Change-Id: Ibe3f1f8b81bcfcb18551d3ca4cda464e4bdbcbce
2023-11-28 14:58:22 +01:00
Bartosz Dziewoński
12c930aac2 StatusValue: Improve logging/debug output with multibyte characters
Use mb_str_split and mb_substr to improve the chances that the fancy
table lines will line up, and to avoid corrupting multibyte characters
at boundaries. Remove max lengths in the sprintf format.

I'm not sure if this is the ideal output format, but I don't have the
heart to replace it.

Change-Id: Iff9ba98a12490938fcfefdeaebd5f1d82cd0ce7b
2023-10-05 15:04:30 -04:00
thiemowmde
9b03cde58e Merge sequences of if that end doing the same thing anyway
Motivation:
* Avoid code duplication.
* Hopefully make it easier to read.
* Also order stuff from cheap to expensive, if possible.

Change-Id: I575e3f2027ce60a0d0885be5b9bd3e07bc035eee
2023-06-16 16:09:42 +02:00
jenkins-bot
6494f7b6dd Merge "Remove unused arguments to private functions" 2023-02-08 22:42:46 +00:00
Umherirrender
ed169d991e Remove unused arguments to private functions
Found by phan dead detection

Change-Id: I93379b7b9a733206d0e53add04fcdb9478c58755
2023-02-08 19:00:47 +00:00
Gergő Tisza
0a0e4cf607 Add StatusValue::hasMessagesExcept()
Opposite check of StatusValue::hasMessage(), to ignore
known-to-be-irrelevant errors.

Bug: T272081
Change-Id: If0aaba4c607663b3b38ebfdc66ed3edbce6bd7d8
2023-02-07 06:18:49 +00:00
Bartosz Dziewoński
6d9c0afc48 StatusValue: Add type hints for array shapes
Change-Id: Ie113df876fdc773599a0564c18dd8beaed752640
2022-12-22 13:35:24 +01:00
Umherirrender
b15e689d49 Remove unused local variables
Various variables are left from ealier refactor are now unused
and can be removed to make the code easier to read

Change-Id: Id51770af1f08e85c7e7a02234a2cd2ab5b47ee7a
2022-09-19 23:07:07 +02:00
daniel
951966c63c Make StatusValue accept MessageValue.
This allows us to use MessageValue in more places. In particular, it improves code re-use
between REST handlers (which use MessageValue) and old style API modules
(which use Status a lot).

Change-Id: I14f1d4444b725273675dd229d716ac6396752c90
2022-09-01 09:00:21 +00:00
daniel
21d1848ce2 StatusValue: include type in toString output
StatusValue::toString generates tabular output. This output should
contain the type of each entry (error or warning). The index number is
not relevant and can be omitted.

Example output:

<OK, collected 2 message(s) on the way, no value set>
+----------+---------------------------+--------------------------------------+
| warning  | foo                       | Lorem ipsum dolor sit amet, consecte |
|          |                           | tur adipisici elit, sed eiusmod temp |
|          |                           | or incidunt ut labore et do...       |
| error    | bar                       | [ [ a, b, c ], ABCDEFGHIJKLMNOPQRSTU |
|          |                           | VWXYZ ABCDEFGHIJKLMNOPQRSTUVWXYZ ABC |
|          |                           | DEFGHIJKLMNOPQRSTUVWXYZ ABC...       |
+----------+---------------------------+--------------------------------------+

Change-Id: Ia86159da2020518062e3fadc7b40da935342357f
2022-06-10 11:09:14 +00:00
Taavi Väänänen
6128d841c3
UserAuthority: Fix wikitext escaping for block errors
Status and Message behave differently when given a list of message
parameters. Since the errors generated by PermissionManager is intended
to be used by Message, we need to update UserAuthority to do that.

Fixed StatusValue::replaceMessage to support MessageSpecifier objects
like StatusValue::hasMessage does. This is so that the block message
formatting in ApiBase does not break.

Bug: T306494
Change-Id: I26405c680839b4679709e750d6d54e699c1efa66
2022-05-04 21:46:38 +03:00
daniel
e239b02a5e Add convenience methods for asserting status.
This ensures that assertions work in a uniform way,
and provides meaningful messages in cause of failure.

Change-Id: Ic01715b9a55444d3df6b5d4097e78cb8ac082b3e
2022-03-16 22:44:25 +01:00
Gergő Tisza
0ff1138537
StatusValue: improve toString formatting
Don't just cut off the message key at 25 characters, it makes
debugging very annoying. But do cut off the parameter at some
reasonable length, it could be arbitrarily large.

Change-Id: I8a2665434d6370477eb36a6827eb634e230d280a
2021-11-25 21:53:21 -08:00
TChin
fa5d3dba51 Support fluent interface for StatusValue
Also refactored some calls to use it.

Bug: T287485
Change-Id: If9e96ce87dc3f3641e9b3ebe093f92d8fa49b9ee
2021-08-02 15:14:11 -04:00
libraryupgrader
5357695270 build: Updating dependencies
composer:
* mediawiki/mediawiki-codesniffer: 36.0.0 → 37.0.0
  The following sniffs now pass and were enabled:
  * Generic.ControlStructures.InlineControlStructure
  * MediaWiki.PHPUnit.AssertCount.NotUsed

npm:
* svgo: 2.3.0 → 2.3.1
  * https://npmjs.com/advisories/1754 (CVE-2021-33587)

Change-Id: I2a9bbee2fecbf7259876d335f565ece4b3622426
2021-07-22 03:36:05 +00:00
Petr Pchelko
0061ed3bed Convert WikiPage to authority.
Change-Id: Iba5e36e5a13c304be51b82a952b2a3044e122c8c
2021-02-27 00:50:07 +00:00
Reedy
de8be2f820 StatusValue: Minor code clean up
* Call non static function non statically
* Replace += 1 with ++

Change-Id: Id44e08ad9a72068d6eae881a9b400640e6d540c5
2021-02-19 23:14:25 +00:00
Umherirrender
8de3b7d324 Use static closures where safe to use
This is micro-optimization of closure code to avoid binding the closure
to $this where it is not needed.

Created by I25a17fb22b6b669e817317a0f45051ae9c608208

Change-Id: I0ffc6200f6c6693d78a3151cb8cea7dce7c21653
2021-02-11 00:13:52 +00:00
Cindy Cicalese
546ae552a9 Status/StatusValue errors/warnings should be unique
Bug: T272100
Depends-On: I4a45d2db5de350384e7f57a728b2089f29dc1767
Change-Id: Idfc93029ef177b92830866de941394b383fcbb34
2021-02-06 10:26:29 -05:00
Ammarpad
b5ec7226a4 StatusValue: Reduce logic duplication
Make StatusValue::fatal() call StatusValue::error() method instead
of duplicating its logic.

Change-Id: If6274f59d35d85252afff8d0254eaebb2b1328aa
2021-01-17 07:33:44 +01:00
daniel
bd5c3d06a2 Annotate newable classes
This annotates classes that can safely be instantiated by
extensions, per the Stable Interface Policy.

Bug: T247862
Change-Id: Ia280f559874fc0750265ddeb7f831e65fd7d7d6a
2020-07-07 15:38:57 +02:00
Brad Jorsch
c7e451ce95 StatusValue: Fix __toString() to not choke on special parameters
e.g. nested messages, Message::plaintextParam(), and so on.

I'm not inclined to do too much here, since long term we should replace
Message with MessageValue and that will likely require reworking or
replacing StatusValue too.

Bug: T245155
Change-Id: Ie727de19162467574815853d2584c472a9171240
2020-02-14 16:51:31 -05:00
jenkins-bot
2796f33ff5 Merge "Make Status::wrap and StatusValue::splitByErrorType work together" 2019-10-08 12:03:08 +00:00
Gergő Tisza
773a732a8d
Make Status::wrap and StatusValue::splitByErrorType work together
Status::wrap creates a new status object with most of its fields
references to the wrapped StatusValue. (This seems like a bad idea
but fixing it would potentially introduce subtle behavior changes
in a lot of code, so it is not attempted at this time.) When
splitting such a Status object by error type, the cloning causes
the error arrays of the new Status objects to be references to the
old one so writing them will result in a mess.

Change-Id: I9801647c39578a76be66f6e8c3c06c352660ca1e
2019-10-07 13:36:15 +02:00
Daimona Eaytoy
2612175ef3 Use real variargs for variadic methods
These were all checked via codesearch to ensure that nothing is overriding
these methods.

Change-Id: Ie2d8c1fd01478f46f3d0d1259417182b0d955a12
2019-10-06 14:47:54 +00:00
Daimona Eaytoy
e7ee0bc0f5 Better docs to make phan happy
Change-Id: I8a9a6330cf1f889cbb77dfc28c93774204767323
2019-09-07 16:47:18 +00:00
Derick Alangi
2dca5bbbf5 Remove unnecessary semi-colons
Change-Id: I9eb65bdfbd3aa581effc14ead801b9e89b0359c3
2019-06-12 14:35:59 +01:00
Umherirrender
130ec2523d Fix PhanTypeMismatchDeclaredParam
Auto fix MediaWiki.Commenting.FunctionComment.DefaultNullTypeParam sniff

Change-Id: I865323fd0295aabd06f3e3c75e0e5043fb31069e
2018-07-07 00:34:30 +00:00
Max Semenik
6e956d55aa Replace call_user_func_array(), part 2
Uses new PHP 5.6 syntax like ...parameter unpacking and
calling anything looking like a callback to make the code more readable.
There are much more occurrences but this commit is intentionally limited
to an easily reviewable size.

In one occurrence, a simple conditional instead of trickery was much more readable.

This patch finishes all the easy stuf in the core, the remainder is either unobvious
or would result in smaller readability gains. It will be carefully dealt with in
further commits.

Change-Id: I79a16c48bfb98b75e5b99f2f6f4fa07b3ae02c5b
2018-06-07 20:19:26 -07:00
Thiemo Mättig
ef470ebf7f Remove @param comments that literally repeat what the code says
These comments do not add anything. I argue they are worse than having
no comments, because I have to read them first to understand they
actually don't explain anything. Removing them makes room for actual
improvements in the future (if needed).

Change-Id: Iee70aad681b3385e9af282d5581c10addbb91ac4
2018-01-10 14:14:26 +01:00
Thiemo Mättig
c785efd25e Fix and make some types in PHPDoc and JSDoc tags more specific
Change-Id: I7c109067579f570ecccaabc75b0b4db7eac4e6c2
2017-08-05 18:46:44 +02:00
jenkins-bot
aa3319c4c0 Merge "Miscellaneous indentation tweaks" 2017-02-28 18:38:36 +00:00
Bartosz Dziewoński
ecdef925bb Miscellaneous indentation tweaks
I was bored. What? Don't look at me that way.

I mostly targetted mixed tabs and spaces, but others were not spared.
Note that some of the whitespace changes are inside HTML output,
extended regexps or SQL snippets.

Change-Id: Ie206cc946459f6befcfc2d520e35ad3ea3c0f1e0
2017-02-27 19:23:54 +01:00
addshore
a28fa6b546 Fix StatusValue::setResult phpdoc typo
Change-Id: If727e357d4deb138ec4e391f231b074677ec2437
2017-02-27 09:18:03 +00:00
jenkins-bot
91267f914e Merge "Improve Status/StatusValue::splitByErrorType type hint" 2016-10-24 05:11:30 +00:00
Gergő Tisza
1bcff02923 Force boolean type for $ok in StatusValue::setResult
Change-Id: Ie9e933fba3445872e39d70ab3a8230f204106996
2016-10-24 04:07:53 +00:00
Gergő Tisza
223aa1e97e Improve Status/StatusValue::splitByErrorType type hint
Change-Id: I6deb76f8906aa86481b84ce1aaebaddf3a52e31d
2016-10-24 00:50:40 +00:00