| 1 | // Someone has to maintain this wherever it lives. If it live in Core, it becomes a public API. |
| 2 | // If it lives in some client-side target of mediawiki-title that accepts a MediaWiki config instead |
| 3 | // of a SiteInfo, it still becomes a public API. If it lives where used, it becomes a copy and paste |
| 4 | // implementation where each copy can deviate but deletion is easy. See additional discussion in |
| 5 | // T218358 and I95b08e77eece5cd4dae62f6f237d492d6b0fe42b. |
| 6 | const UriUtil = require( './UriUtil.js' ); |
| 7 | |
| 8 | /** |
| 9 | * Returns the decoded wiki page title referenced by the passed link as a string when parsable. |
| 10 | * The title query parameter is returned, if present. Otherwise, a heuristic is used to attempt |
| 11 | * to extract the title from the path. |
| 12 | * |
| 13 | * The API is the source of truth for page titles. This function should only be used in |
| 14 | * circumstances where the API cannot be consulted. |
| 15 | * |
| 16 | * Assuming the current page is on metawiki, consider the following example links and |
| 17 | * `newFromUri()` outputs: |
| 18 | * |
| 19 | * https://meta.wikimedia.org/wiki/Foo → Foo (path title) |
| 20 | * http://meta.wikimedia.org/wiki/Foo → Foo (mismatching protocol) |
| 21 | * /wiki/Foo → Foo (relative URI) |
| 22 | * /w/index.php?title=Foo → Foo (title query parameter) |
| 23 | * /wiki/Talk:Foo → Talk:Foo (non-main namespace URI) |
| 24 | * /wiki/Foo bar → Foo_bar (name with spaces) |
| 25 | * /wiki/Foo%20bar → Foo_bar (name with percent encoded spaces) |
| 26 | * /wiki/Foo+bar → Foo+bar (name with +) |
| 27 | * /w/index.php?title=Foo%2bbar → Foo+bar (query parameter with +) |
| 28 | * / → null (mismatching article path) |
| 29 | * /wiki/index.php?title=Foo → null (mismatching script path) |
| 30 | * https://archive.org/ → null (mismatching host) |
| 31 | * https://foo.wikimedia.org/ → null (mismatching host) |
| 32 | * https://en.wikipedia.org/wiki/Bar → null (mismatching host) |
| 33 | * |
| 34 | * This function invokes `Uri.isInternal()` to validate that this link is assuredly a local |
| 35 | * wiki link and that the internal usage of both the title query parameter and value of |
| 36 | * wgArticlePath are relevant. |
| 37 | * |
| 38 | * This function doesn't throw. `null` is returned for any unparseable input. |
| 39 | * |
| 40 | * @ignore |
| 41 | * @param {mw.Uri|Object|string} [uri] Passed to Uri. |
| 42 | * @param {Object|boolean} [options] Passed to Uri. |
| 43 | * @param {Object|boolean} [options.validateReadOnlyLink] If true, only links that would show a |
| 44 | * page for reading are considered. E.g., `/wiki/Foo` and `/w/index.php?title=Foo` would |
| 45 | * validate but `/w/index.php?title=Foo&action=bar` would not. |
| 46 | * @return {mw.Title|null} A Title or `null`. |
| 47 | */ |
| 48 | function newFromUri( uri, options ) { |
| 49 | let mwUri; |
| 50 | let title; |
| 51 | |
| 52 | try { |
| 53 | // uri may or may not be a Uri but the Uri constructor accepts a Uri parameter. |
| 54 | mwUri = new mw.Uri( uri, options ); |
| 55 | } catch ( e ) { |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | if ( !UriUtil.isInternal( mwUri ) ) { |
| 60 | return null; |
| 61 | } |
| 62 | |
| 63 | if ( ( options || {} ).validateReadOnlyLink && !isReadOnlyUri( mwUri ) ) { |
| 64 | // An unknown query parameter is used. This may not be a read-only link. |
| 65 | return null; |
| 66 | } |
| 67 | |
| 68 | if ( mwUri.query.title ) { |
| 69 | // True if input starts with wgScriptPath. |
| 70 | |
| 71 | const regExp = new RegExp( '^' + mw.util.escapeRegExp( mw.config.get( 'wgScriptPath' ) ) + '/' ); |
| 72 | |
| 73 | // URL has a nonempty `title` query parameter like `/w/index.php?title=Foo`. The script |
| 74 | // path should match. |
| 75 | const matches = regExp.test( mwUri.path ); |
| 76 | if ( !matches ) { |
| 77 | return null; |
| 78 | } |
| 79 | |
| 80 | // The parameter was already decoded at Uri construction. |
| 81 | title = mwUri.query.title; |
| 82 | } else { |
| 83 | // True if input starts with wgArticlePath and ends with a nonempty page title. The |
| 84 | // first matching group (index 1) is the page title. |
| 85 | |
| 86 | const regExp = new RegExp( '^' + mw.util.escapeRegExp( mw.config.get( 'wgArticlePath' ) ).replace( '\\$1', '(.+)' ) ); |
| 87 | |
| 88 | // No title query parameter is present so the URL may be "pretty" like `/wiki/Foo`. |
| 89 | // `Uri.path` should not contain query parameters or a fragment, as is assumed in |
| 90 | // `Uri.getRelativePath()`. Try to isolate the title. |
| 91 | const matches = regExp.exec( mwUri.path ); |
| 92 | if ( !matches || !matches[ 1 ] ) { |
| 93 | return null; |
| 94 | } |
| 95 | |
| 96 | try { |
| 97 | // `Uri.path` was not previously decoded, as is assumed in `Uri.getRelativePath()`, |
| 98 | // and decoding may now fail. Do not use `Uri.decode()` which is designed to be |
| 99 | // paired with `Uri.encode()` and replaces `+` characters with spaces. |
| 100 | title = decodeURIComponent( matches[ 1 ] ); |
| 101 | } catch ( e ) { |
| 102 | return null; |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | // Append the fragment, if present. |
| 107 | title += mwUri.fragment ? '#' + mwUri.fragment : ''; |
| 108 | |
| 109 | return mw.Title.newFromText( title ); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Validates that the passed link is for reading. |
| 114 | * |
| 115 | * The following links return true: |
| 116 | * /wiki/Foo |
| 117 | * /w/index.php?title=Foo |
| 118 | * /w/index.php?oldid=123 |
| 119 | * |
| 120 | * The following links return false: |
| 121 | * /w/index.php?title=Foo&action=bar |
| 122 | * |
| 123 | * @private |
| 124 | * @static |
| 125 | * @method isReadOnlyUri |
| 126 | * @param {mw.Uri} uri A Uri to an internal wiki page. |
| 127 | * @return {boolean} True if uri has no query parameters or only known parameters for reading. |
| 128 | */ |
| 129 | function isReadOnlyUri( uri ) { |
| 130 | const length = Object.keys( uri.query ).length; |
| 131 | return length === ( ( 'oldid' in uri.query ? 1 : 0 ) + ( 'title' in uri.query ? 1 : 0 ) ); |
| 132 | } |
| 133 | |
| 134 | module.exports = { |
| 135 | newFromUri |
| 136 | }; |