Developing extensions

From semantic-mediawiki.org
Jump to: navigation, search

Our Github repositories are generally follow the guideline explained on this page.

Specifying a dependency

It is a recommended guideline by this project that an extension specifies its dependency to ensure it is tested and usable for a Semantic MediaWiki release it was intended for by maintaining a composer.json with something like:

"require": {
	"php": ">=5.3.2",
	"composer/installers": "^1.0.12",
	"mediawiki/semantic-media-wiki": "~2.3"
},

...

"autoload": {
	"files" : [
		"SemanticFoo.php"
	],
	"psr-4": {
		"SFOO\\": "src/"
	}
},

Furthermore, using Composer also ensures that classes are autoloaded (PSR-4) without the need to maintain a list of class names.

It is not always easy to minimize direct access to GLOBALS and when a reference is necessary, using $GLOBALS is the recommended convention.

Writing code

It has been good practice to structure the code base around the following:

 |-i18n/
 |-src/
 |-tests/
 |- ...

Code quality should be considered when writing an extension in order for fellow developers to be able to understand, read, and review the code.

Some guidelines are:

Writing tests

It is suggested that an extension provides tests so that referenced API's (interfaces, classes etc.) used with either MediaWiki or Semantic MediaWiki can be tested and are verifiable (and in case those were modified can be caught before deployment).

Continuous integration

wp:Continuous integration can help increase the quality of a deployable extension. Most repositories are using Travis-CI which is fairly easy to setup and integrable with SMW.

To ease execution and usage, the following can be added to your composer.json which allows to run a test suite via composer phpunit.

"config": {
	"process-timeout": 0
},
"scripts": {
	"phpunit": "php ../../tests/phpunit/phpunit.php -c phpunit.xml.dist"
}

To minimize the chance of a potential breakage, we

To finalize the setup, the following should be added:

  • A .travis.yml to describe the environment tests are expected be executed
  • Copy several scripts (examples here can be mostly copied with minor adaptation), to direct the download and setup of the environment

Having followed above steps, the output of composer phpunit (from an extension base directory) should be similar to:

composer phpunit
php ../../tests/phpunit/phpunit.php -c phpunit.xml.dist

Semantic Result Formats: 2.3

Semantic MediaWiki: 2.4-alpha (SMWSQLStore3, mysql)
MediaWiki:          1.25.1 (MediaWiki vendor autoloader)

Execution time:     2016-01-26 06:01
Xdebug:             2.3.3 (enabled)

PHPUnit 3.7.38 by Sebastian Bergmann.
Configuration read from
/home/travis/build/SemanticMediaWiki/mw/extensions/SemanticResultFormats/phpunit.xml.dist

..............................................
Time: 194 ms, Memory: 21.00Mb

OK (46 tests, 46 assertions)

See also