Commit graph

610 commits

Author SHA1 Message Date
Adam Wight 262fbe24eb Encapsulate ref object: limited to ReferenceStack
This encapsulation gives us field name, type validation and code
documentation.

This patch only affects ReferenceStack and continues to return
approximately the same array outputs to callers.  Some additional
information is included and the placeholder column has a new name.

Bug: T353451
Change-Id: I405fe7ac241f6991fd4c526bfbb58fbc34f2e147
2024-01-09 09:59:16 +01:00
Adam Wight c23b824c34 Reorder conditions
The placeholder field will only be set if the ref exists, so we can
put these in a more logical order.

Change-Id: I2ddfb501fcc3aca936bb45c0d40e4f68c5d2b192
2024-01-09 09:57:03 +01:00
Adam Wight 1434dc5ca6 Switch to a 1-based "count"
The previous patch deprecated the last conditional depending on magic
meanings of 0 and -1, so now we're free to let "count" take on a more
natural meaning: the number of times a footnote mark appears in
article text.

Includes a small hack to avoid changing parser output, by
artificially decrementing the count by one during rendering.  The
hack can be removed and test output updated in a separate patch.

Bug: T353227
Change-Id: I6f76c50357b274ff97321533e52f435798048268
2024-01-08 11:45:36 +01:00
Adam Wight 86edddc8c2 Use semantic field to test ref type
Stop relying on the magic number distinction between "count" = 0 and -1,
by explicitly testing the "name" field instead.

Bug: T353227
Change-Id: I9dce16b01814e19f508d45b927de570049f0e0f5
2024-01-06 16:46:56 +01:00
Adam Wight 6b0ebb3066 Reduce deeply nested variables
These can be hard to read so this patch introduces named, temporary
variables.

PHP reference assignment is helpful here, and has the nice property
of responding correctly to `isset` as if it were called on the
referenced variable.  However, we're prevented from using this trick
in more places in the code because of an unfortunate side-effect that
PHP will store `null` under the referenced array key.  In some cases
(the ones here), this is harmless because we always test using
`isset` and null behaves the same as an unset value.  In other cases
such as arrays that are iterated over, the spurious key and null
value would be more of a nuisance.

Bug: T353227
Change-Id: Ie43592a2f10677ba19842e92fa29eb4bf3be240c
2024-01-06 16:39:46 +01:00
jenkins-bot 0071289f79 Merge "Inline constant for "placeholder" key" 2024-01-05 11:53:19 +00:00
jenkins-bot 0f4c90cc54 Merge "Store group in ref items" 2024-01-05 11:53:17 +00:00
jenkins-bot 9b770fba99 Merge "Include more information in missing parent placeholder" 2024-01-05 11:50:06 +00:00
jenkins-bot 1f7d6527a4 Merge "Render list-defined parent without a backlink" 2024-01-05 11:40:37 +00:00
Adam Wight a6cb979d88 Inline constant for "placeholder" key
Minor refactoring of an internal field, which can be treated like the
other columns.

Change-Id: I255578694c5ab9f2ad3cbe232217af3cea60669c
2024-01-05 11:22:30 +00:00
Adam Wight fd648aec98 Store group in ref items
Encapsulate all information about a ref inside of the internal
structure, rather than relying on the container to be organized by
group.

Bug: T353451
Change-Id: I4c91e8089638b7655bf120402a4a5fcbd1b35452
2024-01-05 11:22:12 +00:00
Adam Wight 76e6e870d4 Include more information in missing parent placeholder
This allows the subreferences to be collected together under a heading.

Bug: T353451
Change-Id: Ibf28f0baca14de8140c87b03ad4aa86d2f81a20d
2024-01-05 11:21:12 +00:00
Adam Wight 5a69c54900 Render list-defined parent without a backlink
In this case, there was never a ref with this name in the article so
no backlinks should be rendered.

TODO:
* test case with empty parent backlink and LDR parent

Bug: T353451
Change-Id: I8a7abd05a48ce83da3beb92b15e894d53252bd33
2024-01-05 12:07:22 +01:00
thiemowmde b01b420199 Track errors in a status object instead of an array
This is another improvement after I7390b68. Status objects are made
to keep track of multiple errors. The only difference is: The merge
method skips duplicates when the message and all parameters are
identical. This causes a minor user-facing change. One of the
shortest possible examples is:

 <references>
 <ref />
 <ref />
 </references>

This showed two identical, indistinguishable error messages before,
but will only show one now. We argue this is fine. The duplicates
are confusing and of (almost) no value to the user. In case the
information is relevant the correct solution is to make the error
messages distinguishable, or introduce a message like "multiple
<ref> tags defined in <references> have the same error". This is
something for a later patch, if needed.

Bug: T353266
Change-Id: I444105462ed24d5ba37b057622b4dc847b40f8d8
2024-01-05 10:49:08 +01:00
jenkins-bot 4d14f9c701 Merge "Merge two code paths about <references> sections" 2024-01-04 16:06:06 +00:00
jenkins-bot 733824005a Merge "Drop unused cite_reference(s)_link_prefix messages" 2024-01-04 16:04:34 +00:00
jenkins-bot ab20cb3cdf Merge "Rename appendText() to resolveFollow()" 2024-01-04 15:29:44 +00:00
thiemowmde ddda536792 Drop unused cite_reference(s)_link_prefix messages
Same as Icfa8215 where we removed the …_suffix messages.

This patch is not blocked on anything according to CodeSearch:
https://codesearch.wmcloud.org/search/?q=cite_references%3F_link_prefix

According to GlobalSearch there are 2 usages we need to talk about:
https://global-search.toolforge.org/?q=.&regex=1&namespaces=8&title=Cite.references%3F.link.prefix.*

zh.wiktionary replaces "cite_ref-" with "_ref-", and "cite_note-"
with "_note-", i.e. they did nothing but remove the word "cite". This
happened in 2006, with no explanation.

ka.wikibooks and ka.wikiquote replace "cite_note-" with "_შენიშვნა-",
which translates back to "_note-". One user did this in 2007,
16 seconds apart.

It appears like both are attempts to localize what can be localized,
no matter if it's really necessary or not.
https://zh.wiktionary.org/wiki/Special:Contributions/Shibo77?offset=20060510
https://ka.wikiquote.org/wiki/Special:Contributions/Trulala?offset=20070219
Note how one user experimented with an "a" in some of the edits to
see what effect the change might have, to imediatelly revert it.

The modifications don't really have an effect on anything, except on
the anchors in the resulting <a href="#_ref-5"> and <sup id="_ref-5">
HTML. It might also be briefly visible in the browser's address bar
when such a link is clicked. We can only assume the two users did this
to make the URL appear shorter (?). A discussion apparently never
happened. Bot users are inactive.

Both pieces of HTML are generated in the Cite code. Removing the
messages will change all places the same time. All links will
continue to work. The only possible effect is that hard-coded
weblinks to an individual reference will link to the top of the
article instead. But:
a) This is extremely unlikely to happen. There is no reason to link
   to a reference from outside of the article.
b) Such links are not guaranteed to work anyway as they can break
   for a multitude of other reasons, e.g. the <ref> being renamed,
   removed, or replaced.
c) Even if such a link breaks, it still links to the correct article.

There is also no on-wiki code on zh.wiktionary that would do anything
with the shortened prefix:
https://zh.wiktionary.org/w/index.php?search=insource%3A%2F_%28ref%7Cnote%29-%2F&title=Special%3A%E6%90%9C%E7%B4%A2&profile=advanced&fulltext=1&ns2=1&ns4=1&ns8=1&ns10=1&ns12=1&ns828=1&ns2300=1

I argue this is safe to remove, even without contacting the mentioned
communities first.

Bug: T321217
Change-Id: I160a119710dc35679dbdc2f39ddf453dbd5a5dfa
2024-01-04 13:17:42 +01:00
thiemowmde ca3203699c Capitalized dir="RTL" should not trigger any error
This fixes a minor issue introduced in I294b59f. Two identical
dir="…" with different capitalizations should not be reported as an
error.

