Merge "rdbms: Use correct value for 'sslmode' in DatabasePostgres"
[lhc/web/wiklou.git] / includes / diff / SlotDiffRenderer.php
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 use Wikimedia\Assert\Assert;
24
25 /**
26 * Renders a diff for a single slot (that is, a diff between two content objects).
27 *
28 * Callers should obtain this class by invoking ContentHandler::getSlotDiffRendererClass
29 * on the content handler of the new content object (ie. the one shown on the right side
30 * of the diff), or of the old one if the new one does not exist.
31 *
32 * The default implementation just does a text diff on the native text representation.
33 * Content handler extensions can subclass this to provide a more appropriate diff method by
34 * overriding ContentHandler::getSlotDiffRendererClass. Other extensions that want to interfere
35 * with diff generation in some way can use the GetSlotDiffRenderer hook.
36 *
37 * @ingroup DifferenceEngine
38 */
39 abstract class SlotDiffRenderer {
40
41 /**
42 * Get a diff between two content objects. One of them might be null (meaning a slot was
43 * created or removed), but both cannot be. $newContent (or if it's null then $oldContent)
44 * must have the same content model that was used to obtain this diff renderer.
45 * @param Content|null $oldContent
46 * @param Content|null $newContent
47 * @return string
48 */
49 abstract public function getDiff( Content $oldContent = null, Content $newContent = null );
50
51 /**
52 * Add modules needed for correct styling/behavior of the diff.
53 * @param OutputPage $output
54 */
55 public function addModules( OutputPage $output ) {
56 }
57
58 /**
59 * Return any extra keys to split the diff cache by.
60 * @return array
61 */
62 public function getExtraCacheKeys() {
63 return [];
64 }
65
66 /**
67 * Helper method to normalize the input of getDiff().
68 * Verifies that at least one of $oldContent and $newContent is not null, verifies that
69 * they are instances of one of the allowed classes (if provided), and replaces null with
70 * empty content.
71 * @param Content|null &$oldContent
72 * @param Content|null &$newContent
73 * @param string|array|null $allowedClasses
74 */
75 protected function normalizeContents(
76 Content &$oldContent = null, Content &$newContent = null, $allowedClasses = null
77 ) {
78 if ( !$oldContent && !$newContent ) {
79 throw new InvalidArgumentException( '$oldContent and $newContent cannot both be null' );
80 }
81
82 if ( $allowedClasses ) {
83 if ( is_array( $allowedClasses ) ) {
84 $allowedClasses = implode( '|', $allowedClasses );
85 }
86 Assert::parameterType( $allowedClasses . '|null', $oldContent, '$oldContent' );
87 Assert::parameterType( $allowedClasses . '|null', $newContent, '$newContent' );
88 }
89
90 if ( !$oldContent ) {
91 $oldContent = $newContent->getContentHandler()->makeEmptyContent();
92 } elseif ( !$newContent ) {
93 $newContent = $oldContent->getContentHandler()->makeEmptyContent();
94 }
95 }
96
97 }