4 * @author Neil Kandalgaonkar, 2010
5 * @author Timo Tijhof, 2011
8 * Relies on: mw.config (wgFormattedNamespaces, wgNamespaceIds, wgCaseSensitiveNamespaces), mw.util.wikiGetlink
18 * @param title {String} Title of the page. If no second argument given,
19 * this will be searched for a namespace.
20 * @param namespace {Number} (optional) Namespace id. If given, title will be taken as-is.
21 * @return {Title} this
23 var Title = function( title
, namespace ) {
24 this._ns
= 0; // integer namespace id
25 this._name
= null; // name in canonical 'database' form
26 this._ext
= null; // extension
28 if ( arguments
.length
=== 2 ) {
29 setNameAndExtension( this, title
);
30 this._ns
= fixNsId( namespace );
31 } else if ( arguments
.length
=== 1 ) {
32 setAll( this, title
);
38 * Strip some illegal chars: control chars, colon, less than, greater than,
39 * brackets, braces, pipe, whitespace and normal spaces. This still leaves some insanity
40 * intact, like unicode bidi chars, but it's a good start..
44 clean = function( s
) {
45 if ( s
!== undefined ) {
46 return s
.replace( /[\x00-\x1f\x23\x3c\x3e\x5b\x5d\x7b\x7c\x7d\x7f\s]+/g, '_' );
51 * Convert db-key to readable text.
55 text = function ( s
) {
56 if ( s
!== null && s
!== undefined ) {
57 return s
.replace( /_
/g
, ' ' );
66 fixName = function( s
) {
67 return clean( $.trim( s
) );
73 fixExt = function( s
) {
74 return clean( s
.toLowerCase() );
78 * Sanatize namespace id.
79 * @param id {Number} Namespace id.
80 * @return {Number|Boolean} The id as-is or boolean false if invalid.
82 fixNsId = function( id
) {
83 // wgFormattedNamespaces is an object of *string* key-vals (ie. arr["0"] not arr[0] )
84 var ns
= mw
.config
.get( 'wgFormattedNamespaces' )[id
.toString()];
86 // Check only undefined (may be false-y, such as '' (main namespace) ).
87 if ( ns
=== undefined ) {
95 * Get namespace id from namespace name by any known namespace/id pair (localized, canonical or alias).
97 * @example On a German wiki this would return 6 for any of 'File', 'Datei', 'Image' or even 'Bild'.
98 * @param ns {String} Namespace name (case insensitive, leading/trailing space ignored).
99 * @return {Number|Boolean} Namespace id or boolean false if unrecognized.
101 getNsIdByName = function( ns
) {
102 // toLowerCase throws exception on null/undefined. Return early.
106 ns
= clean( $.trim( ns
.toLowerCase() ) ); // Normalize
107 var id
= mw
.config
.get( 'wgNamespaceIds' )[ns
];
108 if ( id
=== undefined ) {
109 mw
.log( 'mw.Title: Unrecognized namespace: ' + ns
);
112 return fixNsId( id
);
116 * Helper to extract namespace, name and extension from a string.
118 * @param title {mw.Title}
119 * @param raw {String}
122 setAll = function( title
, s
) {
123 var matches
= s
.match( /^(?:([^:]+):)?(.*?)(?:\.(\w{1,5}))?$/ ),
124 ns_match
= getNsIdByName( matches
[1] );
125 if ( matches
.length
&& ns_match
) {
126 if ( matches
[1] ) { title
._ns
= ns_match
; }
127 if ( matches
[2] ) { title
._name
= fixName( matches
[2] ); }
128 if ( matches
[3] ) { title
._ext
= fixExt( matches
[3] ); }
130 // Consistency with MediaWiki: Unknown namespace > fallback to main namespace.
132 setNameAndExtension( title
, s
);
138 * Helper to extract name and extension from a string.
140 * @param title {mw.Title}
141 * @param raw {String}
144 setNameAndExtension = function( title
, raw
) {
145 var matches
= raw
.match( /^(?:)?(.*?)(?:\.(\w{1,5}))?$/ );
146 if ( matches
.length
) {
147 if ( matches
[1] ) { title
._name
= fixName( matches
[1] ); }
148 if ( matches
[2] ) { title
._ext
= fixExt( matches
[2] ); }
150 throw new Error( 'mw.Title: Could not parse title "' + raw
+ '"' );
159 * Wether this title exists on the wiki.
160 * @param title {mixed} prefixed db-key name (string) or instance of Title
161 * @return {mixed} Boolean true/false if the information is available. Otherwise null.
163 Title
.exists = function( title
) {
164 var type
= $.type( title
), obj
= Title
.exist
.pages
, match
;
165 if ( type
=== 'string' ) {
167 } else if ( type
=== 'object' && title
instanceof Title
) {
168 match
= obj
[title
.toString()];
170 throw new Error( 'mw.Title.exists: title must be a string or an instance of Title' );
172 if ( typeof match
=== 'boolean' ) {
179 * @var Title.exist {Object}
183 * @var Title.exist.pages {Object} Keyed by PrefixedDb title.
184 * Boolean true value indicates page does exist.
188 * @example Declare existing titles: Title.exist.set(['User:John_Doe', ...]);
189 * @example Declare titles inexisting: Title.exist.set(['File:Foo_bar.jpg', ...], false);
190 * @param titles {String|Array} Title(s) in strict prefixedDb title form.
191 * @param state {Boolean} (optional) State of the given titles. Defaults to true.
194 set: function( titles
, state
) {
195 titles
= $.isArray( titles
) ? titles
: [titles
];
196 state
= state
=== undefined ? true : !!state
;
197 var pages
= this.pages
, i
, len
= titles
.length
;
198 for ( i
= 0; i
< len
; i
++ ) {
199 pages
[ titles
[i
] ] = state
;
211 * Get the namespace number.
214 getNamespaceId: function(){
219 * Get the namespace prefix (in the content-language).
220 * In NS_MAIN this is '', otherwise namespace name plus ':'
223 getNamespacePrefix: function(){
224 return mw
.config
.get( 'wgFormattedNamespaces' )[this._ns
].replace( / /g
, '_' ) + (this._ns
=== 0 ? '' : ':');
228 * The name, like "Foo_bar"
231 getName: function() {
232 if ( $.inArray( this._ns
, mw
.config
.get( 'wgCaseSensitiveNamespaces' ) ) !== -1 ) {
235 return $.ucFirst( this._name
);
240 * The name, like "Foo bar"
243 getNameText: function() {
244 return text( this.getName() );
248 * Get full name in prefixed DB form, like File:Foo_bar.jpg,
249 * most useful for API calls, anything that must identify the "title".
251 getPrefixedDb: function() {
252 return this.getNamespacePrefix() + this.getMain();
256 * Get full name in text form, like "File:Foo bar.jpg".
259 getPrefixedText: function() {
260 return text( this.getPrefixedDb() );
264 * The main title (without namespace), like "Foo_bar.jpg"
267 getMain: function() {
268 return this.getName() + this.getDotExtension();
272 * The "text" form, like "Foo bar.jpg"
275 getMainText: function() {
276 return text( this.getMain() );
280 * Get the extension (returns null if there was none)
281 * @return {String|null} extension
283 getExtension: function() {
288 * Convenience method: return string like ".jpg", or "" if no extension
291 getDotExtension: function() {
292 return this._ext
=== null ? '' : '.' + this._ext
;
296 * Return the URL to this title
300 return mw
.util
.wikiGetlink( this.toString() );
304 * Wether this title exists on the wiki.
305 * @return {mixed} Boolean true/false if the information is available. Otherwise null.
308 return Title
.exists( this );
313 fn
.toString
= fn
.getPrefixedDb
;
314 fn
.toText
= fn
.getPrefixedText
;
317 Title
.prototype = fn
;