Jump to content

Flow/Architecture/API: Difference between revisions

From mediawiki.org
Content deleted Content added
SPage (WMF) (talk | contribs)
update
Mark historical
 
(35 intermediate revisions by 9 users not shown)
Line 1: Line 1:
{{historical|This page was the original design for the Flow API. The documentation of it actually now works is at [[Extension:StructuredDiscussions/API]]}}
:''See also: [[Extension:Flow/API]]''
Flow has an extensive API that's heavily exercised by one client – the JavaScript on Flow boards – as you can see if you interact with a Flow board with JavaScript enabled and watch the API requests in your browser's developer tools > Network tab.
Flow has an extensive API that's heavily exercised by one client – the JavaScript on Flow boards – as you can see if you interact with a Flow board with JavaScript enabled and watch the API requests in your browser's developer tools > Network tab.


Most URL (HTTP <code>GET</code>) and form (HTTP <code>POST</code>) actions are mirrored in the Flow API.
Most URL (HTTP <code>GET</code>) and form (HTTP <code>POST</code>) actions are mirrored in the Flow API.

If you have questions, feel free to ask on {{irc|wikimedia-collaboration}} or one of the other places given at [[Flow#Contact and links]].


== General comments ==
== General comments ==
Flow pages are not wiki pages.
Flow pages are not wikitext pages.
* they use a different ContentModel
* they use a different ContentModel
* they store revisioned content differently
* they store revisioned content differently
Consequently, many typical wiki page API requests will not work, such as <code>[[ResourceLoader/Default_modules#mw.Api.23newSection|newSection]]</code> ({{bug|57989}}).
Consequently, many typical wikitext page API requests, such as <code>[[ResourceLoader/Default_modules#mw.Api.23newSection|newSection]]</code>, will not work ({{bug|57989}}).


The Flow team has considered mimicking the page API, but there's no one right answer to where to modify. In general you can either
The Flow team has considered mimicking the wikitext APIs, but there's no one right answer to where to modify. For example should a page edit update the board header, or should it create a new topic?


== action=query ==
== action=query ==
; <code>prop=info</code>
: returns <code>"contentmodel": "flow-board"</code> if it's a Flow board. However, per below, you need to also check that the namespace is not 2600 (NS_TOPIC) currently. Unlike prop=flowinfo, this same API request can also be used for other purposes (e.g. checking protection status).

; <code>prop=flowinfo</code>
; <code>prop=flowinfo</code>
: returns <code>enabled</code> key if Flow is enabled on a page, see {{gerrit|111255}}. No parameters. As of September 2014 this returns enabled for Flow Topic:''UUID'' pages as well as Flow boards.
: returns <code>enabled</code> key if Flow is enabled on a page, see {{gerrit|111255}}. No parameters. As of September 2014 this returns enabled for Flow Topic:''UUID'' pages as well as Flow boards.
: (You can also check if the content model of a page is <code>'flow-board'</code>. Both Flow boards and individual Topic:''UUID'' pages have this content model.)
: You can also check if the content model of a page is <code>'flow-board'</code>. As of November 2014 both Flow boards and individual Topic:''UUID'' pages have this content model, {{bug|71196}}.)


; ''obsolete'' <code>list=flow</code>
; ''obsolete'' <code>list=flow</code>
Line 23: Line 28:


The read and write API consists of submodules of <code>action=flow</code>, you can see them on the API page [//www.mediawiki.org/w/api.php www.mediawiki.org/w/api.php].
The read and write API consists of submodules of <code>action=flow</code>, you can see them on the API page [//www.mediawiki.org/w/api.php www.mediawiki.org/w/api.php].

Unfortunately [[Special:ApiSandbox]] is not aware of submodules ({{bug|64008}}), so you can't drive this action=flow API from it.


=== Read API ===
=== Read API ===
Line 36: Line 39:


=== Write API ===
=== Write API ===
You need to decide if your goal is to add something to the Flow board header, create a new topic, reply to an existing topic, or some other interaction.
You need to decide if your goal is to reply to an existing topic, create a new topic, add something to the Flow board header, or some other interaction.

If you want to to the Flow header, XXX


==== Sketch of replying ====
==== Sketch of replying ====
Line 62: Line 63:
* Obviously you get the topic and post to respond by making API queries, rather than hardcoding these numbers.
* Obviously you get the topic and post to respond by making API queries, rather than hardcoding these numbers.


Note that a wiki may store Flow content with <code>$wgFlowContentFormat</code> 'html' or 'wikitext', but in any edit submission you must (?) submit wikitext.
Note that a wiki may store Flow content with <code>$wgFlowContentFormat</code> 'html' or 'wikitext', but in any edit submission you must submit wikitext.


If successful, the server responds with the <code>post-id</code> of the new post, and the HTML to insert into the DOM.
If successful, the server responds with the <code>post-id</code> of the new post, and the HTML to insert into the DOM.
Line 70: Line 71:
On WMF wikis, Flow stores post content as HTML with embedded Parsoid markup so that the original wikitext can be reconstituted for editing. ''This will not be the case on all wikis, others might store as wikitext.''
On WMF wikis, Flow stores post content as HTML with embedded Parsoid markup so that the original wikitext can be reconstituted for editing. ''This will not be the case on all wikis, others might store as wikitext.''


=== Block requests ===
When you make a "Block" API request such as <code>&submodule=new-topic</code>, the API normally returns a <code>flow.''new-topic''.result.topiclist</code> containing updated information about the top 10 topics on the board, including their HTML. This allows the Flow board to redisplay the board contents, but it can be a lot of data.

If you are just trying to add a topic and you're ''not'' interested in the board contents, you should pass the flag <code>ntmetadataonly=true</code> ([[gerrit:180588]])

Flow returns the metadata in a top-level <code>flow.newtopic.committed</code> key in the API response. For consistency the information about the new content is returned as topic-page, topic-id, topic-revision-id, post-id, header-post-id (but see [[phab:T84954]]), etc.

=== Editing the board header ===
If you want to update the Flow board header, it's similar
* make a GET request <code>action=flow, submodule=view-header, vhformat=wikitext</code> to get the current contents. This will return a <code>flow.view-header.result.header.revision</code> including a UUID of the most recent version of the header in <code>revisionId</code>, and its contents in <code>content.content</code>.
* make a POST request, <code>action=flow, submodule=edit-header</code>. Set <code>ehprev_revision</code> to the revision returned by the GET request and set <code>ehcontent</code> to your updated wikitext.

The update may fail with <code>error.code = 'prev_revision'</code> if someone else updated this board's header between the two requests; if so the return has a <code>prev_revision.revision_id</code> giving the new latest revision, so you can retry with that. However, you should avoid clobbering the previous editors' work (e.g. by presenting an edit conflict form, or re-running the algorithm if a bot is making the requests, etc.).


=== flow-parsoid-utils ===
=== flow-parsoid-utils ===
Requests conversion between html and wikitext. If the wiki isn't running Parsoid, conversions from html to wikitext will fail.
Requests conversion between html and wikitext. If the wiki isn't running Parsoid, conversions from html to wikitext will fail.


This is meant to be an "internal" API for Flow to be able to communicate with Parsoid client-side. If you want to convert wikitext<-->html using Parsoid, you should use it's own [[Parsoid#The_Parsoid_web_API|API]].
This is meant to be an "internal" API for Flow to be able to communicate with Parsoid client-side. If you want to convert wikitext<-->html using Parsoid, you should use its own [[Parsoid#The_Parsoid_web_API|API]].


== Future API changes ==
== Future API changes ==
Eventually move to a [[Requests_for_comment/Services_and_narrow_interfaces|service-oriented architecture]].
Eventually move to a [[Requests_for_comment/Services_and_narrow_interfaces|service-oriented architecture]].
Might look like [[Requests_for_comment/Content_API|proposed Content API]].
Might look like [[Requests_for_comment/Content_API|proposed Content API]].

External clients should be able to use [[OAuth]] to authorize bots to make Flow changes on their behalf.
This should work for the most common actions, such as posting a new topic and replying.
Anything requiring the Flow rights (e.g. flow-hide, flow-lock, flow-delete, flow-edit-post) will likely be forbidden via OAuth until it's properly exposed, {{bug|72998}}.

=== Should existing API calls that interact with a page "just work" on a Flow board? ===
What should happen to regular page API requests made to a Flow-enabled page is [[phab:62808]].
Probably not in general, since the caller really has to decide whether the change is to the board header or a new topic

But perhaps some variations where intent is obvious:
* Specifically <code>action=edit&section=new</code> on a talk page means create a new topic, so why not have that work on a Flow board ([[phab:T59989]], status {{as of |2014|03}} is "Declined")
* Likewise <code>action=edit&appendtext=''wiki+text+to+add''</code> and <code>prependtext=...</code>could append and prepend to a Flow board header ([[phab:T85197]])
*: These may not work as well. For <code>appendtext</code> especially, there is no way to tell whether they are intending to add to the header, add a new topic, or add content to the end of the last topic.


=== Other API issues ===
=== Other API issues ===
* Should existing API calls to edit to a page "just work" on a Flow-enabled "page"?
* What should happen to regular page API requests made to a Flow-enabled page, {{bug|60808}}.


* post editing rights for bots.
* post editing rights for bots.
** ordinarily only the poster can edit his or her reply, unlike a regular wiki page.
** ordinarily only the poster can edit their reply, unlike a regular wiki page.


=== Missing APIs? ===
=== Missing APIs? ===
* Do people need an API to enumerate pages / namespaces that are using Flow?
=== API to enumerate pages / namespaces that are using Flow? ===
** currently, would just return values of <code>$wgFlowOccupyPages</code> and <code>$wgFlowOccupyNamespaces</code>. When a Flow board becomes a content type, the former could be replaced by a query for pages with the <code>flowboard</code> contentmodel, though there does not seem to be a generator for this.
Currently, would just return values of <code>$wgFlowOccupyPages</code> and <code>$wgFlowOccupyNamespaces</code>. Now that a Flow board has a special content type, the former could be replaced by a query for pages with the <code>flow-board</code> contentmodel, though there does not seem to be a generator for this.


* API to generate a link to a post/topic/board given a UUID
=== API to generate a link to a post/topic/board given a UUID? ===
{{Flow file|file=includes/UrlGenerator.php}} can fabricate all kinds of URLs. May API queries return these URLs in the <code>links</code> object for each topic and post, but clients can't create their own. Given a topic UUID you can simply make a title object for <code>Topic:''UUID''</code> but fabricating a link to a post or a link to view a topic in context on a board requires knowledge of Flow internals.
** Options: wiki link or URL; show standalone topic or show Flow board paginated to topic
* PossibleoOptions: wiki link or URL; show standalone topic or show Flow board paginated to topic
** must caller be aware of whether the UUID is a workflow (topic) or post or header?
** note redirector functionality (e.g. [[Special:Flow/workflow/ru0aehx63b43khdl]] and [[Special:Flow/post/ru22c9f18csotjs7]]) is in release 1.24wmf4.


== Use cases ==
== Use cases ==
:''please update, especially with links to bots'' ([[Flow/Bots]])
:''please update, especially with links to bots'' ([[Flow/Bots]])

=== Posting a new topic from on-wiki JavaScript ===
If an on-wiki user script or gadget needs to be post a new topic, the best way is to use [https://doc.wikimedia.org/mediawiki-core/master/js/#!/api/mw.messagePoster.factory MessagePoster]. The advantage is that you don't need to know whether the talk pages uses old-style talk pages or Flow. It will work either way (LiquidThreads is not supported).

<syntaxhighlight lang="javascript">
mw.loader.using( [ 'mediawiki.Title', 'mediawiki.messagePoster' ], function () {
var title = new mw.Title( 'Talk:MyTalkPage' );
var posterPromise = mw.messagePoster.factory.create( title );
posterPromise.done( function ( poster ) {
poster.post( 'This is the topic title', 'This is the body of a message posted with MessagePoster' );
} );
} );
</syntaxhighlight>


=== [[Extension:MassMessage]] ===
=== [[Extension:MassMessage]] ===
Detects whether it is interacting with a Flow board by checking <code>$title->hasContentModel( 'flow-board' )</code>; if so then adds its message to the Flow board by making an API request with <code>action=flow> <code>submodule=new-topic</code> (see [https://gerrit.wikimedia.org/r/#/c/162379/2/includes/job/MassMessageJob.php patch]).
Detects whether it is interacting with a Flow board by checking <code>$title->hasContentModel( 'flow-board' )</code>; if so then adds its message to the Flow board by making an API request with <code>action=flow</code> <code>submodule=new-topic</code> (see [https://gerrit.wikimedia.org/r/#/c/162379/2/includes/job/MassMessageJob.php patch]).

=== [[:en:User:HostBot/Co-op| HostBot for Co-op pages]] ===
This creates a Flow board pairing a new editor with a mentor.
It makes a <code>new-topic</code> API request to add a welcome message to a non-existent page, and because it's in the "flow-bot" group which has the <code>flow-create-board</code> right this succeeds and creates a Flow board.



=== Bots patrolling new content ===
=== Bots patrolling new content ===
Line 108: Line 150:


=== Bots making edits ===
=== Bots making edits ===
''There is in-progress support in pywikibot, in the [https://phabricator.wikimedia.org/project/profile/1247/ pywikibot-flow] project. Some key functionality, including creating new topics, replying, and resolving/reopening topics, is already implemented. Bot owners need to add the code examples listed at [[Manual:Pywikibot/Flow]].''
* Adding topics (and/or posts)
* Adding topics (and/or posts)
* Correcting old posts
* Correcting old posts

Latest revision as of 21:24, 28 January 2023

Flow has an extensive API that's heavily exercised by one client – the JavaScript on Flow boards – as you can see if you interact with a Flow board with JavaScript enabled and watch the API requests in your browser's developer tools > Network tab.

Most URL (HTTP GET) and form (HTTP POST) actions are mirrored in the Flow API.

If you have questions, feel free to ask on #wikimedia-collaboration connect or one of the other places given at Flow#Contact and links.

General comments

[edit | edit source]

Flow pages are not wikitext pages.

  • they use a different ContentModel
  • they store revisioned content differently

Consequently, many typical wikitext page API requests, such as newSection, will not work (bug 57989).

The Flow team has considered mimicking the wikitext APIs, but there's no one right answer to where to modify. For example should a page edit update the board header, or should it create a new topic?

action=query

[edit | edit source]
prop=info
returns "contentmodel": "flow-board" if it's a Flow board. However, per below, you need to also check that the namespace is not 2600 (NS_TOPIC) currently. Unlike prop=flowinfo, this same API request can also be used for other purposes (e.g. checking protection status).
prop=flowinfo
returns enabled key if Flow is enabled on a page, see Gerrit change 111255. No parameters. As of September 2014 this returns enabled for Flow Topic:UUID pages as well as Flow boards.
You can also check if the content model of a page is 'flow-board'. As of November 2014 both Flow boards and individual Topic:UUID pages have this content model, bug 71196.)
obsolete list=flow
any flowaction is passed on to WorkflowLoader, which will create multiple block objects and pass the flowaction onto them. flowaction can be view, header-view, and topic-summary-view. Each of these they take different parameters.

action=flow API

[edit | edit source]

The read and write API consists of submodules of action=flow, you can see them on the API page www.mediawiki.org/w/api.php.

Read API

[edit | edit source]

JS in the browser calls the API to

  • paginate in the next 10 topics
  • request topics in different sort orders
  • get the wikitext when editing existing posts

etc.

action=flow with submodule= topiclist-view, post-view, topic-view, header-view, topic-summary-view, etc.

Write API

[edit | edit source]

You need to decide if your goal is to reply to an existing topic, create a new topic, add something to the Flow board header, or some other interaction.

Sketch of replying

[edit | edit source]

In a reply you're replying to a particular post in a workflow (a topic). Flow objects are identified by UUIDs see Flow/Architecture.

To reply to a topic

  • you'll need a token. Flow recently switched to using the regular 'edit' token for most operations, so you can use mw.api.postWithEditToken to get the editing token if it hasn't been cached yet in the session. it's cached.
  • Issue a flow API request with flowaction=reply, identifying the topic workflow. The parameters for a reply include the topic and the post.
action=flow
submodule=reply
format=json
repreplyTo=050fed5dc6bd5085237590b11c2fa805
repcontent=My reply, with ''wikitext''.

render=true
token=43a71deb105e7c0be7e8eeab4bdff4f7+\
workflow=050f698e3f6e5624fa1590b11c27932f
  • Set render=true if you want the server to respond with the "fully-decorated" HTML to interact with the new post (preview buttons, actions, etc.).
  • UUID format will soon change to be more compact, using 88-bit represented as alphadecimal.
  • Obviously you get the topic and post to respond by making API queries, rather than hardcoding these numbers.

Note that a wiki may store Flow content with $wgFlowContentFormat 'html' or 'wikitext', but in any edit submission you must submit wikitext.

If successful, the server responds with the post-id of the new post, and the HTML to insert into the DOM.

Note this API request doesn't name the page on which the topic appears. Users interact with topics on a Flow board with a wiki page name, but a topic might (eventually) appear on multiple pages, e.g. a user's "subscription feed".

On WMF wikis, Flow stores post content as HTML with embedded Parsoid markup so that the original wikitext can be reconstituted for editing. This will not be the case on all wikis, others might store as wikitext.

Block requests

[edit | edit source]

When you make a "Block" API request such as &submodule=new-topic, the API normally returns a flow.new-topic.result.topiclist containing updated information about the top 10 topics on the board, including their HTML. This allows the Flow board to redisplay the board contents, but it can be a lot of data.

If you are just trying to add a topic and you're not interested in the board contents, you should pass the flag ntmetadataonly=true (gerrit:180588)

Flow returns the metadata in a top-level flow.newtopic.committed key in the API response. For consistency the information about the new content is returned as topic-page, topic-id, topic-revision-id, post-id, header-post-id (but see phab:T84954), etc.

Editing the board header

[edit | edit source]

If you want to update the Flow board header, it's similar

  • make a GET request action=flow, submodule=view-header, vhformat=wikitext to get the current contents. This will return a flow.view-header.result.header.revision including a UUID of the most recent version of the header in revisionId, and its contents in content.content.
  • make a POST request, action=flow, submodule=edit-header. Set ehprev_revision to the revision returned by the GET request and set ehcontent to your updated wikitext.

The update may fail with error.code = 'prev_revision' if someone else updated this board's header between the two requests; if so the return has a prev_revision.revision_id giving the new latest revision, so you can retry with that. However, you should avoid clobbering the previous editors' work (e.g. by presenting an edit conflict form, or re-running the algorithm if a bot is making the requests, etc.).

flow-parsoid-utils

[edit | edit source]

Requests conversion between html and wikitext. If the wiki isn't running Parsoid, conversions from html to wikitext will fail.

This is meant to be an "internal" API for Flow to be able to communicate with Parsoid client-side. If you want to convert wikitext<-->html using Parsoid, you should use its own API.

Future API changes

[edit | edit source]

Eventually move to a service-oriented architecture. Might look like proposed Content API.

External clients should be able to use OAuth to authorize bots to make Flow changes on their behalf. This should work for the most common actions, such as posting a new topic and replying. Anything requiring the Flow rights (e.g. flow-hide, flow-lock, flow-delete, flow-edit-post) will likely be forbidden via OAuth until it's properly exposed, bug 72998.

Should existing API calls that interact with a page "just work" on a Flow board?

[edit | edit source]

What should happen to regular page API requests made to a Flow-enabled page is phab:62808. Probably not in general, since the caller really has to decide whether the change is to the board header or a new topic

But perhaps some variations where intent is obvious:

  • Specifically action=edit&section=new on a talk page means create a new topic, so why not have that work on a Flow board (phab:T59989, status As of March 2014 is "Declined")
  • Likewise action=edit&appendtext=wiki+text+to+add and prependtext=...could append and prepend to a Flow board header (phab:T85197)
    These may not work as well. For appendtext especially, there is no way to tell whether they are intending to add to the header, add a new topic, or add content to the end of the last topic.

Other API issues

[edit | edit source]
  • post editing rights for bots.
    • ordinarily only the poster can edit their reply, unlike a regular wiki page.

Missing APIs?

[edit | edit source]

API to enumerate pages / namespaces that are using Flow?

[edit | edit source]

Currently, would just return values of $wgFlowOccupyPages and $wgFlowOccupyNamespaces. Now that a Flow board has a special content type, the former could be replaced by a query for pages with the flow-board contentmodel, though there does not seem to be a generator for this.

[edit | edit source]

includes/UrlGenerator.php can fabricate all kinds of URLs. May API queries return these URLs in the links object for each topic and post, but clients can't create their own. Given a topic UUID you can simply make a title object for Topic:UUID but fabricating a link to a post or a link to view a topic in context on a board requires knowledge of Flow internals.

  • PossibleoOptions: wiki link or URL; show standalone topic or show Flow board paginated to topic

Use cases

[edit | edit source]
please update, especially with links to bots (Flow/Bots)

Posting a new topic from on-wiki JavaScript

[edit | edit source]

If an on-wiki user script or gadget needs to be post a new topic, the best way is to use MessagePoster. The advantage is that you don't need to know whether the talk pages uses old-style talk pages or Flow. It will work either way (LiquidThreads is not supported).

mw.loader.using( [ 'mediawiki.Title', 'mediawiki.messagePoster' ], function () {
    var title = new mw.Title( 'Talk:MyTalkPage' );
    var posterPromise = mw.messagePoster.factory.create( title );
    posterPromise.done( function ( poster ) {
        poster.post( 'This is the topic title', 'This is the body of a message posted with MessagePoster' );
    } );
} );

Detects whether it is interacting with a Flow board by checking $title->hasContentModel( 'flow-board' ); if so then adds its message to the Flow board by making an API request with action=flow submodule=new-topic (see patch).

This creates a Flow board pairing a new editor with a mentor. It makes a new-topic API request to add a welcome message to a non-existent page, and because it's in the "flow-bot" group which has the flow-create-board right this succeeds and creates a Flow board.


Bots patrolling new content

[edit | edit source]
  • ? have to watch for changes
  • ask for new and changed topics and posts

Bots making edits

[edit | edit source]

There is in-progress support in pywikibot, in the pywikibot-flow project. Some key functionality, including creating new topics, replying, and resolving/reopening topics, is already implemented. Bot owners need to add the code examples listed at Manual:Pywikibot/Flow.

  • Adding topics (and/or posts)
  • Correcting old posts
  • Modifying talkpage headers
  • Checking if the bot has already made a particular post to a particular page
    • Often implemented by a subject line check, or a check for a particular <!-- comment -->.
    • Ability for users to remove or otherwise signal that the bot-raised issue is taken care of, so the bot can know to repost it if it recurs.
  • Opting out of bot messages (eg. Enwiki's template:nobots).

Tools showing count of edits

[edit | edit source]
  • Contributions with Flow at some wiki aren't included in X!'s Edit Counter for that wiki, maybe because Flow is in a separate database. See bug 61887, though this is more a DB tool than a bot.
[edit | edit source]

In bug 57512 comment#22 Brad Jorsch commented:

And there are also a number of API modules that query the existing links tables; there should at least be some way to (1) identify that a page is Flow and (2) search these flow-specific links tables to find the actual "thing(s)" doing the linking in the particular page.

Flow/Architecture#Link handling sketches the implementation.

Non-issues

[edit | edit source]

No need to check for unsigned edits, replies are known.

See also

[edit | edit source]