2017-05-26 17:35:07 +00:00
|
|
|
/**
|
2017-06-08 10:08:58 +00:00
|
|
|
* @module title
|
2017-05-26 17:35:07 +00:00
|
|
|
*/
|
|
|
|
|
2018-06-21 15:47:13 +00:00
|
|
|
const mw = mediaWiki;
|
2017-04-27 12:41:22 +00:00
|
|
|
|
2019-02-11 17:39:12 +00:00
|
|
|
/**
|
|
|
|
* Fast, native check if we are parsing a self-link, with the only difference beeing the hash.
|
|
|
|
*
|
|
|
|
* @param {HTMLAnchorElement} el
|
2019-08-05 12:35:21 +00:00
|
|
|
* @return {boolean}
|
2019-02-11 17:39:12 +00:00
|
|
|
*/
|
|
|
|
function isOwnPageAnchorLink( el ) {
|
|
|
|
return el.hash &&
|
|
|
|
// Note: The protocol is ignored for the sake of simplicity.
|
|
|
|
// Can't compare username and password because they aren't readable from `location`.
|
|
|
|
el.host === location.host &&
|
|
|
|
el.pathname === location.pathname &&
|
|
|
|
el.search === location.search;
|
|
|
|
}
|
|
|
|
|
2017-04-27 12:41:22 +00:00
|
|
|
/**
|
|
|
|
* Gets the title of a local page from an href given some configuration.
|
|
|
|
*
|
2018-07-13 13:07:35 +00:00
|
|
|
* @param {string} href
|
2017-04-27 12:41:22 +00:00
|
|
|
* @param {mw.Map} config
|
2018-07-13 13:07:35 +00:00
|
|
|
* @return {string|undefined}
|
2017-04-27 12:41:22 +00:00
|
|
|
*/
|
2017-07-28 17:32:46 +00:00
|
|
|
export function getTitle( href, config ) {
|
2017-04-27 12:41:22 +00:00
|
|
|
// Skip every URI that mw.Uri cannot parse
|
2018-03-19 19:39:41 +00:00
|
|
|
let linkHref;
|
2017-04-27 12:41:22 +00:00
|
|
|
try {
|
|
|
|
linkHref = new mw.Uri( href );
|
|
|
|
} catch ( e ) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// External links
|
|
|
|
if ( linkHref.host !== location.hostname ) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
2018-03-19 19:39:41 +00:00
|
|
|
const queryLength = Object.keys( linkHref.query ).length;
|
2019-01-24 10:37:30 +00:00
|
|
|
let title;
|
2017-04-27 12:41:22 +00:00
|
|
|
|
|
|
|
// No query params (pretty URL)
|
|
|
|
if ( !queryLength ) {
|
2019-02-11 18:07:02 +00:00
|
|
|
const pattern = mw.RegExp.escape( config.get( 'wgArticlePath' ) ).replace( '\\$1', '([^?#]+)' ),
|
|
|
|
matches = new RegExp( pattern ).exec( linkHref.path );
|
|
|
|
|
2019-01-24 10:37:30 +00:00
|
|
|
// We can't be sure decodeURIComponent() is able to parse every possible match
|
|
|
|
try {
|
|
|
|
title = matches && decodeURIComponent( matches[ 1 ] );
|
|
|
|
} catch ( e ) {
|
|
|
|
// Will return undefined below
|
|
|
|
}
|
2019-08-05 12:38:14 +00:00
|
|
|
} else if ( queryLength === 1 && 'title' in linkHref.query ) {
|
2017-04-27 12:41:22 +00:00
|
|
|
// URL is not pretty, but only has a `title` parameter
|
2019-01-24 10:37:30 +00:00
|
|
|
title = linkHref.query.title;
|
2017-04-27 12:41:22 +00:00
|
|
|
}
|
|
|
|
|
2019-08-05 12:42:21 +00:00
|
|
|
return title ? `${title}${linkHref.fragment ? `#${linkHref.fragment}` : ''}` : undefined;
|
2017-04-27 12:41:22 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 10:45:11 +00:00
|
|
|
/**
|
|
|
|
* Given a page title it will return the mediawiki.Title if it is an eligible
|
|
|
|
* link for showing page previews, null otherwise
|
|
|
|
*
|
2019-01-24 10:37:30 +00:00
|
|
|
* @param {string|undefined} title page title to check if it should show preview
|
2018-09-13 13:28:47 +00:00
|
|
|
* @param {number[]} contentNamespaces contentNamespaces as specified in
|
2017-06-08 10:45:11 +00:00
|
|
|
* wgContentNamespaces
|
2017-10-09 14:56:15 +00:00
|
|
|
* @return {mw.Title|null}
|
2017-06-08 10:45:11 +00:00
|
|
|
*/
|
2017-07-28 17:32:46 +00:00
|
|
|
export function isValid( title, contentNamespaces ) {
|
2017-06-08 10:45:11 +00:00
|
|
|
if ( !title ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is title in a content namespace?
|
2018-03-19 19:39:41 +00:00
|
|
|
const mwTitle = mw.Title.newFromText( title );
|
2018-09-13 13:28:47 +00:00
|
|
|
if ( mwTitle && contentNamespaces.indexOf( mwTitle.namespace ) >= 0 ) {
|
2017-06-08 10:45:11 +00:00
|
|
|
return mwTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-06-08 15:23:44 +00:00
|
|
|
/**
|
2019-02-14 10:13:33 +00:00
|
|
|
* Return an mw.Title from an HTMLAnchorElement if valid for page previews. Convenience
|
2017-06-08 15:23:44 +00:00
|
|
|
* method
|
|
|
|
*
|
2019-02-14 10:13:33 +00:00
|
|
|
* @param {HTMLAnchorElement} el
|
2017-06-08 15:23:44 +00:00
|
|
|
* @param {mw.Map} config
|
|
|
|
* @return {mw.Title|null}
|
|
|
|
*/
|
2017-07-28 17:32:46 +00:00
|
|
|
export function fromElement( el, config ) {
|
2019-02-11 17:39:12 +00:00
|
|
|
if ( isOwnPageAnchorLink( el ) ) {
|
|
|
|
// No need to check the namespace. A self-link can't point to different one.
|
2019-04-20 07:24:38 +00:00
|
|
|
try {
|
|
|
|
return mw.Title.newFromText( config.get( 'wgPageName' ) + decodeURIComponent( el.hash ) );
|
|
|
|
} catch ( e ) {
|
|
|
|
return null;
|
|
|
|
}
|
2019-02-11 17:39:12 +00:00
|
|
|
}
|
|
|
|
|
2018-01-18 18:48:16 +00:00
|
|
|
return isValid(
|
|
|
|
getTitle( el.href, config ),
|
|
|
|
config.get( 'wgContentNamespaces' )
|
|
|
|
);
|
2017-06-08 15:23:44 +00:00
|
|
|
}
|