Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
WikidataProperties
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
1 / 1
 isItemId
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare( strict_types = 1 );
4
5namespace MediaWiki\Extension\ArticleGuidance;
6
7/**
8 * Wikidata property and entity ID constants used across the extension.
9 */
10class WikidataProperties {
11
12    // Properties (P-IDs)
13
14    /** P18 – image */
15    public const PROP_IMAGE = 'P18';
16
17    /** P31 – instance of */
18    public const PROP_INSTANCE_OF = 'P31';
19
20    /** P106 – occupation */
21    public const PROP_OCCUPATION = 'P106';
22
23    /** P171 – parent taxon */
24    public const PROP_PARENT_TAXON = 'P171';
25
26    /** P279 – subclass of */
27    public const PROP_SUBCLASS_OF = 'P279';
28
29    /** Human-readable names for known properties, keyed by property ID */
30    public const PROPERTY_NAMES = [
31        self::PROP_IMAGE => 'image',
32        self::PROP_INSTANCE_OF => 'instance of',
33        self::PROP_OCCUPATION => 'occupation',
34        self::PROP_PARENT_TAXON => 'parent taxon',
35        self::PROP_SUBCLASS_OF => 'subclass of',
36    ];
37
38    /**
39     * Shape of a Wikidata item ID. Same rule as Wikibase's ItemId::PATTERN, which this
40     * extension cannot use because it does not depend on Wikibase. Lowercase "q" is
41     * not accepted.
42     */
43    private const ITEM_ID_PATTERN = '/^Q[1-9]\d{0,9}\z/';
44
45    /** Largest item number that Wikibase accepts (ItemId is an Int32EntityId) */
46    private const MAX_ITEM_NUMBER = 2147483647;
47
48    /**
49     * Whether a string is a Wikidata item ID that can exist, e.g. "Q42".
50     *
51     * Wikidata rejects a whole wbgetentities request when one of its IDs is
52     * "Q0", has leading zeros or is too large, so these must be refused before
53     * they reach a request.
54     *
55     * @param string $id
56     * @return bool
57     */
58    public static function isItemId( string $id ): bool {
59        return preg_match( self::ITEM_ID_PATTERN, $id ) === 1
60            && (int)substr( $id, 1 ) <= self::MAX_ITEM_NUMBER;
61    }
62}