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-03-15 16:30:11 +00:00
|
|
|
let mw = window.mediaWiki;
|
2017-04-27 12:41:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the title of a local page from an href given some configuration.
|
|
|
|
*
|
|
|
|
* @param {String} href
|
|
|
|
* @param {mw.Map} config
|
|
|
|
* @return {String|undefined}
|
|
|
|
*/
|
2017-07-28 17:32:46 +00:00
|
|
|
export function getTitle( href, config ) {
|
2018-03-15 16:30:11 +00:00
|
|
|
let linkHref,
|
2017-04-27 12:41:22 +00:00
|
|
|
matches,
|
|
|
|
queryLength,
|
|
|
|
titleRegex = new RegExp( mw.RegExp.escape( config.get( 'wgArticlePath' ) )
|
|
|
|
.replace( '\\$1', '(.+)' ) );
|
|
|
|
|
|
|
|
// Skip every URI that mw.Uri cannot parse
|
|
|
|
try {
|
|
|
|
linkHref = new mw.Uri( href );
|
|
|
|
} catch ( e ) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
// External links
|
|
|
|
if ( linkHref.host !== location.hostname ) {
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
|
|
|
queryLength = Object.keys( linkHref.query ).length;
|
|
|
|
|
|
|
|
// No query params (pretty URL)
|
|
|
|
if ( !queryLength ) {
|
|
|
|
matches = titleRegex.exec( linkHref.path );
|
|
|
|
return matches ? decodeURIComponent( matches[ 1 ] ) : undefined;
|
|
|
|
} else if ( queryLength === 1 && linkHref.query.hasOwnProperty( 'title' ) ) {
|
|
|
|
// URL is not pretty, but only has a `title` parameter
|
|
|
|
return linkHref.query.title;
|
|
|
|
}
|
|
|
|
|
|
|
|
return undefined;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
*
|
|
|
|
* @param {String} title page title to check if it should show preview
|
|
|
|
* @param {Number[]} contentNamespaces contentNamespaces as specified in
|
|
|
|
* 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 ) {
|
2018-03-15 16:30:11 +00:00
|
|
|
let mwTitle;
|
2017-06-08 10:45:11 +00:00
|
|
|
|
|
|
|
if ( !title ) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Is title in a content namespace?
|
|
|
|
mwTitle = mw.Title.newFromText( title );
|
|
|
|
if ( mwTitle && ( $.inArray( mwTitle.namespace, contentNamespaces ) >= 0 ) ) {
|
|
|
|
return mwTitle;
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2017-06-08 15:23:44 +00:00
|
|
|
/**
|
|
|
|
* Return a mw.Title from a HTMLElement if valid for hovercards. Convenience
|
|
|
|
* method
|
|
|
|
*
|
|
|
|
* @param {Element} el
|
|
|
|
* @param {mw.Map} config
|
|
|
|
* @return {mw.Title|null}
|
|
|
|
*/
|
2017-07-28 17:32:46 +00:00
|
|
|
export function fromElement( el, config ) {
|
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
|
|
|
}
|