Jump to content

Developers

From NeoWiki Demo

NeoWiki exposes a programmable surface: a graph database, parser functions, a Lua library, special pages, and a REST API. This page demonstrates each, showing the wikitext source alongside the rendered result.

Browse

Query the graph

The {{#sparql_raw}} parser function runs a SPARQL query and returns the raw JSON.

Source:

{{#sparql_raw:SELECT ?museum ?visitors WHERE { ?m <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <{{SERVER}}/schema/Museum> . ?m <http://www.w3.org/2000/01/rdf-schema#label> ?museum . ?m <{{SERVER}}/prop/Annual_visitors> ?visitors } ORDER BY DESC(?visitors) LIMIT 5}}

Result:

{
    "head": {
        "vars": [
            "museum",
            "visitors"
        ]
    },
    "results": {
        "bindings": [
            {
                "museum": {
                    "type": "literal",
                    "value": "Mus\u00e9e d'Orsay"
                },
                "visitors": {
                    "datatype": "http://www.w3.org/2001/XMLSchema#int",
                    "type": "literal",
                    "value": "3270000"
                }
            },
            {
                "museum": {
                    "type": "literal",
                    "value": "Museo del Prado"
                },
                "visitors": {
                    "datatype": "http://www.w3.org/2001/XMLSchema#int",
                    "type": "literal",
                    "value": "3200000"
                }
            },
            {
                "museum": {
                    "type": "literal",
                    "value": "Rijksmuseum"
                },
                "visitors": {
                    "datatype": "http://www.w3.org/2001/XMLSchema#int",
                    "type": "literal",
                    "value": "2700000"
                }
            },
            {
                "museum": {
                    "type": "literal",
                    "value": "Kunsthistorisches Museum"
                },
                "visitors": {
                    "datatype": "http://www.w3.org/2001/XMLSchema#int",
                    "type": "literal",
                    "value": "1800000"
                }
            }
        ]
    },
    "meta": {
        "query-time-ms": 6,
        "result-size-total": 4,
        "warnings": []
    }
}

SPARQL queries has more examples, from Lua and over REST too.

Cypher queries need a Neo4j backend. On a wiki with one, the {{#cypher_raw}} parser function runs the same query in Cypher:

{{#cypher_raw:MATCH (m:Museum) RETURN m.name AS name, m.`Annual visitors` AS visitors ORDER BY visitors DESC LIMIT 5}}

Read values in wikitext

The {{#neowiki_value}} parser function reads a single property from a subject on a page.

Source:

{{#neowiki_value:Annual visitors|page=Rijksmuseum}}

Result: 2700000

Source:

{{#neowiki_value:Founded at|page=ACME Inc}}

Result: 2005

Create subjects from wikitext

The {{#create_subject}} parser function renders a button that opens the Subject creator, optionally fixed to a Schema and a target page.

Source:

{{#create_subject: schema=City | page=new | text=Add a city}}

Result:

Read values in Lua

The mw.neowiki Lua library exposes structured data to your Scribunto modules. The example below reads a single property and runs a SPARQL query.

Module:LuaExample:

local p = {}
local nw = require( 'mw.neowiki' )

function p.foundedYear( frame )
	local year = nw.getValue( 'Founded', { page = frame.args[1] } )
	return tostring( year or '' )
end

function p.largestMuseums( frame )
	if not nw.sparqlQuery then
		return "''This list needs a SPARQL store.''"
	end

	local base = mw.site.server
	local result = nw.sparqlQuery(
		'SELECT ?label ?visitors WHERE { ' ..
		'?m <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <' .. base .. '/schema/Museum> . ' ..
		'?m <http://www.w3.org/2000/01/rdf-schema#label> ?label . ' ..
		'?m <' .. base .. '/prop/Annual_visitors> ?visitors ' ..
		'} ORDER BY DESC(?visitors) LIMIT 3'
	)

	local list = {}
	for _, binding in ipairs( result.results.bindings ) do
		list[#list + 1] = '* ' .. binding.label.value .. ' (' .. binding.visitors.value .. ' visitors)'
	end
	return table.concat( list, '\n' )
end

return p

Invoke it from wikitext:

{{#invoke:LuaExample|foundedYear|Rijksmuseum}}

{{#invoke:LuaExample|largestMuseums}}

Result:

Rijksmuseum was founded in 1800.

Museums with the most visitors in this demo:

  • Musée d'Orsay (3270000 visitors)
  • Museo del Prado (3200000 visitors)
  • Rijksmuseum (2700000 visitors)

For the full library reference, see the Lua API documentation.

Reactive UI

NeoWiki components embed in pages as reactive UIs that update without a page reload. See Subject views for the same Sandbox subject in three layouts, with edits propagating between them.

REST API

NeoWiki exposes a REST API for reading and writing structured data. The OpenAPI 3.0 spec is auto-generated:

Representative read endpoints:

  • GET /neowiki/v0/subject/{subjectId}
  • GET /neowiki/v0/schema/{schemaName}
  • GET /neowiki/v0/schema/{schemaName}/json-schema: a Schema's Subjects as a JSON Schema document, for validating data before upload; try Person

For full endpoint documentation, see the OpenAPI spec above.