Extension:Page Object Model

Page Object Model or POM is a set of classes for abstraction of MediaWiki syntax to allow easy extraction and manipulation of pages within other programs (extensions, bots, API handlers and etc). The name shows the similarity of concepts between POM and DOM.

Semantic Forms development
Original idea of POM was developed when Semantic Forms code became quite complex and in the same time useful for other extensions. Extension is currently under development and this page will be updated as it progresses.

Third party extensions
Other extensions and maintenance scripts can benefit from having page and template parsing abstracted for them.

Examples include:
 * command line tools that want to manipulate just some template parameters without parsing whole page
 * MediaWiki API extensions to allow AJAX tools to edit some parts of the page without caring about parsing the content of the pages
 * tools to allow for automated addition or editing of information, also using the MW API
 * outside applications that can use a semantic wiki as a database, such an online spreadsheet or a PDA application, also using the MW API

MediaWiki syntax
Although Semantic Forms will require more complex and high level handling of data which will probably use parameter typing and page definitions, it's better to separate that functionality into another logical layer to allow other code (not necessarily using Semantic Forms or Extension:Semantic MediaWiki) to be able to use it without introducing unnecessary dependencies, therefore it's proposed to only include handling for native MediaWiki syntax rules with initial development of template handling logic.

Possible class structure
Here is a possible class structure for POM:


 * POM::Page - represents a page.
 * templates gets either all templates or all POM::TemplateCollection objects for the page
 * templates('template_name') is a reference to the POM::TemplateCollection object for 'template name' template


 * POM::Element - represents any data element within a page.


 * POM::TemplateCollection - subclass of POM::Element - represents all templates on a page that are of a certain type.
 * count gets the number of such templates
 * [i] references the i-th instance of this set
 * append($template) adds another template to this set


 * POM::Template - subclass of POM::Element - represents a single template instance on a page.
 * parameter('parameter_name') is a reference to the POM::TemplateParameter object with this name
 * setParameter($name, value) sets the value of a parameter in this template


 * POM::TemplateParameter - subclass of POM::Element - represents a single parameter in a template. (This class might not be necessary.)

Creating POM object
$pagecontents = getPageFromName('User:Sergey Chernyshev'); // some function that returns page contents $userpage = new POM::Page($pagecontents);

Handling multiple instance templates
// instance of POM::TemplateCollection class (a collection of POM::Template objects) my $favorites = $userpage->templates('Favorite');

// instance of POM::Template class my $new_favorite = $favorites->createInstance;

$new_favorite->setParameter('url', 'http://google.com'); $new_favorite->setParameter('title', 'Google');

$favorites->append($new_favorite);

$favorites[4]->setParameter('url', 'http://www.google.com'); $favorites[5]->delete;

Command line image localization tool
Some fictional tool that will download files referenced in template by "fileURL" parameter and add local version using "mediaTitle" parameter to change external links to images/PDF documents to locally hosted.

Real life scenario is to have local copies for TechPresentations.org presentation files backed up locally.

Pseudocode
Get the url of the file to download $javascript_the_good_parts = getPageContents('JavaScript The Good Parts'); $article = new POM::Page($javascript_the_good_parts); $url = $article->templates('Presentation')[0]->getParameter('fileURL'); Download the file and save it as local Wiki media title (out of scope of POM) $filename = download($url); $mediaTitle = addLocalFile(makeLocalTitle($url), $filename); Set new parameter that references this new local media title $article->templates('Presentation')[0]->setParameter('mediaTitle') = $mediaTitle; $resultwikitext = $article->toWikiText; ...

Download
At the moment only SVN download is available

SVN
To download code for Page Object Model project, issue following command:

svn checkout http://mediawiki-page-object-model.googlecode.com/svn/trunk/ PageObjectModel

Links

 * Extension:Semantic Forms extension
 * Extension:Semantic MediaWiki extension
 * original proposal for POM - might be a place for development of higher level of abstraction needed for Semantic Forms and similar extensions.
 * Document Object Model (DOM) - W3C's standard object model for representing HTML or XML and related formats