Extension:WikiLambda/API

From mediawiki.org

The WikiLambda extension has a simple API for internal and external use. These APIs let you search and fetch WikiFunctions content and retrieve it in different formats and after different transformations.

These APIs are modules and submodules of MediaWiki Action API. You can make requests, explore the examples and see the results in the MediaWiki API Sandbox.


wikilambda_fetch[edit]

This API allows to retrieve one or more of ZObject pages from Wikifunctions. Every requested ZObject will be returned as a string encoding its canonical JSON representation. If a particular language is requested, the ZObject will return its root object label in the selected language (or fallbacks).

This API, designed as an Action API module, is created for its external use and hence the returned ZObjects are encoded as strings. For internal use in the Wikifunctions application, please see below the documentation of #list=wikilambdaload_zobjects

Documentation[edit]

Method: GET

Action: wikilambda_fetch

Request: api.php?action=wikilambda_fetch&format=json&zids=Z111|Z112&language=en

Response:

{
  "Z111": {
    "wikilambda_fetch": "{\n    \"Z1K1\": \"Z2\",\n    \"Z2K1\": \"Z111\"\n}"
  },
  "Z112": {
    "wikilambda_fetch": "{\n    \"Z1K1\": \"Z2\",\n    \"Z2K1\": \"Z112\"\n}"
  }
}

Parameters[edit]

name description
zids

required | string

List of ZObject Identifiers (ZIDs) to fetch, separated by pipes.

E.g. zids=Z1|Z2|Z3

language

optional | string

String code for the language in which to return the results. Must be an accepted MediaWiki language code. If not present, the API will return the root label in all available languages.

E.g. language=es

Possible Errors[edit]

This API expects no errors to be found. If any of the ZIDs is invalid or cannot be found, the returned object will consist solely on the found error and will not return the valid objects.


error name error type (Z50) description
Invalid ZID Z549 The given ZID is an invalid ZObject ID. This means that it does not follow the expected format of ID starting with the capital letter Z and followed by a natural number.
ZID not found Z500 The given ZID does not exist in this wiki.

Examples[edit]

api.php?action=wikilambda_fetch&format=json&zids=Z1
Returns the canonical representation of the ZObject with ZID Z1 with its labels in all available languages.


api.php?action=wikilambda_fetch&format=json&zids=Z1|Z2|Z3
Returns the canonical representation of the ZObjects with ZIDs Z1, Z2 and Z3 with its labels in all available languages.


api.php?action=wikilambda_fetch&format=json&zids=Z4|Z6&language=es
Returns the canonical representation of the ZObjects with ZIDs Z4 and Z6 with its root-level label in Spanish or available fallback languages.


wikilambda_edit[edit]

This API allows to create/edit persisted ZObject pages in Wikifunctions. The API expects the parameter zobject to be a valid JSON representation of the ZObject in its canonical form.

To create a new ZObject, the parameter zid must be omitted, and Wikifunctions will find and assign the next available ZID. Also, the keys from the JSON ZObject that refer to its identity (for example, Z2K1 or the values of Z4K1 and Z3K2), must be set to the Null Reference (Z0).

To edit an existing ZObject, the parameter zid must have the ZID of the ZObject to edit (which will be the same one as the internal identity keys).

Documentation[edit]

Method: GET

Action: wikilambda_edit

Request: api.php?action=wikilambda_edit&format=json&summary=TestSummary&zid=Z10001&zobject=<URL_ENCODED_JSON_OBJECT>

Response:

{
    "wikilambda_edit": {
        "success": "",
        "articleId": 1096,
        "title": "Z10001",
        "page": "Z10001"
    }
}

Parameters[edit]

name description
summary

required | string

Summary message for the edit.

E.g. summary=New%20zobject

zid

optional | string

ZID of the ZObject to edit. In case of creating a new ZObject, the field must be empty.

E.g. zid=Z10001

zobject

required | string

