Merge "Simplify HTMLTitleTextField::validate"
[lhc/web/wiklou.git] / includes / diff / DifferenceEngineSlotDiffRenderer.php
1 <?php
2 /**
3 * Adapter for turning a DifferenceEngine into a SlotDiffRenderer.
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 /**
25 * B/C adapter for turning a DifferenceEngine into a SlotDiffRenderer.
26 * Before SlotDiffRenderer was introduced, getDiff() functionality was provided by DifferenceEngine
27 * subclasses. Convert such a subclass into a SlotDiffRenderer.
28 * @deprecated
29 * @ingroup DifferenceEngine
30 */
31 class DifferenceEngineSlotDiffRenderer extends SlotDiffRenderer {
32
33 /** @var DifferenceEngine */
34 private $differenceEngine;
35
36 public function __construct( DifferenceEngine $differenceEngine ) {
37 $this->differenceEngine = clone $differenceEngine;
38
39 // Set state to loaded. This should not matter to any of the methods invoked by
40 // the adapter, but just in case a load does get triggered somehow, make sure it's a no-op.
41 $fakeContent = ContentHandler::getForModelID( CONTENT_MODEL_WIKITEXT )->makeEmptyContent();
42 $this->differenceEngine->setContent( $fakeContent, $fakeContent );
43
44 $this->differenceEngine->markAsSlotDiffRenderer();
45 }
46
47 /** @inheritDoc */
48 public function getDiff( Content $oldContent = null, Content $newContent = null ) {
49 if ( !$oldContent && !$newContent ) {
50 throw new InvalidArgumentException( '$oldContent and $newContent cannot both be null' );
51 }
52 if ( !$oldContent || !$newContent ) {
53 $someContent = $newContent ?: $oldContent;
54 $emptyContent = $someContent->getContentHandler()->makeEmptyContent();
55 $oldContent = $oldContent ?: $emptyContent;
56 $newContent = $newContent ?: $emptyContent;
57 }
58 return $this->differenceEngine->generateContentDiffBody( $oldContent, $newContent );
59 }
60
61 public function addModules( OutputPage $output ) {
62 $oldContext = null;
63 if ( $output !== $this->differenceEngine->getOutput() ) {
64 $oldContext = $this->differenceEngine->getContext();
65 $newContext = new DerivativeContext( $oldContext );
66 $newContext->setOutput( $output );
67 $this->differenceEngine->setContext( $newContext );
68 }
69 $this->differenceEngine->showDiffStyle();
70 if ( $oldContext ) {
71 $this->differenceEngine->setContext( $oldContext );
72 }
73 }
74
75 public function getExtraCacheKeys() {
76 return $this->differenceEngine->getExtraCacheKeys();
77 }
78
79 }