1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205
|
<?php
namespace MediaWiki\Rest\Handler;
use MediaWiki\Content\TextContent;
use MediaWiki\Parser\ParserFactory;
use MediaWiki\Rest\Handler;
use MediaWiki\Rest\LocalizedHttpException;
use MediaWiki\Rest\StringStream;
use MediaWiki\Revision\RevisionAccessException;
use MediaWiki\Revision\RevisionLookup;
use MediaWiki\Revision\RevisionRecord;
use MediaWiki\Revision\SlotRecord;
use MediaWiki\Revision\SuppressedDataException;
use Wikimedia\Message\MessageValue;
use Wikimedia\ParamValidator\ParamValidator;
class CompareHandler extends Handler {
private RevisionLookup $revisionLookup;
private ParserFactory $parserFactory;
/** @var RevisionRecord[] */
private $revisions = [];
/** @var string[] */
private $textCache = [];
public function __construct(
RevisionLookup $revisionLookup,
ParserFactory $parserFactory
) {
$this->revisionLookup = $revisionLookup;
$this->parserFactory = $parserFactory;
}
public function execute() {
$fromRev = $this->getRevisionOrThrow( 'from' );
$toRev = $this->getRevisionOrThrow( 'to' );
if ( $fromRev->getPageId() !== $toRev->getPageId() ) {
throw new LocalizedHttpException(
new MessageValue( 'rest-compare-page-mismatch' ), 400 );
}
if ( !$this->getAuthority()->authorizeRead( 'read', $toRev->getPage() ) ) {
throw new LocalizedHttpException(
new MessageValue( 'rest-compare-permission-denied' ), 403 );
}
$data = [
'from' => [
'id' => $fromRev->getId(),
'slot_role' => $this->getRole(),
'sections' => $this->getSectionInfo( 'from' )
],
'to' => [
'id' => $toRev->getId(),
'slot_role' => $this->getRole(),
'sections' => $this->getSectionInfo( 'to' )
],
'diff' => [ 'PLACEHOLDER' => null ]
];
$rf = $this->getResponseFactory();
$wrapperJson = $rf->encodeJson( $data );
$diff = $this->getJsonDiff();
$response = $rf->create();
$response->setHeader( 'Content-Type', 'application/json' );
// A hack until getJsonDiff() is moved to SlotDiffRenderer and only nested inner diff is returned
$innerDiff = substr( $diff, 1, -1 );
$response->setBody( new StringStream(
str_replace( '"diff":{"PLACEHOLDER":null}', $innerDiff, $wrapperJson ) ) );
return $response;
}
/**
* @param string $paramName
* @return RevisionRecord|null
*/
private function getRevision( $paramName ) {
if ( !isset( $this->revisions[$paramName] ) ) {
$this->revisions[$paramName] =
$this->revisionLookup->getRevisionById( $this->getValidatedParams()[$paramName] );
}
return $this->revisions[$paramName];
}
/**
* @param string $paramName
* @return RevisionRecord
* @throws LocalizedHttpException
*/
private function getRevisionOrThrow( $paramName ) {
$rev = $this->getRevision( $paramName );
if ( !$rev ) {
throw new LocalizedHttpException(
new MessageValue( 'rest-compare-nonexistent', [ $paramName ] ), 404 );
}
if ( !$this->isAccessible( $rev ) ) {
throw new LocalizedHttpException(
new MessageValue( 'rest-compare-inaccessible', [ $paramName ] ), 403 );
}
return $rev;
}
/**
* @param RevisionRecord $rev
* @return bool
*/
private function isAccessible( $rev ) {
return $rev->userCan( RevisionRecord::DELETED_TEXT, $this->getAuthority() );
}
private function getRole() {
return SlotRecord::MAIN;
}
private function getRevisionText( $paramName ) {
if ( !isset( $this->textCache[$paramName] ) ) {
$revision = $this->getRevision( $paramName );
try {
$content = $revision
->getSlot( $this->getRole(), RevisionRecord::FOR_THIS_USER, $this->getAuthority() )
->getContent()
->convert( CONTENT_MODEL_TEXT );
if ( $content instanceof TextContent ) {
$this->textCache[$paramName] = $content->getText();
} else {
throw new LocalizedHttpException(
new MessageValue(
'rest-compare-wrong-content',
[ $this->getRole(), $paramName ]
),
400 );
}
} catch ( SuppressedDataException $e ) {
throw new LocalizedHttpException(
new MessageValue( 'rest-compare-inaccessible', [ $paramName ] ), 403 );
} catch ( RevisionAccessException $e ) {
throw new LocalizedHttpException(
new MessageValue( 'rest-compare-nonexistent', [ $paramName ] ), 404 );
}
}
return $this->textCache[$paramName];
}
/**
* @return string
*/
private function getJsonDiff() {
// TODO: properly implement
// This is a prototype only. SlotDiffRenderer should be extended to support this use case.
$fromText = $this->getRevisionText( 'from' );
$toText = $this->getRevisionText( 'to' );
if ( !function_exists( 'wikidiff2_inline_json_diff' ) ) {
throw new LocalizedHttpException(
new MessageValue( 'rest-compare-wikidiff2' ), 500 );
}
return wikidiff2_inline_json_diff( $fromText, $toText, 2 );
}
/**
* @param string $paramName
* @return array
*/
private function getSectionInfo( $paramName ) {
$text = $this->getRevisionText( $paramName );
$parserSections = $this->parserFactory->getInstance()->getFlatSectionInfo( $text );
$sections = [];
foreach ( $parserSections as $i => $parserSection ) {
// Skip section zero, which comes before the first heading, since
// its offset is always zero, so the client can assume its location.
if ( $i !== 0 ) {
$sections[] = [
'level' => $parserSection['level'],
'heading' => $parserSection['heading'],
'offset' => $parserSection['offset'],
];
}
}
return $sections;
}
/**
* @inheritDoc
*/
public function needsWriteAccess() {
return false;
}
public function getParamSettings() {
return [
'from' => [
ParamValidator::PARAM_TYPE => 'integer',
ParamValidator::PARAM_REQUIRED => true,
Handler::PARAM_SOURCE => 'path',
],
'to' => [
ParamValidator::PARAM_TYPE => 'integer',
ParamValidator::PARAM_REQUIRED => true,
Handler::PARAM_SOURCE => 'path',
],
];
}
}
|