Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 7
CRAP
0.00% covered (danger)
0.00%
0 / 1
DiffOpMove
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 7
56
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 getValue
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getOldIndex
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getNewIndex
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __serialize
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 __unserialize
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 toArray
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2/**
3 * WikiLambda DiffOpMove: a list item that kept its content but changed position.
4 *
5 * @file
6 * @ingroup Extensions
7 * @copyright 2020– Abstract Wikipedia team; see AUTHORS.txt
8 * @license MIT
9 */
10
11namespace MediaWiki\Extension\WikiLambda\Diff;
12
13use Diff\DiffOp\DiffOpChange;
14
15/**
16 * A re-ordering needs saying differently from a change of content: reporting it
17 * as a change describes the positions the item passed rather than the item that
18 * moved, and reporting it as a removal and an addition claims the item left the
19 * list and a different one arrived.
20 *
21 * It cannot, however, be a new kind of operation. Diff keeps a private, fixed
22 * list of the operation types it will hold — add, remove, change, list, map,
23 * diff — and throws on anything else, with no way to extend it. So a move is
24 * modelled as the specialisation of a change that it is: a change whose old and
25 * new values are the same item, which additionally knows where that item was and
26 * where it now is.
27 *
28 * That the type stays "change" is not a workaround to be undone later; it is
29 * what keeps the rules in authorization-rules.yml applying to a re-ordering
30 * exactly as they applied before it could be told apart, so recognising moves
31 * cannot reduce the rights an edit requires. Callers that want to distinguish a
32 * move must test for this class, not for the type.
33 *
34 * Both indices are as the item sits in its list, which for a typed list means
35 * that index 1 is the first item, index 0 being the reference to the type of the
36 * items.
37 */
38class DiffOpMove extends DiffOpChange {
39
40    private int $oldIndex;
41
42    private int $newIndex;
43
44    /**
45     * @param mixed $value The item, unchanged in content
46     * @param int $oldIndex Where it was
47     * @param int $newIndex Where it now is
48     */
49    public function __construct( $value, int $oldIndex, int $newIndex ) {
50        // The item did not change, so both sides of the change are the same.
51        parent::__construct( $value, $value );
52        $this->oldIndex = $oldIndex;
53        $this->newIndex = $newIndex;
54    }
55
56    /**
57     * The item that moved. Its content is the same on both sides, so this is
58     * simply a clearer name for either of them.
59     *
60     * @return mixed
61     */
62    public function getValue() {
63        return $this->getNewValue();
64    }
65
66    /**
67     * @return int
68     */
69    public function getOldIndex(): int {
70        return $this->oldIndex;
71    }
72
73    /**
74     * @return int
75     */
76    public function getNewIndex(): int {
77        return $this->newIndex;
78    }
79
80    /**
81     * @return array
82     */
83    public function __serialize(): array {
84        return [ $this->getNewValue(), $this->oldIndex, $this->newIndex ];
85    }
86
87    /**
88     * @param array $data
89     */
90    public function __unserialize( $data ): void {
91        [ $value, $this->oldIndex, $this->newIndex ] = $data;
92        parent::__unserialize( [ $value, $value ] );
93    }
94
95    /**
96     * @see DiffOp::toArray
97     * @param callable|null $valueConverter optional callback used to convert any
98     *        complex values to arrays.
99     * @return array
100     */
101    public function toArray( ?callable $valueConverter = null ): array {
102        return [
103            'type' => $this->getType(),
104            'value' => $this->objectToArray( $this->getNewValue(), $valueConverter ),
105            'oldindex' => $this->oldIndex,
106            'newindex' => $this->newIndex,
107        ];
108    }
109}