Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
AbstractContentUtils
100.00% covered (success)
100.00%
12 / 12
100.00% covered (success)
100.00%
4 / 4
8
100.00% covered (success)
100.00%
1 / 1
 isValidWikidataItemReference
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isValidAbstractWikiTitle
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 isNullWikidataItemReference
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 makeCacheKeyForAbstractFragment
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
5
1<?php
2/**
3 * WikiLambda Abstract Content utilities class
4 *
5 * @file
6 * @ingroup Extensions
7 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
8 * @license MIT
9 */
10
11namespace MediaWiki\Extension\WikiLambda\AbstractContent;
12
13class AbstractContentUtils {
14    /**
15     * Is the input a Wikidata item reference key (e.g. Q1 or Q12345)?
16     *
17     * @param string $input
18     * @return bool
19     */
20    public static function isValidWikidataItemReference( string $input ): bool {
21        return preg_match( "/^Q[1-9]\d*$/", $input );
22    }
23
24    /**
25     * Is the input a valid Abstract Content title?
26     * * With or without namespace preceding the Id
27     * * Id is a valid Wikidata item reference (e.g. Q1 or Q12345)
28     *
29     * @param string $input
30     * @return bool
31     */
32    public static function isValidAbstractWikiTitle( string $input ): bool {
33        return preg_match( '/^(?:[^:\/]+:)?Q[1-9]\d*$/', $input );
34    }
35
36    /**
37     * Is the input a null Wikidata item reference (Q0)?
38     *
39     * @param string $input
40     * @return bool
41     */
42    public static function isNullWikidataItemReference( string $input ): bool {
43        return ( $input === 'Q0' );
44    }
45
46    /**
47     * Walk a given input Object, and make a cache key constructed of its keys and values.
48     * This is intended to build a key for an Abstract Content fragment while in Abstract mode,
49     * and unlike ZObject cache keys in Repo Mode, it won't add revision Ids to the references.
50     *
51     * NOTE: Whether the input is passed as a stdClass or as associative array it will not
52     * make any difference for the output string key, as demonstrated in the test cases.
53     *
54     * @param \stdClass|array $query
55     * @return string
56     */
57    public static function makeCacheKeyForAbstractFragment( $query ): string {
58        $accumulator = '';
59
60        foreach ( $query as $key => $value ) {
61            $accumulator .= $key . '|';
62            if ( is_array( $value ) || is_object( $value ) ) {
63                $accumulator .= self::makeCacheKeyForAbstractFragment( $value );
64            } elseif ( is_scalar( $value ) ) {
65                $accumulator .= $value;
66            }
67            $accumulator .= ',';
68        }
69
70        return $accumulator;
71    }
72}