Turns out the implementation in the Cite extension doesn't care
about this capitalization at all. That's why I suggest to do the
normalization as early as possible. This is slightly different in
the Parsoid implementation.

Bug: T202593
Change-Id: I96b4a281d6020d61d1f36ec027cf833bbb244f03
2024-01-03 16:30:16 +00:00
thiemowmde 8b86a4adac Give a different error from too_many_keys when 'follow' attribute conflicts
* Same as Ie64f4ab in the Cite codebase.
* Mark the changed tests as standalone since this Parsoid code isn't yet
  released to vendor and integrated tests run with vendor.

Bug: T299280
Depends-On: Ie64f4ab4831966f66f812ea67cc244718f818afb
Change-Id: I0ea1bc3f57576d215ba4060a0e886e588ffda0b3
2024-01-02 21:27:56 +00:00
Ed Sanders 71c52196f0 Document generated CSS classes
Change-Id: Id4bc9bfbffb278372f2e234861ad521db6d43643
2024-01-02 21:05:40 +00:00
jenkins-bot d8711ce3ed Merge "Show warning when dir="…" don't match" 2023-12-21 21:24:13 +00:00
Adam Wight 4ed93908a3 Ref sequence vs. key
Internal ref key is always an int, but another string `key` is
created in the formatters.  This patch makes the typing explicit.  We
can distinguish between these two different values in a later patch.

Bug: T353451
Change-Id: Id5e40517705961f4d54622e91264430d9f62008d
2023-12-21 10:03:18 +01:00
thiemowmde 6321074484 Remove redundant PHPDoc blocks that are identical to the code
Thanks to strict types and a recent MediaWiki CodeSniffer update a
lot of the PHPDoc comments in this codebase became redundant. Only
very few comments in this codebase contain additional information.
Such comments don't add any new information to what the code alone
already says. We started removing them in many other codebases
already.

In case someone wants to add more documentation to a method the
basic PHPDoc block can usually automatically be generated with a
button press in the IDE.

The only additional change in this patch is that I occasionally
add a missing `void` return type. This is necessary to be able to
remove the comment.

Change-Id: Id7d6d6a437175a9d017f564daf7ed16e76f09158
2023-12-19 17:10:23 +01:00
thiemowmde 3a128dce4b Use correct Sanitizer method for id/fragment escaping
Same as Idf50dad in the Cite extension.

Bug: T298278
Change-Id: Idde6c21e083f42642c3dd2fe64bbd3c4d2b63847
2023-12-19 15:38:36 +00:00
thiemowmde 8094a0ebf5 Move property initializations from constructor to property
This is doing the same as before, in pretty much the same execution
order. The only difference is the syntax.

In JavaScript it's relevant to not do array initializations to early.
Otherwise different instances share the same array. But this doesn't
happen in PHP.

Change-Id: I56363ccadf29f2b806f765ab8f54a3c1863fc10f
2023-12-19 14:57:12 +00:00
thiemowmde bb01b0d74b Merge two code paths about <references> sections
I'm not sure how much this helps. But this merges two code paths
that are both about "we are in the middle of a <references> section
right now.

Nothing changes, as proven by the tests.

Bug: T353266
Change-Id: I446e224b81d35c47736a437d78527c0cc8636f77
2023-12-19 15:04:08 +01:00
thiemowmde d73a76dce6 Rename appendText() to resolveFollow()
There is only 1 user left after Icf16965.

Bug: T353266
Change-Id: I4cafdcbe0a23dd7950613a385cb552e7a84e7f26
2023-12-19 14:49:52 +01:00
thiemowmde b181614ba1 Show warning when dir="…" don't match
This classifies as a "warning" because we still show everything,
just with an error message appended.

Disabling the Parsoid tests right away hopefully makes it easier to
do the same change in Parsoid.

Bug: T202593
Depends-On: If14acd1070617ca8c4d15be6b1759bd47ead4926
Change-Id: I294b59f989f553932b40d08308906dd72d92d2cd
2023-12-19 14:17:30 +01:00
thiemowmde eeb8e28e52 Remove obsolete comment about Sanitizer::safeEncodeAttribute
By now I'm sure this really doesn't belong here. The code in the Cite
extension is doing this because it generates HTML by concatenating
plain strings. In such a context the necessary HTML entity encoding
(&quot; and such) must be done manually. Here in the Parsoid context
this is not needed.

This is split from I7249bd0. See the discussion over there.

Change-Id: I5589e5c2147bfc9f205a0ff80d8bdd247ab49c63
2023-12-18 21:02:36 +01:00
jenkins-bot d79ae15bd0 Merge "Add basic class-level documentation to more classes" 2023-12-18 12:35:58 +00:00
jenkins-bot a10437ad1d Merge "Avoid the term "book referencing" in a few places" 2023-12-18 11:58:33 +00:00
Subramanya Sastry 0754bc9ffd Fix encoding of non-breaking spaces if found in ref names
* This partly replicates the fixes in I9435a2d and Ia01f2fd. More
  to be done in later patches.
* Updated html/parsoid test output (which matches the change in the
  html/php section).

Depends-On: I401656265253a429691cc76adc5db5b129cff2cc
Change-Id: I7249bd03a7942ff7725a20178a051300b777e3a8
2023-12-15 16:38:24 +00:00
thiemowmde 54ac87e5ce Separate ReferenceStack::appendText() from setText()
This moves one more error situation into the stack class, together
with other error situations that are already there.

Bug: T353266
Change-Id: Icf169650f67f64e6d29d175c3b47cf558b8de3d4
2023-12-15 16:41:05 +01:00
thiemowmde 742a9ffbf5 Track warnings separately in ReferenceStack
Check out how this gets rid of so many "to do" as well as
"deprecated" comments.

Next qustion: The elements in the stack become more and more
complicated. It's probably worth converting them from arrays into
first-class objects. But this is for another patch.

Bug: T353266
Change-Id: If14acd1070617ca8c4d15be6b1759bd47ead4926
2023-12-15 16:41:04 +01:00
thiemowmde 13138d4ed0 Avoid the term "book referencing" in a few places
We are discussing this for a long time and finally renamed the tag
on Phabricator: https://phabricator.wikimedia.org/tag/cite-extends

This patch updates only places where it can't have any negative
consequences.

This is also a direct follow-up to Ic73f1b7 where this class was
created.

Bug: T353269
Change-Id: I644fe41d3386b9bf02b83366654301633efd535f
2023-12-15 15:49:04 +01:00
xiplus f7a181ed42 Give a different error from too_many_keys when 'follow' attribute conflicts
Add message "cite_error_ref_follow_conflicts" for tags with
conflicting parameters.

Bug: T299280
Change-Id: Ie64f4ab4831966f66f812ea67cc244718f818afb
2023-12-15 15:23:53 +01:00
thiemowmde e7dea09216 Add basic class-level documentation to more classes
Bug: T353227
Change-Id: I3953543a111121cc49f6ea89988351b80b03e828
2023-12-15 14:27:58 +01:00
thiemowmde 4377f0923d More simple and consistent @covers and @license tags
Same arguments as in Iafa2412. The one reason to use more detailled
per-method @covers annotations is to avoid "accidental coverage"
where code is marked as being covered by tests that don't assert
anything that would be meaningful for this code. This is especially a
problem with older, bigger classes with lots of side effects.

But all the new classes we introduced over the years are small, with
predictable, local effects.

That's also why we keep the more detailled @covers annotations for
the original Cite class.

Bug: T353227
Bug: T353269
Change-Id: I69850f4d740d8ad5a7c2368b9068dc91e47cc797
2023-12-15 12:12:16 +01:00
thiemowmde d0d5fbbee6 Add temporary ErrorReporter::firstError helper function
I hope this makes other refactorings a little easier.

