|
| 1 | +interface DiffInfo { |
| 2 | + diff?: number | string; |
| 3 | + oldid?: number | string; |
| 4 | + title?: string; |
| 5 | +} |
| 6 | + |
| 7 | +/** |
| 8 | + * What it says on the tin. Attempt to parse out a `title`, `diff`, |
| 9 | + * or `oldid` from a URL. This is useful for converting diff URLs into actual |
| 10 | + * diff information, and especially useful for {{copied}} templates. |
| 11 | + * |
| 12 | + * @param url The URL to parse |
| 13 | + * @return Parsed info: `diff` or `oldid` revision IDs, and/or the page title. |
| 14 | + */ |
| 15 | +export default function parseDiffUrl( url: URL | string ): DiffInfo { |
| 16 | + if ( typeof url === 'string' ) { |
| 17 | + url = new URL( url ); |
| 18 | + } |
| 19 | + |
| 20 | + // Attempt to get values from URL parameters (when using `/w/index.php?action=diff`) |
| 21 | + let oldid: string | number = url.searchParams.get( 'oldid' ); |
| 22 | + let diff: string | number = url.searchParams.get( 'diff' ); |
| 23 | + let title = url.searchParams.get( 'title' ); |
| 24 | + |
| 25 | + // Attempt to get information from this URL. |
| 26 | + tryConvert: { |
| 27 | + if ( title && oldid && diff ) { |
| 28 | + // Skip if there's nothing else we need to get. |
| 29 | + break tryConvert; |
| 30 | + } |
| 31 | + |
| 32 | + // Attempt to get values from Special:Diff short-link |
| 33 | + const diffSpecialPageCheck = |
| 34 | + // eslint-disable-next-line security/detect-unsafe-regex |
| 35 | + /\/wiki\/Special:Diff\/(prev|next|\d+)(?:\/(prev|next|\d+))?/i.exec( url.pathname ); |
| 36 | + if ( diffSpecialPageCheck != null ) { |
| 37 | + if ( |
| 38 | + diffSpecialPageCheck[ 1 ] != null && |
| 39 | + diffSpecialPageCheck[ 2 ] == null |
| 40 | + ) { |
| 41 | + // Special:Diff/diff |
| 42 | + diff = diffSpecialPageCheck[ 1 ]; |
| 43 | + } else if ( |
| 44 | + diffSpecialPageCheck[ 1 ] != null && |
| 45 | + diffSpecialPageCheck[ 2 ] != null |
| 46 | + ) { |
| 47 | + // Special:Diff/oldid/diff |
| 48 | + oldid = diffSpecialPageCheck[ 1 ]; |
| 49 | + diff = diffSpecialPageCheck[ 2 ]; |
| 50 | + } |
| 51 | + break tryConvert; |
| 52 | + } |
| 53 | + |
| 54 | + // Attempt to get values from Special:PermanentLink short-link |
| 55 | + const permanentLinkCheck = |
| 56 | + /\/wiki\/Special:Perma(nent)?link\/(\d+)/i.exec( url.pathname ); |
| 57 | + if ( permanentLinkCheck != null ) { |
| 58 | + oldid = permanentLinkCheck[ 2 ]; |
| 59 | + break tryConvert; |
| 60 | + } |
| 61 | + |
| 62 | + // Attempt to get values from article path with ?oldid or ?diff |
| 63 | + // eslint-disable-next-line security/detect-non-literal-regexp |
| 64 | + const articlePathRegex = new RegExp( mw.util.getUrl( '(.*)' ) ) |
| 65 | + .exec( url.pathname ); |
| 66 | + if ( articlePathRegex != null ) { |
| 67 | + title = articlePathRegex[ 1 ]; |
| 68 | + break tryConvert; |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + // Convert numbers to numbers |
| 73 | + if ( oldid != null && !isNaN( +oldid ) ) { |
| 74 | + oldid = +oldid; |
| 75 | + } |
| 76 | + if ( diff != null && !isNaN( +diff ) ) { |
| 77 | + diff = +diff; |
| 78 | + } |
| 79 | + |
| 80 | + // Try to convert a page title |
| 81 | + try { |
| 82 | + title = new mw.Title( title ).getPrefixedText(); |
| 83 | + } catch ( e ) { |
| 84 | + console.warn( 'Failed to normalize page title during diff URL conversion.' ); |
| 85 | + } |
| 86 | + |
| 87 | + return { |
| 88 | + diff: diff, |
| 89 | + oldid: oldid, |
| 90 | + title: title |
| 91 | + }; |
| 92 | +} |
0 commit comments