JSON representation of the ZObject to be saved, in canonical form. If it's a new ZObject to be saved, the self-referential keys must be replaced by Z0 instead of the ZID. For example Z2K1 and Z4K1 must take the value of Z0, and the key IDs must be Z0K1, Z0K2, etc.

E.g. zobject={"Z1K1":"Z2","Z2K1":"Z0","Z2K2":"value","Z2K3":{"Z1K1":"Z12","Z12K1":[]}}

token

required | string

CSRF token


Possible Errors[edit]

This API can return a wide variety of errors, because the provided ZObject is parsed and validated against the canonical schemata. All the validation errors will be wrapped in a error of type Z502. Apart from the validation errors, Wikifunctions can detect a variety of problems described below.

error name error type (Z50) description
Validation error Z502 The given ZObject is not well formed. The error body will contain the nested sub-errors that better describe the problem. For more information about nesting of validation error, read the Representation of errors page in meta.
Invalid page title Z556 The given ZID is an invalid page title and could not be created.
Unmatching ZID and page title Z555 The given ZID and the content of the self-referential keys (E.g. Z2K1) does not match.
User does not have permission to edit Z557 The user is not logged in or does not have the required permissions to create or edit pages.
Disallowed root type Z553 The ZPersistentObject or ZObject passed as the value of the zobject parameter is disallowed from being saved directly as a page. Disallowed ZObjects are Z3/Key, Z5/Error, Z16/Code, Z17/Argument declaration, Z18/Argument reference and Z39/Key reference.
Disallowed root type Z553 The ZPersistentObject or ZObject passed as the value of the zobject parameter is disallowed from being saved directly as a page. Disallowed ZObjects are Z3/Key, Z5/Error, Z16/Code, Z17/Argument declaration, Z18/Argument reference and Z39/Key reference.
Label clash Z554 The given label for a language clashes with another ZObject's label in that language.

Examples[edit]

api.php?action=wikilambda_edit&format=json&summary=New zobject&zobject=urlencode( ZOBJECT )&token=%2B\
Where ZOBJECT is:
{
	"Z1K1": "Z2",
	"Z2K1": "Z0",
	"Z2K2": "zobject value",
	"Z2K3": {
		"Z1K1": "Z12",
		"Z12K1": [{
			"Z1K1": "Z11",
			"Z11K1": "Z1002",
			"Z11K2": "zobject label"
		}]
	}
}
Creates and saves a new ZObject in the database (empty values) and returns the newly assigned ZID.


api.php?action=wikilambda_edit&format=json&summary=Edit zobject&zid=Z10004&zobject=urlencode( ZOBJECT )&token=%2B\
Where ZOBJECT is:
{
	"Z1K1": "Z2",
	"Z2K1": {
		"Z1K1": "Z6",
		"Z6K1": "Z10004"
	},
	"Z2K2": "new zobject value",
	"Z2K3": {
		"Z1K1": "Z12",
		"Z12K1": [{
			"Z1K1": "Z11",
			"Z11K1": "Z1002",
			"Z11K2": "new zobject label"
		}]
	}
}
Edits the ZObject with title Z10004 and overwrites it with the given JSON ZObject.

wikilambda_function_call[edit]

One of the entrypoints to the Wikifunctions orchestration and evaluation back-end. This API receives a required JSON formatted ZObject and sends it to the function-orchestrator services for it to be evaluated and returned. Hence, the ZObject sent must be a Z7/Function call.

Documentation[edit]

Method: GET

Action: wikilambda_function_call

Request: api.php?action=wikilambda_function_call&format=json&wikilambda_function_call_zobject=<URL_ENCODED_JSON_FUNCTION>

Response:

{
    "query": {
        "wikilambda_function_call": {
            "Orchestrated": {
                "success": "",
                "data": "{\"Z1K1\":\"Z22\",\"Z22K1\":{\"Z1K1\":\"Z40\",\"Z40K1\":\"Z42\"},\"Z22K2\":\"Z23\"}"
            }
        }
    }
}

