TheDJ has uploaded a new change for review.
https://gerrit.wikimedia.org/r/236078
Change subject: [WIP] API: Compare should be able to deal with text
......................................................................
[WIP] API: Compare should be able to deal with text
A bit of experiementation with the Compare API.
Added totext, and allow comparing with not yet existing pages.
Figured out that now I lost the summary of course.. to be continued
later. Ideas and feedback welcome of course
Bug: T109166
Change-Id: Ic87efe9f0140ef06c6be728839beb1f06930d3cc
---
M includes/api/ApiComparePages.php
M resources/src/mediawiki.action/mediawiki.action.edit.preview.js
2 files changed, 81 insertions(+), 32 deletions(-)
git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core
refs/changes/78/236078/1
diff --git a/includes/api/ApiComparePages.php b/includes/api/ApiComparePages.php
index 2300912..f6d93d6 100644
--- a/includes/api/ApiComparePages.php
+++ b/includes/api/ApiComparePages.php
@@ -31,20 +31,70 @@
$rev1 = $this->revisionOrTitleOrId( $params['fromrev'],
$params['fromtitle'], $params['fromid'] );
$rev2 = $this->revisionOrTitleOrId( $params['torev'],
$params['totitle'], $params['toid'] );
- $revision = Revision::newFromId( $rev1 );
+ if ( !is_null( $params['fromtitle'] ) ) {
+ $fromTitle = Title::newFromText( $params['fromtitle'] );
+ if ( !$fromTitle->exists() ) {
+ $fromContent = ContentHandler::getForModelID(
$fromTitle->getContentModel() )->makeEmptyContent();
+ }
+ }
+ $fromRevision = Revision::newFromId( $rev1 );
+ if ( $fromRevision ) {
+ $fromTitle = $fromRevision->getTitle();
+ $fromContent = $fromRevision->getContent(
Revision::FOR_THIS_USER, $this->getUser() );
+ $section = isset( $params['section'] ) ?
$params['section'] : false;
+ if ( $fromContent && $section !== false ) {
+ $fromContent = $fromContent->getSection(
$section, false );
+ if ( !$fromContent ) {
+ $this->dieUsage(
+ "There is no section {$section}
in r" . $fromRevision->getId(),
+ 'nosuchsection'
+ );
+ }
+ }
+ }
- if ( !$revision ) {
+ if ( !$fromContent ) {
$this->dieUsage( 'The diff cannot be retrieved, ' .
'one revision does not exist or you do not have
permission to view it.', 'baddiff' );
}
- $contentHandler = $revision->getContentHandler();
- $de = $contentHandler->createDifferenceEngine(
$this->getContext(),
- $rev1,
- $rev2,
- null, // rcid
- true,
- false );
+ $toRevision = Revision::newFromId( $rev2 );
+ if ( $toRevision ) {
+ $toContent = $toRevision->getContent(
Revision::FOR_THIS_USER, $this->getUser() );
+ if ( $toContent && $section !== false ) {
+ $toContent = $toContent->getSection( $section,
false );
+ if ( !$toContent ) {
+ $this->dieUsage(
+ "There is no section {$section}
in r" . $toRevision->getId(),
+ 'nosuchsection'
+ );
+ }
+ }
+ } else if ( !is_null( $params['totext'] ) ) {
+ $toContent = ContentHandler::makeContent(
+ $params['totext'],
+ $fromTitle,
+ $fromTitle->getContentModel(),
+ $fromRevision ?
$fromRevision->getContentFormat() :
$fromContent->getContentHandler()->getDefaultFormat()
+ );
+ $popts = ParserOptions::newFromUserAndLang(
$this->getUser(), $fromContent->getContentHandler()->getPageViewLanguage(
$fromTitle ) );
+ $toContent = $toContent->preSaveTransform( $fromTitle,
$this->getUser(), $popts );
+ }
+
+ if ( ( $fromContent && !$fromContent->isEmpty() ) || (
$toContent && !$toContent->isEmpty() ) ) {
+ if ( !$fromContent ) {
+ $fromContent =
$toContent->getContentHandler()->makeEmptyContent();
+ }
+ if ( !$toContent ) {
+ $toContent =
$fromContent->getContentHandler()->makeEmptyContent();
+ }
+ } else {
+ $this->dieUsage( 'The diff cannot be retrieved, ' .
+ 'one revision does not exist or you do not have
permission to view it.', 'baddiff' );
+ }
+
+ $de =
$fromContent->getContentHandler()->createDifferenceEngine( $this->getContext(),
$rev1, $rev2 );
+ $de->setContent( $fromContent, $toContent );
$vals = array();
if ( isset( $params['fromtitle'] ) ) {
@@ -53,15 +103,19 @@
if ( isset( $params['fromid'] ) ) {
$vals['fromid'] = $params['fromid'];
}
- $vals['fromrevid'] = $rev1;
+ if ( $rev1 ) {
+ $vals['fromrevid'] = $rev1;
+ }
+
if ( isset( $params['totitle'] ) ) {
$vals['totitle'] = $params['totitle'];
}
if ( isset( $params['toid'] ) ) {
$vals['toid'] = $params['toid'];
}
- $vals['torevid'] = $rev2;
-
+ if ( $rev2 ) {
+ $vals['torevid'] = $rev2;
+ }
$difftext = $de->getDiffBody();
if ( $difftext === false ) {
@@ -101,10 +155,10 @@
return $title->getLatestRevID();
}
- $this->dieUsage(
- 'A title, a page ID, or a revision number is needed for
both the from and the to parameters',
- 'inputneeded'
- );
+ #$this->dieUsage(
+ # 'A title, a page ID, or a revision number is needed for
both the from and the to parameters',
+ # 'inputneeded'
+ #);
}
public function getAllowedParams() {
@@ -123,6 +177,10 @@
'torev' => array(
ApiBase::PARAM_TYPE => 'integer'
),
+ 'totext' => null,
+ 'section' => array(
+ ApiBase::PARAM_DFLT => null,
+ ),
);
}
diff --git a/resources/src/mediawiki.action/mediawiki.action.edit.preview.js
b/resources/src/mediawiki.action/mediawiki.action.edit.preview.js
index 5f1058f..5cb61f2 100644
--- a/resources/src/mediawiki.action/mediawiki.action.edit.preview.js
+++ b/resources/src/mediawiki.action/mediawiki.action.edit.preview.js
@@ -89,25 +89,17 @@
$wikiPreview.hide();
// First PST the input, then diff it
- postData.onlypst = '';
- request = api.post( postData );
- request.done( function ( response ) {
- var postData;
postData = {
- action: 'query',
- indexpageids: '',
- prop: 'revisions',
- titles: mw.config.get( 'wgPageName' ),
- rvdifftotext: response.parse.text['*'],
- rvprop: ''
+ action: 'compare',
+ fromtitle: mw.config.get( 'wgPageName'
),
+ totext: $textbox.textSelection(
'getContents' ),
};
if ( section !== '' ) {
- postData.rvsection = section;
+ postData.section = section;
}
- return api.post( postData ).done( function (
result2 ) {
+ request = api.post( postData ).done( function (
result ) {
try {
- var diffHtml =
result2.query.pages[result2.query.pageids[0]]
- .revisions[0].diff['*'];
+ var diffHtml =
result.compare['*'];
$wikiDiff.find( 'table.diff
tbody' ).html( diffHtml );
} catch ( e ) {
// "result.blah is undefined"
error, ignore
@@ -115,7 +107,6 @@
}
$wikiDiff.show();
} );
- } );
} else {
$wikiDiff.hide();
$.extend( postData, {
@@ -213,7 +204,7 @@
var isSubject = ( section === 'new' ),
summaryMsg = isSubject ? 'subject-preview' :
'summary-preview',
$summaryPreview = $editform.find(
'.mw-summary-preview' ).empty();
- if ( response.parse.parsedsummary &&
response.parse.parsedsummary['*'] !== '' ) {
+ if ( response.parse && response.parse.parsedsummary &&
response.parse.parsedsummary['*'] !== '' ) {
$summaryPreview.append(
mw.message( summaryMsg ).parse(),
' ',
--
To view, visit https://gerrit.wikimedia.org/r/236078
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ic87efe9f0140ef06c6be728839beb1f06930d3cc
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: TheDJ <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits