Commit graph

70 commits

Author SHA1 Message Date
Bartosz Dziewoński 5ad0a7bfcb Add comments about unwrapping Parsoid sections
Follow-up to c9deff179f.

Change-Id: Ib32442020749103012ec508d870d40b7b96f05a0
2023-01-27 17:07:56 +01:00
Bartosz Dziewoński c9deff179f ApiDiscussionToolsEdit: Unwrap Parsoid sections before parsing
Otherwise, when a transclusion covers a section boundary, both whole
sections are considered to be transcluded.

We already use this method everywhere else (by way of
HookUtils::parseRevisionParsoidHtml), and VisualEditor also applies
the same transformation (mw.libs.ve.unwrapParsoidSections) before
opening the page for editing.

Bug: T327704
Change-Id: I9d8288e2740d816edb9cbc01d7e5642d52c610d3
2023-01-26 19:26:14 +00:00
Bartosz Dziewoński af68c835bb Update exception handling for new code conventions
Change code to match the documented consensus formed on T321683:
https://www.mediawiki.org/wiki/Manual:Coding_conventions/PHP#Exception_handling

* Do not directly throw Exception, Error or MWException
* Document checked exceptions with @throws
* Do not document unchecked exceptions

For this extension, I think it makes sense to consider DOMException an
unchecked exception too (in addition to the usual LogicException and
RuntimeException).

Depends-On: Id07e301c3f20afa135e5469ee234a27354485652
Depends-On: I869af06896b9757af18488b916211c5a41a8c563
Depends-On: I42d9b7465d1406a22ef1b3f6d8de426c60c90e2c
Change-Id: Ic9d9efd031a87fa5a93143f714f0adb20f0dd956
2023-01-22 18:17:11 +00:00
Bartosz Dziewoński e72f58ca78 Remove some redundant PHPDoc comments
Inspired by this Wikitech-l discussion:
https://lists.wikimedia.org/hyperkitty/list/wikitech-l@lists.wikimedia.org/thread/NWXPNHRNLEVXHSWX33H473OAWQP6CDOA/

To keep this simple for now, I am only removing redundant PHPDoc
comments on constructors, and only when all the documentation for
parameters completely duplicates type hints.

More could be done, but that can happen later when we have better
tooling. Redundant comments on constructors that take a dozen services
are by far the most annoying for me and I want them gone now.

Change-Id: I86cbf7d6e48035cfa06f780c8fb1b02e68709a0c
2022-11-29 18:47:18 +00:00
Bartosz Dziewoński 469b7720af Place replies outside transclusions, disallow replying to transcluded comments
Bug: T313100
Change-Id: I3993c96ec8d3d0add33d779860d158327985107d
2022-11-14 19:17:18 +01:00
Bartosz Dziewoński 433e57394c Use PHP 7.4 property types
Change-Id: I788db64f0c0c00894d77256b7f016d44eda4bbb1
2022-10-28 21:56:38 +02:00
Bartosz Dziewoński a46af344dc Use RevisionLookup methods instead of ApiParsoidTrait methods
Part of my secret plan to delete ApiParsoidTrait.

* Inject RevisionLookup into ApiVisualEditor
* Use RevisionLookup::getRevisionById instead of ApiParsoidTrait::getValidRevision
* Use RevisionLookup::getRevisionByTitle instead of ApiParsoidTrait::getLatestRevision
* Use standard MediaWiki error messages

Change-Id: I7244ee4916fb011fad5faa1d9f837e83f6ac2dc1
2022-10-09 18:17:32 +00:00
Bartosz Dziewoński 2679fa3b04 Replace VisualEditor ParsoidHelper with ParsoidClient
Depends-On: I21c4a8cd86f8d085e75a601ed6d2509dedd75d42
Change-Id: I9533d7c05047c2d873c8c6ee5944df965e3b379d
2022-09-19 18:00:14 +02:00
Bartosz Dziewoński dc07d802a9 Inject services into API classes
This replaces the remaining uses of MediaWikiServices::getInstance()
in API classes.

Change-Id: I5f602a7e7b5a21a7d40023571018604528ea8edb
2022-09-03 00:23:41 +02:00
Bartosz Dziewoński 9b10b9c282 ApiDiscussionToolsEdit: etag is optional
Bug: T316234
Change-Id: If283af8e3f96c12d65e75dc88343a80f20e0f3ee
2022-08-25 21:18:09 +02:00
Bartosz Dziewoński d223626585 Add signature on separate line if wikitext comment ends with a list
Bug: T263217
Change-Id: Idd15a9add798368493ae7af5270f972895470de9
2022-08-06 16:08:13 +02:00
Bartosz Dziewoński 880f9755e0 Separate ContentThreadItem and DatabaseThreadItem etc.
Rename ThreadItem to ContentThreadItem, then create a new ThreadItem
interface containing only the methods that we'll be able to implement
using only the persistently stored data (no parsing), then create a
DatabaseThreadItem. Do the same for CommentItem and HeadingItem.

ThreadItemSet gets a similar treatment, but it's basically only for
Phan's type checking. (This is sad.)

Change-Id: I1633049befe8ec169753b82eb876459af1f63fe8
2022-07-04 23:35:50 +02:00
Ed Sanders 639fabf647 Pass useskin/mobileformat when previewing/refreshing page contents
These are required to render content correctly on mobile.

Change-Id: I421e1f172e208c4bf9d50eee68f1b0220a78dfbe
2022-06-27 17:15:28 +01:00
Ed Sanders 904f1c2f25 Api: Make type/title required args of previewMessage
Change-Id: I0a0d9bfe3510d9bd6076408ba3819c82cd44ec0c
2022-06-27 12:50:10 +01:00
gerritbot 731d530534 Fix usage of ApiBase::PARAM_* deprecated constants
The ones that are replaced with ParamValidator

Bug: T275455
Change-Id: I9ed72118c05cff879142ed89a3906c077413d25b
2022-04-04 00:40:51 +00:00
gerritbot 15fd46f064 Update use of deprecated constants in getAllowedParams()
Replacing StringDef ones

Bug: T275455
Change-Id: I9d1c7700f7c48112d9b2479733cc0fcd7ab6f7fe
2022-04-03 22:03:42 +02:00
jenkins-bot eb275b7810 Merge "Use namespaced VisualEditor traits and classes" 2022-03-29 21:01:28 +00:00
Reedy 400d6227b8 Use namespaced VisualEditor traits and classes
Change-Id: I06eefd96068675607ed66a8a307d86c24d21f916
2022-03-29 20:32:44 +01:00
Ed Sanders 43a04ab7ba ApiDiscussionToolsEdit: Use CommentParser to get name/title of last topic
This is a bit slower, but reduces logic duplication, and doesn't rely
on data-mw-comment-name, which we want to get rid of.

Change-Id: I79dc0937f3fc13677deb55b413796b54b747790e
2022-03-28 22:30:59 +01:00
Ed Sanders 5cc06baf2e ApiDiscussionToolsEdit: Add $preview- prefix to variables used in parsing preview
Avoids conflict with variables used later.

Change-Id: I0b74d4d041ae57e2ba18660da0871760bf5d5561
2022-03-28 22:30:41 +01:00
Ed Sanders 8bb41c9fb2 ApiDiscussionToolsEdit: Use existing $this->commentParser reference
Change-Id: I3e2b907103372e05de9035ab8058c0766cf39819
2022-03-28 18:30:36 +01:00
Bartosz Dziewoński c7723baf72 CommentParser: Replace uses of Title with TitleValue
Another small step towards removing the reliance on global state.

Change-Id: Ifb4a5bcbef6606d02f1c7aa7385d72822cb0bad0
2022-03-18 18:24:34 +00:00
Bartosz Dziewoński 4134a80fdc Fix handling of disabled 'mobileformat'
Boolean parameters must be omitted completely if they're false.

Bug: T303262
Change-Id: Icc1bb4f557ce4de137f3e40b186074190f194dea
2022-03-08 14:00:22 +01:00
jenkins-bot 3c91a800ed Merge "Improve detecting already signed comments" 2022-03-02 14:14:13 +00:00
Ed Sanders d1b1745908 Avoid undefined index for mobileformat
Bug: T302344
Change-Id: I32844c61eeaf05771df2dd8b88e3c98f069cfada
2022-02-22 21:24:19 +00:00
Bartosz Dziewoński 0ecc8a4c05 Improve detecting already signed comments
Previously, we required a signature at the end of the comment.
This was a pretty rough heuristic that did not correctly handle
many comments that we would consider entirely properly signed
in CommentParser (e.g. comments wrapped in formatting like
<small>…</small>, comments with a post-scriptum or in parentheses,
or comments generated by various templates).

