2 * @author Neil Kandalgaonkar, 2010
3 * @author Timo Tijhof, 2011-2013
7 /*jshint latedef:false */
12 * Parse titles into an object structure. Note that when using the constructor
13 * directly, passing invalid titles will result in an exception. Use #newFromText to use the
14 * logic directly and get null for invalid titles which is easier to work with.
17 * @param {string} title Title of the page. If no second argument given,
18 * this will be searched for a namespace
19 * @param {number} [namespace=NS_MAIN] If given, will used as default namespace for the given title
20 * @throws {Error} When the title is invalid
22 function Title( title
, namespace ) {
23 var parsed
= parse( title
, namespace );
25 throw new Error( 'Unable to parse title' );
28 this.namespace = parsed
.namespace;
29 this.title
= parsed
.title
;
30 this.ext
= parsed
.ext
;
31 this.fragment
= parsed
.fragment
;
57 * @property NS_SPECIAL
78 * @property FILENAME_MAX_BYTES
80 FILENAME_MAX_BYTES
= 240,
85 * @property TITLE_MAX_BYTES
87 TITLE_MAX_BYTES
= 255,
90 * Get the namespace id from a namespace name (either from the localized, canonical or alias
93 * Example: On a German wiki this would return 6 for any of 'File', 'Datei', 'Image' or
98 * @method getNsIdByName
99 * @param {string} ns Namespace name (case insensitive, leading/trailing space ignored)
100 * @return {number|boolean} Namespace id or boolean false
102 getNsIdByName = function ( ns
) {
105 // Don't cast non-strings to strings, because null or undefined should not result in
106 // returning the id of a potential namespace called "Null:" (e.g. on null.example.org/wiki)
107 // Also, toLowerCase throws exception on null/undefined, because it is a String method.
108 if ( typeof ns
!== 'string' ) {
111 ns
= ns
.toLowerCase();
112 id
= mw
.config
.get( 'wgNamespaceIds' )[ ns
];
113 if ( id
=== undefined ) {
119 rUnderscoreTrim
= /^_+|_+$/g,
121 rSplit
= /^(.+?)_*:_*(.*)$/,
123 // See MediaWikiTitleCodec.php#getTitleInvalidRegex
124 rInvalid
= new RegExp(
125 '[^' + mw
.config
.get( 'wgLegalTitleChars' ) + ']' +
126 // URL percent encoding sequences interfere with the ability
127 // to round-trip titles -- you can't link to them consistently.
129 // XML/HTML character references produce similar issues.
130 '|&[A-Za-z0-9\u0080-\uFFFF]+;' +
135 // From MediaWikiTitleCodec.php#L225 @26fcab1f18c568a41
136 // "Clean up whitespace" in function MediaWikiTitleCodec::splitTitleString()
137 rWhitespace
= /[ _\u0009\u00A0\u1680\u180E\u2000-\u200A\u2028\u2029\u202F\u205F\u3000\s]+/g,
140 * Slightly modified from Flinfo. Credit goes to Lupo and Flominator.
143 * @property sanitationRules
152 // Space, underscore, tab, NBSP and other unusual spaces
154 pattern
: rWhitespace
,
158 // unicode bidi override characters: Implicit, Embeds, Overrides
160 pattern
: /[\u200E\u200F\u202A-\u202E]/g,
164 // control characters
166 pattern
: /[\x00-\x1f\x7f]/g,
170 // URL encoding (possibly)
172 pattern
: /%([0-9A-Fa-f]{2})/g,
176 // HTML-character-entities
178 pattern
: /&(([A-Za-z0-9\x80-\xff]+|#[0-9]+|#x[0-9A-Fa-f]+);)/g,
182 // slash, colon (not supported by file systems like NTFS/Windows, Mac OS 9 [:], ext4 [/])
188 // brackets, greater than
194 // brackets, lower than
200 // everything that wasn't covered yet
202 pattern
: new RegExp( rInvalid
.source
, 'g' ),
206 // directory structures
208 pattern
: /^(\.|\.\.|\.\/.*|\.\.\/.*|.*\/\.\/.*|.*\/\.\.\/.*|.*\/\.|.*\/\.\.)$/g,
215 * Internal helper for #constructor and #newFromtext.
217 * Based on Title.php#secureAndSplit
222 * @param {string} title
223 * @param {number} [defaultNamespace=NS_MAIN]
224 * @return {Object|boolean}
226 parse = function ( title
, defaultNamespace
) {
227 var namespace, m
, id
, i
, fragment
, ext
;
229 namespace = defaultNamespace
=== undefined ? NS_MAIN
: defaultNamespace
;
232 // Normalise whitespace to underscores and remove duplicates
233 .replace( /[ _\s]+/g, '_' )
235 .replace( rUnderscoreTrim
, '' );
237 // Process initial colon
238 if ( title
!== '' && title
[ 0 ] === ':' ) {
239 // Initial colon means main namespace instead of specified default
245 .replace( rUnderscoreTrim
, '' );
248 if ( title
=== '' ) {
252 // Process namespace prefix (if any)
253 m
= title
.match( rSplit
);
255 id
= getNsIdByName( m
[ 1 ] );
256 if ( id
!== false ) {
257 // Ordinary namespace
261 // For Talk:X pages, make sure X has no "namespace" prefix
262 if ( namespace === NS_TALK
&& ( m
= title
.match( rSplit
) ) ) {
263 // Disallow titles like Talk:File:x (subject should roundtrip: talk:file:x -> file:x -> file_talk:x)
264 if ( getNsIdByName( m
[ 1 ] ) !== false ) {
272 i
= title
.indexOf( '#' );
277 // Get segment starting after the hash
280 // NB: Must not be trimmed ("Example#_foo" is not the same as "Example#foo")
281 .replace( /_
/g
, ' ' );
286 // Trim underscores, again (strips "_" from "bar" in "Foo_bar_#quux")
287 .replace( rUnderscoreTrim
, '' );
290 // Reject illegal characters
291 if ( title
.match( rInvalid
) ) {
295 // Disallow titles that browsers or servers might resolve as directory navigation
297 title
.indexOf( '.' ) !== -1 && (
298 title
=== '.' || title
=== '..' ||
299 title
.indexOf( './' ) === 0 ||
300 title
.indexOf( '../' ) === 0 ||
301 title
.indexOf( '/./' ) !== -1 ||
302 title
.indexOf( '/../' ) !== -1 ||
303 title
.slice( -2 ) === '/.' ||
304 title
.slice( -3 ) === '/..'
310 // Disallow magic tilde sequence
311 if ( title
.indexOf( '~~~' ) !== -1 ) {
315 // Disallow titles exceeding the TITLE_MAX_BYTES byte size limit (size of underlying database field)
316 // Except for special pages, e.g. [[Special:Block/Long name]]
317 // Note: The PHP implementation also asserts that even in NS_SPECIAL, the title should
318 // be less than 512 bytes.
319 if ( namespace !== NS_SPECIAL
&& $.byteLength( title
) > TITLE_MAX_BYTES
) {
323 // Can't make a link to a namespace alone.
324 if ( title
=== '' && namespace !== NS_MAIN
) {
328 // Any remaining initial :s are illegal.
329 if ( title
[ 0 ] === ':' ) {
333 // For backwards-compatibility with old mw.Title, we separate the extension from the
334 // rest of the title.
335 i
= title
.lastIndexOf( '.' );
336 if ( i
=== -1 || title
.length
<= i
+ 1 ) {
337 // Extensions are the non-empty segment after the last dot
340 ext
= title
.slice( i
+ 1 );
341 title
= title
.slice( 0, i
);
345 namespace: namespace,
353 * Convert db-key to readable text.
361 text = function ( s
) {
362 if ( s
!== null && s
!== undefined ) {
363 return s
.replace( /_
/g
, ' ' );
370 * Sanitizes a string based on a rule set and a filter
376 * @param {Array} filter
379 sanitize = function ( s
, filter
) {
380 var i
, ruleLength
, rule
, m
, filterLength
,
381 rules
= sanitationRules
;
383 for ( i
= 0, ruleLength
= rules
.length
; i
< ruleLength
; ++i
) {
385 for ( m
= 0, filterLength
= filter
.length
; m
< filterLength
; ++m
) {
386 if ( rule
[ filter
[ m
] ] ) {
387 s
= s
.replace( rule
.pattern
, rule
.replace
);
395 * Cuts a string to a specific byte length, assuming UTF-8
396 * or less, if the last character is a multi-byte one
400 * @method trimToByteLength
402 * @param {number} length
405 trimToByteLength = function ( s
, length
) {
406 var byteLength
, chopOffChars
, chopOffBytes
;
408 // bytelength is always greater or equal to the length in characters
409 s
= s
.substr( 0, length
);
410 while ( ( byteLength
= $.byteLength( s
) ) > length
) {
411 // Calculate how many characters can be safely removed
412 // First, we need to know how many bytes the string exceeds the threshold
413 chopOffBytes
= byteLength
- length
;
414 // A character in UTF-8 is at most 4 bytes
415 // One character must be removed in any case because the
416 // string is too long
417 chopOffChars
= Math
.max( 1, Math
.floor( chopOffBytes
/ 4 ) );
418 s
= s
.substr( 0, s
.length
- chopOffChars
);
424 * Cuts a file name to a specific byte length
428 * @method trimFileNameToByteLength
429 * @param {string} name without extension
430 * @param {string} extension file extension
431 * @return {string} The full name, including extension
433 trimFileNameToByteLength = function ( name
, extension
) {
434 // There is a special byte limit for file names and ... remember the dot
435 return trimToByteLength( name
, FILENAME_MAX_BYTES
- extension
.length
- 1 ) + '.' + extension
;
438 // Polyfill for ES5 Object.create
439 createObject
= Object
.create
|| ( function () {
440 return function ( o
) {
442 if ( o
!== Object( o
) ) {
443 throw new Error( 'Cannot inherit from a non-object' );
453 * Constructor for Title objects with a null return instead of an exception for invalid titles.
456 * @param {string} title
457 * @param {number} [namespace=NS_MAIN] Default namespace
458 * @return {mw.Title|null} A valid Title object or null if the title is invalid
460 Title
.newFromText = function ( title
, namespace ) {
461 var t
, parsed
= parse( title
, namespace );
466 t
= createObject( Title
.prototype );
467 t
.namespace = parsed
.namespace;
468 t
.title
= parsed
.title
;
470 t
.fragment
= parsed
.fragment
;
476 * Constructor for Title objects from user input altering that input to
477 * produce a title that MediaWiki will accept as legal
480 * @param {string} title
481 * @param {number} [defaultNamespace=NS_MAIN]
482 * If given, will used as default namespace for the given title.
483 * @param {Object} [options] additional options
484 * @param {string} [options.fileExtension='']
485 * If the title is about to be created for the Media or File namespace,
486 * ensures the resulting Title has the correct extension. Useful, for example
487 * on systems that predict the type by content-sniffing, not by file extension.
488 * If different from empty string, `forUploading` is assumed.
489 * @param {boolean} [options.forUploading=true]
490 * Makes sure that a file is uploadable under the title returned.
491 * There are pages in the file namespace under which file upload is impossible.
492 * Automatically assumed if the title is created in the Media namespace.
493 * @return {mw.Title|null} A valid Title object or null if the input cannot be turned into a valid title
495 Title
.newFromUserInput = function ( title
, defaultNamespace
, options
) {
496 var namespace, m
, id
, ext
, parts
;
498 // defaultNamespace is optional; check whether options moves up
499 if ( arguments
.length
< 3 && $.type( defaultNamespace
) === 'object' ) {
500 options
= defaultNamespace
;
501 defaultNamespace
= undefined;
504 // merge options into defaults
505 options
= $.extend( {
510 namespace = defaultNamespace
=== undefined ? NS_MAIN
: defaultNamespace
;
512 // Normalise whitespace and remove duplicates
513 title
= $.trim( title
.replace( rWhitespace
, ' ' ) );
515 // Process initial colon
516 if ( title
!== '' && title
[ 0 ] === ':' ) {
517 // Initial colon means main namespace instead of specified default
523 .replace( rUnderscoreTrim
, '' );
526 // Process namespace prefix (if any)
527 m
= title
.match( rSplit
);
529 id
= getNsIdByName( m
[ 1 ] );
530 if ( id
!== false ) {
531 // Ordinary namespace
537 if ( namespace === NS_MEDIA
538 || ( ( options
.forUploading
|| options
.fileExtension
) && ( namespace === NS_FILE
) )
541 title
= sanitize( title
, [ 'generalRule', 'fileRule' ] );
543 // Operate on the file extension
544 // Although it is possible having spaces between the name and the ".ext" this isn't nice for
545 // operating systems hiding file extensions -> strip them later on
546 parts
= title
.split( '.' );
548 if ( parts
.length
> 1 ) {
550 // Get the last part, which is supposed to be the file extension
553 if ( options
.fileExtension
) {
554 // Does the supplied file name carry the desired file extension?
555 if ( Title
.normalizeExtension( ext
) !== Title
.normalizeExtension( options
.fileExtension
) ) {
556 // No, push back, whatever there was after the dot
560 // Always canonicalize to the desired file extension (e.g. 'jpeg' to 'jpg')
561 ext
= options
.fileExtension
;
564 // Remove whitespace of the name part (that W/O extension)
565 title
= $.trim( parts
.join( '.' ) );
567 // Cut, if too long and append file extension
568 title
= trimFileNameToByteLength( title
, ext
);
572 // Missing file extension
573 title
= $.trim( parts
.join( '.' ) );
575 if ( options
.fileExtension
) {
577 // Cut, if too long and append the desired file extension
578 title
= trimFileNameToByteLength( title
, options
.fileExtension
);
582 // Name has no file extension and a fallback wasn't provided either
588 title
= sanitize( title
, [ 'generalRule' ] );
590 // Cut titles exceeding the TITLE_MAX_BYTES byte size limit
591 // (size of underlying database field)
592 if ( namespace !== NS_SPECIAL
) {
593 title
= trimToByteLength( title
, TITLE_MAX_BYTES
);
597 // Any remaining initial :s are illegal.
598 title
= title
.replace( /^\:+/, '' );
600 return Title
.newFromText( title
, namespace );
604 * Sanitizes a file name as supplied by the user, originating in the user's file system
605 * so it is most likely a valid MediaWiki title and file name after processing.
606 * Returns null on fatal errors.
609 * @param {string} uncleanName The unclean file name including file extension but
611 * @param {string} [fileExtension] the desired file extension
612 * @return {mw.Title|null} A valid Title object or null if the title is invalid
614 Title
.newFromFileName = function ( uncleanName
, fileExtension
) {
616 return Title
.newFromUserInput( 'File:' + uncleanName
, {
617 fileExtension
: fileExtension
,
623 * Get the file title from an image element
625 * var title = mw.Title.newFromImg( $( 'img:first' ) );
628 * @param {HTMLElement|jQuery} img The image to use as a base
629 * @return {mw.Title|null} The file title or null if unsuccessful
631 Title
.newFromImg = function ( img
) {
632 var matches
, i
, regex
, src
, decodedSrc
,
634 // thumb.php-generated thumbnails
635 thumbPhpRegex
= /thumb\.php/,
638 /\/[a-f0-9]\/[a-f0-9]{2}\/([^\s\/]+)\/[^\s\/]+-(?:\1|thumbnail)[^\s\/]*$/,
640 // Thumbnails in non-hashed upload directories
641 /\/([^\s\/]+)\/[^\s\/]+-(?:\1|thumbnail)[^\s\/]*$/,
644 /\/[a-f0-9]\/[a-f0-9]{2}\/([^\s\/]+)$/,
646 // Full-size images in non-hashed upload directories
650 recount
= regexes
.length
;
652 src
= img
.jquery
? img
[ 0 ].src
: img
.src
;
654 matches
= src
.match( thumbPhpRegex
);
657 return mw
.Title
.newFromText( 'File:' + mw
.util
.getParamValue( 'f', src
) );
660 decodedSrc
= decodeURIComponent( src
);
662 for ( i
= 0; i
< recount
; i
++ ) {
663 regex
= regexes
[ i
];
664 matches
= decodedSrc
.match( regex
);
666 if ( matches
&& matches
[ 1 ] ) {
667 return mw
.Title
.newFromText( 'File:' + matches
[ 1 ] );
675 * Whether this title exists on the wiki.
678 * @param {string|mw.Title} title prefixed db-key name (string) or instance of Title
679 * @return {boolean|null} Boolean if the information is available, otherwise null
681 Title
.exists = function ( title
) {
683 type
= $.type( title
),
684 obj
= Title
.exist
.pages
;
686 if ( type
=== 'string' ) {
687 match
= obj
[ title
];
688 } else if ( type
=== 'object' && title
instanceof Title
) {
689 match
= obj
[ title
.toString() ];
691 throw new Error( 'mw.Title.exists: title must be a string or an instance of Title' );
694 if ( typeof match
=== 'boolean' ) {
702 * Store page existence
705 * @property {Object} exist
706 * @property {Object} exist.pages Keyed by title. Boolean true value indicates page does exist.
708 * @property {Function} exist.set The setter function.
710 * Example to declare existing titles:
712 * Title.exist.set( ['User:John_Doe', ...] );
714 * Example to declare titles nonexistent:
716 * Title.exist.set( ['File:Foo_bar.jpg', ...], false );
718 * @property {string|Array} exist.set.titles Title(s) in strict prefixedDb title form
719 * @property {boolean} [exist.set.state=true] State of the given titles
725 set: function ( titles
, state
) {
726 titles
= $.isArray( titles
) ? titles
: [ titles
];
727 state
= state
=== undefined ? true : !!state
;
732 for ( i
= 0; i
< len
; i
++ ) {
733 pages
[ titles
[ i
] ] = state
;
740 * Normalize a file extension to the common form, making it lowercase and checking some synonyms,
741 * and ensure it's clean. Extensions with non-alphanumeric characters will be discarded.
742 * Keep in sync with File::normalizeExtension() in PHP.
744 * @param {string} extension File extension (without the leading dot)
745 * @return {string} File extension in canonical form
747 Title
.normalizeExtension = function ( extension
) {
749 lower
= extension
.toLowerCase(),
757 if ( squish
.hasOwnProperty( lower
) ) {
758 return squish
[ lower
];
759 } else if ( /^[0-9a-z]+$/.test( lower
) ) {
772 * Get the namespace number
774 * Example: 6 for "File:Example_image.svg".
778 getNamespaceId: function () {
779 return this.namespace;
783 * Get the namespace prefix (in the content language)
785 * Example: "File:" for "File:Example_image.svg".
786 * In #NS_MAIN this is '', otherwise namespace name plus ':'
790 getNamespacePrefix: function () {
791 return this.namespace === NS_MAIN
?
793 ( mw
.config
.get( 'wgFormattedNamespaces' )[ this.namespace ].replace( / /g
, '_' ) + ':' );
797 * Get the page name without extension or namespace prefix
799 * Example: "Example_image" for "File:Example_image.svg".
801 * For the page title (full page name without namespace prefix), see #getMain.
805 getName: function () {
807 $.inArray( this.namespace, mw
.config
.get( 'wgCaseSensitiveNamespaces' ) ) !== -1 ||
812 return this.title
[ 0 ].toUpperCase() + this.title
.slice( 1 );
816 * Get the page name (transformed by #text)
818 * Example: "Example image" for "File:Example_image.svg".
820 * For the page title (full page name without namespace prefix), see #getMainText.
824 getNameText: function () {
825 return text( this.getName() );
829 * Get the extension of the page name (if any)
831 * @return {string|null} Name extension or null if there is none
833 getExtension: function () {
838 * Shortcut for appendable string to form the main page name.
840 * Returns a string like ".json", or "" if no extension.
844 getDotExtension: function () {
845 return this.ext
=== null ? '' : '.' + this.ext
;
849 * Get the main page name
851 * Example: "Example_image.svg" for "File:Example_image.svg".
855 getMain: function () {
856 return this.getName() + this.getDotExtension();
860 * Get the main page name (transformed by #text)
862 * Example: "Example image.svg" for "File:Example_image.svg".
866 getMainText: function () {
867 return text( this.getMain() );
871 * Get the full page name
873 * Example: "File:Example_image.svg".
874 * Most useful for API calls, anything that must identify the "title".
878 getPrefixedDb: function () {
879 return this.getNamespacePrefix() + this.getMain();
883 * Get the full page name (transformed by #text)
885 * Example: "File:Example image.svg" for "File:Example_image.svg".
889 getPrefixedText: function () {
890 return text( this.getPrefixedDb() );
894 * Get the page name relative to a namespace
898 * - "Foo:Bar" relative to the Foo namespace becomes "Bar".
899 * - "Bar" relative to any non-main namespace becomes ":Bar".
900 * - "Foo:Bar" relative to any namespace other than Foo stays "Foo:Bar".
902 * @param {number} namespace The namespace to be relative to
905 getRelativeText: function ( namespace ) {
906 if ( this.getNamespaceId() === namespace ) {
907 return this.getMainText();
908 } else if ( this.getNamespaceId() === NS_MAIN
) {
909 return ':' + this.getPrefixedText();
911 return this.getPrefixedText();
916 * Get the fragment (if any).
918 * Note that this method (by design) does not include the hash character and
919 * the value is not url encoded.
921 * @return {string|null}
923 getFragment: function () {
924 return this.fragment
;
928 * Get the URL to this title
930 * @see mw.util#getUrl
931 * @param {Object} [params] A mapping of query parameter names to values,
932 * e.g. `{ action: 'edit' }`.
935 getUrl: function ( params
) {
936 return mw
.util
.getUrl( this.toString(), params
);
940 * Whether this title exists on the wiki.
942 * @see #static-method-exists
943 * @return {boolean|null} Boolean if the information is available, otherwise null
945 exists: function () {
946 return Title
.exists( this );
951 * @alias #getPrefixedDb
954 Title
.prototype.toString
= Title
.prototype.getPrefixedDb
;
957 * @alias #getPrefixedText
960 Title
.prototype.toText
= Title
.prototype.getPrefixedText
;
965 }( mediaWiki
, jQuery
) );