Extension:Page Schemas

Page Schemas is a MediaWiki extension that allows for defining all the schema information about a "class", or data type - for naming, display, data input, and browsing - within a single piece of XML contained within a category page. This XML then is used to generate all the necessary additional pages needed to put the schema into effect, such as template pages and, for instance, if Page Forms is installed, form pages. The XML can be created and edited via a helper form, preventing users from having to generate or modify it manually.

Page Schemas allows other extensions to define their own fields to add to the XML, using hooks. Currently, the following extensions hook into Page Schemas:
 * Cargo
 * Page Forms
 * Semantic MediaWiki
 * Semantic Drilldown
 * Semantic Internal Objects

Special pages
Page Schemas defines two pages that can show up as tabs on category pages: "Edit schema" (which appears as "Create schema" if no schema exists yet) and "Generate pages". "Edit schema" allows for creating or editing the XML via the helper form, while "Generate pages" provides a button that lets the administrator create the template(s), semantic properties, form etc. automatically from the XML definition. "Generate pages" also includes checkboxes for each of the pages to be generated, to let administrators choose not to override certain wiki pages that already exist.

Any user who is allowed to edit a certain category page will see the "Edit schema/Create schema" tab for that category, and will be allowed to use that page to modify the schema.

In order to generate pages, a user must have the 'generatepages' permission. By default, this permission is given only to members of the 'sysop' user group. You can add to or modify the set of allowed users, though, by modifying the $wgGroupPermissions array in LocalSettings.php. To enable all registered users to generate pages from the schema, for instance, you would add the following:

Code and download
You can download the Page Schemas code, in .zip format, here.

You can also download the code via Git from the MediaWiki source code repository. From a command line, you can call the following:

To view the code online, including version history for each file, go here.

Installation
After you've obtained a 'PageSchemas' directory, place this directory within the main MediaWiki 'extensions' directory. Then, in the file 'LocalSettings.php' in the main MediaWiki directory, add the following line:

Sample XML structure
Here is some sample schema XML to be used


 * for a page called (e.g.)
 * to define datastructures within that category

This example defines a form with a name and 2 fields ( and  ):

Using a simple schema
Let's use the sample XML (above) to create data. Note that the process may vary according to your wiki host, SMW extensions you have installed (and their versions), etc.

Referata
0. Create a free wiki on Referata as described here. I created 'Testing Page Schemas', then logged in. (Note that the Referata scratchpad cannot be used to generate from schema.)

1. Goto your wiki's main page (mine is here), and click the link to edit the page.

2. In the resulting edit page, enter something like

and then save the page.

3. Your main page will now have a redlinked. Click on that to create the category.

4. In the resulting edit page: paste the sample XML into the edit area, and save it.

5. In the resulting category page (mine is here), click the link= to show your schema, which should resemble this:

Form: City standardInputs Template: City format            standard Field: Population Label             Pop. Semantic property: Has population Type              Number Form input Input type        text size              20 mandatory Section: History Level             2 Other parameter rows              10 mandatory

6. Near top right of that page, you should see a dropdown named. This should expand to include a list of links including. Click on the latter link.

7. In the resulting page (title= ), you should see 3 items: ,   ,. Ensure all are selected, then click button=.

8. The response at the resulting page will be like

The selected pages will be generated.

because the page-generation process is asynchronous. Nevertheless, you will probably be able to test-browse to your form's page immediately ... except that you will need to guess its URI. To do that, ...

9. Go back to your wiki's main page (mine is here) and refresh it. The redlink to your category should blue, since you have created the category (in step#=4 above). Copy the URI for your category: it should be something like mine,

http://page-schemas-test.referata.com/wiki/Category:Cities

To get the URI for your form's page (presuming you have followed the steps above using the schema XML above), change  to   in your URI to get something like

http://page-schemas-test.referata.com/wiki/Form:City

10. Browse to your form's page, and click the link to.

11. The resulting page (which should resemble this, if you have the permissions to see it) should show you a text field (for entering the {city, page} name) and a button. Enter a city name (which could be anything you want--I used ), and click the button: this will allow you to create that page using the form you defined in your schema.

