Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
CRAP
100.00% covered (success)
100.00%
1 / 1
DiffItemIdentifier
100.00% covered (success)
100.00%
16 / 16
100.00% covered (success)
100.00%
3 / 3
13
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getHandle
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
5
 describe
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
7
1<?php
2/**
3 * WikiLambda DiffItemIdentifier: names an item of a list by its content, so a
4 * diff need not refer to it by its position.
5 *
6 * @file
7 * @ingroup Extensions
8 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
9 * @license MIT
10 */
11
12namespace MediaWiki\Extension\WikiLambda\Diff;
13
14use MediaWiki\Extension\WikiLambda\ZObjectUtils;
15
16/**
17 * Answers "which one is this?" for an item of a typed list.
18 *
19 * A list index is a poor way to name a change: it means nothing to a reader,
20 * and it is not even stable, since inserting or removing an item renumbers
21 * every item after it. What a reader wants is the item's identity — which
22 * argument, which key, which option — and that is derivable from the item's
23 * own content.
24 *
25 * Which part of the content identifies an item is DiffItemKeyer's business;
26 * this class turns that part into something a reader can read, resolving keys
27 * and references to their labels.
28 *
29 * The handles produced are plain text, never HTML. They are for display only:
30 * they are localised, and they may be shared by items a reader would not
31 * confuse, so they must never be used to pair items across revisions — that is
32 * what DiffItemKeyer::getJoinKey() is for.
33 */
34class DiffItemIdentifier {
35
36    /**
37     * The longest plain string accepted as a handle. A handle stands in for an
38     * index in a breadcrumb, so it has to stay short enough to read as a name;
39     * anything longer says more about the change than about which item changed.
40     */
41    private const MAX_HANDLE_LENGTH = 40;
42
43    public function __construct(
44        private readonly DiffLabelResolver $labels
45    ) {
46    }
47
48    /**
49     * Name a list item by its content, or return null when nothing about it
50     * identifies it and the caller should fall back to its position.
51     *
52     * @param mixed $item The item, as a nested array or a leaf
53     * @return string|null Plain text, not HTML
54     */
55    public function getHandle( $item ): ?string {
56        if ( !is_array( $item ) ) {
57            return $this->describe( $item );
58        }
59
60        // A monolingual value is identified by its language, as a multilingual
61        // container holds at most one entry per language.
62        if ( DiffItemKeyer::isMonolingual( $item ) ) {
63            $languageZid = DiffItemKeyer::languageZidOf( $item );
64            return $languageZid === null ? null : $this->labels->getLanguage( $languageZid )['name'];
65        }
66
67        $key = DiffItemKeyer::identifyingKey( $item );
68        return $key === null ? null : $this->describe( $item[$key] );
69    }
70
71    /**
72     * Turn an identifying value into display text: a key becomes its label, a
73     * reference becomes the label of its target, and a short string stands for
74     * itself. Anything else — a nested structure, a long string — identifies
75     * nothing readably and gives null.
76     *
77     * @param mixed $value
78     * @return string|null
79     */
80    private function describe( $value ): ?string {
81        if ( !is_string( $value ) || $value === '' ) {
82            return null;
83        }
84
85        if ( ZObjectUtils::isValidZObjectGlobalKey( $value ) ) {
86            return $this->labels->getKeyLabel( $value );
87        }
88
89        if ( ZObjectUtils::isValidZObjectReference( $value ) ) {
90            $reference = $this->labels->getReference( $value );
91            return ( $reference === null ) ? $value : ( $reference['label'] ?? $value );
92        }
93
94        return mb_strlen( $value ) <= self::MAX_HANDLE_LENGTH ? $value : null;
95    }
96}