Bug: T353266
Change-Id: Ib574d4d54ba2c8bc1310822539336ad71c4309ef
2023-12-14 17:16:49 +01:00
jenkins-bot 6fc8ee7fec Merge "Get rid of "guarded <references>" terminology" 2023-12-14 14:25:57 +00:00
jenkins-bot 9ec01ef894 Merge "Introduce named constant for "__placeholder__" string" 2023-12-14 14:20:14 +00:00
thiemowmde cb71e87b0e Introduce named constant for "__placeholder__" string
This is a concept that's only relevant when a sub-reference (formerly
known as BookReferencing) appears before the parent reference it
belongs to. Let the name reflect this.

Bug: T353227
Change-Id: Iabf259e72942ea70cb1cc1e0ca5a5d8cf15d7225
2023-12-14 09:45:06 +01:00
thiemowmde 12c7ad7504 Get rid of "guarded <references>" terminology
This patch only moves existing code around without changing any
behavior. What I basically did was merging the old "guardedReferences"
method into "references", and then splitting the resulting code in
other ways. Now we see a few other concepts emerging. But the idea
something would be "guarded" (how?) is gone.

The most critical detail in this patch are the new method names, and
how the code is split. The names should tell a story, and the methods
should do exactly what the name says. Suggestions?

Bug: T353266
Change-Id: I8b7921ce24487e9657e4193ea6a2e3e7d7b0b1c3
2023-12-14 08:44:40 +01:00
thiemowmde a6a0f66130 Extract validation to a separate class
This removes almost 200 lines from the main class.

This patch intentionally doesn't make any changes to the code but
only moves it around. Further improvements are for later patches.

Bug: T353269
Change-Id: Ic73f1b7458b3f7b7b89806a88a1111161e3cf094
2023-12-14 07:43:29 +00:00
jenkins-bot bf53249893 Merge "Move a bit of code out of Cite::guardedReferences" 2023-12-14 02:10:06 +00:00
jenkins-bot 61850c091a Merge "Remove PHPDocs that just repeat what the code already says" 2023-12-14 02:04:15 +00:00
Sam Wilson b03dd1bba8 Load WikiEditor ref toolbar button on other content types
Allow other extensions to provide lists of page content
models for which they want to load the Cite toolbar button.
This will, for example, make it possible for ProofreadPage
to have the button on Page pages.

Bug: T348403
Change-Id: Id28cb0b6cb8a2b86a66b17232575afe513969c54
2023-12-13 21:45:19 +00:00
thiemowmde 04208b5fd1 Remove PHPDocs that just repeat what the code already says
We removed a bunch of now redundant docs already, see e.g. Ie0692fa.

Change-Id: I55c62d935bdec8bedaebc2666fca3eb17309b0c7
2023-12-13 12:44:41 +01:00
jenkins-bot c3aa27f2a1 Merge "Avoid a few isset() in favor of more recent syntax" 2023-12-12 23:58:45 +00:00
thiemowmde 9425bb3248 Move a bit of code out of Cite::guardedReferences
The main benefit is that the two lines that set and reset
$this->inReferencesGroup are now next to each other. More can be
done in later patches.

Bug: T353266
Change-Id: Ib3f40c40e0b1854f8e5a32af600f28931fffdb8c
2023-12-12 18:06:58 +00:00
jenkins-bot cdc2bd2b96 Merge "Skip URL encoding in id="…" attributes that aren't URLs" 2023-12-12 14:34:07 +00:00
thiemowmde 89bd26fcf5 Skip URL encoding in id="…" attributes that aren't URLs
I played around with a few options (see patchset 1) but ended
introducing new terminology:

* "Backlink" describes the ↑ button down in the list of <references>
  that jumps back up into the article. The code was already using
  "backlink" in some places.
* "Backlink target" is the id="…" attribute up there, visible as the
  typical [1] in the article.
* I use "jump" to describe the idea that clicking the [1] jumps down
  to the full reference.
* "Jump target" is the id="…" down there in the list of <references>.
* "Jump link" is the same id, but encoded to be used as the href="…"
  attribute when clicking the [1].

I hope this makes sense. Suggestions welcome.

Another benefit is that "normalization" is really only normalization
now, not any URL and/or HTML encoding.

Bug: T298278
Change-Id: I5a64ac43aef895110b61df65b27f683b131886fb
2023-12-12 13:56:37 +00:00
thiemowmde fee8606db6 Avoid a few isset() in favor of more recent syntax
As well as replacing a few `=== null` comparisons with the new ??=
operator.

Bug: T353227
Change-Id: I5b273f452d1e46d37fc28861b54c4e1f19a7a65a
2023-12-12 12:13:42 +00:00
jenkins-bot dda42b9aed Merge "Constants for internal enum" 2023-12-12 09:47:39 +00:00
Adam Wight 62497db444 Constants for internal enum
Change-Id: I6864bc7761abe653ab1f591c26e61a8ffb1ea3b6
2023-12-12 10:02:00 +01:00
jenkins-bot 039a8bbfb1 Merge "Rename internal variable" 2023-12-12 08:38:49 +00:00
jenkins-bot f7294f1b54 Merge "Parse error messages as late as possible" 2023-12-12 08:38:47 +00:00
Adam Wight c021c201c0 Rename internal variable
Updates the name to reflect the purpose and the new name for a cite
tag, now "ref".

Change-Id: I778641ad58048dd2c33b6487171c338555b0789b
2023-12-12 08:11:18 +01:00
thiemowmde 44ba7a89e2 Parse error messages as late as possible
This moves the actual parsing down to be done much later in the
process. This won't make any difference in production but makes it
easier to refactor the code further.

Note I tried to use a StatusValue object but couldn't because it
merges seemingly identical messages, while the plain array is fine
with containing duplicates. There is one parser test that covers
this. While we could change this it needs discussion and most
probably a PM decision.

Change-Id: I7390b688a33dace95753470a927bbe4de43ea03a
2023-12-11 18:28:35 +00:00
thiemowmde 6a18eac513 Fix regular expressions not being case-insensitive
The "parser marker" placeholders are case-sensitive, e.g. for a tag
that's written like <rEf> the placeholder will also say …-rEf-…. This
was really just a mistake.

The error is as old as this code is. Added in commit 75004e33 in
2009.

Note we shouldn't use /i at the end because the marker itself should
not be case-insensitive. Only the tag name.

Instead of adding more (slow) test cases I update two that are
exactly about this part of Cite (nested tags) anyway.

Bug: T64335
Change-Id: I44c7a42a0da682a1082952fd1af817bf7d45378c
2023-12-11 19:21:12 +01:00
Umherirrender c9773965ca Use namespaced classes
Done automatically via script

Change-Id: I40d64a194ad420c75dfa85711c53e35586895929
2023-12-10 23:18:51 +01:00
thiemowmde 202c0d3636 Drop unused …_suffix and …_key_with_num messages
The three messages cite_reference_link_key_with_num,
cite_reference_link_suffix, and cite_references_link_suffix are not
used for anything.

According to CodeSearch:
https://codesearch.wmcloud.org/search/?i=1&q=cite_references?_link_(key|suffix)

According to GlobalSearch:
https://global-search.toolforge.org/?q=.&regex=1&namespaces=8&title=Cite.references?.link.(key|suffix).*
For comparison:
https://global-search.toolforge.org/?q=.&regex=1&namespaces=8&title=Cite.references?.link.prefix.*

They are not meant to be localized, as noted in qqq.json. As many
messages in Cite the idea is that individual wikis can customize the
generated HTML (!) via such messages. These particular ones apparently
have been introduced just because it's technically possible, but never
been used for anything. They exist since the very first commit from
2005: https://phabricator.wikimedia.org/rECITb714bf09

Note how these messages aren't even visible anywhere, except in the
browser's address bar as part of a #… fragment.

This obviously doesn't solve T321217 but helps minimizing the
surface.

Bug: T321217
Change-Id: Icfa82155e3b02df39bb6e924bc472f6edc565d5f
2023-12-08 09:26:05 +01:00
thiemowmde 0bae6eb224 Fix confusing wording of "invalid parameter in <ref>" message
This error message really always meant nothing but "there is an
unknown parameter in your <ref> tag". It's unnecessarily confusing
only for historical reasons. See T299280#9384546 for a long
explanation.