12. The resulting page should have fields matching your schema (2 fields,  and  ), plus an additional field name ~=   (that will append to your page--feel free to leave that blank), plus the usual MediaWiki edit-page UI--and not much else! Fill-in the fields as you see fit, and click the button to save the page.

13. The resulting page should contain the data you entered! and vaguely resemble my page.

Congratulations! You have now either


 * successfully created SMW datastructures from a Page Schema
 * shown great diligence but failed. If you believe you should not have failed, and want help, consider contacting the [mailto:yaron57@gmail.com Referata admin].

Tutorial video
You can watch a screencast for Page Schemas (created by Yury Katkov) explaining how it's used and can be useful. Note that Page-Schemas-related UIs have changed since this video was made.

Importing into Page Schemas
The following utilities can import data structures (both MediaWiki-based and external) into the Page Schemas format:
 * db-to-smw-page-schemas, a script in Node.js that queries a database's schema and generates corresponding category pages with Page Schemas XML.
 * A Java Library that allows to read and write PageSchemas directly to a wiki using the MediaWiki API. Feel free to contact the author via his github account if you'd like to learn more details or give feedback.

Common problems

 * The generating of pages is done via MediaWiki "jobs", to ensure that the system is not overloaded if many pages are generated at the same time. This means that the pages might not be generated immediately, and may take minutes, hours or even longer to complete. Normally, jobs get activated every time a page is viewed on the wiki; to speed up the process (or slow it down), you can change the number of jobs run when a page is viewed; the default is 1. For information on how to change it, see the $wgJobRunRate page. To force the wiki to run all jobs immediately, execute the script runJobs.php.
 * If you are using MediaWiki 1.22, there is a good chance that trying to generate pages automatically will not work, because of a bug in MW 1.22 in which jobs that modify pages simply do not get run. If that is happening for you, it is recommended to add this call in LocalSettings.php, which should restore the correct behavior:


 * If you see duplicate entries of the Semantic MediaWiki fields, most likely there are two different calls to enableSemantics - possibly one in LocalSettings.php and one in the settings file for Semantic Bundle, if you have that installed. You should remove one of them.

Authors
Page Schemas was created by Ankit Garg as part of the 2011 Google Summer of Code, and was mentored by Yaron Koren. It was later modified significantly, and is currently maintained, by Yaron Koren. Parts of it are based on the TemplateInfo extension, by Yaron Koren and Roan Kattouw.

Significant code contributions were also made by Himeshi De Silva and Vedmaka.

Version history
Page Schemas is currently at version 0.4.6.

The version history for Page Schemas is:
 * 0.1 - August 23, 2011 - initial version
 * 0.2 - October 13, 2011 - overhaul; many changes to display and code structure
 * 0.3 - October 25, 2011 - extensions hook into Page Schemas via a single hook and a subclass, instead of a variety of hooks; various other changes
 * 0.3.1 - August 24, 2012 - template display format can be selected; various small fixes and improvements
 * 0.4 - October 7, 2013 - handling added for page sections; "Generate pages" UI improved; handling added for MW 1.21
 * 0.4.1 - December 9, 2013 - template-specific attributes appear only for multiple-instance templates; handling removed for MW 1.16
 * 0.4.2 - February 11, 2014 - "namespace" attribute added for fields; handling removed for MW 1.17
 * 0.4.3 - May 8, 2014 - fix for &lt;PageSchema&gt; tag when called from within a script; i18n changed to use JSON files
 * 0.4.4 - January 16, 2015 - JavaScript fixes; querying of page_props table simplified
 * 0.4.5 - February 17, 2015 - support added for template attributes for non-multiple-instance templates
 * 0.4.6 - February 3, 2016 - improved handling of bad XML; improved handling for recent MW versions; other fixes

Roadmap
Features and improvements that it would be great to add to Page Schemas in the future include:
 * Allowing Page Forms to use the Page Schemas XML directly when generating a form, instead of requiring a separate form definition.
 * More tools to export/import data structure files in formats such as UML and SQL to/from the Page Schemas XML format.
 * Making use of the proposed ContentHandler for storing XML natively, instead of in a wiki page, if/when it gets added to MediaWiki.