Skip to content

Commit 2dc0405

Browse files
integrate new parser into script
1 parent 0c33ddb commit 2dc0405

File tree

6 files changed

+55
-75
lines changed

6 files changed

+55
-75
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ node_modules
1313
build
1414
.rollup.cache
1515
tsconfig.buildinfo
16+
coverage/

src/models/ContributionSurveyRow.ts

Lines changed: 39 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import cloneRegex from '../util/cloneRegex';
22
import { ContributionSurveyRevision } from './ContributionSurveyRevision';
33
import DeputyCasePage from '../wiki/DeputyCasePage';
44
import MwApi from '../MwApi';
5+
import ContributionSurveyRowParser, {
6+
RawContributionSurveyRow
7+
} from './ContributionSurveyRowParser';
58

69
export enum ContributionSurveyRowStatus {
710
// The row has not been processed yet.
@@ -25,16 +28,6 @@ export enum ContributionSurveyRowStatus {
2528
*/
2629
export default class ContributionSurveyRow {
2730

28-
/**
29-
* Wikitext for checking if a given row is a contribution survey row.
30-
* $1 - Page name
31-
* $2 - Post-page name text (`(3 edits)`)
32-
* $3 - ID of first revision
33-
* $4 - Comments
34-
*/
35-
static readonly rowWikitextRegex =
36-
/\*?(?:'''.''' )?\[\[:?(.+?)]](?: ?([^{:]*) ?(?:: ?)?)?(?:(?:\[\[Special:Diff\/(\d+)\|.+?]])+|(.+$))/gm;
37-
3831
/**
3932
* A set of regular expressions that will match a specific contribution survey row
4033
* comment. Used to determine the status of the comment.
@@ -53,16 +46,6 @@ export default class ContributionSurveyRow {
5346
[ ContributionSurveyRowStatus.PresumptiveRemoval ]: /\{\{x}}/gi
5447
};
5548

56-
/**
57-
* Determines if a given wikitext line is a valid contribution survey row.
58-
*
59-
* @param text The wikitext to check
60-
* @return Whether the provided wikitext is a contribution survey row or not
61-
*/
62-
static isContributionSurveyRowText( text: string ): boolean {
63-
return cloneRegex( ContributionSurveyRow.rowWikitextRegex ).test( text );
64-
}
65-
6649
/**
6750
* Identifies a row's current status based on the comment's contents.
6851
*
@@ -140,23 +123,31 @@ export default class ContributionSurveyRow {
140123
*/
141124
private diffs?: Map<number, ContributionSurveyRevision>;
142125

126+
/**
127+
* Data returned by the ContributionSurveyRowParser. Run on the wikitext
128+
* during instantiation.
129+
*
130+
* @private
131+
*/
132+
private readonly data: RawContributionSurveyRow;
133+
143134
/**
144135
* Creates a new contribution survey row from MediaWiki parser output.
145136
*
146137
* @param casePage The case page of this row
147138
* @param wikitext The wikitext of the row
148139
*/
149140
constructor( casePage: DeputyCasePage, wikitext: string ) {
150-
const rowExec = cloneRegex( ContributionSurveyRow.rowWikitextRegex ).exec( wikitext );
141+
this.data = new ContributionSurveyRowParser( wikitext ).parse();
151142

152143
this.casePage = casePage;
153144
this.wikitext = wikitext;
154-
this.title = new mw.Title( rowExec[ 1 ] );
155-
this.extras = rowExec[ 2 ];
156-
this.comment = rowExec[ 4 ];
157-
this.status = this.originalStatus = rowExec[ 4 ] == null ?
145+
this.title = new mw.Title( this.data.page );
146+
this.extras = this.data.extras;
147+
this.comment = this.data.comments;
148+
this.status = this.originalStatus = this.data.comments == null ?
158149
ContributionSurveyRowStatus.Unfinished :
159-
ContributionSurveyRow.identifyCommentStatus( rowExec[ 4 ] );
150+
ContributionSurveyRow.identifyCommentStatus( this.data.comments );
160151

161152
if ( ( ContributionSurveyRow.commentMatchRegex as any )[ this.status ] != null ) {
162153
if (
@@ -187,49 +178,34 @@ export default class ContributionSurveyRow {
187178
return this.diffs;
188179
}
189180

190-
const rowExec = cloneRegex( ContributionSurveyRow.rowWikitextRegex ).exec( this.wikitext );
191181
const revisionData: Map<number, ContributionSurveyRevision> = new Map();
192-
const diffs = [];
182+
const revids = this.data.revids;
193183

194184
// Load revision information
195-
if ( rowExec[ 3 ] !== null && rowExec.length !== 0 ) {
196-
const diffRegex = cloneRegex( /Special:Diff\/(\d+)/g );
197-
let diffMatch = diffRegex.exec( rowExec[ 0 ] );
198-
while ( diffMatch != null ) {
199-
if ( diffMatch[ 1 ] == null ) {
200-
console.warn( 'Could not parse revision ID: ' + diffMatch[ 0 ] );
201-
} else {
202-
diffs.push( +diffMatch[ 1 ] );
203-
}
204-
205-
diffMatch = diffRegex.exec( rowExec[ 0 ] );
185+
const toCache = [];
186+
for ( const revisionID of revids ) {
187+
const cachedDiff = await window.deputy.storage.db.get( 'diffCache', revisionID );
188+
if ( cachedDiff ) {
189+
revisionData.set(
190+
revisionID, new ContributionSurveyRevision( this, cachedDiff )
191+
);
192+
} else {
193+
toCache.push( revisionID );
206194
}
207-
208-
const toCache = [];
209-
for ( const revisionID of diffs ) {
210-
const cachedDiff = await window.deputy.storage.db.get( 'diffCache', revisionID );
211-
if ( cachedDiff ) {
212-
revisionData.set(
213-
revisionID, new ContributionSurveyRevision( this, cachedDiff )
214-
);
215-
} else {
216-
toCache.push( revisionID );
217-
}
195+
}
196+
if ( toCache.length > 0 ) {
197+
const expandedData = await window.deputy.api.getExpandedRevisionData( toCache );
198+
for ( const revisionID in expandedData ) {
199+
revisionData.set(
200+
+revisionID,
201+
new ContributionSurveyRevision( this, expandedData[ revisionID ] )
202+
);
218203
}
219-
if ( toCache.length > 0 ) {
220-
const expandedData = await window.deputy.api.getExpandedRevisionData( toCache );
221-
for ( const revisionID in expandedData ) {
222-
revisionData.set(
223-
+revisionID,
224-
new ContributionSurveyRevision( this, expandedData[ revisionID ] )
225-
);
226-
}
227204

228-
for ( const revisionID in expandedData ) {
229-
await window.deputy.storage.db.put(
230-
'diffCache', expandedData[ revisionID ]
231-
);
232-
}
205+
for ( const revisionID in expandedData ) {
206+
await window.deputy.storage.db.put(
207+
'diffCache', expandedData[ revisionID ]
208+
);
233209
}
234210
}
235211

src/models/ContributionSurveyRowParser.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/**
22
* Data that constructs a raw contribution survey row.
33
*/
4-
interface RawContributionSurveyRow {
4+
export interface RawContributionSurveyRow {
55
type: 'detailed' | 'pageonly';
66
/**
77
* The bullet and all trailing whitespace. This matches the starting bullet

src/ui/root/DeputyContributionSurveySection.tsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -102,10 +102,8 @@ export default class DeputyContributionSurveySection implements DeputyUIElement
102102
for ( const obj of this.wikitextLines ) {
103103
if ( typeof obj === 'string' ) {
104104
final.push( obj );
105-
} else if ( obj.modified ) {
106-
final.push( obj.wikitext );
107105
} else {
108-
final.push( obj.originalWikitext );
106+
final.push( obj.wikitext );
109107
}
110108
}
111109

@@ -297,12 +295,12 @@ export default class DeputyContributionSurveySection implements DeputyUIElement
297295
const line = wikitextLines[ i ];
298296

299297
let rowElement;
300-
if ( ContributionSurveyRow.isContributionSurveyRowText( line ) ) {
298+
try {
301299
const csr = new ContributionSurveyRow( this.casePage, line );
302300
rowElement = new DeputyContributionSurveyRow(
303301
csr, rowElements[ csr.title.getPrefixedText() ], line, this
304302
);
305-
} else {
303+
} catch ( e ) {
306304
rowElement = line;
307305
}
308306
if ( typeof rowElement !== 'string' ) {

src/ui/root/DeputyFinishedContributionSurveyRow.tsx

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ export default class DeputyFinishedContributionSurveyRow {
5555

5656
const parser = window.deputy.session.rootSession.parser;
5757
// Use DiscussionTools to identify the user and timestamp.
58-
const parsedComment = parser.parse( props.originalElement )?.commentItems?.[ 0 ];
58+
let parsedComment;
59+
try {
60+
parsedComment = parser.parse( props.originalElement )?.commentItems?.[ 0 ];
61+
} catch ( e ) {
62+
console.warn( 'Failed to parse user signature.', e );
63+
}
5964
if ( !parsedComment ) {
6065
// See if the Deputy trace exists.
6166
const fromTrace = guessTrace( props.row.wikitext );

tests/unit/browser/ContributionSurveyRowUnitTests.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,14 +43,14 @@ describe( 'ContributionSurveyRow static unit tests', () => {
4343
'*[[:Example]] (1 edit, 1 major, +173)',
4444
'*[[:Example]] (1 edit, 1 major, +173) {{n}}',
4545
'*[[:Example]] {{?}}',
46-
'*[[:Example]] {{done}} with {{y}}'
47-
];
48-
// Cases that require special treatment from other parsing methods.
49-
const expectFalse: string[] = [
46+
'*[[:Example]] {{done}} with {{y}}',
5047
// WikiProject Tropical cyclones
5148
'*[[:1852 Atlantic hurricane season]]',
5249
// 20110727 11
53-
'*[[:c:File:Corrected Pueblo County, CO, Courthouse IMG 5089.JPG]]',
50+
'*[[:c:File:Corrected Pueblo County, CO, Courthouse IMG 5089.JPG]]'
51+
];
52+
// Cases that require special treatment from other parsing methods.
53+
const expectFalse: string[] = [
5454
// Not part of a list
5555
'[[:1852 Atlantic hurricane season]]',
5656
// Not a contribution survey row

0 commit comments

Comments
 (0)