Debian Codegraph @

extensions/VisualEditor/editcheck/modules/BaseEditCheck.js

raw symbols SCIP
1 mw.editcheck.BaseEditCheck = function MWBaseEditCheck( config ) {
2 this.config = config;
3 };
4
5 OO.initClass( mw.editcheck.BaseEditCheck );
6
7 mw.editcheck.BaseEditCheck.static.onlyCoveredNodes = false;
8
9 mw.editcheck.BaseEditCheck.static.choices = [
10 {
11 action: 'accept',
12 label: ve.msg( 'editcheck-dialog-action-yes' ),
13 icon: 'check'
14 },
15 {
16 action: 'reject',
17 label: ve.msg( 'editcheck-dialog-action-no' ),
18 icon: 'close'
19 }
20 ];
21
22 mw.editcheck.BaseEditCheck.static.description = ve.msg( 'editcheck-dialog-addref-description' );
23
24 /**
25 * @param {ve.dm.Surface} surfaceModel
26 * @return {mw.editcheck.EditCheckAction[]}
27 */
28 mw.editcheck.BaseEditCheck.prototype.onBeforeSave = null;
29
30 /**
31 * @param {ve.dm.Surface} surfaceModel
32 * @return {mw.editcheck.EditCheckAction[]}
33 */
34 mw.editcheck.BaseEditCheck.prototype.onDocumentChange = null;
35
36 /**
37 * @param {string} choice `action` key from static.choices
38 * @param {mw.editcheck.EditCheckAction} action
39 * @param {ve.ui.EditCheckContextItem} contextItem
40 */
41 mw.editcheck.BaseEditCheck.prototype.act = null;
42
43 /**
44 * @param {mw.editcheck.EditCheckAction} action
45 * @return {Object[]}
46 */
47 mw.editcheck.BaseEditCheck.prototype.getChoices = function () {
48 return this.constructor.static.choices;
49 };
50
51 /**
52 * @param {mw.editcheck.EditCheckAction} action
53 * @return {string}
54 */
55 mw.editcheck.BaseEditCheck.prototype.getDescription = function () {
56 return this.constructor.static.description;
57 };
58
59 /**
60 * Find out whether the check should be applied
61 *
62 * This is a general check for its applicability to the viewer / page, rather
63 * than a specific check based on the current edit. It's used to filter out
64 * checks before any maybe-expensive content analysis happens.
65 *
66 * @return {boolean} Whether the check should be shown
67 */
68 mw.editcheck.BaseEditCheck.prototype.canBeShown = function () {
69 // all checks are only in the main namespace for now
70 if ( mw.config.get( 'wgNamespaceNumber' ) !== mw.config.get( 'wgNamespaceIds' )[ '' ] ) {
71 return false;
72 }
73 // some checks are configured to only be for logged in / out users
74 if ( mw.editcheck.ecenable ) {
75 return true;
76 }
77 // account status:
78 // loggedin, loggedout, or any-other-value meaning 'both'
79 // we'll count temporary users as "logged out" by using isNamed here
80 if ( this.config.account === 'loggedout' && mw.user.isNamed() ) {
81 return false;
82 }
83 if ( this.config.account === 'loggedin' && !mw.user.isNamed() ) {
84 return false;
85 }
86 // some checks are only shown for newer users
87 if ( this.config.maximumEditcount && mw.config.get( 'wgUserEditCount', 0 ) > this.config.maximumEditcount ) {
88 return false;
89 }
90 return true;
91 };
92
93 /**
94 * Get content ranges where at least the minimum about of text has been changed
95 *
96 * @param {ve.dm.Document} documentModel
97 * @return {ve.Range[]}
98 */
99 mw.editcheck.BaseEditCheck.prototype.getModifiedContentRanges = function ( documentModel ) {
100 return mw.editcheck.getModifiedRanges( documentModel, this.constructor.static.onlyCoveredNodes )
101 .filter(
102 ( range ) => range.getLength() >= this.config.minimumCharacters &&
103 this.isRangeInValidSection( range, documentModel )
104 );
105 };
106
107 /**
108 * Check if a modified range is a section we don't ignore (config.ignoreSections)
109 *
110 * @param {ve.Range} range
111 * @param {ve.dm.Document} documentModel
112 * @return {boolean}
113 */
114 mw.editcheck.BaseEditCheck.prototype.isRangeInValidSection = function ( range, documentModel ) {
115 const ignoreSections = this.config.ignoreSections || [];
116 if ( ignoreSections.length === 0 && !this.config.ignoreLeadSection ) {
117 // Nothing is forbidden, so everything is permitted
118 return true;
119 }
120 const isHeading = ( nodeType ) => nodeType === 'mwHeading';
121 // Note: we set a limit of 1 here because otherwise this will turn around
122 // to keep looking when it hits the document boundary:
123 const heading = documentModel.getNearestNodeMatching( isHeading, range.start, -1, 1 );
124 if ( !heading ) {
125 // There's no preceding heading, so work out if we count as being in a
126 // lead section. It's only a lead section if there's more headings
127 // later in the document, otherwise it's just a stub article.
128 return !(
129 this.config.ignoreLeadSection &&
130 !!documentModel.getNearestNodeMatching( isHeading, range.start, 1 )
131 );
132 }
133 if ( ignoreSections.length === 0 ) {
134 // There's nothing left to deny
135 return true;
136 }
137 const compare = new Intl.Collator( documentModel.getLang(), { sensitivity: 'accent' } ).compare;
138 const headingText = documentModel.data.getText( false, heading.getRange() );
139 // If the heading text matches any of ignoreSections, return false.
140 return !ignoreSections.some( ( section ) => compare( headingText, section ) === 0 );
141 };