3 * Renders a diff for a single slot.
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.
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.
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
21 * @ingroup DifferenceEngine
23 use Wikimedia\Assert\Assert
;
26 * Renders a diff for a single slot (that is, a diff between two content objects).
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.
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.
37 * @ingroup DifferenceEngine
39 abstract class SlotDiffRenderer
{
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
49 abstract public function getDiff( Content
$oldContent = null, Content
$newContent = null );
52 * Add modules needed for correct styling/behavior of the diff.
53 * @param OutputPage $output
55 public function addModules( OutputPage
$output ) {
59 * Return any extra keys to split the diff cache by.
62 public function getExtraCacheKeys() {
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
71 * @param Content|null &$oldContent
72 * @param Content|null &$newContent
73 * @param string|array|null $allowedClasses
75 protected function normalizeContents(
76 Content
&$oldContent = null, Content
&$newContent = null, $allowedClasses = null
78 if ( !$oldContent && !$newContent ) {
79 throw new InvalidArgumentException( '$oldContent and $newContent cannot both be null' );
82 if ( $allowedClasses ) {
83 if ( is_array( $allowedClasses ) ) {
84 $allowedClasses = implode( '|', $allowedClasses );
86 Assert
::parameterType( $allowedClasses . '|null', $oldContent, '$oldContent' );
87 Assert
::parameterType( $allowedClasses . '|null', $newContent, '$newContent' );
91 $oldContent = $newContent->getContentHandler()->makeEmptyContent();
92 } elseif ( !$newContent ) {
93 $newContent = $oldContent->getContentHandler()->makeEmptyContent();