Index: trunk/phase3/RELEASE-NOTES-1.19 |
— | — | @@ -53,6 +53,7 @@ |
54 | 54 | * (bug 28904) Update jQuery version from 1.4.4 to 1.6.1 (the latest version) |
55 | 55 | * (bug 29441) Expose CapitalLinks config in JS to allow modules to properly |
56 | 56 | handle titles on case-sensitive wikis. |
| 57 | +* (bug 29397) Implement mw.Title module in core. |
57 | 58 | |
58 | 59 | === Bug fixes in 1.19 === |
59 | 60 | * (bug 28868) Show total pages in the subtitle of an image on the |
Index: trunk/phase3/tests/qunit/index.html |
— | — | @@ -36,6 +36,7 @@ |
37 | 37 | <script src="../../resources/jquery/jquery.autoEllipsis.js"></script> |
38 | 38 | <script src="../../resources/jquery/jquery.colorUtil.js"></script> |
39 | 39 | <script src="../../resources/jquery/jquery.tabIndex.js"></script> |
| 40 | + <script src="../../resources/mediawiki/mediawiki.Title.js"></script> |
40 | 41 | |
41 | 42 | <!-- QUnit: Load framework --> |
42 | 43 | <link rel="stylesheet" href="../../resources/jquery/jquery.qunit.css" /> |
— | — | @@ -52,6 +53,7 @@ |
53 | 54 | <script src="suites/resources/jquery/jquery.autoEllipsis.js"></script> |
54 | 55 | <script src="suites/resources/jquery/jquery.colorUtil.js"></script> |
55 | 56 | <script src="suites/resources/jquery/jquery.tabIndex.js"></script> |
| 57 | + <script src="suites/resources/mediawiki/mediawiki.Title.js"></script> |
56 | 58 | |
57 | 59 | <!-- TestSwarm: If a test swarm is running this, |
58 | 60 | the following script will allow it to extract the results. |
Index: trunk/phase3/tests/qunit/jquery.qunit.completenessTest.config.js |
— | — | @@ -1,13 +1,18 @@ |
2 | 2 | // Return true to ignore |
3 | 3 | var mwTestIgnore = function( val, tester, funcPath ) { |
4 | 4 | |
5 | | - // Don't record methods of the properties of mw.Map instances |
6 | | - // Because we're therefor skipping any injection for |
7 | | - // "new mw.Map()", manually set it to true here. |
| 5 | + // Don't record methods of the properties of constructors, |
| 6 | + // to avoid getting into a loop (prototype.constructor.prototype..). |
| 7 | + // Since we're therefor skipping any injection for |
| 8 | + // "new mw.Foo()", manually set it to true here. |
8 | 9 | if ( val instanceof mw.Map ) { |
9 | 10 | tester.methodCallTracker['Map'] = true; |
10 | 11 | return true; |
11 | 12 | } |
| 13 | + if ( val instanceof mw.Title ) { |
| 14 | + tester.methodCallTracker['Title'] = true; |
| 15 | + return true; |
| 16 | + } |
12 | 17 | |
13 | 18 | // Don't record methods of the properties of a jQuery object |
14 | 19 | if ( val instanceof $ ) { |
Index: trunk/phase3/resources/jquery/jquery.qunit.completenessTest.js |
— | — | @@ -155,6 +155,7 @@ |
156 | 156 | |
157 | 157 | // ...the prototypes are fine tho |
158 | 158 | $.each( currVar.prototype, function( key, value ) { |
| 159 | + if ( key === 'constructor' ) return; |
159 | 160 | |
160 | 161 | // Clone and brake reference to parentPathArray |
161 | 162 | var tmpPathArray = $.extend( [], parentPathArray ); |
— | — | @@ -182,6 +183,7 @@ |
183 | 184 | |
184 | 185 | // ... the prototypes are fine tho |
185 | 186 | $.each( currVar.prototype, function( key, value ) { |
| 187 | + if ( key === 'constructor' ) return; |
186 | 188 | |
187 | 189 | // Clone and brake reference to parentPathArray |
188 | 190 | var tmpPathArray = $.extend( [], parentPathArray ); |
Index: trunk/phase3/resources/Resources.php |
— | — | @@ -439,6 +439,9 @@ |
440 | 440 | 'mediawiki.htmlform' => array( |
441 | 441 | 'scripts' => 'resources/mediawiki/mediawiki.htmlform.js', |
442 | 442 | ), |
| 443 | + 'mediawiki.Title' => array( |
| 444 | + 'scripts' => 'resources/mediawiki/mediawiki.Title.js', |
| 445 | + ), |
443 | 446 | 'mediawiki.user' => array( |
444 | 447 | 'scripts' => 'resources/mediawiki/mediawiki.user.js', |
445 | 448 | 'dependencies' => array( |
Index: trunk/phase3/resources/mediawiki/mediawiki.Title.js |
— | — | @@ -0,0 +1,316 @@ |
| 2 | +/** |
| 3 | + * mediaWiki.Title |
| 4 | + * |
| 5 | + * @author Neil Kandalgaonkar, 2010 |
| 6 | + * @author Timo Tijhof, 2011 |
| 7 | + * @since 1.19 |
| 8 | + * |
| 9 | + * Relies on: mw.config (wgFormattedNamespaces, wgNamespaceIds, wgCaseSensitiveNamespaces), mw.util.wikiGetlink |
| 10 | + */ |
| 11 | +(function( $ ) { |
| 12 | + |
| 13 | + /* Local space */ |
| 14 | + |
| 15 | + /** |
| 16 | + * Title |
| 17 | + * @constructor |
| 18 | + * |
| 19 | + * @param title {String} Title of the page. If no second argument given, |
| 20 | + * this will be searched for a namespace. |
| 21 | + * @param namespace {Number} (optional) Namespace id. If given, title will be taken as-is. |
| 22 | + * @return {Title} this |
| 23 | + */ |
| 24 | +var Title = function( title, namespace ) { |
| 25 | + this._ns = 0; // integer namespace id |
| 26 | + this._name = null; // name in canonical 'database' form |
| 27 | + this._ext = null; // extension |
| 28 | + |
| 29 | + if ( arguments.length === 2 ) { |
| 30 | + this.setNameAndExtension( title ).setNamespaceById( namespace ); |
| 31 | + } else if ( arguments.length === 1 ) { |
| 32 | + // If title is like "Blabla: Hello" ignore exception by setNamespace(), |
| 33 | + // and instead assume NS_MAIN and keep prefix |
| 34 | + try { |
| 35 | + this.setAll( title ); |
| 36 | + } catch(e) { |
| 37 | + this.setNameAndExtension( title ); |
| 38 | + } |
| 39 | + } |
| 40 | + return this; |
| 41 | + }, |
| 42 | + |
| 43 | + /** |
| 44 | + * Strip some illegal chars: control chars, colon, less than, greater than, |
| 45 | + * brackets, braces, pipe, whitespace and normal spaces. This still leaves some insanity |
| 46 | + * intact, like unicode bidi chars, but it's a good start.. |
| 47 | + * @param s {String} |
| 48 | + * @return {String} |
| 49 | + */ |
| 50 | + clean = function( s ) { |
| 51 | + if ( s !== undefined ) { |
| 52 | + return s.replace( /[\x00-\x1f\x23\x3c\x3e\x5b\x5d\x7b\x7c\x7d\x7f\s]+/g, '_' ); |
| 53 | + } |
| 54 | + }, |
| 55 | + |
| 56 | + /** |
| 57 | + * Convert db-key to readable text. |
| 58 | + * @param s {String} |
| 59 | + * @return {String} |
| 60 | + */ |
| 61 | + text = function ( s ) { |
| 62 | + if ( s !== null && s !== undefined ) { |
| 63 | + return s.replace( /_/g, ' ' ); |
| 64 | + } else { |
| 65 | + return ''; |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + /* Static space */ |
| 70 | + |
| 71 | + /** |
| 72 | + * Wether this title exists on the wiki. |
| 73 | + * @param title {mixed} prefixed db-key name (string) or instance of Title |
| 74 | + * @return {mixed} Boolean true/false if the information is available. Otherwise null. |
| 75 | + */ |
| 76 | + Title.exists = function( title ) { |
| 77 | + var type = $.type( title ), obj = Title.exist.pages, match; |
| 78 | + if ( type === 'string' ) { |
| 79 | + match = obj[title]; |
| 80 | + } else if ( type === 'object' && title instanceof Title ) { |
| 81 | + match = obj[title.toString()]; |
| 82 | + } else { |
| 83 | + throw new Error( 'mw.Title.exists: title must be a string or an instance of Title' ); |
| 84 | + } |
| 85 | + if ( typeof match === 'boolean' ) { |
| 86 | + return match; |
| 87 | + } |
| 88 | + return null; |
| 89 | + }; |
| 90 | + |
| 91 | + /** |
| 92 | + * @var Title.exist {Object} |
| 93 | + */ |
| 94 | + Title.exist = { |
| 95 | + /** |
| 96 | + * @var Title.exist.pages {Object} Keyed by PrefixedDb title. |
| 97 | + * Boolean true value indicates page does exist. |
| 98 | + */ |
| 99 | + pages: {}, |
| 100 | + /** |
| 101 | + * @example Declare existing titles: Title.exist.set(['User:John_Doe', ...]); |
| 102 | + * @example Declare titles inexisting: Title.exist.set(['File:Foo_bar.jpg', ...], false); |
| 103 | + * @param titles {String|Array} Title(s) in strict prefixedDb title form. |
| 104 | + * @param state {Boolean} (optional) State of the given titles. Defaults to true. |
| 105 | + * @return {Boolean} |
| 106 | + */ |
| 107 | + set: function( titles, state ) { |
| 108 | + titles = $.isArray( titles ) ? titles : [titles]; |
| 109 | + state = state === undefined ? true : !!state; |
| 110 | + var pages = this.pages, i, len = titles.length; |
| 111 | + for ( i = 0; i < len; i++ ) { |
| 112 | + pages[ titles[i] ] = state; |
| 113 | + } |
| 114 | + return true; |
| 115 | + } |
| 116 | + }; |
| 117 | + |
| 118 | + /* Public methods */ |
| 119 | + |
| 120 | + var fn = { |
| 121 | + constructor: Title, |
| 122 | + /** |
| 123 | + * @param id {Number} Canonical namespace id. |
| 124 | + * @return {mw.Title} this |
| 125 | + */ |
| 126 | + setNamespaceById: function( id ) { |
| 127 | + // wgFormattedNamespaces is an object of *string* key-vals, |
| 128 | + var ns = mw.config.get( 'wgFormattedNamespaces' )[id.toString()]; |
| 129 | + |
| 130 | + // Cannot cast to boolean, ns may be '' (main namespace) |
| 131 | + if ( ns === undefined ) { |
| 132 | + this._ns = false; |
| 133 | + } else { |
| 134 | + this._ns = Number( id ); |
| 135 | + } |
| 136 | + return this; |
| 137 | + }, |
| 138 | + |
| 139 | + /** |
| 140 | + * Set namespace by any known namespace/id pair (localized, canonical or alias) |
| 141 | + * On a German wiki this could be 'File', 'Datei', 'Image' or even 'Bild' for NS_FILE. |
| 142 | + * @param ns {String} A namespace name (case insensitive, space insensitive) |
| 143 | + * @return {mw.Title} this |
| 144 | + */ |
| 145 | + setNamespace: function( ns ) { |
| 146 | + ns = clean( $.trim( ns.toLowerCase() ) ); // Normalize |
| 147 | + var id = mw.config.get( 'wgNamespaceIds' )[ns]; |
| 148 | + if ( id === undefined ) { |
| 149 | + throw new Error( 'mw.Title: Unrecognized canonical namespace: ' + ns ); |
| 150 | + } |
| 151 | + return this.setNamespaceById( id ); |
| 152 | + }, |
| 153 | + |
| 154 | + /** |
| 155 | + * Get the namespace number. |
| 156 | + * @return {Number} |
| 157 | + */ |
| 158 | + getNamespaceId: function(){ |
| 159 | + return this._ns; |
| 160 | + }, |
| 161 | + |
| 162 | + /** |
| 163 | + * Get the namespace prefix (in the content-language). |
| 164 | + * In NS_MAIN this is '', otherwise namespace name plus ':' |
| 165 | + * @return {String} |
| 166 | + */ |
| 167 | + getNamespacePrefix: function(){ |
| 168 | + return mw.config.get( 'wgFormattedNamespaces' )[this._ns].replace( / /g, '_' ) + (this._ns === 0 ? '' : ':'); |
| 169 | + }, |
| 170 | + |
| 171 | + /** |
| 172 | + * Set the "name" portion, removing illegal characters. |
| 173 | + * @param s {String} Page name (without namespace prefix) |
| 174 | + * @return {mw.Title} this |
| 175 | + */ |
| 176 | + setName: function( s ) { |
| 177 | + this._name = clean( $.trim( s ) ); |
| 178 | + return this; |
| 179 | + }, |
| 180 | + |
| 181 | + /** |
| 182 | + * The name, like "Foo_bar" |
| 183 | + * @return {String} |
| 184 | + */ |
| 185 | + getName: function() { |
| 186 | + if ( $.inArray( this._ns, mw.config.get( 'wgCaseSensitiveNamespaces' ) ) !== -1 ) { |
| 187 | + return this._name; |
| 188 | + } else { |
| 189 | + return $.ucFirst( this._name ); |
| 190 | + } |
| 191 | + }, |
| 192 | + |
| 193 | + /** |
| 194 | + * The name, like "Foo bar" |
| 195 | + * @return {String} |
| 196 | + */ |
| 197 | + getNameText: function() { |
| 198 | + return text( this.getName() ); |
| 199 | + }, |
| 200 | + |
| 201 | + /** |
| 202 | + * Get full name in prefixed DB form, like File:Foo_bar.jpg, |
| 203 | + * most useful for API calls, anything that must identify the "title". |
| 204 | + */ |
| 205 | + getPrefixedDb: function() { |
| 206 | + return this.getNamespacePrefix() + this.getMain(); |
| 207 | + }, |
| 208 | + |
| 209 | + /** |
| 210 | + * Get full name in text form, like "File:Foo bar.jpg". |
| 211 | + * @return {String} |
| 212 | + */ |
| 213 | + getPrefixedText: function() { |
| 214 | + return text( this.getPrefixedDb() ); |
| 215 | + }, |
| 216 | + |
| 217 | + /** |
| 218 | + * The main title (without namespace), like "Foo_bar.jpg" |
| 219 | + * @return {String} |
| 220 | + */ |
| 221 | + getMain: function() { |
| 222 | + return this.getName() + this.getDotExtension(); |
| 223 | + }, |
| 224 | + |
| 225 | + /** |
| 226 | + * The "text" form, like "Foo bar.jpg" |
| 227 | + * @return {String} |
| 228 | + */ |
| 229 | + getMainText: function() { |
| 230 | + return text( this.getMain() ); |
| 231 | + }, |
| 232 | + |
| 233 | + /** |
| 234 | + * Set the "extension" portion, removing illegal characters. |
| 235 | + * @param s {String} |
| 236 | + * @return {mw.Title} this |
| 237 | + */ |
| 238 | + setExtension: function( s ) { |
| 239 | + this._ext = clean( s.toLowerCase() ); |
| 240 | + return this; |
| 241 | + }, |
| 242 | + |
| 243 | + /** |
| 244 | + * Get the extension (returns null if there was none) |
| 245 | + * @return {String|null} extension |
| 246 | + */ |
| 247 | + getExtension: function() { |
| 248 | + return this._ext; |
| 249 | + }, |
| 250 | + |
| 251 | + /** |
| 252 | + * Convenience method: return string like ".jpg", or "" if no extension |
| 253 | + * @return {String} |
| 254 | + */ |
| 255 | + getDotExtension: function() { |
| 256 | + return this._ext === null ? '' : '.' + this._ext; |
| 257 | + }, |
| 258 | + |
| 259 | + /** |
| 260 | + * @param s {String} |
| 261 | + * @return {mw.Title} this |
| 262 | + */ |
| 263 | + setAll: function( s ) { |
| 264 | + var matches = s.match( /^(?:([^:]+):)?(.*?)(?:\.(\w{1,5}))?$/ ); |
| 265 | + if ( matches.length ) { |
| 266 | + if ( matches[1] ) { this.setNamespace( matches[1] ); } |
| 267 | + if ( matches[2] ) { this.setName( matches[2] ); } |
| 268 | + if ( matches[3] ) { this.setExtension( matches[3] ); } |
| 269 | + } else { |
| 270 | + throw new Error( 'mw.Title: Could not parse title "' + s + '"' ); |
| 271 | + } |
| 272 | + return this; |
| 273 | + }, |
| 274 | + |
| 275 | + /** |
| 276 | + * @param s {String} |
| 277 | + * @return {mw.Title} this |
| 278 | + */ |
| 279 | + setNameAndExtension: function( s ) { |
| 280 | + var matches = s.match( /^(?:)?(.*?)(?:\.(\w{1,5}))?$/ ); |
| 281 | + if ( matches.length ) { |
| 282 | + if ( matches[1] ) { this.setName( matches[1] ); } |
| 283 | + if ( matches[2] ) { this.setExtension( matches[2] ); } |
| 284 | + } else { |
| 285 | + throw new Error( 'mw.Title: Could not parse title "' + s + '"' ); |
| 286 | + } |
| 287 | + return this; |
| 288 | + }, |
| 289 | + |
| 290 | + /** |
| 291 | + * Return the URL to this title |
| 292 | + * @return {String} |
| 293 | + */ |
| 294 | + getUrl: function() { |
| 295 | + return mw.util.wikiGetlink( this.toString() ); |
| 296 | + }, |
| 297 | + |
| 298 | + /** |
| 299 | + * Wether this title exists on the wiki. |
| 300 | + * @return {mixed} Boolean true/false if the information is available. Otherwise null. |
| 301 | + */ |
| 302 | + exists: function() { |
| 303 | + return Title.exists( this ); |
| 304 | + } |
| 305 | + }; |
| 306 | + |
| 307 | + // Alias |
| 308 | + fn.toString = fn.getPrefixedDb; |
| 309 | + fn.toText = fn.getPrefixedText; |
| 310 | + |
| 311 | + // Assign |
| 312 | + Title.prototype = fn; |
| 313 | + |
| 314 | + // Expose |
| 315 | + mw.Title = Title; |
| 316 | + |
| 317 | +})(jQuery); |
Property changes on: trunk/phase3/resources/mediawiki/mediawiki.Title.js |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 318 | + native |