Jump to content

Extension:JsonForms

From mediawiki.org
MediaWiki extensions manual
JsonForms
Release status: stable
Implementation Hook , Special page
Description Json-schema compliant form manager
Author(s) thomas-topway-it (thomas-topway-ittalk)
Latest version 1.1.8
MediaWiki >= 1.39
  • $wgJsonFormsReCaptchaSecretKey
  • $wgJsonFormsDataMaptilerApiKey
  • $wgJsonFormsDisableVersionCheck
  • $wgJsonFormsReCaptchaSiteKey
  • $wgJsonFormsEditSlotsNamespaces
  • $wgJsonFormsDoPurge
  • $wgJsonFormsDisableSidebarLink
  • $wgJsonFormsEditSchemaNamespaces
  • jsonforms-caneditdata
  • jsonforms-canmanageschemas
  • jsonforms-canmanageforms
Licence GNU General Public License 2.0 or later
Download
Translate the JsonForms extension if it is available at translatewiki.net
Issues Open tasks · Report a bug

JsonForms is a Json-schema compliant Form Manager for MediaWiki, based on json-editor (completely refactored and enhanced), and the OOUI library.

It allows to easily create forms (also with AI assistants, since all the schema language is encoded in a meta-schema included in the extension), to display them both via a special page or within wiki articles, to associate structured data to articles and to show Infoboxes.

JsonForms is "data-engine" agnostic, that is it can be complemented with query engines like SemanticMediaWiki, VisualData, and Cargo, and by design it only saves data in regular slots (json-content model) which can then be printed in wiki articles through templates in a recursive fashion, or intercepted by other extensions via hooks.


Features

[edit]
  • forms are based on the official json-schema language and can be created via the UI provided by the extension or manually/through AI assistants
  • supports advanced UX/workflow thanks to watchers, enum/autocomplete providers, dynamic composition of input fields or other form components based on the value of other fields, conditional display of properties based on complex conditions, a schema-based UX for additional properties, and value converters (user-defined functions by which to interactively convert the value back and forth to another value, especially useful for the use with meta-schemas)
  • can be used to create/edit articles with forms and to easily associate Infoboxes to articles based on the data represented by a given schema, automatically or using templates.
  • minimal impact on the wiki. There are no storage tables and by contrast to VisualData, JsonForms does not use custom content models. The content model used to store data and metadata is Json and when the extension is uninstalled the data will be shown by MediaWiki as article sections and can be removed either overwriting the article or by other tools.



Installation

[edit]
  • Download JsonForms and place the file(s) in a directory called JsonForms in your extensions/ folder.
  • Add the following code at the bottom of your LocalSettings.php
wfLoadExtension( 'JsonForms' );
  • Run php ./extensions/JsonForms/maintenance/ImportData.php to import the json schemas used by the extensions
  • Yes Done – Navigate to Special:Version on your wiki to verify that the extension is successfully installed.


New article

[edit]

JsonForms provides a New Article special page from which to easily create a regular article, an article combined with a schema, or a data-article.


Demo


Schema Builder

[edit]

The Schema Builder is a json-schema compliant schema editor and builder, completed with preview. See here for all the options.


Demo


Create Page Form

[edit]

Create form is a Form Builder through which "to wrap" a schema with parameters functional to create or edit articles with schemas, such as the form action (create or edit), the pagename formula, the target slot, whether to let the user edit the freetext or categories, etc. – See here for all the options.


Demo


Forms SpecialPage

[edit]

The SpecialPage Forms lists all available forms (created using the Form Manager as mentioned above) and immediately displays them ready to be filled-in by the user. (each form is accessible from an url like Special:JsonForms/NewContact or can be printed via a parser function)




Demo


Slot Manager

[edit]

The Slot Manager is a powerful interface which allows to edit all slots of a wiki article, their content model and text. It supports whatever role, therefore it's not limited to the roles provided by JsonForms.


Demo


Edit Schema Action

[edit]

The Edit schema action allows to edit the schema's data associated to an article, and to set the options to display an infobox.



Demo


Infoboxes

[edit]

Infoboxes, when enabled, are created automatically by the extension based on the schema's data.