Parameters[edit]

name description
wikilambda_function_call_zobject

required | string

JSON representation of the ZObject to send for its execution

E.g. {"Z1K1":"Z7","Z7K1":"Z881","Z881K1":"Z6"}


Possible Errors[edit]

Describe function-orchestrator and evaluator most probable errors

error name error type (Z50) description
Connection error (TODO) Unable to connect to the function orchestrator service.
Validation error Z502 The given function call is not well formed. The error body will contain the nested sub-errors that better describe the problem. For more information about nesting of validation error, read the Representation of errors page in meta.
Zid not found Z504 One of the Type ZIDs from the objects involved in the function call cannot be found and hence cannot be validated.
Argument type mismatch Z506 The type of an argument instantiated in the function call does not match with its function argument definition.
Error in evaluation Z507 Something happened during the execution step which raised an error. The returned value of this error will also include the error propagated from the function evaluator and/or executor services.
Argument value error Z516 One of the arguments passed to a built-in function had a wrong value or type.
Return type mismatch Z517 The type returned by a function call does not match the expected type from that function definition.
Object type mismatch Z518 One of the arguments passed to the function call cannot be processed or their type evaluated.
Unexpected ZObject type Z542 (TODO) Currently this is Z539

The ZObject passed as wikilambda_function_call_zobject argument has an incorrect type, while it should be a Z7/Function call

Examples[edit]

api.php?action=wikilambda_function_call&format=json&wikilambda_function_call_zobject=<URL_ENCODED_JSON_FUNCTION>
Where the function is:
{
	"Z1K1": "Z7",
	"Z7K1": "Z802",
	"Z802K1": {
		"Z1K1": "Z40",
		"Z40K1": "Z42"
	},
	"Z802K2": ["arbitrary ZObject"],
	"Z802K3": {
		"Z1K1": "Z40",
		"Z40K1": "Z42"
	}
}
Requests the execution of the function with ZID Z802 (built-in function If(condition, then, else)), where the value of Z802K1/Condition is Z42/False. The expected result is to be returned the value of Z802K3/Else. The returned result, as expected:
{
    "query": {
        "wikilambda_function_call": {
            "Orchestrated": {
                "success": "",
                "data": "{\"Z1K1\":\"Z22\",\"Z22K1\":{\"Z1K1\":\"Z40\",\"Z40K1\":\"Z42\"},\"Z22K2\":\"Z23\"}"
            }
        }
    }
}
Observe what happens when, instead of passing Z42/False as the condition, we pass Z41/True:
{
    "query": {
        "wikilambda_function_call": {
            "Orchestrated": {
                "success": "",
                "data": "{\"Z1K1\":\"Z22\",\"Z22K1\":[\"arbitrary ZObject\"],\"Z22K2\":\"Z23\"}"
            }
        }
    }
}

wikilambda_perform_test[edit]

Endpoint to query test results for a ZFunction. Accepts a Z8/Function, a list of Z14/Implementations, and a list of Z20/Testers. Accepts either a list of ZIDs or the full ZObject for the function, implementations, or testers. Also accepts a flag to utilize a back-end cache or not.

Documentation[edit]

Method: GET

Action: wikilambda_perform_test

Request: api.php?action=wikilambda_function_call&format=json&wikilambda_function_call_zobject=<URL_ENCODED_JSON_FUNCTION>

Response:

{
    "query": {
        "wikilambda_perform_test": {
            "Tested": {
                "success": "",
                "data": "[]"
            }
        }
    }
}

Parameters[edit]

name description
wikilambda_perform_test_zfunction

required | string

String ZID of the Z8/Function to test, or JSON representation of the literal Z8/Function.

E.g. Z101

wikilambda_perform_test_zimplementations

required | string

Valid JSON representation of a list of Z14/Implementation objects. Each object can be either a canonical reference (ZID to that implementation) or the literal Z14/Implementation object.

