r94644 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r94643‎ | r94644 | r94645 >
Date:15:22, 16 August 2011
Author:catrope
Status:ok
Tags:
Comment:
1.18: Back out mw.Title, which isn't used by anything in 1.18 (AJAXCategories was backed out, UploadWizard has its own copy) and which has some strangely named public functions we'll probably want to change in 1.19. This reverts almost all of r90331 (which introduced mw.Title; the other revs to it were post-branch), only the changes to jquery.qunit.completenessTest.js (don't descend into a constructor) were left in.
Modified paths:
  • /branches/REL1_18/phase3/RELEASE-NOTES-1.18 (modified) (history)
  • /branches/REL1_18/phase3/resources/Resources.php (modified) (history)
  • /branches/REL1_18/phase3/resources/mediawiki/mediawiki.Title.js (deleted) (history)
  • /branches/REL1_18/phase3/tests/qunit/data/testrunner.js (modified) (history)
  • /branches/REL1_18/phase3/tests/qunit/index.html (modified) (history)
  • /branches/REL1_18/phase3/tests/qunit/suites/resources/mediawiki/mediawiki.Title.js (deleted) (history)

Diff [purge]

Index: branches/REL1_18/phase3/RELEASE-NOTES-1.18
@@ -175,7 +175,6 @@
176176 * (bug 28904) (bug 29773) Update jQuery version from 1.4.4 to 1.6.2 (the latest version)
177177 * (bug 29441) Expose CapitalLinks config in JS to allow modules to properly
178178 handle titles on case-sensitive wikis.
179 -* (bug 29397) Implement mw.Title module in core.
180179 * In MySQL 4.1.9+ with replication enabled, the slave lag should come from
181180 SHOW SLAVE STATUS instead of SHOW PROCESSLIST.
182181 * Language codes in $wgDummyLanguageCodes are now excluded on localization
Index: branches/REL1_18/phase3/tests/qunit/data/testrunner.js
@@ -33,10 +33,6 @@
3434 tester.methodCallTracker['Map'] = true;
3535 return true;
3636 }
37 - if ( val instanceof mw.Title ) {
38 - tester.methodCallTracker['Title'] = true;
39 - return true;
40 - }
4137
4238 // Don't record methods of the properties of a jQuery object
4339 if ( val instanceof $ ) {
Index: branches/REL1_18/phase3/tests/qunit/index.html
@@ -47,7 +47,6 @@
4848 <script src="../../resources/jquery/jquery.localize.js"></script>
4949 <script src="../../resources/jquery/jquery.tabIndex.js"></script>
5050 <script src="../../resources/jquery/jquery.tablesorter.js"></script>
51 - <script src="../../resources/mediawiki/mediawiki.Title.js"></script>
5251 <script src="../../resources/mediawiki.special/mediawiki.special.js"></script>
5352 <script src="../../resources/mediawiki.special/mediawiki.special.recentchanges.js"></script>
5453
@@ -73,7 +72,6 @@
7473 <script src="suites/resources/jquery/jquery.localize.js"></script>
7574 <script src="suites/resources/jquery/jquery.tabIndex.js"></script>
7675 <script src="suites/resources/jquery/jquery.tablesorter.test.js" charset="UTF-8"></script>
77 - <script src="suites/resources/mediawiki/mediawiki.Title.js"></script>
7876 <script src="suites/resources/mediawiki.special/mediawiki.special.recentchanges.js"></script>
7977 </head>
8078 <body>
Index: branches/REL1_18/phase3/tests/qunit/suites/resources/mediawiki/mediawiki.Title.js
@@ -1,136 +0,0 @@
2 -module( 'mediawiki.Title.js' );
3 -
4 -// mw.Title relies on these three config vars
5 -// Restore them after each test run
6 -var _titleConfig = function() {
7 -
8 - mw.config.set({
9 - "wgFormattedNamespaces": {
10 - "-2": "Media",
11 - "-1": "Special",
12 - "0": "",
13 - "1": "Talk",
14 - "2": "User",
15 - "3": "User talk",
16 - "4": "Wikipedia",
17 - "5": "Wikipedia talk",
18 - "6": "File",
19 - "7": "File talk",
20 - "8": "MediaWiki",
21 - "9": "MediaWiki talk",
22 - "10": "Template",
23 - "11": "Template talk",
24 - "12": "Help",
25 - "13": "Help talk",
26 - "14": "Category",
27 - "15": "Category talk",
28 - /* testing custom / localized */
29 - "100": "Penguins"
30 - },
31 - "wgNamespaceIds": {
32 - "media": -2,
33 - "special": -1,
34 - "": 0,
35 - "talk": 1,
36 - "user": 2,
37 - "user_talk": 3,
38 - "wikipedia": 4,
39 - "wikipedia_talk": 5,
40 - "file": 6,
41 - "file_talk": 7,
42 - "mediawiki": 8,
43 - "mediawiki_talk": 9,
44 - "template": 10,
45 - "template_talk": 11,
46 - "help": 12,
47 - "help_talk": 13,
48 - "category": 14,
49 - "category_talk": 15,
50 - "image": 6,
51 - "image_talk": 7,
52 - "project": 4,
53 - "project_talk": 5,
54 - /* testing custom / alias */
55 - "penguins": 100,
56 - "antarctic_waterfowl": 100
57 - },
58 - "wgCaseSensitiveNamespaces": []
59 - });
60 -};
61 -
62 -test( '-- Initial check', function() {
63 - expect(1);
64 - ok( mw.Title, 'mw.Title defined' );
65 -});
66 -
67 -test( 'Filename', function() {
68 - expect(4);
69 - _titleConfig();
70 -
71 - var title = new mw.Title( 'File:foo_bar.JPG' );
72 -
73 - equal( title.getMain(), 'Foo_bar.jpg' );
74 - equal( title.getMainText(), 'Foo bar.jpg' );
75 - equal( title.getNameText(), 'Foo bar' );
76 - equal( title.toString(), 'File:Foo_bar.jpg' );
77 -});
78 -
79 -test( 'Transform between Text to Db', function() {
80 - expect(6);
81 - _titleConfig();
82 -
83 - var title = new mw.Title( 'File:foo_bar.JPG' );
84 - title.setName( 'quux pif' );
85 -
86 - equal( title.getMain(), 'Quux_pif.jpg' );
87 - equal( title.getMainText(), 'Quux pif.jpg' );
88 - equal( title.getNameText(), 'Quux pif' );
89 - equal( title.toString(), 'File:Quux_pif.jpg' );
90 -
91 - title.setName( 'glarg_foo_glang' );
92 -
93 - equal( title.toString(), 'File:Glarg_foo_glang.jpg' );
94 - equal( title.getMainText(), 'Glarg foo glang.jpg' );
95 -});
96 -
97 -test( 'Initiate from name and set namespace', function() {
98 - expect(1);
99 - _titleConfig();
100 -
101 - var title = new mw.Title( 'catalonian_penguins.PNG' );
102 - title.setNamespace( 'file' );
103 - equal( title.toString(), 'File:Catalonian_penguins.png' );
104 -});
105 -
106 -test( 'Namespace detection and conversion', function() {
107 - expect(7);
108 - _titleConfig();
109 -
110 - var title;
111 -
112 - title = new mw.Title( 'something.PDF' );
113 - title.setNamespace( 'file' );
114 - equal( title.toString(), 'File:Something.pdf' );
115 -
116 - title = new mw.Title( 'NeilK' );
117 - title.setNamespace( 'user_talk' );
118 - equal( title.toString(), 'User_talk:NeilK' );
119 - equal( title.toText(), 'User talk:NeilK' );
120 -
121 - title = new mw.Title( 'Frobisher' );
122 - title.setNamespaceById( 100 );
123 - equal( title.toString(), 'Penguins:Frobisher' );
124 -
125 - title = new mw.Title( 'flightless_yet_cute.jpg' );
126 - title.setNamespace( 'antarctic_waterfowl' );
127 - equal( title.toString(), 'Penguins:Flightless_yet_cute.jpg' );
128 -
129 - title = new mw.Title( 'flightless_yet_cute.jpg' );
130 - title.setNamespace( 'Penguins' );
131 - equal( title.toString(), 'Penguins:Flightless_yet_cute.jpg' );
132 -
133 - title = new mw.Title( 'flightless_yet_cute.jpg' );
134 - raises( function() {
135 - title.setNamespace( 'Entirely Unknown' );
136 - });
137 -});
Index: branches/REL1_18/phase3/resources/Resources.php
@@ -448,9 +448,6 @@
449449 'mediawiki.htmlform' => array(
450450 'scripts' => 'resources/mediawiki/mediawiki.htmlform.js',
451451 ),
452 - 'mediawiki.Title' => array(
453 - 'scripts' => 'resources/mediawiki/mediawiki.Title.js',
454 - ),
455452 'mediawiki.user' => array(
456453 'scripts' => 'resources/mediawiki/mediawiki.user.js',
457454 'dependencies' => array(
Index: branches/REL1_18/phase3/resources/mediawiki/mediawiki.Title.js
@@ -1,316 +0,0 @@
2 -/**
3 - * mediaWiki.Title
4 - *
5 - * @author Neil Kandalgaonkar, 2010
6 - * @author Timo Tijhof, 2011
7 - * @since 1.18
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);

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r90331Implement mw.Title in core...krinkle09:17, 18 June 2011

Status & tagging log