They can be enabled through the Options tab in the Edit schema action, along with the preferred position in the article (see #Edit Schema Action).

Additionally, custom infoboxes can be created using templates, by entering the root template name or through the parser function jsonformsrender as follows.

{{#jsonformsrender:{{FULLPAGENAME}} 
|schema=Tasks
|slot=jsonforms-data
|print_scalar=false
}}    


Demo


Parser function #jsonforms

[edit]

Forms can be rendered within articles using a parser function (more similar to a plugin code used in Wordpress) as follows:

{{#jsonforms:AddPerson}}   


Additionally, all the parameters set from the form builder can be overridden from the wikitext for the maximum flexibility.

{{#jsonforms:Default
|schema=Tasks
|view=popup
|edit={{FULLPAGENAME}}
|edit_categories=true
|edit_freetext=true
}}


SpecialPage JsonFormsDemo

[edit]

JsonForms offers a demo special page to test the editor with the schemas provided by the extension.


Demo



SpecialPage JsonFormsTests

[edit]

The special page JsonFormsTests provides a form used to test the editor with the JSON-Schema-Test-Suite.

Note that at this point in time not all tests succeed: this is precisely the purpose of this page, that is validating the functioning of the editor against the different Json-schema drafts and the official test cases.


Demo



UI elements

[edit]

Hierarchical navigation with "paged" layout

[edit]

Navigating complex/large schema can be challenging or even impossible for users. JsonForms allows an easy navigation of huge schemas through a "paged" layout. All non-primitives fields (including multiple-type properties) are placed in an independent page of a booklet, which allows hierarchical navigation and is completed with breadcrumbs and a footer for a comfortable form filing.

Source schema: Europass



Interactively add non-required properties

[edit]

Forms with a lot of fields can discourage users to enter data.

JsonForms allows to set non-default properties, based either on the non-required properties or on a list of default properties listed in the schema. Users can then select either primitives or additional form sections through buttons for a progressive data filing.

Source schema: AccidentDetail



Survey format

[edit]

The survey format is equivalent to a standard Excel spreadsheet with arbitrary grouping of rows and columns. This allows to retrieve structured data with rapid filing.


Source schema: SurveyPeople


Data:

{
  "District 1": {
    "male": {
      "with university degree": {
        "employed": "20",
        "unemployed": ""
      },
      "without university degree": {
        "employed": "",
        "unemployed": ""
      }
    },
    "female": {
      "with university degree": {
        "employed": "",
        "unemployed": ""
      },
      "without university degree": {
        "employed": "",
        "unemployed": ""
      }
    },
    "diverse": {
      "with university degree": {
        "employed": "",
        "unemployed": ""
      },
      "without university degree": {
        "employed": "",
        "unemployed": ""
      }
    }
  },
  "District 2": {
    "male": {
      "with university degree": {
        "employed": "",
        "unemployed": ""
      },
      "without university degree": {
        "employed": "",
        "unemployed": ""
      }
    },
    "female": {
      "with university degree": {
        "employed": "",
        "unemployed": ""
      },
      "without university degree": {
        "employed": "",
        "unemployed": ""
      }
    },
    "diverse": {
      "with university degree": {
        "employed": "",
        "unemployed": ""
      },
      "without university degree": {
        "employed": "",
        "unemployed": ""
      }
    }
  }
}


The survey format also comes with a Survey Builder which makes use of value-converters to convert the output value back and forth to a schema suitable for the survey editor. The Survey Builder is also integrated in the Schema Builder so that surveys can be added to complex forms with other properties and nested properties.



Text editors

[edit]

JsonForms at this point in time integrates the following text editors:


plus of course the standard OOUI text widget.


Dependencies Builder

[edit]

The Schema builder includes a complex dependencies builder through which to show/hide properties and nested properties based on logical conditions.

This is for instance a condition used by the form builder

"x-dependency": {
	"and": [
		{
			"path": "action",
			"op": "==",
			"value": "create"
		},
		{
			"path": "slot",
			"op": "!=",
			"value": "main"
		}
	]

to display the "preload" field if the field "action" has value equal to "create" and the "slot" field has value different from "main".

To be noted that the same result can be achieved using the if/then/else keywords as allowed by the json-schema format and supported by the extension, however for real-world cases the x-dependency solution is simpler to use for complex conditions and probably faster to render.


Schema options

[edit]

A schema is a language aimed to describe data used for data validation and input. As such JsonForms provides a clear demarcation between the official json-schema keywords (used mainly for validation) and custom keywords (prefixed with the x- prefix) mainly used for input and UI-logic.

Schemas can therefore be created using the standard json-schema language, enriched with the custom keywords provided by the extension, either trough the Schema Builder, or manually/through an AI assistant, based on the tables below and the meta-schema provided with the extension.


Regular JSON Schema Properties

Show table
Property Type Scope Description Options
type string root, all JSON Schema primitive type string, number, integer, boolean, object, array, null
title string root, all Field title
description string root, all Description or help text
const mixed all Value must be equal to this constant
$comment string all Developer comment
$ref string all Reference to another schema definition local references like #definitions/<subschema>, other schemas in the JsonSchema namespace like JsonSchema:<schema name>, including subschemas (JsonSchema:<schema name>#definitions/<subschema>), and external schemas (HTTP protocols)
$schema string root JSON Schema dialect URI
$id string root Unique identifier URI
default string, number, integer, boolean, null, array, object string, number, integer, boolean, null, array, object Default value when not provided
enum array string, number, integer, boolean Value must be one of specified values
examples array all Example values demonstrating valid input
readOnly boolean all property is read-only
writeOnly boolean all property can be written but not read
deprecated boolean all property is deprecated - show warning
contentEncoding string string Encoding method base64, binary (not implemented)
contentMediaType string string MIME type of content (not implemented)
maxLength integer string Maximum string length
minLength integer string Minimum string length
pattern string string Regular expression pattern
format string string Standard format validation date-time, time, date, email, idn-email, hostname, idn-hostname, ipv4, ipv6, uri, uri-reference, iri, uri-template, json-pointer, relative-json-pointer, regex
exclusiveMaximum number number, integer Must be strictly less than value
exclusiveMinimum number number, integer Must be strictly greater than value
maximum number number, integer Must be less than or equal to value
minimum number number, integer Must be greater than or equal to value
multipleOf number number, integer Must be a multiple of value
items object/array array Schema for array items
additionalItems object array Schema for additional items
contains object array Schema that at least one item must satisfy
maxContains integer array Maximum items satisfying 'contains'
minContains integer array Minimum items satisfying 'contains'
maxItems integer array Maximum items allowed
minItems integer array Minimum items required
prefixItems array array Positional validation for beginning of array not implemented
unevaluatedItems object array Schema for unevaluated items not implemented
uniqueItems boolean array All items must be unique
properties object object Explicitly defined object properties with their schemas
additionalProperties boolean object Whether properties not defined in properties are allowed
patternProperties object object Property validation where property names must match regex patterns
propertyNames object object Schema for validating property names
required array object List of property names that must be present (uniqueItems)
maxProperties integer object Maximum number of properties allowed
minProperties integer object Minimum number of properties required
definitions object object Reusable schema definitions using $ref keyword
oneOf array multiple Valid against exactly one subschema (XOR)
allOf array multiple Valid against all subschemas (AND)
anyOf array multiple Valid against at least one subschema (OR)
not object multiple Must NOT be valid against schema
if object multiple Conditional validation: if validates then 'then' must validate
then object multiple Validates when 'if' validates
else object multiple Validates when 'if' does not validate
links array multiple Hyper-schema links (used to validate a property against a schema)


Custom properties

Show table
Property Type Scope Description Options
x-layout string root, array, object Layout widget default, paged, tabs, booklet, horizontal, table
x-layout-config object root, array, object Additional configuration for the selected layout
x-name string root Unique identifier for this schema definition
x-title-format string base Title format text, html, wikitext
x-description-format string base Description format text, html, wikitext
x-title-template string base Title template (overrides title, usually used in conjunction with x-watch)
x-description-template string base Description template (overrides description, usually used in conjunction with x-watch)
x-no-title boolean base hide title
x-no-description boolean base hide description
x-padding boolean base show/hide padding
x-border boolean base show/hide border
x-group-properties object object group properties under a specific page or tab of booklet or index layout
x-runtime-only boolean base property only used at runtime (e.g., pagename formula)
x-watch array/object base Watched properties for the use with dependency, enum/autocomplete providers or templates). Can be an array of property, or key/value pairs mapping alias to the property name.
x-hidden boolean base Hide the property from the form UI
x-compact boolean common Render the property without title, padding and border
x-css-class string common Custom CSS class name to apply to the schema container
x-css-inline string common Inline CSS to apply to the schema container
x-dependency object common Conditionally displays the field when dependencies are met (usually used in conjunction with x-watch)
x-show boolean common the property will be always visible (useful when used in conjunction of additional properties)
x-format string string, number, integer, null Custom format type string: autocomplete, captcha, color, json, hidden, month, rating, tel, text, textarea, url, uuid, week, wikitext; number: number, range; integer: number, range, rating; null: null, info, button
x-format-type string string Selects whether to use standard JSON Schema formats or custom formats custom, default
x-pattern-message string string User-friendly error message to show when pattern validation fails
x-template string string Template string for generating dynamic input values (e.g., 'Template:Field1-Template:Field2')
x-enum-titles array string Display labels for enum values. Must be the same length as enum array.
x-enum-template object string Dynamic source for enum using templates. Must be a built-in provider or registered in the MediaWiki:DefaultEditorOptions as javascript function
x-enum-provider text string Name of the registered enum provider to be used in selects. Must be a built-in provider or registered in the MediaWiki:DefaultEditorOptions as javascript function
x-input string string, boolean, array, multiple Widget type for input string: OO.ui.TextInputWidget, OO.ui.MultilineTextInputWidget, OO.ui.ComboBoxInputWidget, OO.ui.DropdownInputWidget, OO.ui.ButtonSelectWidget, OO.ui.RadioSelectInputWidget, mw.widgets.TitleInputWidget, mw.widgets.UserInputWidget, mw.widgets.DateInputWidget, mw.widgets.DateTimeInputWidget, LookupElement, autocomplete, ColorPicker, JsonEditor, jsonForms, RatingWidget, VisualEditor, WikiEditor; boolean: OO.ui.CheckboxInputWidget, OO.ui.ToggleSwitchWidget; array: ButtonMultiselectWidget, GroupMultiSelectWidget, mw.widgets.CategoryMultiselectWidget, mw.widgets.TitlesMultiselectWidget, mw.widgets.UsersMultiselectWidget, OO.ui.CheckboxMultiselectInputWidget, OO.ui.MenuTagMultiselectWidget, OO.ui.TagMultiselectWidget; multiple: OO.ui.ButtonMenuSelectWidget, OO.ui.ButtonSelectWidget, OO.ui.DropdownInputWidget
x-input-config object string, number, integer, boolean, array, null, multiple Configuration options passed to the selected input widget. all options from the MediaWiki Core Documentation
x-control-config object string, number, integer, boolean, array Additional control configuration for the field, like align and helpInline, etc. all options from the MediaWiki Core Documentation
x-autocomplete-provider string string Autocomplete configuration for text inputs. Must be a built-in provider or registered in the MediaWiki:DefaultEditorOptions as javascript function
x-default-properties array object List of properties that will be always shown by the editor
x-value-converter string object name of value converter to convert the form output back and forth Must be a built-in converter or registered in the MediaWiki:DefaultEditorOptions as javascript function
x-data object object custom container for data it is and can be used in conjunction with value-converters or enum/autocomplete providers
x-ui-schema object object Name of the schema used to create this schema (used by the Schema Builder to interpret a portion of the value using a dedicated schema, and from within it, using a value-converter to integrate the value in the base schema)
x-message object object Contextual message to display at top of the schema or subschema type: notice, error, warning, success, label, x-label-format, icon, inline: true/false
x-additional-properties-ui object/array object Defines available UI schemas for dynamically adding properties (usually the UI schema includes a value converter to convert the value to a target schema suitable for the base schema)
x-collapsible boolean array, object Determine whether this object should be displayed as a collapsed element collapsed: true/false, label, icon
x-button-action string null Action name registered in MediaWiki:DefaultEditorOptions as javascript function
x-toolbar string array, object whether an additional property should be added to the schema via a dedicated UI button button
x-render-template string base used in conjunction with the parser function jsonformsrender to render a schema using a specific template (by default the default name is determined from the data path, however a fixed template name is necessary with the use with recursive schemas)
x-value-formula string string process the value server-side using wikitext string


Form options

[edit]

Here are all the options for the form descriptor. They can be created via the Form builder and overridden in the wikitext using the parser function {{#jsonforms: <form name>|param a=...|param b=...}}, for the maximum flexibility.


Show table
Parameter Description Default
name Form name, can be referenced by the parser function {{#jsonforms: <form name>}} to print the form within a wiki article
title Form title
hide_title Hide form header
schema schema name (only a single schema is allowed, however the schema can be composed of multiple schemas and can be an array)
action form action ("create", "edit", "none")
edit Page to edit (when form action is "edit")
ugroups Restrict to groups ["user"]
captcha Add captcha to form false
pagename_formula Page name formula: dynamic generation of the article title with substitutions of property values and wikitext
create_only_fields Properties (usually used in the pagename formula) to be shown only on data creation to be superseded by x-runtime-only
overwrite_existing_article_on_create Overwrite existing article: if enabled, existing articles with the same name will be overwritten false
slot Default slot for form data: if different from main the related article will be saved as a Multi-Content-Revision jsonforms-data
edit_freetext Edit freetext using the proper editor false
edit_categories Determine whether the user is allowed to edit categories through the form false
default_categories Default categories
infobox.show_infobox Whether to show an infobox on the target page true
infobox.infobox_position Infobox position right
infobox.infobox_template Base template name used to display the data
view View mode ("inline" or "popup") inline
popup_button_config popup button configuration object
popup_size Popup size ("small", "medium", "large") medium
preload_article Article to preload content from (when the target slot for form data is different than main)
preload_wikitext Preload wikitext into the target article. (Performs substitutions using an expression like <parent.child>)
return return strategy ("none", "target", "article", "url") target
return_page Page to redirect to after completion (Performs substitutions using an expression like <parent.child>)
return_url External URL to redirect to after completion. (Performs substitutions using an expression like <parent.child>)
edit_path Target path of form data (dot separated) (supports <path>. or <path>.[] to add new items to arrays)
edit_jsonpath Used in conjunction with edit_path to determine the related branch of the schema when the schema has schema combinations
edit_schema Partial schema to be used in conjunction with edit_path
display_path Json path to the subschema used to display a partial form (dot separated) (development, could be subject to change)
css_class CSS class applied to the rendered view width form width width 600px
editor_options.base_options Editor options in the MediaWiki: namespace, javascript content model MediaWiki:DefaultEditorOptions
editor_options.validation Validation strategy ("onsubmit", "always") onsubmit
editor_options.template template engine (default, handlebars, etc.) default
editor_options.max_depth max depth 32
editor_options.path_separator path separator .
editor_options.default_additional_properties whether to let the user add additional properties by default false
editor_options.use_lazy_properties additional properties strategy ("never", "always", "threshold") threshold
editor_options.remove_empty_properties remove empty properties from output true
editor_options.remove_empty_properties remove false properties from output false
editor_options.debug show debug false


Rights and privileges

[edit]

The extension creates the following user rights. They can be both included manually in the LocalSettings.php or to be managed through the interface (special page Special:UserRights). Sysops and bureaucrats are assigned with the complete set of permissions by default.


Rights

[edit]
right description
jsonforms-caneditdata Can edit metadata related to wiki articles through the action "edit schema"
jsonforms-canmanageschemas Can create/edit/delete schemas through the SchemaBuilder
jsonforms-canmanageforms Can create/edit/delete forms through the FormBuilder


Groups

[edit]
group Can edit data Can manage schemas Can manage forms
sysop, bureaucrat Yes   Yes   Yes  
jsonforms-admin Yes   Yes   Yes  
jsonforms-editor Yes   Yes   Yes  


Global parameters

[edit]
variable description default
$wgJsonFormsDisableSidebarLink hide side bar links false
$wgJsonFormsEditSchemaNamespaces default namespaces with editable metadata 0, 4 (Main, Project)
$wgJsonFormsEditSlotsNamespaces default namespaces where edit slots is available 0, 4 (Main, Project)
$wgJsonFormsReCaptchaSiteKey ReCaptcha site key false
$wgJsonFormsReCaptchaSecretKey ReCaptcha secret key 0


Example configuration

[edit]
$wgGroupPermissions['*']["jsonforms-caneditdata"] = true;
$wgGroupPermissions['*']["jsonforms-canmanageschemas"] = false;
$wgGroupPermissions['*']["jsonforms-canmanagesforms"] = true;

$wgJsonFormsDisableSidebarLink = false;
$wgJsonFormsEditSchemaNamespaces =  [ 0, 4, 2226, 2230 ];
$wgJsonFormsEditSlotsNamespaces =  [ 0, 4, 2226, 2230 ];

$wgJsonFormsReCaptchaSiteKey = '';
$wgJsonFormsReCaptchaSecretKey = '';


Design principles

[edit]
  • every form used within and by the extension is a JsonForms form with as little additional logic as possible. This includes the forms used to create/edit articles and articles with data, the slot manager, the form and schema builder, and the popup forms used to create additional properties. This principle ensures that complex UX flows are natively supported by the editor
  • the data are stored either in the main slot or in a dedicated slot (jsonforms-data) and the metadata are stored in a different slot (jsonforms-metadata). No metadata are stored in the data slot, including the schema used to create/edit such data, or other representation of such data. This ensures that other applications possibly used to consume such data don't need to know the interface used by JsonForms and can freely structure their data. This is also supported by the fact that the json format does not pose any constraint on the kind of data, therefore the base schema can also be an array of different kind of items, without the need of an additional metaschema for that.
  • the data values are related to their schema (e.g. type string, number, etc.) via the schema name used to edit/create such data stored in the metadata and a map between the data paths and the related json paths. In turn the schema article contains a map between the json paths and a short schema descriptor for each json path. This way the Infobox or template render is "schema-aware", there is version control and there is no schema duplication. To not have schema-duplication allows to centralize the reference schema in such a way that non-disruptive (like change of type or rename of keys) edits immediately reflect in the result printer.
  • as mentioned in the other sections, there is a clear demarcation between the standard json-schema keywords and custom/additional keywords used by the extension. At the same time, the extension aims to propose a sound schema vocabulary which encodes wiki-related features (like x-value-formula) and advanced features offered by the editor (like x-value-converter and x-additional-properties-ui) aimed to build complex UIs and workflows. In this regard, the vocabulary/schema language tries to be as little arbitrary as possible and facilitate the understanding of json-schema, including in educational contexts.


Code design principles

[edit]
  • each kind of form (page forms, slot manager, etc.) has its own client-side resource module (while sharing the underlying editor and all its dependencies) and server-side submit processor. This of course leads to a better delegation and facilitate the maintenance of existing components and creation of new ones
  • despite the original json-editor has been deeply and extensively refactored and further enhanced, the original ideas still represent the backbone of the current application. Those are:
    • ES2015 classes (implying the code is compiled for the browser by a module bundler)
    • schemas-to-subeditors resolvers, which route each kind of schema/subschema to a dedicated editor
    • specialized editors which process and provide an UI for a particular schema/subschema (e.g., string, object, etc.)

Other original components, like the resource loader, the internal notification system (comparable to vuejs' reactive properties and watchers) and the validator, have been largely refactored and redesigned, while other core components, like a centralized state manager, a "DependencyEvaluator', a "ConditionProcessor" and more have been added. Briefly the updates are the following:

  • removal of all DOM-manipulation instructions that were interspersed among the code, and migration towards a Object-oriented theme library (specifically OOUI, however Codex could and probably should be used as well for a later version)
  • separation between the concept of "editor" (a processor for a particular kind of schema/subschema) and "widget". Generally, there is only one editor per schema type, with a few exception, while there could be many editors for each type. As a matter of example, the string editor has a markdown widget, an html widget, a VisualEditor widget, and so forth. Each widget has the same architecture which is significantly simpler than the architecture of an "editor" making easy to create new widgets.
  • state centralization (related to the global value of the editor, there isn't yet a state management for the UI of each particular editor). The state manager is also equipped with a "value transformer" able to handle value-converters declared in the schema.
  • a new "condition" editor, by which to handle schemas/subschemas with conditions (if/then/else)

Another important update is that the editor is immediately hydrated with the initial value. This is especially important in conjunction with watchers, since otherwise watchers may register inconsistent values and disorganize the UI.



Road map

[edit]
  • PHP Unit tests


See also

[edit]