Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
87.50% covered (warning)
87.50%
21 / 24
57.14% covered (warning)
57.14%
4 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
WikifunctionsCallDefaultValues
87.50% covered (warning)
87.50%
21 / 24
57.14% covered (warning)
57.14%
4 / 7
14.38
0.00% covered (danger)
0.00%
0 / 1
 getDefaultValueCallbacks
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 hasDefaultValueCallback
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDefaultValueForType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isTemporalType
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getDefaultLanguage
66.67% covered (warning)
66.67%
2 / 3
0.00% covered (danger)
0.00%
0 / 1
3.33
 getDefaultDate
80.00% covered (warning)
80.00%
4 / 5
0.00% covered (danger)
0.00%
0 / 1
3.07
 getWikidataItem
85.71% covered (warning)
85.71%
6 / 7
0.00% covered (danger)
0.00%
0 / 1
4.05
1<?php
2/**
3 * WikiLambda extension handler for default (empty) values for our parser function
4 *
5 * @file
6 * @ingroup Extensions
7 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
8 * @license MIT
9 */
10
11namespace MediaWiki\Extension\WikiLambda\ParserFunction;
12
13use DateTime;
14use DateTimeZone;
15use MediaWiki\Extension\WikiLambda\WikiLambdaServices;
16use MediaWiki\Parser\ParserOutput;
17use MediaWiki\Title\Title;
18use Wikimedia\Parsoid\Core\LinkTarget;
19
20class WikifunctionsCallDefaultValues {
21
22    private const LANGUAGE = 'Z60';
23    private const WIKIDATA_ITEM = 'Z6001';
24    private const WIKIDATA_ITEM_REFERENCE = 'Z6091';
25    private const GREGORIAN_CALENDAR_DATE = 'Z20420';
26
27    // Use ISO 8601 so the date reader reads it the same way in every language
28    public const GREGORIAN_CALENDAR_DATE_FORMAT = 'Y-m-d';
29
30    /**
31     * Returns the map of type IDs to default value callbacks.
32     * Each callback should return a default value for the given type.
33     *
34     * To add default values for other types:
35     * 1. Add a new entry in the returned array with the type zid
36     *    as the index, and a callable as its value.
37     * 2. The callable can be an anonymous function or a public static
38     *       named function, which should be implemented below.
39     *
40     * @return array
41     */
42    private static function getDefaultValueCallbacks(): array {
43        return [
44            self::LANGUAGE => [ self::class, 'getDefaultLanguage' ],
45            self::WIKIDATA_ITEM => [ self::class, 'getWikidataItem' ],
46            self::WIKIDATA_ITEM_REFERENCE => [ self::class, 'getWikidataItem' ],
47            self::GREGORIAN_CALENDAR_DATE => [ self::class, 'getDefaultDate' ],
48        ];
49    }
50
51    /**
52     * Checks whether a default value callback exists for a given type.
53     *
54     * @param string $type
55     * @return bool
56     */
57    public static function hasDefaultValueCallback( string $type ): bool {
58        return array_key_exists( $type, self::getDefaultValueCallbacks() );
59    }
60
61    /**
62     * Returns a callable that provides a default value for the given type.
63     *
64     * @param string $type
65     * @return callable|null
66     */
67    public static function getDefaultValueForType( string $type ): ?callable {
68        return self::getDefaultValueCallbacks()[ $type ] ?? null;
69    }
70
71    /**
72     * Returns whether the given type has a default value that changes with
73     * time; for example, now only "Gregorian calendar date", but there could
74     * be more time-dependent types in the future with different granularities.
75     *
76     * @param string $type
77     * @return bool
78     */
79    public static function isTemporalType( string $type ): bool {
80        return $type === self::GREGORIAN_CALENDAR_DATE;
81    }
82
83    // Callables for each type:
84
85    /**
86     * Default Value Callable for Language/Z60:
87     * Returns the language of the page
88     *
89     * @param array $context
90     * @return string
91     */
92    public static function getDefaultLanguage( array $context = [] ): string {
93        // Context doesn't have the info of the page language; return empty string
94        if ( !isset( $context['pageLanguage'] ) || $context['pageLanguage'] === '' ) {
95            return '';
96        }
97        // Return page language Bcp47 code, FunctionCallHandler will convert it to zid
98        return $context[ 'pageLanguage' ];
99    }
100
101    /**
102     * Default Value Callable for Date/Z20420:
103     * Returns today's date in the wiki's local timezone in the format 'yyyy-mm-dd'
104     *
105     * @param array $context
106     * @return string
107     */
108    public static function getDefaultDate( array $context = [] ): string {
109        global $wgLocaltimezone;
110
111        $cmc = $context['contentMetadataCollector'] ?? null;
112        // In some test cases, $cmc will be a StubMetadataCollector rather than a ParserOutput, so we can't do this
113        if ( $cmc && $cmc instanceof ParserOutput ) {
114            // Make sure our fragment's cache expiry is set to at most 24 hours, as we're adding
115            // a one-day-variant piece of content.
116            $cmc->updateRuntimeAdaptiveExpiry( 24 * 60 * 60 );
117        }
118
119        $date = new DateTime( 'now', new DateTimeZone( $wgLocaltimezone ?? 'UTC' ) );
120        return $date->format( self::GREGORIAN_CALENDAR_DATE_FORMAT );
121    }
122
123    /**
124     * Default Value Callable for Wikidata Item/Z6001 and Wikidata Item Reference/Z6091
125     * Returns the Wikidata Item ID linked to the client page
126     *
127     * @param array $context
128     * @return string
129     */
130    public static function getWikidataItem( array $context = [] ): string {
131        // Context doesn't have info of the page title; return empty string
132        if ( !isset( $context['linkTarget'] ) ||
133            !( $context['linkTarget'] instanceof LinkTarget ) ) {
134            return '';
135        }
136
137        $prefixedTitle = Title::newFromLinkTarget( $context['linkTarget'] )->getPrefixedText();
138
139        $entityLookup = WikiLambdaServices::getWikidataEntityLookup();
140        $entityId = $entityLookup->getWikidataItemForTitle( $prefixedTitle );
141
142        return $entityId === null ? '' : $entityId;
143    }
144}