Merge "editcheck: Remove mw.editcheck.Diff"

This commit is contained in:
jenkins-bot 2024-10-18 18:01:11 +00:00 committed by Gerrit Code Review
commit c2379d7d9c
8 changed files with 38 additions and 45 deletions

View file

@ -9,9 +9,9 @@ mw.editcheck.AddReferenceEditCheck.static.name = 'addReference';
mw.editcheck.AddReferenceEditCheck.static.description = ve.msg( 'editcheck-dialog-addref-description' );
mw.editcheck.AddReferenceEditCheck.prototype.onBeforeSave = function ( diff ) {
return this.findAddedContent( diff ).map( ( range ) => {
const fragment = diff.surface.getModel().getFragment( new ve.dm.LinearSelection( range ) );
mw.editcheck.AddReferenceEditCheck.prototype.onBeforeSave = function ( surfaceModel ) {
return this.findAddedContent( surfaceModel.getDocument() ).map( ( range ) => {
const fragment = surfaceModel.getFragment( new ve.dm.LinearSelection( range ) );
return new mw.editcheck.EditCheckAction( {
highlight: fragment,
selection: this.adjustForPunctuation( fragment.collapseToEnd() ),
@ -20,10 +20,9 @@ mw.editcheck.AddReferenceEditCheck.prototype.onBeforeSave = function ( diff ) {
} );
};
mw.editcheck.AddReferenceEditCheck.prototype.findAddedContent = function ( diff, includeReferencedContent ) {
mw.editcheck.AddReferenceEditCheck.prototype.findAddedContent = function ( documentModel, includeReferencedContent ) {
// Broken out so a helper for tagging can call it
const documentModel = diff.documentModel;
const ranges = this.getModifiedRangesFromDiff( diff ).filter( ( range ) => {
const ranges = this.getModifiedContentRanges( documentModel ).filter( ( range ) => {
if ( !includeReferencedContent ) {
// 4. Exclude any ranges that already contain references
for ( let i = range.start; i < range.end; i++ ) {

View file

@ -22,13 +22,13 @@ mw.editcheck.BaseEditCheck.static.choices = [
mw.editcheck.BaseEditCheck.static.description = ve.msg( 'editcheck-dialog-addref-description' );
/**
* @param {mw.editcheck.Diff} diff
* @param {ve.dm.Surface} surfaceModel
* @return {mw.editcheck.EditCheckAction[]}
*/
mw.editcheck.BaseEditCheck.prototype.onBeforeSave = null;
/**
* @param {mw.editcheck.Diff} diff
* @param {ve.dm.Surface} surfaceModel
* @return {mw.editcheck.EditCheckAction[]}
*/
mw.editcheck.BaseEditCheck.prototype.onDocumentChange = null;
@ -93,14 +93,14 @@ mw.editcheck.BaseEditCheck.prototype.canBeShown = function () {
/**
* Get content ranges where at least the minimum about of text has been changed
*
* @param {mw.editcheck.Diff} diff
* @param {ve.dm.Document} documentModel
* @return {ve.Range[]}
*/
mw.editcheck.BaseEditCheck.prototype.getModifiedRangesFromDiff = function ( diff ) {
return diff.getModifiedRanges( this.constructor.static.onlyCoveredNodes )
mw.editcheck.BaseEditCheck.prototype.getModifiedContentRanges = function ( documentModel ) {
return mw.editcheck.getModifiedRanges( documentModel, this.constructor.static.onlyCoveredNodes )
.filter(
( range ) => range.getLength() >= this.config.minimumCharacters &&
this.isRangeInValidSection( range, diff.documentModel )
this.isRangeInValidSection( range, documentModel )
);
};

View file

@ -7,19 +7,20 @@ OO.inheritClass( mw.editcheck.ConvertReferenceEditCheck, mw.editcheck.BaseEditCh
mw.editcheck.ConvertReferenceEditCheck.static.name = 'convertReference';
mw.editcheck.ConvertReferenceEditCheck.prototype.onDocumentChange = function ( diff ) {
mw.editcheck.ConvertReferenceEditCheck.prototype.onDocumentChange = function ( surfaceModel ) {
const seenIndexes = {};
return diff.documentModel.getNodesByType( 'mwReference' ).map( ( node ) => {
const documentModel = surfaceModel.getDocument();
return documentModel.getNodesByType( 'mwReference' ).map( ( node ) => {
const refModel = ve.dm.MWReferenceModel.static.newFromReferenceNode( node );
const index = refModel.getListIndex();
if ( seenIndexes[ index ] ) {
return null;
}
seenIndexes[ index ] = true;
const referenceNode = diff.documentModel.getInternalList().getItemNode( index );
const referenceNode = documentModel.getInternalList().getItemNode( index );
const href = ve.ui.CitoidReferenceContextItem.static.getConvertibleHref( referenceNode );
if ( href ) {
const fragment = diff.surface.getModel().getFragment( new ve.dm.LinearSelection( node.getOuterRange() ) );
const fragment = surfaceModel().getFragment( new ve.dm.LinearSelection( node.getOuterRange() ) );
return new mw.editcheck.EditCheckAction( {
highlight: fragment,
selection: fragment,

View file

@ -51,7 +51,7 @@ ve.ui.EditCheckDialog.prototype.initialize = function () {
ve.ui.EditCheckDialog.prototype.update = function () {
const surfaceView = this.surface.getView();
const checks = mw.editcheck.editCheckFactory.createAllByListener( 'onDocumentChange', this.surface );
const checks = mw.editcheck.editCheckFactory.createAllByListener( 'onDocumentChange', this.surface.getModel() );
const $checks = $( '<div>' );
const selections = [];
checks.forEach( ( check ) => {

View file

@ -53,15 +53,14 @@ mw.editcheck.EditCheckFactory.prototype.getNamesByListener = function ( listener
return this.checksByListener[ listener ];
};
mw.editcheck.EditCheckFactory.prototype.createAllByListener = function ( listener, surface ) {
const diff = new mw.editcheck.Diff( surface );
mw.editcheck.EditCheckFactory.prototype.createAllByListener = function ( listener, surfaceModel ) {
const newChecks = [];
this.getNamesByListener( listener ).forEach( ( checkName ) => {
const check = this.create( checkName, mw.editcheck.config[ checkName ] );
if ( !check.canBeShown() ) {
return;
}
const actions = check[ listener ]( diff );
const actions = check[ listener ]( surfaceModel );
if ( actions.length > 0 ) {
ve.batchPush( newChecks, actions );
}

View file

@ -15,11 +15,11 @@ mw.editcheck.TextMatchEditCheck.static.replacers = [
}
];
mw.editcheck.TextMatchEditCheck.prototype.onDocumentChange = function ( diff ) {
mw.editcheck.TextMatchEditCheck.prototype.onDocumentChange = function ( surfaceModel ) {
const actions = [];
this.constructor.static.replacers.forEach( ( replacer ) => {
diff.documentModel.findText( replacer.query ).forEach( ( range ) => {
const fragment = diff.surface.getModel().getFragment( new ve.dm.LinearSelection( range ) );
surfaceModel.getDocument().findText( replacer.query ).forEach( ( range ) => {
const fragment = surfaceModel.getFragment( new ve.dm.LinearSelection( range ) );
actions.push(
new mw.editcheck.EditCheckAction( {
highlight: fragment,

View file

@ -44,13 +44,18 @@ mw.editcheck.getContentRanges = function ( documentModel, range, covers ) {
return ranges;
};
mw.editcheck.Diff = function MWEditCheckDiff( surface ) {
this.surface = surface;
this.documentModel = surface.getModel().getDocument();
mw.editcheck.hasAddedContentNeedingReference = function ( documentModel, includeReferencedContent ) {
// helper for ve.init.mw.ArticleTarget save-tagging, keep logic below in-sync with AddReferenceEditCheck.
// This is bypassing the normal "should this check apply?" logic for creation, so we need to manually
// apply the "only the main namespace" rule.
if ( mw.config.get( 'wgNamespaceNumber' ) !== mw.config.get( 'wgNamespaceIds' )[ '' ] ) {
return false;
}
const check = mw.editcheck.editCheckFactory.create( 'addReference', mw.editcheck.config.addReference );
return check.findAddedContent( documentModel, includeReferencedContent ).length > 0;
};
OO.initClass( mw.editcheck.Diff );
mw.editcheck.Diff.prototype.getModifiedRanges = function ( coveredNodesOnly ) {
const documentModel = this.documentModel;
mw.editcheck.getModifiedRanges = function ( documentModel, coveredNodesOnly ) {
if ( !documentModel.completeHistory.getLength() ) {
return [];
}
@ -88,18 +93,6 @@ mw.editcheck.Diff.prototype.getModifiedRanges = function ( coveredNodesOnly ) {
return ranges;
};
mw.editcheck.hasAddedContentNeedingReference = function ( surface, includeReferencedContent ) {
// helper for ve.init.mw.ArticleTarget save-tagging, keep logic below in-sync with AddReferenceEditCheck.
// This is bypassing the normal "should this check apply?" logic for creation, so we need to manually
// apply the "only the main namespace" rule.
if ( mw.config.get( 'wgNamespaceNumber' ) !== mw.config.get( 'wgNamespaceIds' )[ '' ] ) {
return false;
}
const diff = new mw.editcheck.Diff( surface );
const check = mw.editcheck.editCheckFactory.create( 'addReference', mw.editcheck.config.addReference );
return check.findAddedContent( diff, includeReferencedContent ).length > 0;
};
mw.editcheck.rejections = [];
mw.editcheck.getRejectionReasons = function () {
@ -168,7 +161,7 @@ if ( mw.config.get( 'wgVisualEditorConfig' ).editCheck || mw.editcheck.ecenable
// clear rejection-reasons between runs of the save process, so only the last one counts
mw.editcheck.rejections.length = 0;
let checks = mw.editcheck.editCheckFactory.createAllByListener( 'onBeforeSave', surface );
let checks = mw.editcheck.editCheckFactory.createAllByListener( 'onBeforeSave', surface.getModel() );
if ( checks.length ) {
ve.track( 'counter.editcheck.preSaveChecksShown' );
mw.editcheck.refCheckShown = true;
@ -245,7 +238,7 @@ if ( mw.config.get( 'wgVisualEditorConfig' ).editCheck || mw.editcheck.ecenable
mw.editcheck.rejections.push( responseData.reason );
}
// TODO: Move on to the next issue, when multicheck is enabled
// checks = mw.editcheck.editCheckFactory.createAllByListener( 'onBeforeSave', surface );
// checks = mw.editcheck.editCheckFactory.createAllByListener( 'onBeforeSave', surface.getModel() );
checks = [];
if ( checks.length ) {

View file

@ -1556,12 +1556,13 @@ ve.init.mw.ArticleTarget.prototype.save = function ( doc, options ) {
this.getSurface().getMode() === 'visual' &&
mw.config.get( 'wgVisualEditorConfig' ).editCheckTagging
) {
const documentModel = this.getSurface().getModel().getDocument();
// New content needing a reference
if ( mw.editcheck.hasAddedContentNeedingReference( this.getSurface() ) ) {
if ( mw.editcheck.hasAddedContentNeedingReference( documentModel ) ) {
taglist.push( 'editcheck-references' );
}
// New content, regardless of if it needs a reference
if ( mw.editcheck.hasAddedContentNeedingReference( this.getSurface(), true ) ) {
if ( mw.editcheck.hasAddedContentNeedingReference( documentModel, true ) ) {
taglist.push( 'editcheck-newcontent' );
}
// Rejection reasons for references