E.g. ["Z201", {"Z1K1": "Z14","Z14K1": "Z101","Z14K4": {"Z1K1": "Z6","Z6K1": "Z201"}}]

wikilambda_perform_test_ztest

required | string

Valid JSON representation of a list of Z20/Tester objects. Each object can be either a canonical reference (ZID to that implementation) or the literal Z20/Tester object.

E.g. ["Z10006", {"Z1K1":"Z20","Z20K1":"Z101","Z20K2":{"Z1K1":"Z7","Z7K1":"Z101"},"Z20K3":{"Z1K1":"Z7","Z7K1":"Z10007"}}]

wikilambda_perform_test_nocache

required | boolean

Whether to use the cached ZObjects or not.

E.g. 1


Possible Errors[edit]

error name error type (Z50) description
Connection error (TODO) Unable to connect to the function orchestrator service.
Validation error Z502 The given function call is not well formed. The error body will contain the nested sub-errors that better describe the problem. For more information about nesting of validation error, read the Representation of errors page in meta.
Zid not found Z504 One of the Type ZIDs from the objects involved in the function call cannot be found and hence cannot be validated.
Argument type mismatch Z506 The type of an argument instantiated in the function call does not match with its function argument definition.
Error in evaluation Z507 Something happened during the execution step which raised an error. The returned value of this error will also include the error propagated from the function evaluator and/or executor services.
Argument value error Z516 One of the arguments passed to a built-in function had a wrong value or type.
Return type mismatch Z517 The type returned by a function call does not match the expected type from that function definition.
Object type mismatch Z518 One of the arguments passed to the function call cannot be processed or their type evaluated.
Unexpected ZObject type Z542 (TODO) Currently this is Z539

The ZObject passed as wikilambda_function_call_zobject argument has an incorrect type, while it should be a Z7/Function call

Examples[edit]

api.php?action=wikilambda_perform_test&format=json&wikilambda_perform_test_zfunction=Z101&wikilambda_perform_test_zimplementations=%5B%22Z201%22%5D
Requests the execution of the function with ZID Z101 and the array of implementations with only one element, with ZID Z201.

wikilambdaload_zobjects[edit]

Similarly to the wikilambda_fetch API detailed below, this API returns an aggregation of ZObject pages from Wikifunctions. This API is a list submodule of the action=query API and is dedicated to extension-wide internal requests. The return format is an array with the valid JSON representation of the ZObjects requested. It provides different parameters to configure the type of format to serialize the ZObjects to (either normal or canonical form) and the language for the object labels.


Documentation[edit]

Method: GET

Action: query

List: wikilambdaload_zobjects

Request: api.php?action=query&format=json&list=wikilambdaload_zobjects&wikilambdaload_zids=Z111|Z112&wikilambdaload_language=en&wikilambdaload_canonical=1

Response:

{
    "batchcomplete": "",
    "query": {
        "wikilambdaload_zobjects": {
            "Z111": {
                "success": "",
                "data": {
                    "Z1K1": "Z2",
                    "Z2K1": "Z111"
                }
            },
            "Z112": {
                "success": "",
                "data": {
                    "Z1K1": "Z2",
                    "Z2K1": "Z112"
                }
            }
        }
    }
}


Parameters[edit]

name description
wikilambdaload_zids

required | string

List of ZObject Identifiers (ZIDs) to fetch, separated by pipes.

E.g. wikilambdaload_zids=Z1|Z2|Z3

wikilambdaload_language

optional | string

String code for the language in which to return the results. Must be an accepted MediaWiki language code. If not present, the API will return the root label in all available languages.

E.g. wikilambdaload_language=es

wikilambdaload_canonical

optional | boolean

Whether to return the ZObjects in canonical or in its normalized form. The default value is false. If set to true, all the requested ZObjects will be returned in its canonical form. If not set, the ZObjects will be normalized and returned in their normal form, except for the ZList (Z10), which will be still returned as an array.