Bug: T299280
Change-Id: Ic224d5828f7b7ac0928c44f526c61654ccf3425e
2023-12-07 10:54:46 +01:00
jenkins-bot e73c7d61ca Merge "Correctly encode non-breaking spaces in reference names" 2023-12-06 18:00:43 +00:00
thiemowmde f9bb125e4c Correctly encode non-breaking spaces in reference names
Note how this currently behaves. The user input is
<ref name="…&nbsp;…">
But what we get in the end is
<li id="…&#160;…">
This implies that the &nbsp; is decoded and re-encoded with a
slightly different entity encoding. (Note that &nbsp; and &#160;
and &#xa0; are all the same character.)

Also note how there is only an underscore in the href="…", but the
non-breaking space is gone. This is identical to what happens in
links and headlines. Try for example [[a&nbsp;_a]]. Multiple
underscores, non-breaking spaces, and normal spaces will be
normalized. We just do the same in the id="…" attributes.

Note this fixes only one of the issues listed in T298278.

Bug: T298278
Change-Id: Ia01f2fdd3b3e9ee6aaa9da60ca3386dcd5d6b1a0
2023-12-05 07:58:38 +01:00
thiemowmde 5f5e9ec9f0 Re-arrange code in preparation for T298278
This patch makes only sense together with I5a64ac4 where it is split
from. See I5a64ac4 for details.

The idea is that this patch just re-arranges the code without making
any changes to how the code behaves. This leaves a minimal change
behind that's much easier to revert, if needed.

Bug: T298278
Change-Id: Ie78313b7f3ac1ec7bce5ac7512e60a3bb011480a
2023-12-04 08:29:53 +01:00
thiemowmde 858fdcefd9 Split off separate key normalization function
This patch does two things:

1. The "normalization" function was never only doing normalization,
but also all the necessary HTML encoding. This is now more visible
and split into two separate functions.

2. To make this easier we change the order slightly. Because of this
the normalization step must now consider spaces. Before spaces have
been converted to underscores by escapeIdForLink.

The results are all the exact same as before.

This is split from I5a64ac4 to make that easier to review.

Bug: T298278
Change-Id: I9435a2ddaa21559e29587c58b7523103141467f7
2023-11-30 09:43:35 +01:00
gerritbot c26cc4fa44 Update UserOptionsLookup's FQN
User-options related classes are being moved to the MediaWiki\User\Options namespace in MediaWiki Core; reflect that change here.

Bug: T352284
Depends-On: I9822eb1553870b876d0b8a927e4e86c27d83bd52
Change-Id: I04337d15d32c1e8bdfdcfa272f1750ffecc8e47e
2023-11-29 12:38:22 +00:00
C. Scott Ananian d07d6affd9 Use DOMCompat::getAttribute() instead of Element::getAttribute()
This uses semantics consistent with the DOM spec (and any future
spec-compliant PHP DOM library, like Dodo or a native one bundled
with PHP).  It also helps avoid certain classes of errors caused
by conflating empty-string values and missing attributes.

A number of minor bugs have been fixed in the process, mostly
involving the string value "0" (and similar) which are falsey
and thus were sometimes treated the same as a missing attribute.

The API for MediaStructure was slightly tweaked to conform to
this "return null for missing attribute" convention.
No one outside Parsoid appears to use MediaStructure yet:

  https://codesearch.wmcloud.org/search/?q=MediaStructure

And Parsoid itself doesn't usethe MediaStructure::get* methods
which were tweaked here.

This patch squashes (and replaces):
 Ib272bd2a9a2da06efc4a4d962cd191107a70f84c
 Iae47c9f3c80f1642426ef985f1af6b44a9c807fb
 Ic3a43516e2a0c9bbd62f9519e5663b545fd975e7
 I7c45552a21ba49c76b1fc56f023d7937d9f41340
 Ic231a53dba9527c6b811cba990ae35f7fc7bf153
 I5209338d221ebf738a505d85fe1a019ea621fcc5
 Icb6b13c2b49edb3cd725999f09f8cb7e3a4c0f99
 I7eca3e616a66cac5b2a792435889cced2e2c9cd9

Change-Id: I11e7efb546c8cf1aac6b49c3361aacbd4eb5cef1
2023-11-22 14:04:11 -05:00
thiemowmde 67bbfe58ad Drop obsolete PHP 7.2 "arrayToObject" workaround
According to T228346 and T254519 this is a workaround for a PHP 7.2
bug. We require PHP 7.4 by now.

Not used anywhere outside of this codebase:
https://codesearch.wmcloud.org/search/?q=PHPU%5Cw*%3A%3AarrayToObject&files=%5C.php%24

Bug: T254519
Change-Id: I4150900e59c5bd865ce102417ac354a54552e623
2023-11-11 21:26:22 +00:00
Ed Sanders e7d3dec076 Document some generated messages
Change-Id: Id9c961f322c2f3730f29345da9ba61196243df5a
2023-11-08 12:07:50 +00:00
thiemowmde 4584c6d2b5 Add fast helper method DOMUtils::hasClass
While this is not really a bottleneck (usually the list of classes
is short) I was still curious. Why create an object, explode a
string into an array (even using a regex) and search the array if
we can do the same with a single regex?

This will be beneficial for other codebases, notably
DiscussionTools.

Change-Id: If9252a97562542e7a4bec29edcc6e8dee0fb8221
2023-11-03 21:27:01 +00:00
Umherirrender 2d6868cfe3 Use the expression assignment operator to simplify code
Suggested by phan, available since php7.4

Change-Id: If31fdd7174cf71d346e4080a70fa302bb3378745
2023-10-14 00:49:13 +02:00
Fomafix 21ceb98b74 Get context directly from $editpage
Change-Id: I9be4b9fb6016dea86f1cfa3893b69e2c92ac6c94
2023-10-10 07:27:29 +00:00
C. Scott Ananian dadeca74b5 Proper class type for DataMw
Slowly inch toward replacing the plethora of `stdClass` in the
Parsoid port with proper types.  The new classes could use additional
documentation of the various properties, but this is a start.

Bug: T226428
Change-Id: I87e73243203145b4067e70b0fa9e30da054fe1ed
2023-10-03 17:38:25 -04:00
gerritbot 198ae5b21c Replace some moved Title class uses, now MediaWiki\Title\Title
Bug: T321681
Change-Id: Idde5511e075fcec40cad12c7369abec3ee4d096c
2023-08-19 04:13:55 +00:00
Subramanya Sastry 4ed31f499e DOMPostProcess: Don't run extension dom processors on nested pipelines
* There is really no reason to run this once per nested pipeline
  since there is a lot of other processing that is skipped on
  nested pipelines.
* Eliminated the 'topLevel' arg to the wtPostProcess extension handler
  since that is no longer relevant.
* We may need to explicitly document this in the extension API spec.

Change-Id: Ib2508df736aeab9c649138249228647c00aaaef5
2023-07-28 14:40:59 +00:00
thiemowmde e750cc2ed2 Remove unused "HTML message" cite_references_no_link
I believe there is no reason to keep this. The only reason might be
a known wiki that uses this as a feature. But such a wiki doesn't
exist:
https://global-search.toolforge.org/?q=.&regex=1&namespaces=8&title=Cite.*no.*link

Bug: T321217
Change-Id: I128d4383da48f9bdda92f03b212ef3997b3a4802
2023-07-27 11:55:13 +02:00
jenkins-bot 1732720608 Merge "Remove (revert) expensive parsing of 1-character message" 2023-07-25 20:29:47 +00:00
thiemowmde 08814c8e38 Remove (revert) expensive parsing of 1-character message
This reverts a very tiny part of Ib3fdc89 from 2 weeks ago. The
reasons are explained in Ib3fdc89. Short version:
* The ->parse() calls have drastic performance implications.
* Allowing wikitext and HTML in this message also makes T321217
  worse.

The new message "cite_reference_backlink_symbol" is kept and still
used in the UI. Just not in these two messages any more. This is a
minor redundancy we want to get rid of at some point. But it's not
critical for the moment. This will be done as part of T321217.

Nothing will break on the wikis. Some wikis have customizations for
"cite_references_link_one" and "cite_references_link_many" in place.
This will continue to work as before Ib3fdc89.

Bug: T339973
Change-Id: I933771e3ad67cd530bcf5ee8469cef35ea1070d2
2023-07-21 14:05:31 +02:00
thiemowmde 25e7aa44dd No expensive transformations on prefix/suffix messages
This is a mistake that exists in this codebase for who knows how
long.

Cite mis-uses the messaging system a lot for internal things we still
want to customize somehow, but are not labels that will ever be shown
on the screen. The prefix/suffix messages in this patch are meant to be
part of the HTML in id="…" attributes. Prefix/suffix must be a static
plain text strings. Using e.g. {{GENDER}} or {{PLURAL}} in these
messages is not even possible because there is no $1 parameter to use.

Note how all other similar messages already use ->plain().

A few wikis override these messages, but stick to the plain-text
convention, as they should:
https://global-search.toolforge.org/?q=.&regex=1&namespaces=8&title=Cite.*reference.*fix
This will continue to work.

This has minor performance implications. Fetching these messages is
faster if we can skip transformations.

Bug: T321217
Change-Id: I7969c255fe4ce897e904897081da5f52678721aa
2023-07-20 16:22:46 +02:00
Subramanya Sastry 39e08e48fd Cite: Rename ext.cite.style to ext.cite.parsoid.styles
* Follows up the change in Cite repo

Change-Id: I043bea12cfcc80761c8c10e71a9c988d9f91637c
2023-07-19 14:32:56 -05:00
Subramanya Sastry 20889b2acb Cite: Add missing resource modules
* But, since Parsoid uses different class names, the JS code
  that inspects Cite content won't work!

  In this specific case, that code looks for .mw-cite-backlink
  which Parsoid doesn't add.

* Given the experience with media output changes in core, it might be
  prudent to switch over class names to what the Cite extension uses
  rather than try to update all the code that are probably referencing
  these classes.

Bug: T328695
Change-Id: Ie240898fa9de45f6d9d9821be39e9ec0af9f6bdf
2023-07-14 22:25:47 +00:00
Arlo Breault 128b758d13 Omit wrapperTag in references call to extTagToDOM
The contents of the domfragment gets migrated to an ol element and
then a query for sups in the ol makes up the nestedRefsHTML string.

Follows-Up: I44abd4f13254bc4512caad85244d523ac19140e7
Change-Id: Ie29d9b1ea7b34121587e77c8693dba79ed166ee1
2023-07-11 15:03:25 -04:00
Jon Harald Søby c66371b3d9 Move Cite-specific settings from WikiEditor
The WikiEditor extension has a button and some help text that
is only applicable if the Cite extension is enabled. Move
that (with some modifications) to the Cite extension instead.

Bug: T339973
Depends-On: I8256660f9c6886d6764b45735284e00308fc56e5
Change-Id: Ib3fdc897dd3330f69c5832003d4c3cb1e6dba2f3
2023-06-28 20:22:14 +02:00
Subramanya Sastry 5a77aa959b ParsoidExtensionAPI: Add method to process HTML embedded in attributes
* Parsoid core cannot know anything about how extensions represent
  information in their DOM. We were missing an extension API method
  and config that lets extensions specify this information along with
  handlers to process embedded content.

* This patch fills that above gap.

* There is a FIXME in ContentUtils::shiftDSR which will be addressed
  in followup patches with this new functionality.

Change-Id: If921ca471f25f21671a60c3f796fdf145267364d
2023-06-10 21:06:54 +05:30
Subramanya Sastry 1e61376d59 ParsoidExtensionAPI: Rename Extension API method
* This method isn't used anywhere else outside (per codesearch)
  so being bold and renaming without any public notification.

Change-Id: I0c4ec7c4b2afb1d0d3cd1a932e0384bd9626799e
2023-06-08 18:55:51 +05:30
jenkins-bot 5affadae9d Merge "Add strict types to all class properties" 2023-06-08 10:41:54 +00:00
Ed Sanders 58db72b0c5 Always use the strict equality flag when using in_array
Change-Id: I21283adfa1c10916bb0ee0b4e87d530bf19d94b9
2023-06-06 13:34:15 +01:00
thiemowmde 5c93bbfd00 Add strict types to all class properties
A good bunch of PHPDoc comments is obsolete when we use strict types.

Change-Id: Ie0692fae4d96c749e9048f7e7c6931ec97998093
2023-06-05 20:01:13 +02:00
thiemowmde 78658dbdb7 Replace misplaced substr_count
There is nothing to count here. All we want to know is if the number
is greater than zero.

Change-Id: I12a6d2413ab5b1c152817cd0f3ca3de165b3e2f7
2023-06-05 16:20:36 +02:00
Svantje Lilienthal 227b50d964 Adding extendsRef to reference model
* reads the new attribute extends from wikitext
* saves it into the reference model
* adds a message to the VE popup of an extension as a first demo
* tests will be added in a separate patch

Bug: T247922
Change-Id: If4d309c4678022642f39e21565950dc45e557d47
2023-05-30 11:56:47 +02:00
Arlo Breault c37977aad2 Assume strings for dataMw->body properties in ref handlers
The diffHandler already has this expectation.

Bug: T337036
Change-Id: I0c3a3532c655df6777407a1e0af2d9bac9495037
2023-05-19 13:28:41 -04:00
Tim Starling 5315297f38 Migrate CiteVisualEditorModule to a virtual file callback
Depends-On: I97d61b5793159cea365740e0563f7b733e0f16de
Bug: T47514
Change-Id: Iabfbb6751707813b7ec68f49b35441ab5dbb5622
2023-05-05 16:25:14 +10:00
Subramanya Sastry fc240841c4 ParsoidExtensionAPI: Simplify extTagToDOM
* leadingWS is always '' in all the uses so far and it originally
  came from Cite when we first extracted the extension API.

* It is unnecessarily confusing and serves no real purpose.

Change-Id: I5f2f0f123dc6938735c09b96a42c1923b0a49085
2023-03-28 15:44:06 -05:00
thiemowmde 6f302bf3f0 Replace string|false in CiteParserTagHooks with nullable ?string
This allows us to use strict types in one more place.

Change-Id: Id31a427b61a5eca720ec695fe219809a8e37d208
2023-02-27 15:32:04 +01:00
Subramanya Sastry a9260875ac SiteConfig: Transition away from hardcoded Cite info
* This builds on the core patch that adds this new method
  to core's SiteConfig class.

Bug: T268777
Depends-On: Ia489cd880de3df73d7cd47d1eb01971f2655effe
Change-Id: Ifd41e4275a9344474e48d013e1116b028c7775d7
2023-02-20 17:56:02 -05:00
Arlo Breault 8958246add Fix multiple ref nestings from the tag parser function
The algorithm in the grammar is fixed up and the restriction to one
level of nesting is lifted.  It was overlooked that parser functions can
be nested.  Some of the history of ref-in-ref can be found in 03bf14b0
and d634925.

Bug: T326521
Change-Id: I8e442d41f68133c1b4f16556fedaff6106e233fb
2023-01-25 01:17:09 +00:00
Isabelle Hurbain-Palatin 57fb5617a9 Fixes data-id for "follow" refs
"follow" refs are cloned, which can lead to issues with their
data-objects conflicting with the initial node: if
additional, possibly out-of-order processing is happening, we may end up
accessing/modifying the data bag. In particular, we considered adding a
call to the DOMNormalizer when serializing the reference bodies, and
this was triggering an exception when accessing the DOMDiff of the
cloned node. We are not considering this right now anymore, but cloning
the NodeData may avoid issues of this type in the future.

This patch also introduces a utility to do the clone+clone data bag in a
single method call.

Change-Id: Iccdae82dec81d488433981d764bea539609497eb
2022-11-25 17:41:33 +00:00
Subramanya Sastry ad919e3730 Parsoid CSS: Add (script-generated) language-specific counters
* These files were generated with the script in
  I3623e42d4cad7975813892a8f0f7765b74a638c5

* These styles mimic the behavior of Language::formatNumNoSeparators
  and Language::localizeSeparators used by a couple of methods.

  ReferencesFormatter:referencesFormatEntryNumericBacklinkLabel
  calls ReferenceMessageLocalizer::localizeDigits which calls
  that core Language method.

  FootnoteMarkFormatter::linkRef calls localizeDigits as well.

  - '.reference a*' CSS rules mimic linkRef localization
  - 'span[rel="mw:referencedBy" ]*' rule mimics localization
     of referencesFormatEntryNumericBacklinkLabel

* Overrode all hand-crafted language CSS files from previous
  patches. The content of some of those hand-crafted CSS files
  will end up in those wikipedia's Common.css -- specifically
  for es, fr, sv wikipedias.

  sv and es have non-canonical formatting strings that aren't
  handled by the script right now and so leaving them behind.

  I need to look at fr output more carefully, so leaving that
  behind as well. But the generated ones are more accurate when
  combined with the wiki-specific CSS files genreated by the other
  script that processes site messages.

* Some files could potentially be removed by looking at
  language fallback chains, but the redundancy is not a
  problem for now.

* Tweak the resource loader script to use "_" instead of "-"
  when looking for a resource file.

Bug: T156350
Change-Id: I000b4538bf2be681b85df5813efed083458a4281
2022-11-18 11:50:20 -06:00
C. Scott Ananian c24f5a18af Turn on phan warnings about the use of deprecated methods
MWParsoid\ParsoidServices depends on the deprecated
\MediaWiki\Parser\Parsoid\ParsoidServices in core; it is not needed
any more in MW 1.40 so it has been removed.

Change-Id: I2c4fd2e9ea2b5e2751074f1b9705e46436a32131
2022-11-04 12:36:24 -04:00
libraryupgrader a3babf81e3 build: Updating mediawiki/mediawiki-phan-config to 0.12.0
Change-Id: I70f1a3ef9e5a10edaf5a8dc9f3e272f99021b9c5
2022-10-09 19:48:09 +00:00
Isabelle Hurbain-Palatin 24b5204c28 Add 'outputHasCoreMwDomSpecMarkup' as option in extension registration
The ''outputHasCoreMwDomSpecMarkup' option in the extension registration
indicates whether the output of an extension complies to the MediaWiki
DOM Spec and should be processed as such.

The initial goal of this setting is to enable the DOMNormalizer pass for
Parsoid HTML generated in extensions, as needed by the implementation of
T309024.

We setup "Cite" as an example of configuration.

Bug: T309024
Change-Id: I26182c67ea17a4aa59414856d5f4614f2cd06c46
2022-09-29 13:25:53 +00:00
Thiemo Kreuz 8fef0dd2aa Improve two error messages
Makes it easier to find the source of the error in the wikitext.

Change-Id: I648a19881210184ab1abe9b948b5efbbbdabcdc9
2022-08-20 12:23:28 +02:00
Isabelle Hurbain-Palatin 51189f14ac Allow multivalues in rel attributes
The 'rel' attribute can be used for multiple values in the same way that
the 'typeof' attribute can. In this patch, we allow for that by
providing methods to handle these multivalues and by refactoring the
code that would only allow for single values.

Bug: T186241
Change-Id: I625b121ae77c06c519c3b578583d41dc220e3294
2022-07-19 18:46:50 +00:00
Arlo Breault 7537d185eb Improve comment around serializing autogenerated references
Follow up to Ia651b10449dc41c2cb439b33a361e8c8e482f502

Change-Id: I0a72e7fbf90a78d04144d66103cbe013e41c5163
2022-06-13 14:26:00 -04:00
Bartosz Dziewoński 40c40974d0 CiteVisualEditorModule: Disable URL loading to fix legacy debug mode
Bug: T309697
Change-Id: I3db2292ce3ec50c7c8950254fb52ba6ec92d8815
2022-06-02 20:51:16 +02:00
Thiemo Kreuz 11255770c5 Make message key parser accept more than just underscores
The best practice for message keys is to use dashes, not underscores.
This codebase is quite old and traditionally uses underscores. I
think we can make it flexible enough to work with both.

Required for Ie64f4ab.

Change-Id: I6f0584299a4f279ed929784927392eb0f72cbc80
2022-06-01 10:34:57 +02:00
jenkins-bot 20ec7ced59 Merge "Use new ResourceLoader namespace" 2022-05-24 23:22:32 +00:00
Tim Starling bb72bc65f5 Use new ResourceLoader namespace
Extensions using Phan need to be updated simultaneously with core due
to T308443.

Bug: T308718
Depends-On: Id08a220e1d6085e2b33f3f6c9d0e3935a4204659
Change-Id: Iebc5768a3125ce2b173e9b55fc3ea20616553824
2022-05-24 23:00:08 +00:00
Subramanya Sastry 74aa1ea647 Cite tests: Update html/parsoid sections with output in integrated mode
These changes are a result of adding Parsoid integrated testing support
in core.

* Some tests got new html/parsoid sections.
* Some tests got their html/parsoid output upgraded to remove bogus HTML.
* Since we are not treating the test runs to be in integrated mode,
  I removed some comments that only pertained to a test run in
  standalone mode.
* There are 8 tests that are failing in integrated mode as well and
  I've tagged 5 known ones with T307741. The other 3 failing ones are
  the responsive reference tests that have a threshold value set.
  Those failures will need investigation and require additional changes
  to the core parser test runner to pass through the config to Parsoid.

Change-Id: I370d57d45cf126f71b3666fb493a887faf6b8e0d
2022-05-11 17:40:46 +00:00
Isabelle Hurbain-Palatin 28d6a05c35 Hoisting references outside of links
When a reference is inserted in a link, Parsoid generates nested links,
which would break browser rendering. In some cases where the generated
HTML is reprocessed in later stages of Parsoid (as is the case in
T301293), it can lead to internal breakages.

In this patch, we propose to hoist references markers (and their <sup>
tag) included in links outside of that link, as next sibling to the link.
It avoids the vast majority of link nesting due to that situation, with
the following side effects:
* the <sup> tagging is now maintained around the marker tag,
* if a ref tag is in the middle of a link, it gets moved to the end of
the link (in the legacy parser, it stays where it is, but the end of the
link gets de-linked).

The selser test failures are consistent with the expected behaviour - wt2wt
does not round-trip correctly, leading to selser failures.

Bug: T301293
Change-Id: Ia39483c2112b1356e14a310fbb48baed946b5caa
2022-05-11 13:45:59 +00:00
Arlo Breault 6d9afe15dd Resolve linter test port-fixme
Implements mocking for the #tag parser function for ref tags.

The ref-in-ref linting cycles tests from 04aa4be can now be restored.

This shows that after I1b598bd359b900d1b89abf5d8105a5d131aea3d1, the
protection added in 04aa4be is no longer necessary, because we only lint
the content where it's defined.

Bug: T237463
Change-Id: I4059e32b9bea8cdc23d2112812c3f7e167e47399
2022-05-10 17:07:08 -04:00
Arlo Breault 18889348c9 Lint html stashed in data-mw of mw:Extension/references
Follow up to I1b598bd359b900d1b89abf5d8105a5d131aea3d1

This also lints the html stashed in data-mw of mw:Extension/ref, when
named references have redefinitions.

The fast fail for linting references with errors is removed since it's
no longer necessary after the work in T51538.

Bug: T214994
Change-Id: I2431b4782339a1ac41c49f7ca0ad3480c0b13bad
2022-05-02 23:37:42 +00:00
Arlo Breault 9fa2aeaba3 Only lint content defined by a specific ref
This change will fix the crasher from T301293, since all the necessary
information to locate the ref contents is contained in the first
encapsulation wrapper node, we therefore don't need to traverse into it
and potentially be tripped up by the node being closed early for having
content model violations.

By using the linkback id from the href, we're potentially linting the
content multiple times.

By using the id from the data-mw, we're only linting the content
specified by the specific ref (with the slight caveat that if two named
refs define exactly the same content, they share an id).

Note that if named refs have multiple definitions, and hence the content
ends up in data-mw, we aren't yet linting it, that's T214994.

However, by not using the linkback ids from the href, we'll need to
traverse the html that mw:Extension/References have stuffed in their
data-mw if we want to lint references defined in the references tags
themselves (see the commented out test).  This has the benefit though of
not running into the issue described in the References::lintHandler
(ie. not having the right tplInfo while traversing the content).

Bug: T301293
Bug: T214994
Change-Id: I1b598bd359b900d1b89abf5d8105a5d131aea3d1
2022-05-02 23:37:39 +00:00
Arlo Breault e3c4e96c71 Prioritize body->html over body->id when diffing refs
Just to keep things consistent since that's the precedence we use when
serializing.

Change-Id: I1456b6a21ae050d58d15620e501a14c29a64f9e3
2022-04-28 14:51:05 -04:00
Subramanya Sastry c3f34b7360 Cite: Document the Parsoid-only responsive refs threshold
* This functionality comes from 74acc71e

Change-Id: I5d985ff75670136a27d52850ea0e41da52fa3c96
2022-04-14 12:32:18 +05:30
C. Scott Ananian e7dd9b5549 Page properties should always be strings
Bug: T305158
Bug: T237531
Change-Id: Ibfd84b52057baa8e249d321ec9df612efd6a29a6
2022-03-31 14:26:40 -04:00
Subramanya Sastry 15a38973c7 html2wt: Use info level for unactionable Cite logspam caused by CX usage
Content Translation can lead to Cite references to nodes that aren't present
in the translated document. For now, I've suppressed these log entries in
Parsoid's logstash dashboard. But, there is no reason to continue emitting
these at error level given the large volume of CX pages and hence a large
volume of these non-actionable log entries.

Change-Id: I8df7e722203d7b866d987d626215bcd53b945d60
2022-03-02 20:07:35 +00:00
Subramanya Sastry 20e5117622 Minor tweak to function name in ParsoidExtensionAPI
Change-Id: Ic8fdfdc12b224460277d6a34247d20911526823a
2022-02-03 22:46:09 +00:00
jenkins-bot e0ccd777b0 Merge "Use correct Sanitizer method for id/fragment escaping" 2022-01-27 08:12:43 +00:00
C. Scott Ananian b2c1624ce0 Passing a string to ParserOutput::addModules()/addModuleStyles() is deprecated
Bug: T296123
Change-Id: Iedea960bd450474966eb60ff8dfbf31c127025b6
2022-01-11 15:23:25 -05:00
Thiemo Kreuz 7b30a165e4 Use correct Sanitizer method for id/fragment escaping
Note how only the HTML5 mode behavior changes, but nothing in
legacy mode.

Also note this does not 100% fix the issue. The esample with a
non-breaking space is still broken. But it's already much better
than before.

Bug: T298278
Change-Id: Idf50dad4219ff4c594a0cc15f63cb10fdac5ffb7
2022-01-03 16:23:45 +01:00
Timo Tijhof bee357337c ve-cite: Export citationTools as native object instead of JSON string message
* Remove the overhead of serializing and then re-parsing client-side,
  instead assign it directly as native object literal.

* Move code for array slicing to PHP.

Change-Id: Iedcc8d57d3bddd3fa32a78b4e7ecc25615d94277
2021-12-07 12:53:24 +00:00
Timo Tijhof fb2c7c37db Combine ext.cite.visualEditor.data into ext.cite.visualEditor
Rather than depending on a separate module with one line of generated
JS code, generate it as a prepended statement to the same module.

Should be a no-op.

Depends-On: I809951d34feb2dbd01b7ae0f4bd98dac7c3f6fe2
Change-Id: I5886bf9f82025048976b7750e8cb751681021fb4
2021-11-19 16:56:51 +00:00
Arlo Breault 2e4f69a492 Implement diffHandler for Cite extension
Bug: T214651
Change-Id: I64585cd89135887e095e3ab17d10c3c7d82af1c9
2021-11-16 20:33:24 +00:00
Tim Starling 0cc211d675 Make DataParsoid be a real class
Use @property to provide the types of undeclared variables to Phan and
PHPStorm, as in my NodeData patch. Declare $dp->tmp since it is
commonly used and does not affect the JSON serialized output since it is
always stripped.

I omitted the constructor, instead of following the suggestion in the
massageLoadedDataParsoid doc comment which proposed injesting a
JSON-like data structure in the constructor. I thought it would be more
efficient to have the initial property assignments inline in the calling
code. This means breaking up many object cast expressions into
individual assignments.

In IncludeOnly, the coalescing null operator was only handling the case
where $start->dataAttribs was unset, which seems unlikely. I made it so
that it checks whether $start->dataAttribs->tsr is unset.

I added strongly typed clone() methods, to preserve type information for
static analysis.

DataParsoid is the type of the data in both the DOM and in tokens. To
simplify the changes to the Token hierarchy, I removed the duplicate
definitions of the public properties $attribs and $dataAttribs.

Change-Id: I16172083e7e9bcb94601d1d6862d1d202a7e3660
2021-10-13 10:20:15 +11:00
C. Scott Ananian 30cfb7c05a Rename deprecated usage of ParserOutput::{get,set}Property()
Bug: T287216
Depends-On: Ie963eea5aa0f0e984ced7c4dfa0fd65d57313cfa
Change-Id: Id4581c6c45f9fc4690900a30d8172951bc461a1b
2021-10-08 10:12:05 -04:00
Umherirrender c0ace40aaa build: Updating mediawiki/mediawiki-phan-config to 0.11.0
Change-Id: Ifb2eec4e791fd0de0a50d8ef85e0947ab9a891e7
2021-09-21 12:13:36 -05:00
Subramanya Sastry f7bc278673 DOMUtils: Get rid of isElt, isText, isComment helpers
* Most of these are remnants from the Parsoid/JS codebase.
* This change follows the pattern we've been using everywhere
  since the port from JS->PHP.
* Also reduces instruction count by about 0.2%.

Change-Id: Ibf21104f6722c34299f03e303dc3401bf053a751
2021-09-20 22:39:38 +00:00
Tim Starling 8f3369b090 Avoid using regexes
Review regex usage, and use an alternative where possible, to improve
performance.

* Add PHPUtils::stripPrefix() and PHPUtils::stripSuffix(). Benchmark in
  doc comment.
* /foo/ -> str_contains()
* /^foo/ -> str_starts_with()
* /^f/ -> ($s[0] ?? '') === 'f'
* /foo$/ -> str_ends_with()
* /^(foo|bar)$/ -> in_array(), benchmark suggests 10x improvement
* preg_replace(/foo/) -> str_replace()
* preg_replace(/^[abc]/) -> strspn(), benchmark suggests 3x improvement.
  Curiously, it is faster without a limit for short input strings,
  although a limit presumably adds robustness.
* preg_replace(/[abc]+$/) -> rtrim()
* preg_match_all() -> substr_count()
* In DOMUtils::hasTypeOf(), use explode() instead of a regex. Validated
  by a benchmark.
* In DOMUtils::addTypeOf(), stop normalizing adjacent spaces. This
  allows us to use implode(explode()) without a filtering loop. The
  patch to Ext/Cite/References.php was to remove spaces added by this
  change. The parserTests.txt changes were a consequence of the
  References.php change.
* In LinkHandlerUtils::getHref() I allowed a single bare slash to be
  counted as a path-absolute URL since I think that was the intention of
  the original code.
* In LinkHandlerUtils::getLinkRoundTripData() I captured the portion of
  interest from the previoulinkHandlers regex instead of running a
  second regex.
* LinkHandlerUtils::linkHandler() had the regex
  /^mw:WikiLink|mw:MediaLink$/ which I think was a bug, missing
  parentheses. I fixed the bug.

The margins are pretty tight for a lot of these. Using polyfills for
str_contains() etc. might change the conclusion.

Also:

* In DOMUtils::matchTypeOf(), avoid calling hasAttribute().
  getAttribute() is documented as returning an empty string if the
  attribute does not exist.

Change-Id: I8d7bdf1bccc869b4dc17058a5822ef34968471e6
2021-09-13 23:01:45 +00:00
libraryupgrader c4872e1e8f build: Updating composer dependencies
* mediawiki/mediawiki-phan-config: 0.10.6 → 0.11.0
* php-parallel-lint/php-parallel-lint: 1.3.0 → 1.3.1

Change-Id: I89eb4438dacb2d0ab36f4986c0675eec5cb61731
2021-09-08 19:30:00 +00:00
Arlo Breault 66355c1ddc Migrate out valid follow contents after processing refs
Follow up to 47dd898

Also renames a variable to be consistent in the two places we get
contents for the ref.

Change-Id: I13e61b8911ff16549fbb0888b9c3313ed5e7701e
2021-08-27 15:00:54 -04:00
Arlo Breault 5c7c37e0c9 Reserialize processed refs if content differs
Follow up to 47dd898

Fixes the test case found in rt,
php bin/parse.php --domain ceb.wikipedia.org --pageName "Martin Van Buren" --offsetType ucs2 < /dev/null

The offsetType is necessary so that the ConvertOffsets pass runs.  The
crasher here is because the embedded html still contains the sealed ref
fragments because we've stored the unprocessed html.

Change-Id: Ic1e1c3e54433bf6d7574420c2eade1349261de0b
2021-08-27 15:00:37 -04:00
Subramanya Sastry 0d26fd19d5 Cite: Rename functions pushing/popping embedded content flags
Change-Id: Ie8736fcc139caba467209b7ba57daaa8f53bc18a
2021-08-26 11:43:52 -05:00
Arlo Breault 47dd8989a7 Don't process ref-in-ref as embedded, unless content differs
Restores linkbacks for ref-in-ref.

Follow up to 568034a where it's noted that it's fine to maintain
linkbacks for ref-in-ref, as long as the ref isn't a named ref that's
trying to redefine the contents for that name, in which case we embed
the contents.

A test case for this can be,

```
<ref name="hiho">off to work</ref>
{{#tag:ref|<i>we go <ref name="ohno">ohno</ref></i>|name="hiho"}}
{{#tag:ref|<i>we go <ref name="ohno2">ohno2</ref></i>|name="test"}}
```

The linkback to #cite_ref-ohno2_3-0 is present while continuing to
suppress the dangling linkback to #cite_ref-ohno_2-0, since that's in
embedded content.  On master, both linkbacks are unnecessarily
suppressed.

Bug: T289331
Change-Id: Ifcf7464e86a4408f5dd9e2fd6d3aa47a0670ca49
2021-08-26 16:41:02 +00:00
Arlo Breault d0e1637d22 Move content differ check up higher
This will be helpful in a subsequent patch where we make use of that
data while processing refs in refs.  Content differing implies that
we'll be embedding it for roundtripping, rather than putting in the dom.

Change-Id: I7bd1d4c503fc58f862960bec82ca514fc29d7eff
2021-08-26 16:38:58 +00:00
Arlo Breault 50dfe518cc Only call ReferencesData::add when adding
This moves determining if we already have a reference created for a
named ref outside of that function, which is helpful for making use of
the cached html for that ref earlier.

Change-Id: Ie416bd95b980f9f95111d7e420945f40e2ada747
2021-08-26 16:37:36 +00:00
C. Scott Ananian 187de4b769 The ::querySelectorAll() and ::getElementsBy* helpers don't always return array
The standard type for these returns is NodeList and HTMLCollection, which
are almost *but not quite* the same as an array.  In two places we got a
little complacent and assumed our non-standard DOMCompat workarounds would
always return arrays.  Tweaked the types of DOMCompat to report that they
return an `iterable`, which is a PHP7.1 "pseudo-type" that unifies
arrays and \Traversable types like HTMLCollection/NodeList.  This
allows phan to catch places where we slip up and assume an array type
return.

It does introduce a new wrinkle, though, since there is no simple way
to turn an iterable into an array.  We're using a simple
`iterable_to_array` helper function for this.

Change-Id: I35bdeb3afa30ef5182e71733a0a606aadcafb435
2021-07-31 03:50:07 +00:00
C. Scott Ananian a1d0fdd776 Allow Node::getAttribute() to return null
In PHP's DOM extension, one of the legacy bugs is that
DOMNode::getAttribute() can never return `null` (to indicate that the
attribute is missing), instead it returns an empty string in that
case.  This isn't (modern) spec-compliant behavior (it's a leftover
from ancient times) and we had to watch this carefully when porting
from JS.

