Jump to content

Extension:StructuredDiscussions/API: Difference between revisions

From mediawiki.org
Content deleted Content added
SPage (WMF) (talk | contribs)
mention the different modules
Posting a new topic from on-wiki JavaScript: documentation link update (documentation system is changed, need more updates in the future)
 
(17 intermediate revisions by 11 users not shown)
Line 1: Line 1:
: ''See also [[Flow/Architecture/API]] and {{gerrit|107411}}''

Most Flow API read and write interactions are submodules of <code>action=flow</code>. Flow also implements a query property <code>flowinfo</code> and a parsing utility module <code>flow-parsoid-utils</code>.
Most Flow API read and write interactions are submodules of <code>action=flow</code>. Flow also implements a query property <code>flowinfo</code> and a parsing utility module <code>flow-parsoid-utils</code>.


= Detection =
== action=query&prop=flowinfo ==
{{TNT|API-head
{{API-head
|module=flowinfo
|module=flowinfo
|version=
|version=
Line 13: Line 11:
|query=yes
|query=yes
}}
}}
Indicates if Flow is enabled on the specified pages.
<code>prop=flowinfo</code> (details below) is one way to check if Flow is enabled on the specified pages.


You can alternatively use <code>prop=info</code> to retrieve page info including content model. The content model will be 'flow-board' if it's enabled. Compare [[Special:ApiSandbox#action=query&titles=Project:Village+pump&prop=info]] and [[Special:ApiSandbox#Extension:StructuredDiscussions/API&prop=info]].
{{Special:ApiHelp/query+flowinfo}}


In client-side JavaScript (for gadgets and user scripts), call <code>mw.config.get( 'wgPageContentModel' )</code>.
== action=flow ==

{{TNT|API-head
{{anchor|1=action=query&prop=flowinfo}}
{{Api help|query+flowinfo}}
= action=flow =
{{API-head
|module=flow
|module=flow
|version=
|version=
Line 27: Line 29:
|query=no
|query=no
}}
}}
{{Api help|flow}}
All action=flow requests take the following parameters:


(Since this edit operation has to be an HTTP POST, the link in the generated example above won't work.)
* <code>submodule</code>: The specific flow submodule you want to use. See below for documentation on some of them
== Replying ==
* <code>workflow</code>: Id of the workflow to take action upon
In a reply you're replying to a particular post in a workflow (a topic).
* <code>page</code>: Page title of the page to take action upon
Flow objects are identified by UUIDs see [[Flow/Architecture]].
* <code>token</code>: An edit token (see [[API:Tokens]]). This is required for all write modules.
* <code>render</code>: Include this to have an HTML rendering be returned in the output. Used by the extension's JS modules.


To reply to a topic
For any action that posts data, you will need a token, which defeats [[cross-site request forgery]]. To get this, use meta=tokens:
* you'll need a token. Flow recently switched to using the regular 'edit' token for most operations, so you can use <code>mw.api.postWithEditToken</code> to get the editing token if it hasn't been cached yet in the session. it's cached.
{{ApiEx
* Issue a <code>flow</code> API request with <code>flowaction=reply</code>, identifying the topic workflow. The parameters for a reply include the topic and the post.
| desc=Gets a CSRF token
<pre>
| p1=action=query
action=flow
| p2=format=json
submodule=reply
| p4=meta=tokens
format=json
| result=<source lang="javascript">
repreplyTo=050fed5dc6bd5085237590b11c2fa805
{
repcontent=My reply, with ''wikitext''.
"query": {
"tokens": {
"csrftoken": "d197bab38bd17ee31f0f439202945fdf54c98fb6+\\"
}
}
}
</source>}}
=== new-topic ===
{{TNT|Api help|flow+new-topic}}


render=true
(Since this edit operation has to be an HTTP POST, the link in the generated example above won't work.)
token=43a71deb105e7c0be7e8eeab4bdff4f7+\
workflow=050f698e3f6e5624fa1590b11c27932f
</pre>
* Set <code>render=true</code> 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 <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.

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 something resembling 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.).

== 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/REL1_41/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>


== action=flow-parsoid-utils ==
= action=flow-parsoid-utils =
{{TNT|API-head
{{API-head
|module=flow-parsoid-utils
|module=flow-parsoid-utils
|version=
|version=
Line 65: Line 99:
|query=no
|query=no
}}
}}
{{TNT|Api help|flow-parsoid-utils}}
{{Api help|flow-parsoid-utils}}

Latest revision as of 11:05, 25 December 2023

Most Flow API read and write interactions are submodules of action=flow. Flow also implements a query property flowinfo and a parsing utility module flow-parsoid-utils.

Detection

[edit | edit source]

prop=flowinfo (details below) is one way to check if Flow is enabled on the specified pages.

You can alternatively use prop=info to retrieve page info including content model. The content model will be 'flow-board' if it's enabled. Compare Special:ApiSandbox#action=query&titles=Project:Village+pump&prop=info and Special:ApiSandbox#Extension:StructuredDiscussions/API&prop=info.

In client-side JavaScript (for gadgets and user scripts), call mw.config.get( 'wgPageContentModel' ).

prop=flowinfo (fli)

(main | query | flowinfo)
  • This module is deprecated.
  • This module requires read rights.
  • Source: Flow
  • License: GPL-2.0-or-later

Get basic Structured Discussions information about a page.

action=flow

[edit | edit source]


action=flow

(main | flow)

Allows actions to be taken on Structured Discussions pages.

Specific parameters:
Other general parameters are available.
submodule

The Structured Discussions submodule to invoke.

edit-header
Edits a board description.
edit-post
Edits a post's content.
edit-title
Edits a topic's title.
edit-topic-summary
Edits a topic summary's content.
lock-topic
Lock or unlock a Structured Discussions topic.
moderate-post
Moderates a Structured Discussions post.
moderate-topic
Moderates a Structured Discussions topic.
new-topic
Creates a new Structured Discussions topic on the given workflow.
reply
Replies to a post.
undo-edit-header
Retrieve information necessary to undo description edits.
undo-edit-post
Retrieve information necessary to undo post edit.
undo-edit-topic-summary
Retrieve information necessary to undo topic summary edits.
view-header
View a board description.
view-post
View a post.
view-post-history
View the revision history of a post.
view-topic
View a topic.
view-topic-history
View the revision history of a topic.
view-topic-summary
View a topic summary.
view-topiclist
View a list of topics.
close-open-topic
Deprecated. Deprecated in favor of action=flow&submodule=lock-topic.
This parameter is required.
One of the following values: edit-header, edit-post, edit-title, edit-topic-summary, lock-topic, moderate-post, moderate-topic, new-topic, reply, undo-edit-header, undo-edit-post, undo-edit-topic-summary, view-header, view-post-history, view-post, view-topic-history, view-topic-summary, view-topic, view-topiclist, close-open-topic
page

The page to take the action on.

Default: Topic:Flow-enabled_page
token

A token retrieved from action=query&meta=tokens

Default: (empty)


(Since this edit operation has to be an HTTP POST, the link in the generated example above won't work.)

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 something resembling 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.).

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' );
    } );
} );

action=flow-parsoid-utils

[edit | edit source]


action=flow-parsoid-utils

(main | flow-parsoid-utils)

Convert text between wikitext and HTML.

Specific parameters:
Other general parameters are available.
from

Format to convert content from.

This parameter is required.
One of the following values: html, wikitext
to

Format to convert content to.

This parameter is required.
One of the following values: html, wikitext
content

Content to be converted.

This parameter is required.
title

Title of the page. Cannot be used together with pageid.

pageid

ID of the page. Cannot be used together with title.

Type: integer