E.g. wikilambdaload_canonical=1


Possible Errors[edit]

Unlike the wikilambda_fetch API, this API can handle errors while still returning the information of the ZObjects that have been found. The returned errors will also be ZObjects of type ZError (Z5). To distinguish between the return of an error in the API execution and the return of a requested ZError object, the response includes the value `success` for each of the requested `ZIDs`.

The possible errors that can be returned for an individual ZID are detailed below.

error name error type (Z50) description
Invalid ZID Z549 The given ZID is an invalid ZObject ID. This means that it does not follow the expected format of ID starting with the capital letter Z and followed by a natural number.
ZID not found Z550 The given ZID does not exist in this wiki.

Examples[edit]

api.php?action=query&format=json&list=wikilambdaload_zobjects&wikilambdaload_zids=Z1|Z2
Returns the normal JSON representation of the ZObjects with ZID Z1 and Z2 with its labels in all available languages.


api.php?action=query&format=json&list=wikilambdaload_zobjects&wikilambdaload_zids=Z3|Z4&wikilambdaload_language=ay
Returns the normal JSON representation of the ZObjects with ZIDs Z3 and Z4 with all its ZMultilingual String (Z12) objects filtered to only return Aymaran labels. If an Aymaran label is not found, it will return those in any of the available fallback languages.


api.php?action=query&format=json&list=wikilambdaload_zobjects&wikilambdaload_zids=Z6&wikilambdaload_canonical=1
Returns the canonical representation of the ZObjects with ZID Z6.


wikilambdasearch_labels[edit]

This API query list generator lets you search ZObjects by their labels or aliases in a given language. It also provides a series of parameters that allow to filter by the ZObject type or by the return type of existing ZFunctions. It principally exists to support the look-ahead search system, and it's generally used in the front-end components that allow the user to search and select persisted ZObjects.

Documentation[edit]

Method: GET

Action: query

List: wikilambdasearch_labels

Request: api.php?action=query&format=json&list=wikilambdasearch_labels&wikilambdasearch_search=foo&wikilambdasearch_language=eu&wikilambdasearch_type=Z4&wikilambdasearch_return_type=Z4&wikilambdasearch_strict_return_type=true

Response:

{
    "batchcomplete": "",
    "query": {
        "wikilambdasearch_labels": [
            {
                "page_namespace": 0,
                "page_title": "Z111",
                "page_type": "Z111",
                "return_type": null,
                "label": "foo",
                "is_primary": 1,
                "page_id": 0,
                "page_content_model": "zobject",
                "page_lang": "eu"
            }
        ]
    }
}


Parameters[edit]

name description
wikilambdasearch_search

required | string

The search term to find in all the present labels.

E.g. wikilambdasearch_search=text

wikilambdasearch_language

required | string

String code for the language in which to return the results. Must be an accepted MediaWiki language code. If the labels aren't present for this language, use fallback languages.

E.g. wikilambdasearch_language=eu

wikilambdasearch_nofallback

optional | boolean (default=false)

Whether to search only in the given language and not its fallback languages. If set to true, it will ignore the fallback languages and only search in the requested language.

E.g. wikilambdasearch_nofallback=1

wikilambdasearch_exact

optional | boolean (default=false)

Whether to search for exact matches. If disabled, input will be taken as a case-insensitive, Unicode-normalised search with accents on Latin characters ignored.

E.g. wikilambdasearch_exact=1

wikilambdasearch_type

optional | string

Restrict matches to a specific type of ZObject. If present, the search term will only be matched against the labels of ZObjects of the specified type. This is particularly useful for UI components such as the Type selector, which autocompletes a search input with only available ZType (Z4) objects.

E.g. wikilambdasearch_type=Z4

wikilambdasearch_return_type

optional | string

Restrict matches to ZFunctions that can return the given type, including those who return a generic ZObject. If present, the search term will only be matched against the labels of the specified type. If present along with the wikilambdasearch_type, the results will contain all results matching the type OR the return_type.