In the time since the port, we've written new code and embedded this
assumption that DOMNode::getAttribute() will never return null into
the new code we've written.  Fix this.  Always use `getAttribute(...)
?? ''` (unless we're just doing an equality test against a non-empty
string, or the code is preceded by a `hasAttribute` test) so that our
code will work whether or not getAttribute returns null for a missing
attribute.

Change-Id: If33200e1053b2dd79abb5dfb3808c05ff3a0bbba
2021-07-30 20:34:47 +00:00
jenkins-bot e584c5cf3f Merge "Refactor newline logic for auto-generated <references> sections" 2021-07-30 14:17:49 +00:00
C. Scott Ananian fd3597cd39 Add class alias file to allow swapping in Dodo for DOMDocument
Change-Id: I56c10d2f4283e9e7b57bf722208fefab007cdf45
2021-07-23 12:20:06 -04:00
libraryupgrader 0af1a039b0 build: Updating mediawiki/mediawiki-codesniffer to 37.0.0
Change-Id: Ia399fafc5280aab1c0cfc129529a822e8d8f8382
2021-07-22 12:34:32 +00:00
DannyS712 55cc7c2828 Remove documentation that repeats the code
Mostly comments along the lines of "{classname} constructor"
in the doc block for the __construct method.

Change-Id: I67ffe070985dc75a5d817b1b5ac97b529d7ab4b8
2021-06-02 09:57:36 +00:00
Thiemo Kreuz 0cc1ddeccb More robust property initialization in ReferenceStack
I tried many things, but wasn't able to reproduce the error
described in T283755. What probably happens goes like this:
* Somehow $this->refs[$group] is initialized, but
  $this->groupRefSequence[$group] is not.
* There are not many places in the code where this can
  happen. There are a few suspicious lines in rollbackRef(),
  but they are all guarded. The only problematic place is in
  appendText().
* This problematic line is only called for <ref> in
  <references>.
* Somehow a <ref> is valid enough to make it to appendText(),
  but not valid enough to make it to pushRef().
* The next time another <ref> is added to the same group, it
  appears like the group already exists ($this->refs[$group]
  is set), but $this->groupRefSequence[$group] is missing.

I was unable to find a wikitext example that would behave like
this.

This patch just makes sure the initialization is done but
doesn't care why it was missing. The following code is fine
with an existing ref that contains nothing but text (which is
how appendText() leaves it behind).

Bug: T283755
Change-Id: I36ac56ef6ed98676a3e8f430a796826351a5f4e9
2021-05-31 14:10:07 +02:00
Aaron Piotrowski 81630c4267 Upgrade to mediawiki/mediawiki-codesniffer 36
Change-Id: I103a662d0af77cafa46cf6445e1580aabd005f31
2021-05-04 10:25:25 -05:00
libraryupgrader 3d79d3518b build: Updating composer dependencies
* mediawiki/mediawiki-codesniffer: 35.0.0 → 36.0.0
* php-parallel-lint/php-parallel-lint: 1.2.0 → 1.3.0

Change-Id: I409d4c564a02f537dbd1ceec1576410aefe0ea64
2021-05-04 01:29:20 +00:00
Arlo Breault be829c15b0 Check for multiples doesn't apply to follows
Follow up to 7bd9f87

Bug: T276388
Change-Id: I68ab87702b967e870c432564b54d86bcbf914174
2021-03-03 18:07:17 -05:00
Adam Wight 1a6fb7b0f9 Coerce type to please linter
Change-Id: I4a196cfc5b7191306446d187ca3fe4358afc24c5
2021-02-24 18:46:18 +00:00