Now we process the user input using the same code that adds reply
links, and only add a signature when we detect that there really
isn't a signature (including template-generated), or if the signature
is in the wrong place and would result in the reply link showing up
in the wrong place as well (not at the end of the comment).

Bug: T278442
Bug: T268558
Bug: T278355
Bug: T291421
Bug: T282983
Change-Id: I46b6110af328ebdf93b7dfc2bd941e04391a1599
2022-02-21 21:21:26 +00:00
jenkins-bot aeabff63c9 Merge "Split off ThreadItemSet from CommentParser" 2022-02-21 16:33:58 +00:00
Bartosz Dziewoński 8e44b43df0 Split off ThreadItemSet from CommentParser
Goal:
-----
Finishing the work from Iadb7757debe000025e52770ca51ebcf24ca8ee66
by changing CommentParser::parse() to return a data object, instead of
the whole parser.

Changes:
--------
ThreadItemSet.php:
ThreadItemSet.js:
* New data class to access the results of parsing a discussion. Most
  methods and properties are moved from CommentParser with no changes.

CommentParser.php:
Parser.js:
* parse() returns a new ThreadItemSet.
* Remove methods moved to ThreadItemSet.
* Placeholder headings are generated slightly differently, as we process
  things in a different order.
* Grouping threads and computing IDs/names is no longer lazy. We always
  needed IDs/names anyway.
* computeId() explicitly uses a ThreadItemSet to check the existing IDs
  when de-duplicating.

controller.js:
* Move the code for turning some nodes annotated by CommentFormatter
  into a ThreadItemSet (previously a Parser) from controller#init to
  ThreadItemSet.static.newFromAnnotatedNodes, and rewrite it to handle
  assigning parents/replies and recalculating legacy IDs more nicely.
* mw.dt.pageThreads is now a ThreadItemSet.

Change-Id: I49bfe019aa460651447fd383f73eafa9d7180a92
2022-02-21 16:22:32 +00:00
jenkins-bot b6e930a101 Merge "Change CommentParser into a service" 2022-02-20 14:01:11 +00:00
Bartosz Dziewoński 4613ae78e7 Change CommentParser into a service
Goal:
-----
To have a method like CommentParser::parse(), which just takes a node
to parse and a title and returns plain data, so that we don't need to
keep track of the config to construct a CommentParser object (the
required config like content language is provided by services) and
we don't need to keep that object around after parsing.

Changes:
--------
CommentParser.php:
* …is now a service. Constructor only takes services as arguments.
  The node and title are passed to a new parse() method.
* parse() should return plain data, but I split this part to a separate
  patch for ease of review: I49bfe019aa460651447fd383f73eafa9d7180a92.
* CommentParser still cheats and accesses global state in a few places,
  e.g. calling Title::makeTitleSafe or CommentUtils::getTitleFromUrl,
  so we can't turn its tests into true unit tests. This work is left
  for future commits.

LanguageData.php:
* …is now a service, instead of a static class.

Parser.js:
* …is not a real service, but it's changed to behave in a similar way.
  Constructor takes only the required config as argument,
  and node and title are instead passed to a new parse() method.

CommentParserTest.php:
parser.test.js:
* Can be simplified, now that we don't need a useless node and title
  to test internal methods that don't use them.

testUtils.js:
* Can be simplified, now that we don't need to override internal
  ResourceLoader stuff just to change the parser config.

Change-Id: Iadb7757debe000025e52770ca51ebcf24ca8ee66
2022-02-19 19:51:57 +01:00
Ed Sanders d1e56c7dc5 ApiDiscussionToolsEdit: Pass useskin & mobileformat to ApiVisualEditorEdit
From there they are passed on ApiParse.

Change-Id: I8411379694961868c2540c42739fb4f5d7b61972
2022-02-18 16:40:38 +00:00
Ed Sanders 6d655dee0a Remove DiscussionToolsEnableTopicSubscriptionBackend config
This is now deployed on all wikis, and going forward I don't think
we need to make this configurable.

