Debian Codegraph @

includes/diff/SlotDiffRenderer.php

raw symbols SCIP
1 <?php
2 /**
3 * Renders a diff for a single slot.
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 * @ingroup DifferenceEngine
22 */
23
24 use MediaWiki\Content\Content;
25 use MediaWiki\Context\IContextSource;
26 use MediaWiki\Output\OutputPage;
27 use MediaWiki\Title\Title;
28 use Wikimedia\Assert\Assert;
29
30 /**
31 * Renders a diff for a single slot (that is, a diff between two content objects).
32 *
33 * Callers should obtain instances of this class by invoking ContentHandler::getSlotDiffRenderer
34 * on the content handler of the new content object (ie. the one shown on the right side
35 * of the diff), or of the old one if the new one does not exist.
36 *
37 * The default implementation just does a text diff on the native text representation.
38 * Content handler extensions can subclass this to provide a more appropriate diff method by
39 * overriding ContentHandler::getSlotDiffRendererInternal. Other extensions that want to interfere
40 * with diff generation in some way can use the GetSlotDiffRenderer hook.
41 *
42 * @stable to extend
43 * @ingroup DifferenceEngine
44 */
45 abstract class SlotDiffRenderer {
46
47 /**
48 * Get a diff between two content objects. One of them might be null (meaning a slot was
49 * created or removed), but both cannot be. $newContent (or if it's null then $oldContent)
50 * must have the same content model that was used to obtain this diff renderer.
51 * @param Content|null $oldContent
52 * @param Content|null $newContent
53 * @return string HTML. One or more <tr> tags, or an empty string if the inputs are identical.
54 * @throws IncompatibleDiffTypesException
55 */
56 abstract public function getDiff( ?Content $oldContent = null, ?Content $newContent = null );
57
58 /**
59 * Localize language-independent text returned by getDiff(), making it
60 * suitable for display. Subclasses overriding this should arrange for
61 * injection of a MessageLocalizer.
62 *
63 * @param string $diff
64 * @param array $options Associative array of options:
65 * - reducedLineNumbers: If true, remove "line 1" but allow other line numbers
66 * @return string
67 */
68 public function localizeDiff( string $diff, array $options = [] ) {
69 return $diff;
70 }
71
72 /**
73 * Get the content to add above the main diff table.
74 *
75 * @since 1.41
76 * @param IContextSource $context
77 * @param Title $newTitle
78 * @return (string|null)[] An array of HTML fragments to assemble into the prefix
79 * area. They will be deduplicated and sorted by key.
80 */
81 public function getTablePrefix( IContextSource $context, Title $newTitle ): array {
82 return [];
83 }
84
85 /**
86 * Add modules needed for correct styling/behavior of the diff.
87 * @stable to override
88 * @param OutputPage $output
89 */
90 public function addModules( OutputPage $output ) {
91 }
92
93 /**
94 * Return any extra keys to split the diff cache by.
95 * @stable to override
96 * @return string[]
97 */
98 public function getExtraCacheKeys() {
99 return [];
100 }
101
102 /**
103 * Helper method to normalize the input of getDiff().
104 * Verifies that at least one of $oldContent and $newContent is not null, verifies that
105 * they are instances of one of the allowed classes (if provided), and replaces null with
106 * empty content.
107 * @param Content|null &$oldContent
108 * @param Content|null &$newContent
109 * @param string|array|null $allowedClasses
110 * @throws IncompatibleDiffTypesException
111 */
112 protected function normalizeContents(
113 ?Content &$oldContent = null, ?Content &$newContent = null, $allowedClasses = null
114 ) {
115 if ( !$oldContent && !$newContent ) {
116 throw new InvalidArgumentException( '$oldContent and $newContent cannot both be null' );
117 }
118
119 if ( $allowedClasses ) {
120 if ( is_string( $allowedClasses ) ) {
121 $allowedClasses = explode( '|', $allowedClasses );
122 }
123 $allowedClassesOrNull = array_merge( $allowedClasses, [ 'null' ] );
124
125 // The new content (or the old one if the new one is null) must always match the renderer
126 // since the ContentHandler of that model should be used to create it
127 Assert::parameterType( $allowedClassesOrNull, $newContent, '$newContent' );
128 if ( !$newContent ) {
129 Assert::parameterType( $allowedClassesOrNull, $oldContent, '$oldContent' );
130 }
131
132 // If there are two content objects, the old one can be arbitrary as it is possible
133 // to generate a diff between any two revisions; so an incompatible model should be
134 // handled as a user error, not a logic error.
135 if ( $oldContent && $newContent ) {
136 $allowed = false;
137 foreach ( $allowedClasses as $class ) {
138 if ( $oldContent instanceof $class ) {
139 $allowed = true;
140 break;
141 }
142 }
143 if ( !$allowed ) {
144 throw new IncompatibleDiffTypesException( $oldContent->getModel(), $newContent->getModel() );
145 }
146 }
147 }
148
149 if ( !$oldContent ) {
150 $oldContent = $newContent->getContentHandler()->makeEmptyContent();
151 } elseif ( !$newContent ) {
152 $newContent = $oldContent->getContentHandler()->makeEmptyContent();
153 }
154 }
155
156 }