Page MenuHomeGitPull.it

First steps with boz-mw MediaWiki API framework
Updated 51 Days AgoPublic

This is boz-mw, another MediaWiki API handler in PHP with batteries included! It has a tons of features that will make your head spin!

This is a library to interact with MediaWiki and Wikibase APIs. There are also some boz-mw command line tools.

Features

You may ask what we can offer to you:

  • read/write support for Wikidata
  • read/write support for Wikimedia Commons' Structured Data
  • file upload support for Wikimedia Commons
  • support for some other known wiki(s)
  • support for your custom wiki
  • lightweight project, well designed using OOP

Actually, this framework is useful for:

Download

git clone https://gitpull.it/source/boz-mw.git

Command line tools

See command line tools.

Command line script replace.php

The replace.php allows you to do some sobstitutions in a wiki.

Command line script mega-export.php

The mega-export.php allows you to export the _full_ page history of whatever page.

API framework showcase

Here some usage examples.

Start: select your wiki

First of all you should open the connection to your wiki.

Some examples:

<?php
require 'boz-mw/autoload.php';

// load Wikipedia in Italian
$itwiki = \wm\WikipediaIt::instance();

// load Wikidata
$wikidata = \wm\Wikidata::instance();

// load Wikimedia Commons
$commons = \wm\Commons::instance();

// load Wikimedia Commons (example 2)
$commons = web\MediaWikis::findFromUID( 'commonswiki' );

// load Wikipedia in Italian (example 2)
$itwiki = web\MediaWikis::findFromUID( 'itwiki' );

Basic API query

To obtain a simple information from the server (no continuation support):

<?php
require 'boz-mw/autoload.php';

// load it.wiki
$wiki = \wm\WikipediaIt::instance();

$response =
	$wiki->fetch( [
		'action' => 'query',
		'prop'   => 'info',
		'titles' => 'Pagina principale',
	] );

// get the first result (the only one)
$page = $response->query->pages[ 0 ];

// show interesting information
print_r( $page->title );
print_r( $page->pageid );

Note: See MediaWiki API action=query documentation.

API query with continuation

To obtain a long result set from the server (with continuation support):

<?php
require 'boz-mw/autoload.php';

$wiki = \wm\WikipediaIt::instance();

$queries =
	$wiki->createQuery( [
		'action'  => 'query',
		'list'    => 'categorymembers',
		'cmtitle' => 'Categoria:Software con licenza GNU GPL',
	] );

foreach( $queries as $query ) {
	print_r( $query );
}

Login and Edit API query

<?php
require 'boz-mw/autoload.php';

$wiki = \wm\WikipediaIt::instance();

$user     = '';
$password = '';
$wiki->login( $user, $password );

$wiki->edit( [
	'title'   => 'Special:Nothing',
	'text'    => 'My wikitext',
	'summary' => 'My edit summary',
] );
NOTE: See MediaWiki action=edit documentation. Yes, the token parameter is automatically handled for you automagically.

You can also call login() without parameters if you specify a global username and password on the top of your script:

\mw\API::$DEFAULT_USERNAME = '':
\mw\API::$DEFAULT_PASSWORD = '';

Wikidata SPARQL query

What if you want to list all the cats from Wikidata?

<?php
require 'boz-mw/autoload.php';

// you should know how to build a SPARQL query
$query  = 'SELECT ?item ?itemLabel WHERE {';
$query .= ' ?item wdt:P31 wd:Q146 ';
$query .= ' SERVICE wikibase:label { bd:serviceParam wikibase:language "en". } ';
$query .= '}';

// query Wikidata and decode the response
$rows = \wm\Wikidata::querySPARQL( $query );

// for each cat
foreach( $rows as $row ) {

	// example: 'http://www.wikidata.org/entity/Q5317221'
	$url = $row->item->value;

	// example: 'Q5317221'
	$id = basename( $url );

	// example: 'Dusty the Klepto Kitty'
	$itemLabel = $row->itemLabel->value;

	echo "Found cat ID: $id. Name: $itemLabel \n";
}

Wikidata edit API

<?php
require 'boz-mw/autoload.php';

$data = new \wb\DataModel();

// add a Commons category
$statement = new \wb\StatementCommonsCategory( 'P373', 'Test category name' );
$data->addClaim( $statement );

// set a new label value
$label = \wb\Label( 'en', "New label" );
$data->setLabel( $label );

// save
\wm\Wikidata::instance()->editEntity(
	'id'   => 'Q4115189',
	'data' => $data->getJSON(),
] );

Upload API query

Uploading a file requires to respect the RFC1341 about an HTTP multipart request.

Well, we made it easy:

<?php
require 'boz-mw/autoload.php';

// this can be an URL or a local pathname
$photo_url = 'http://.../libre-image.jpg';
$wiki->upload( [
	'comment'  => 'upload file about...',
	'text'     => 'bla bla [[bla]]',
	'filename' => 'Libre image.jpg',
	\network\ContentDisposition::createFromNameURLType( 'file', $photo_url, 'image/jpg' ),
] );

Eventually see the ContentDisposition class for some other constructors.

Where to test

Please use your own wiki to test this framework or at least use the Wikimedia Wikis' Sandboxes!

Some known pages you can destroy:

Other examples?

Feel free to fork and improve this documentation! Or just look inside the /include directory where there is some inline documentation for you!

Known usages

Last Author
valerio.bozzolan
Last Edited
Nov 29 2020, 16:39

Document Hierarchy

Event Timeline

valerio.bozzolan changed the title from First Steps With Boz-mw to First steps with boz-mw MediaWiki API framework.Mar 4 2020, 00:00
valerio.bozzolan created this object.
valerio.bozzolan edited the content of this document. (Show Details)Apr 21 2020, 08:29
valerio.bozzolan edited the content of this document. (Show Details)Sep 21 2020, 13:15
valerio.bozzolan edited the content of this document. (Show Details)Sep 21 2020, 14:02
valerio.bozzolan edited the content of this document. (Show Details)Sep 21 2020, 14:06
valerio.bozzolan changed the edit policy from "Administrators" to "All Users".
valerio.bozzolan edited the content of this document. (Show Details)Nov 22 2020, 00:54
valerio.bozzolan edited the content of this document. (Show Details)Nov 22 2020, 01:09
valerio.bozzolan edited the content of this document. (Show Details)
valerio.bozzolan edited the content of this document. (Show Details)Nov 29 2020, 16:39