| 1 | <?php |
| 2 | |
| 3 | namespace MediaWiki\Extension\Notifications; |
| 4 | |
| 5 | /** |
| 6 | * MediaWiki Extension: Echo |
| 7 | * |
| 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 9 | * of this software and associated documentation files (the "Software"), to deal |
| 10 | * in the Software without restriction, including without limitation the rights |
| 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 12 | * copies of the Software, and to permit persons to whom the Software is |
| 13 | * furnished to do so, subject to the following conditions: |
| 14 | * |
| 15 | * The above copyright notice and this permission notice shall be included in |
| 16 | * all copies or substantial portions of the Software. |
| 17 | * |
| 18 | * This program is distributed WITHOUT ANY WARRANTY. |
| 19 | */ |
| 20 | |
| 21 | /** |
| 22 | * @file |
| 23 | * @ingroup Extensions |
| 24 | * @author Erik Bernhardson |
| 25 | */ |
| 26 | |
| 27 | /** |
| 28 | * Represents a single set of changes all effecting neighboring lines |
| 29 | */ |
| 30 | class DiffGroup { |
| 31 | /** |
| 32 | * @var int[] The left and right position this change starts at |
| 33 | */ |
| 34 | protected $position; |
| 35 | |
| 36 | /** |
| 37 | * @var string[] The lines that have been added |
| 38 | */ |
| 39 | protected $new = []; |
| 40 | |
| 41 | /** |
| 42 | * @var string[] The lines that have been removed |
| 43 | */ |
| 44 | protected $old = []; |
| 45 | |
| 46 | /** |
| 47 | * @param int $leftPos The starting line number in the left text |
| 48 | * @param int $rightPos The starting line number in the right text |
| 49 | */ |
| 50 | public function __construct( $leftPos, $rightPos ) { |
| 51 | // +1 due to the original code use 1 indexing for this result |
| 52 | $this->position = [ |
| 53 | 'right-pos' => $rightPos + 1, |
| 54 | 'left-pos' => $leftPos + 1, |
| 55 | ]; |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * @param string $line Line in the right text but not in the left |
| 60 | */ |
| 61 | public function add( $line ) { |
| 62 | $this->new[] = $line; |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * @param string $line Line in the left text but not in the right |
| 67 | */ |
| 68 | public function subtract( $line ) { |
| 69 | $this->old[] = $line; |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * @return array[] set of changes |
| 74 | * Each change consists of: |
| 75 | * An 'action', one of: |
| 76 | * - add |
| 77 | * - subtract |
| 78 | * - change |
| 79 | * 'content' that was added or removed, or in the case |
| 80 | * of a change, 'old_content' and 'new_content' |
| 81 | * 'left_pos' and 'right_pos' (in 1-indexed lines) of the change. |
| 82 | */ |
| 83 | public function getChangeSet() { |
| 84 | $old = implode( "\n", $this->old ); |
| 85 | $new = implode( "\n", $this->new ); |
| 86 | $position = $this->position; |
| 87 | $changeSet = []; |
| 88 | |
| 89 | // The implodes must come first because we consider [ '' ] to also be false |
| 90 | // meaning a blank link replaced with content is an addition |
| 91 | if ( $old && $new ) { |
| 92 | $min = min( count( $this->old ), count( $this->new ) ); |
| 93 | $changeSet[] = $position + [ |
| 94 | 'action' => 'change', |
| 95 | 'old_content' => implode( "\n", array_slice( $this->old, 0, $min ) ), |
| 96 | 'new_content' => implode( "\n", array_slice( $this->new, 0, $min ) ), |
| 97 | ]; |
| 98 | $position['left-pos'] += $min; |
| 99 | $position['right-pos'] += $min; |
| 100 | $old = implode( "\n", array_slice( $this->old, $min ) ); |
| 101 | $new = implode( "\n", array_slice( $this->new, $min ) ); |
| 102 | } |
| 103 | |
| 104 | if ( $new ) { |
| 105 | $changeSet[] = $position + [ |
| 106 | 'action' => 'add', |
| 107 | 'content' => $new, |
| 108 | ]; |
| 109 | } elseif ( $old ) { |
| 110 | $changeSet[] = $position + [ |
| 111 | 'action' => 'subtract', |
| 112 | 'content' => $old, |
| 113 | ]; |
| 114 | } |
| 115 | |
| 116 | return $changeSet; |
| 117 | } |
| 118 | } |