E.g. wikilambdasearch_return_type=Z40

wikilambdasearch_strict_return_type

optional | boolean (default=false)

If the parameter wikilambdasearch_return_type is present, setting this parameter to true will exclude the generic ZObjects from the result and will only return the rows that match strictly the return type passed as a parameter. If the parameter wikilambdasearch_return_type is not set, this value is irrelevant: it will not affect those matches returned by type (parameter wikilambdasearch_type).

E.g. wikilambdasearch_strict_return_type=true

wikilambdasearch_limit

optional | integer

The maximum number of results to return. The value must be between 1 and 5000. Enter max to use the maximum limit.

E.g. wikilambdasearch_limit=10

wikilambdasearch_continue

optional | integer

When more results are available, use this to continue.

E.g. wikilambdasearch_continue=1


Possible Errors[edit]

None. When nothing is found it will return an empty list.

Examples[edit]

api.php?action=query&format=json&list=wikilambdasearch_labels&wikilambdasearch_search=ThisLabelDoesNotExist&wikilambdasearch_language=en
No labels in English match or contain the substring ThisLabelDoesNotExist, so an empty batch is returned.


api.php?action=query&format=json&list=wikilambdasearch_labels&wikilambdasearch_search=TEX&wikilambdasearch_language=en
Return every label matching or containing the substring TEX in English. This might ensure that at least two builtin ZObjects will be returned: Z12 "Multilingual text" and Z11 "Monolingual text".


api.php?action=query&format=json&list=wikilambdasearch_labels&wikilambdasearch_search=TEX&wikilambdasearch_language=en&wikilambdasearch_exact=1
Return every label matching or containing the exact substring TEX in English. This will not return the two ZObjects returned in the previous example, as this search is case-sensitive.


api.php?action=query&format=json&list=wikilambdasearch_labels&wikilambdasearch_language=en&wikilambdasearch_return_type=Z40
Return the English label of every entry that has a ZBoolean (Z40) return type, including those that have a generic ZObject (Z1) return type.


api.php?action=query&format=json&list=wikilambdasearch_labels&wikilambdasearch_language=en&wikilambdasearch_return_type=Z40&wikilambdasearch_strict_return_type=true
Return the English label of every entry that has strictly a ZBoolean (Z40) return type.


wikilambdafn_search[edit]

This API query list searches for the IDs of all ZObjects associated with a provided Z8/Function ZID and that belong to a given type (either Z14/Implementations or Z20/Testers).

Documentation[edit]

Method: GET

Action: query

List: wikilambdafn_search

Request: api.php?action=query&format=json&list=wikilambdafn_search&wikilambdafn_zfunction_id=Z801&wikilambdafn_type=Z20

Response:

{
    "batchcomplete": "",
    "query": {
        "wikilambdafn_search": [
            "Z8010",
            "Z8011",
            "Z8012",
            "Z8013"
        ]
    }
}


Parameters[edit]

name description
wikilambdafn_zfunction_id

required | string

ZID belonging to the function of which we want to retrieve the associated ZObjects.

E.g. wikilambdafn_zfunction_id=Z801

wikilambdafn_type

required | string

Type of ZObjects related to the given function that we would like to retrieve. The value of this field is not restricted, although the typical values related to functions would be Z14/Implementation or Z20/Tester.

E.g. wikilambdafn_type=Z20


Possible Errors[edit]

None. When nothing is found it will return an empty list.


Examples[edit]

api.php?action=query&format=json&list=wikilambdafn_search&wikilambdafn_zfunction_id=Z801&wikilambdafn_type=Z20
Returns the list of Z20/Testers associated to the function with ZID Z801


api.php?action=query&format=json&list=wikilambdafn_search&wikilambdafn_zfunction_id=Z801&wikilambdafn_type=Z14
Returns the list of Z14/Implementations associated to the function with ZID Z801