Change-Id: I231976267ba6cdfeec622efaa15983a84c330649
2022-02-04 18:22:10 +00:00
Bartosz Dziewoński 165ca9b847 Improve CommentModifier::addReply() API for re-use and testing
Goal: To be able to re-use or test the transformations we previously
  performed in addWikitextReply() / addHtmlReply(), without requiring
  a Comment object or adding the result as a reply.

Change-Id: I040c4be9b6b9bddba661f30fd0566f8850673074
2022-02-03 21:12:48 +00:00
Ed Sanders 34011b7a07 Parser: Pass in title of page being parsed
Will be used to parse selflinks in the future.

Change-Id: I2bc29d1c5c69cb6309f582f162f9af7d96ce8913
2022-01-12 21:17:59 +00:00
Ed Sanders d2443f7785 Rename DiscussionToolsAutoTopicSubWhere to DiscussionToolsAutoTopicSubEditor
Better describes that we are checking the editor used to make
the edit, rather than descibing some virtual "location".

Change options to 'discussiontoolsapi' and 'any'.

Change-Id: I3024517e70ed61c738e4bf46a2ac7b58c975d98a
2021-12-15 16:24:35 +00:00
Bartosz Dziewoński 8d3cf30f60 Automatic topic subscriptions (only for reply tool and new topic tool)
Bug: T284836
Change-Id: I0f98c26c997f66b7a43cd4b971fe72a37d12db5d
2021-11-15 22:45:42 +01:00
Ed Sanders 6022982eb9 Generate form tokens in the client to prevent double posting
Store used tokens in session storage.

Bug: T286409
Change-Id: I88c81689d9cf3447f6cc77c849a84d52c8b2c0db
2021-10-29 00:17:14 +01:00
libraryupgrader 26b69d2c70 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: I76996ed939d706739d2094077c64eeca6f51126a
2021-09-08 23:14:53 +00:00
Bartosz Dziewoński 8de8d80cde Deal with document body consistently
Use `DOMCompat::getBody( ... )` as a nicer getter than
`->getElementsByTagName( 'body' )->item( 0 )`.

Remove overly defensive checks and redundant annotations on its
return value. Since we're dealing with HTML documents throughout,
the document body is guaranteed to exist.

We previously needed some of them to convince Phan when it thought
the body may be null, but this seems to no longer be needed.

Change-Id: If7aee7b6adbfa78269c7ba28b26a6eaa21fe935b
2021-08-03 15:12:55 +02:00
C. Scott Ananian 25272e7a4a Don't refer directly to PHP dom extension classes; avoid nonstandard behavior
These changes ensure that DiscussionTools is independent of DOM
library choice, and will not break if/when Parsoid switches to an
alternate (more standards-compliant) DOM library.

We run `phan` against the Dodo standards-compliant DOM library,
so this ends up flagging uses of non-standard PHP extensions to
the DOM.  These will be suppressed for now with a "Nonstandard DOM"
comment that can be grepped for, since they will eventually
will need to be rewritten or worked around.

Most frequent issues:

* Node::nodeValue and Node::textContent and Element::getAttribute()
can return null in a spec-compliant implementation.  Add `?? ''` to
make spec-compliant results consistent w/ what PHP returns.

* DOMXPath doesn't accept anything except DOMDocument.  These uses
should be replaced with DOMCompat::querySelectorAll() or similar
(which end up using DOMXPath under the covers for DOMDocument any way,
but are implemented more efficiently in a spec-compliant
implementation).

* A couple of times we have code like:
  `while ($node->firstChild!==null) { $node = $node->firstChild; }`
and phan's analysis isn't strong enough to determine that $node is still
non-null after the while.  This same issue should appear with DOMDocument
but phan doesn't complain for some reason.

One apparently legit issue:

* Node::insertBefore() is once called in a funny way which leans on
the fact that the second option is optional in PHP.  This seems to be
a workaround for an ancient PHP bug, and can probably be safely
removed.

