Skip to content

Commit

Permalink
integrate new parser into script
Browse files Browse the repository at this point in the history
  • Loading branch information
ChlodAlejandro committed Oct 18, 2022
1 parent 0c33ddb commit 2dc0405
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 75 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ node_modules
build
.rollup.cache
tsconfig.buildinfo
coverage/
102 changes: 39 additions & 63 deletions src/models/ContributionSurveyRow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import cloneRegex from '../util/cloneRegex';
import { ContributionSurveyRevision } from './ContributionSurveyRevision';
import DeputyCasePage from '../wiki/DeputyCasePage';
import MwApi from '../MwApi';
import ContributionSurveyRowParser, {
RawContributionSurveyRow
} from './ContributionSurveyRowParser';

export enum ContributionSurveyRowStatus {
// The row has not been processed yet.
Expand All @@ -25,16 +28,6 @@ export enum ContributionSurveyRowStatus {
*/
export default class ContributionSurveyRow {

/**
* Wikitext for checking if a given row is a contribution survey row.
* $1 - Page name
* $2 - Post-page name text (`(3 edits)`)
* $3 - ID of first revision
* $4 - Comments
*/
static readonly rowWikitextRegex =
/\*?(?:'''.''' )?\[\[:?(.+?)]](?: ?([^{:]*) ?(?:: ?)?)?(?:(?:\[\[Special:Diff\/(\d+)\|.+?]])+|(.+$))/gm;

/**
* A set of regular expressions that will match a specific contribution survey row
* comment. Used to determine the status of the comment.
Expand All @@ -53,16 +46,6 @@ export default class ContributionSurveyRow {
[ ContributionSurveyRowStatus.PresumptiveRemoval ]: /\{\{x}}/gi
};

/**
* Determines if a given wikitext line is a valid contribution survey row.
*
* @param text The wikitext to check
* @return Whether the provided wikitext is a contribution survey row or not
*/
static isContributionSurveyRowText( text: string ): boolean {
return cloneRegex( ContributionSurveyRow.rowWikitextRegex ).test( text );
}

/**
* Identifies a row's current status based on the comment's contents.
*
Expand Down Expand Up @@ -140,23 +123,31 @@ export default class ContributionSurveyRow {
*/
private diffs?: Map<number, ContributionSurveyRevision>;

/**
* Data returned by the ContributionSurveyRowParser. Run on the wikitext
* during instantiation.
*
* @private
*/
private readonly data: RawContributionSurveyRow;

/**
* Creates a new contribution survey row from MediaWiki parser output.
*
* @param casePage The case page of this row
* @param wikitext The wikitext of the row
*/
constructor( casePage: DeputyCasePage, wikitext: string ) {
const rowExec = cloneRegex( ContributionSurveyRow.rowWikitextRegex ).exec( wikitext );
this.data = new ContributionSurveyRowParser( wikitext ).parse();

this.casePage = casePage;
this.wikitext = wikitext;
this.title = new mw.Title( rowExec[ 1 ] );
this.extras = rowExec[ 2 ];
this.comment = rowExec[ 4 ];
this.status = this.originalStatus = rowExec[ 4 ] == null ?
this.title = new mw.Title( this.data.page );
this.extras = this.data.extras;
this.comment = this.data.comments;
this.status = this.originalStatus = this.data.comments == null ?
ContributionSurveyRowStatus.Unfinished :
ContributionSurveyRow.identifyCommentStatus( rowExec[ 4 ] );
ContributionSurveyRow.identifyCommentStatus( this.data.comments );

if ( ( ContributionSurveyRow.commentMatchRegex as any )[ this.status ] != null ) {
if (
Expand Down Expand Up @@ -187,49 +178,34 @@ export default class ContributionSurveyRow {
return this.diffs;
}

const rowExec = cloneRegex( ContributionSurveyRow.rowWikitextRegex ).exec( this.wikitext );
const revisionData: Map<number, ContributionSurveyRevision> = new Map();
const diffs = [];
const revids = this.data.revids;

// Load revision information
if ( rowExec[ 3 ] !== null && rowExec.length !== 0 ) {
const diffRegex = cloneRegex( /Special:Diff\/(\d+)/g );
let diffMatch = diffRegex.exec( rowExec[ 0 ] );
while ( diffMatch != null ) {
if ( diffMatch[ 1 ] == null ) {
console.warn( 'Could not parse revision ID: ' + diffMatch[ 0 ] );
} else {
diffs.push( +diffMatch[ 1 ] );
}

diffMatch = diffRegex.exec( rowExec[ 0 ] );
const toCache = [];
for ( const revisionID of revids ) {
const cachedDiff = await window.deputy.storage.db.get( 'diffCache', revisionID );
if ( cachedDiff ) {
revisionData.set(
revisionID, new ContributionSurveyRevision( this, cachedDiff )
);
} else {
toCache.push( revisionID );
}

const toCache = [];
for ( const revisionID of diffs ) {
const cachedDiff = await window.deputy.storage.db.get( 'diffCache', revisionID );
if ( cachedDiff ) {
revisionData.set(
revisionID, new ContributionSurveyRevision( this, cachedDiff )
);
} else {
toCache.push( revisionID );
}
}
if ( toCache.length > 0 ) {
const expandedData = await window.deputy.api.getExpandedRevisionData( toCache );
for ( const revisionID in expandedData ) {
revisionData.set(
+revisionID,
new ContributionSurveyRevision( this, expandedData[ revisionID ] )
);
}
if ( toCache.length > 0 ) {
const expandedData = await window.deputy.api.getExpandedRevisionData( toCache );
for ( const revisionID in expandedData ) {
revisionData.set(
+revisionID,
new ContributionSurveyRevision( this, expandedData[ revisionID ] )
);
}

for ( const revisionID in expandedData ) {
await window.deputy.storage.db.put(
'diffCache', expandedData[ revisionID ]
);
}
for ( const revisionID in expandedData ) {
await window.deputy.storage.db.put(
'diffCache', expandedData[ revisionID ]
);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/models/ContributionSurveyRowParser.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/**
* Data that constructs a raw contribution survey row.
*/
interface RawContributionSurveyRow {
export interface RawContributionSurveyRow {
type: 'detailed' | 'pageonly';
/**
* The bullet and all trailing whitespace. This matches the starting bullet
Expand Down
8 changes: 3 additions & 5 deletions src/ui/root/DeputyContributionSurveySection.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -102,10 +102,8 @@ export default class DeputyContributionSurveySection implements DeputyUIElement
for ( const obj of this.wikitextLines ) {
if ( typeof obj === 'string' ) {
final.push( obj );
} else if ( obj.modified ) {
final.push( obj.wikitext );
} else {
final.push( obj.originalWikitext );
final.push( obj.wikitext );
}
}

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

let rowElement;
if ( ContributionSurveyRow.isContributionSurveyRowText( line ) ) {
try {
const csr = new ContributionSurveyRow( this.casePage, line );
rowElement = new DeputyContributionSurveyRow(
csr, rowElements[ csr.title.getPrefixedText() ], line, this
);
} else {
} catch ( e ) {
rowElement = line;
}
if ( typeof rowElement !== 'string' ) {
Expand Down
7 changes: 6 additions & 1 deletion src/ui/root/DeputyFinishedContributionSurveyRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ export default class DeputyFinishedContributionSurveyRow {

const parser = window.deputy.session.rootSession.parser;
// Use DiscussionTools to identify the user and timestamp.
const parsedComment = parser.parse( props.originalElement )?.commentItems?.[ 0 ];
let parsedComment;
try {
parsedComment = parser.parse( props.originalElement )?.commentItems?.[ 0 ];
} catch ( e ) {
console.warn( 'Failed to parse user signature.', e );
}
if ( !parsedComment ) {
// See if the Deputy trace exists.
const fromTrace = guessTrace( props.row.wikitext );
Expand Down
10 changes: 5 additions & 5 deletions tests/unit/browser/ContributionSurveyRowUnitTests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ describe( 'ContributionSurveyRow static unit tests', () => {
'*[[:Example]] (1 edit, 1 major, +173)',
'*[[:Example]] (1 edit, 1 major, +173) {{n}}',
'*[[:Example]] {{?}}',
'*[[:Example]] {{done}} with {{y}}'
];
// Cases that require special treatment from other parsing methods.
const expectFalse: string[] = [
'*[[:Example]] {{done}} with {{y}}',
// WikiProject Tropical cyclones
'*[[:1852 Atlantic hurricane season]]',
// 20110727 11
'*[[:c:File:Corrected Pueblo County, CO, Courthouse IMG 5089.JPG]]',
'*[[:c:File:Corrected Pueblo County, CO, Courthouse IMG 5089.JPG]]'
];
// Cases that require special treatment from other parsing methods.
const expectFalse: string[] = [
// Not part of a list
'[[:1852 Atlantic hurricane season]]',
// Not a contribution survey row
Expand Down

0 comments on commit 2dc0405

Please sign in to comment.