commit | f57861bc8b2a7a5f72e2a34080fdd5ba0e74ed0b | [log] [tgz] |
---|---|---|
author | jenkins-bot <jenkins-bot@gerrit.wikimedia.org> | Tue Nov 12 16:01:59 2024 +0000 |
committer | Gerrit Code Review <gerrit@wikimedia.org> | Tue Nov 12 16:01:59 2024 +0000 |
tree | 9889a2fcfd55f6ba25c43856e01946ffc3bf74f6 | |
parent | e482feea0660cb721a45ad4f331ee4e86005b8c3 [diff] | |
parent | 063f20e673da30ff6a81fb03fd226d083d79fba5 [diff] |
Merge "More narrow return type for DefaultConfig trait"
Extension to Wikibase Repository that performs constraint checks.
Clone WikibaseQualityConstraints
inside the extensions/
directory of your MediaWiki installations.
cd .../extensions/ git clone https://gerrit.wikimedia.org/r/mediawiki/extensions/WikibaseQualityConstraints
Install dependencies. The simplest way is to set up composer-merge-plugin and then run composer install
in the MediaWiki base directory; alternatively, you can run composer install
inside the WikibaseQualityConstraints
directory.
Load the extension.
wfLoadExtension( 'WikibaseQualityConstraints' );
Run php maintenance/update.php --quick
.
Configure the extension. You can find the configuration settings with documentation in extension.json
. (Note that the variable name for LocalSettings.php
always begins with wg
, e. g. $wgWBQualityConstraintsClassId
for the WBQualityConstraintsClassId
setting.)
Specify the entity IDs of entities that are used to define constraints. See the “Data import” section for an automatic way to do this.
If you have one or more SPARQL endpoints, configure the primary endpoint in WBQualityConstraintsSparqlEndpoint
, and any additional endpoints in WBQualityConstraintsAdditionalSparqlEndpoints
.
Alternatively, to check “format” constraints without running a full SPARQL server, you can use the minisparql server.
Run php maintenance/run.php WikibaseQualityConstraints:ImportConstraintStatements.php
.
The extension includes a client-side script that checks constraints on entity pages and displays violations on statements. It is loaded by default for all logged-in users; anonymous users can load it on a page by entering the following code into the web console:
mw.loader.load( 'wikibase.quality.constraints.gadget' );
For local development, it’s easiest to import some data from Wikidata. You can use the ImportConstraintEntities
maintenance script script to do this; it will import all the required entities from Wikidata that don’t exist in your local wiki yet and then print a config snippet which you can append to your LocalSettings.php
.
# working directory should be the MediaWiki installation folder, i.e. where LocalSettings.php is php maintenance/run.php WikibaseQualityConstraints:ImportConstraintEntities.php | tee -a LocalSettings.php
(The new entities will not show up in your wiki’s recent changes until they have been processed in the job queue; try running the maintenance/runJobs.php
script if it doesn’t happen automatically.)
There are two ways to run the tests of this extension:
Using the included configuration file:
# from the MediaWiki installation folder composer phpunit -- -c extensions/WikibaseQualityConstraints/phpunit.xml.dist
This creates test coverage reports (in tests/coverage/
and build/logs/clover.xml
) and is therefore fairly slow.
Without the configuration file:
# from the MediaWiki installation folder composer phpunit:entrypoint extensions/WikibaseQualityConstraints/tests/phpunit/
This runs the tests without coverage report and is therefore much faster.
You can run the tests, combined with linting and few other tools for asserting code quality by
# from this extension's folder npm install grunt test
To add a new constraint type, the following steps are necessary:
src/ConstraintCheck/Checker/
, named after the class name. It should be in the WikibaseQuality\ConstraintReport\ConstraintCheck\Checker
namespace.ConstraintChecker
interface./** * @author YOUR NAME HERE * @license GPL-2.0-or-later */
Config
, EntityLookup
, …) should be injected as constructor parameters.ConstraintParameterParser
(add a config setting for the associated property in extension.json
and a method to parse the parameter in ConstraintParameterParser
), and then add tests for them in ConstraintParameterParserTest
. This should be done in a separate commit.i18n/en.json
. A violation message should have a key like wbqc-violation-message-constraintType
.i18n/qqq.json
. Use the same message key, and insert the documentation in the same location where you also added the message in en.json
(that is, en.json
and qqq.json
should contain message keys in the same order).extension.json
, as members of the config
object.…ConstraintId
entry.WBQualityConstraints…ConstraintId
."public": true
).ConstraintCheckerServices.php
, add a constant likepublic const …_CHECKER = 'WBQC_…Checker';at the end of the list of constants. The value should be
'WBQC_'
followed by the class name, and the constant name should be the class name converted to all caps separated by underscores.ConstraintCheckerServices.php
, add a method like/** * @param MediaWikiServices|null $services * @return ConstraintChecker */ public static function get…Checker( MediaWikiServices $services = null ) { return self::getService( $services, self::…_CHECKER ); }at the end of the class.
ServiceWiring-ConstraintCheckers.php
, append a new function likeConstraintCheckerServices::…_CHECKER => function( MediaWikiServices $services ) { return new …Checker( // injected services ); },to the array of services.
ServiceWiring.php
, append a new entry like$config->get( 'WBQualityConstraints…ConstraintId' ) => ConstraintCheckerServices::get…Checker( $services ),to the
$checkerMap
array in the DELEGATING_CONSTRAINT_CHECKER
function.ServicesTest.php
, append a new entry like[ …Checker::class ],to the array in
provideConstraintCheckerServiceClasses()
.Test
(i. e., …CheckerTest
).tests/phpunit/Checker/
, either in the most suitable subdirectory or directly in that directory if none of the subdirectories are suitable. (The division into subdirectories there is dubious anyways, and we may get rid of it in the future.)/** * @covers WikibaseQuality\ConstraintReport\ConstraintCheck\Checker\…Checker * * @group WikibaseQualityConstraints * * @author YOUR NAME HERE * @license GPL-2.0-or-later */
checkConstraintParameters
method.ResultAssertions
trait’s methods to check constraint check results.NewItem
and NewStatement
builders to construct test data.Config
, use the DefaultConfig
trait.ConstraintParameters
trait and use it in the tests.getConstraintMock
function from one of the existing tests, adjusting the getConstraintTypeItemId
mocked return value. (Hopefully we’ll improve this in the future.)DelegatingConstraintChecker
.DelegatingConstraintCheckerTest
, add an entry for your constraint type to the $constraints
array in addDBData()
.constraint_guid
should be P1$
, followed by a new UUID (e. g. cat /proc/sys/kernel/random/uuid
or journalctl --new-id128
).pid
should be 1
. (Not '1'
!)constraint_type_qid
should be $this->getConstraintTypeItemId( '…' )
, where …
is just the …
part of the WBQualityConstraints…ConstraintId
extension.json
config key.constraint_parameters
should be a valid JSON serialization of constraint parameters. If the constraint type doesn’t have any parameters, you can pass {}
, otherwise there should ideally be methods to create the parameters in the ConstraintParameters
trait so that you can use json_encode( $this->…Parameter( … ) )
(perhaps with array_merge
if there are multiple parameters).extension.json
. For example:"WBQualityConstraintsMediaInfoId": { "value": "Q3898244", "description": "The item ID of the 'MediaInfo' item, which represents the 'mediainfo' entity type for 'allowed entity types' constraints.", "public": true }
src/ConstraintCheck/Helper/ConstraintParameterParser.php
. The new allowed entity type should be added to the switch/case within the method parseEntityTypeItem()
. Don't forget to add it to the default case as well!You can see which dependencies have new releases by first running npm ci
to ensure your local dependencies are up-to-date, and then running npm outdated
.
.eslintrc.json
) since version 25.0.0 because of changes since eslint 9 (see issue #176). See T364065 for progress with our eslint 9 migration.Running composer update
and composer outdated --direct
will give you a list of any outdated PHP dependencies. There are currently no notes or restrictions on package updates.