Bug: T287611
Bug: T217867
Change-Id: I3c4f41c3819770f85d68157c9f690d650b7266a3
2021-07-30 18:15:40 -04:00
C. Scott Ananian 5203d30ea6 Use DOMCompat::newDocument() to create a new Document
For compatibility with Parsoid's document abstraction (Parsoid may
switch to an alternate DOM library in the future), don't explicitly
create a new document object using `new DOMDocument`; instead use
the Parsoid wrapper `DOMCompat::newDocument()`.  This ensures that
the Document object created will be compatible with Parsoid.

There are a number of other subtle dependencies on the PHP `dom`
extension in DiscussionTools, like explicit `instanceof` tests; those
will be tweaked in a follow-up patch
(I3c4f41c3819770f85d68157c9f690d650b7266a3) since they do not affect
correctness so long as Parsoid is aliasing Document to a subclass of
the built-in DOMDocument.  Similarly, the Phan warnings we suppress
do not cause runtime errors (because of the fixes included in
c5265341afd9efde6b54ba56dc009aab88eff83c) but phan will be happier
once the follow-up patch lands and aligns all the DOM types.

Bug: T287611
Depends-On: If0671255779571a91d3472a9d90d0f2d69dd1f7d
Change-Id: Ib98bd5b76de7a0d32a29840d1ce04379c72ef486
2021-07-30 18:15:11 -04:00
jenkins-bot ffcbce4f2d Merge "Pass whole context to internal action API requests" 2021-05-26 16:46:13 +00:00
Bartosz Dziewoński fffffe8f8f Pass whole context to internal action API requests
Otherwise, the global context is used (RequestContext::getMain()),
which is undesirable when you're building a rubegoldbergian
contraption and we're already inside an internal action API request
with a fake context.

Change-Id: I01daf8dc70b5751bc1e157fe598988cd5d3219e5
2021-05-26 12:55:33 +00:00
Bartosz Dziewoński 0ad1ee3cff Use the edited page's title for magic words in API error messages
Bug: T247661
Depends-On: Ic3d70efc23744ef6e90abc445f3babebf45c4697
Change-Id: I8b0ad22232ef14f59887c24d5727271f46350eb9
2021-05-21 18:03:56 +02:00
Bartosz Dziewoński 566d4d92db Trim wikitext for preview
And change some uses of trim() to htmlTrim() for consistency.

Bug: T276612
Change-Id: I1f55f4793af8f3dda597288c9e3427abe31c37c9
2021-03-24 18:53:41 +01:00
Bartosz Dziewoński 42ce942c86 Introduce comment "names" to identify comments across revisions/pages
The existing comment IDs can't be used to find the same comment on
a different revision or page (when it's transcluded), because they
depend on the comment's parent and its position on the page.

Comment names depend only on the author and timestamp. The trade-off
is that they can't distinguish comments posted within the same minute,
or in the same edit, so we will still need the IDs sometimes.

Prefer using comment names when replying, if they're not ambiguous.
This fixes T273413 and T275821.

Heading names depend on the author and timestamp of the oldest comment.
This way we don't have to detect changes to the heading text, but we
can't distinguish headings without any comments.

Bug: T274685
Bug: T273413
Bug: T275821
Change-Id: Id85c50ba38d1e532cec106708c077b908a3fcd49
2021-03-23 16:08:42 +00:00
Bartosz Dziewoński 03f22c538e Improve API parameter documentation
These messages are used on Special:ApiSandbox.

Change-Id: I07809d3f9402d30c5d2d364e0634bd020f86f64f
2021-02-13 00:08:31 +01:00
Bartosz Dziewoński f21e6dfc7c New error message when the page doesn't exist
The previous confusing error was coming from the getLatestRevision() call.

Bug: T273068
Change-Id: I5bb53c875bb08f6fb087875a6e55fb033d182056
2021-01-27 16:41:03 +01:00
Bartosz Dziewoński ca9db69b40 Fix check for null edits
Yet again, the CAPTCHA being neither an error nor a success bites us.

Bug: T272922
Change-Id: I5ef85df452638ad3208e7a1cadad44c83237dbfe
2021-01-26 01:06:31 +01:00
Bartosz Dziewoński 1c8ca53c92 Catch when no changes are actually saved when posting a comment
Bug: T268069
Change-Id: Ib9c136d846668335884a242322d5b0d4e038c6b1
2021-01-08 22:39:18 +00:00