wiki.techinc.nl/includes/diff/DifferenceEngineSlotDiffRenderer.php

72 lines
2.6 KiB
PHP
Raw Normal View History

[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
<?php
/**
* Adapter for turning a DifferenceEngine into a SlotDiffRenderer.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*
* @file
* @ingroup DifferenceEngine
*/
/**
* B/C adapter for turning a DifferenceEngine into a SlotDiffRenderer.
* Before SlotDiffRenderer was introduced, getDiff() functionality was provided by DifferenceEngine
* subclasses. Convert such a subclass into a SlotDiffRenderer.
* @deprecated
* @ingroup DifferenceEngine
*/
class DifferenceEngineSlotDiffRenderer extends SlotDiffRenderer {
/** @var DifferenceEngine */
private $differenceEngine;
public function __construct( DifferenceEngine $differenceEngine ) {
$this->differenceEngine = clone $differenceEngine;
// Set state to loaded. This should not matter to any of the methods invoked by
// the adapter, but just in case a load does get triggered somehow, make sure it's a no-op.
$fakeContent = ContentHandler::getForModelID( CONTENT_MODEL_WIKITEXT )->makeEmptyContent();
$this->differenceEngine->setContent( $fakeContent, $fakeContent );
$this->differenceEngine->markAsSlotDiffRenderer();
}
/** @inheritDoc */
public function getDiff( Content $oldContent = null, Content $newContent = null ) {
$this->normalizeContents( $oldContent, $newContent );
[MCR] Render multi-slot diffs Move logic for rendering a diff between two content objects out of DifferenceEngine, into a new SlotDiffRenderer class. Make DifferenceEngine use multiple SlotDiffRenderers, one per slot. This separates the class tree for changing high-level diff properties such as the header or the revision selection method (same as before: subclass DifferenceEngine and override ContentHandler::getDiffEngineClass or implement GetDifferenceEngine) and the one for changing the actual diff rendering for a given content type (subclass SlotDiffRenderer and override ContentHandler::getSlotDiffRenderer or implement GetSlotDiffRenderer). To keep B/C, when SlotDiffRenderer is not overridden for a given content type but DifferenceEngine is, that DifferenceEngine will be used instead. The weak point of the scheme is overriding the DifferenceEngine methods passing control to the SlotDiffRenderers (the ones calling getDifferenceEngines), without calling the parent. These are: showDiffStyle, getDiffBody, getDiffBodyCacheKeyParams. Extensions doing that will probably break in unpredictable ways (most likely, only showing the main slot diff). Nothing in gerrit does it, at least. A new GetSlotDiffRenderer hook is added to modify rendering for content models not owned by the extension, much like how GetDifferenceEngine works. Also deprecates public access to mNewRev/mOldRev and creates public getters instead. DifferenceEngine never supported external changes to those properties, this just acknowledges it. Bug: T194731 Change-Id: I2f8a9dbebd2290b7feafb20e2bb2a2693e18ba11 Depends-On: I04e885a33bfce5bccc807b9bcfe1eaa577a9fd47 Depends-On: I203d8895bf436b7fee53fe4718dede8a3b1768bc
2018-07-11 09:24:07 +00:00
return $this->differenceEngine->generateContentDiffBody( $oldContent, $newContent );
}
public function addModules( OutputPage $output ) {
$oldContext = null;
if ( $output !== $this->differenceEngine->getOutput() ) {
$oldContext = $this->differenceEngine->getContext();
$newContext = new DerivativeContext( $oldContext );
$newContext->setOutput( $output );
$this->differenceEngine->setContext( $newContext );
}
$this->differenceEngine->showDiffStyle();
if ( $oldContext ) {
$this->differenceEngine->setContext( $oldContext );
}
}
public function getExtraCacheKeys() {
return $this->differenceEngine->getExtraCacheKeys();
}
}