r69297 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r69296‎ | r69297 | r69298 >
Date:22:45, 12 July 2010
Author:neilk
Status:deferred
Tags:
Comment:
copying features from ajaxcategories, very basic category picker. Fixed some issues of how we choose URL namespaces
Modified paths:
  • /trunk/extensions/UploadWizard/SpecialUploadWizard.php (modified) (history)
  • /trunk/extensions/UploadWizard/UploadWizard.i18n.php (modified) (history)
  • /trunk/extensions/UploadWizard/UploadWizardPage.js (modified) (history)
  • /trunk/extensions/UploadWizard/js/jquery/plugins/jquery.mwCoolCats.js (modified) (history)
  • /trunk/extensions/UploadWizard/js/mw.UploadWizard.js (modified) (history)

Diff [purge]

Index: trunk/extensions/UploadWizard/UploadWizard.i18n.php
@@ -149,7 +149,9 @@
150150 'mwe-upwiz-license-gfdl' => 'GNU Free Documentation License',
151151 /* this is just for testing, we are not going to privilege the US and A */
152152 'mwe-upwiz-license-pd-us' => 'Public Domain (US)',
153 - 'mwe-upwiz-categories' => 'Categories'
 153+ 'mwe-upwiz-categories' => 'Categories',
 154+ 'mwe-upwiz-categories-add' => 'Add',
 155+ 'mwe-upwiz-category-remove' => 'Remove this category'
154156 );
155157
156158 /** Message documentation (Message documentation)
Index: trunk/extensions/UploadWizard/SpecialUploadWizard.php
@@ -94,7 +94,7 @@
9595 public function addJS() {
9696 global $wgUser, $wgOut;
9797 global $wgUseAjax, $wgAjaxLicensePreview, $wgEnableAPI;
98 - global $wgEnableFirefogg, $wgFileExtensions, $wgCanonicalNamespaceNames;
 98+ global $wgEnableFirefogg, $wgFileExtensions
9999
100100 $wgOut->addScript( Skin::makeVariablesScript( array(
101101 // uncertain if this is relevant. Can we do license preview with API?
@@ -108,9 +108,6 @@
109109 // our edit token
110110 'wgEditToken' => $wgUser->editToken(),
111111
112 - // URL prefixes in this MediaWiki, e.g. images under Image:
113 - 'wgCanonicalNamespaceNames' => $wgCanonicalNamespaceNames
114 -
115112 // 'wgFilenamePrefixBlacklist' => UploadBase::getFilenamePrefixBlacklist();
116113
117114
Index: trunk/extensions/UploadWizard/js/jquery/plugins/jquery.mwCoolCats.js
@@ -14,115 +14,117 @@
1515 })();
1616 (function($){
1717 $.fn.mwCoolCats = function(options) {
18 - var defaults = {
19 - showform: false,
20 - formlabel: 'Add cat(s):',
21 - buttontext: 'Add',
22 - listlabel: 'Cats:',
23 - deletetext: 'x',
24 - delimitter: ',',
25 - linkformat: '',
26 - //submitstyle: 'string',
27 - /* Events and Callbacks */
28 - addcat: function(e){ },
29 - removecat: function(e){ }
30 - };
31 - var catList = Array();
32 - var self;
33 - var orig;
34 - var origname;
 18+ var defaults = {
 19+ buttontext: 'Add'
 20+ };
3521 var settings = $.extend( {}, defaults, options);
36 - return this.each( function() {
37 - orig = $(this);
38 - $(this).wrap('<div class="cat-widget"></div>');
39 - $(this).parent()
40 - .append( $j( '<button type="button">'+settings.buttontext+'</button>' )
41 - .click( function(e) {
42 - e.stopPropagation();
43 - e.preventDefault();
44 - _processInput();
45 - return false;
46 - }) );
47 - $(this).parent().append('<ul class="cat-list pkg"></ul>');
48 - $(this).parent().append( $(this).clone().attr('type','hidden').removeAttr('class').val('') );
 22+ var $container;
 23+ return this.each( function() {
 24+ var _this = $j( this );
4925
 26+ _this.suggestions( {
 27+ 'fetch': _fetchSuggestions,
 28+ 'cancel': function() {
 29+ var req = $j( this ).data( 'request' );
 30+ if ( req.abort ) {
 31+ req.abort();
 32+ }
 33+ }
 34+ } );
 35+ _this.suggestions();
 36+
 37+ _this.wrap('<div class="cat-widget"></div>');
 38+ $container = _this.parent(); // set to the cat-widget class we just wrapped
 39+ $container.append( $j( '<button type="button">'+settings.buttontext+'</button>' )
 40+ .click( function(e) {
 41+ e.stopPropagation();
 42+ e.preventDefault();
 43+ _processInput();
 44+ return false;
 45+ }) );
 46+ $container.prepend('<ul class="cat-list pkg"></ul>');
 47+
5048 //XXX ensure this isn't blocking other stuff needed.
51 - $(this).parents('form').submit( function() {
 49+ _this.parents('form').submit( function() {
5250 _processInput();
5351 });
54 - $(this).parents('form').keydown( function(e) {
 52+
 53+ _this.keyup(function(e) {
5554 if(e.keyCode == 13) {
56 - e.preventDefault();
57 - return false;
58 - }
59 - });
60 -
61 - origname = $(this).attr('name');
62 - $(this).removeAttr('name');
63 - $(this).keyup(function(e) {
64 - if(e.keyCode == 13) {
6555 e.stopPropagation();
6656 e.preventDefault();
6757 _processInput();
6858 }
6959 });
70 - obj = $(this).parent(); // set to the cat-widget class we just wrapped
71 - self = obj;
 60+
 61+ var categoryNamespace = mw.getConfig( 'categoryNamespace' );
 62+ this.getWikiText = function() {
 63+ return _getCats().map( function() { return '[[' + categoryNamespace + ':' + this + ']]'; } )
 64+ .toArray()
 65+ .join( "\n" );
 66+ };
 67+
7268 _processInput();
73 - });
74 - function _split( str ) {
75 - var delim = RegExp.escape(settings.delimitter);
76 - var delim_scan = new RegExp('^((([\'"])(.*?)\\3|.*?)(' + delim + '\\s*|$))', '');
77 - str = str.replace(/(^\s+|\s+$)/g, '');
78 - var cats = [];
79 - while (str.length && str.match(delim_scan)) {
80 - str = str.substr(RegExp.$1.length);
81 - orig.val(str);
82 - var cat = RegExp.$4 ? RegExp.$4 : RegExp.$2;
83 - cat = cat.replace(/(^\s+|\s+$)/g, '');
84 - cat = cat.replace(/\s+/g, ' ');
85 - if (cat != '') cats.push(cat);
86 - }
87 - return cats;
88 - };
 69+ });
 70+
8971 function _processInput() {
90 - var newcats = _split( self.find('input').val() );
91 - for(i = 0; i < newcats.length; i++) {
92 - _insertCat( newcats[i] );
93 - };
94 - self.find('ul li a.delete').click( function() {
95 - var cat = $(this).parent().find('a.cat').html();
96 - $(this).parent().remove();
97 - _removeCat( cat );
98 - });
99 - };
100 - function _removeCat( cat ) {
101 - var str = '';
102 - for (var i=0;i < catList.length;i++) {
103 - if (catList[i] == cat) {
104 - catList.splice(i,1);
105 - i--;
106 - } else {
107 - if (str != '') { str = str + settings.delimitter; }
108 - str = str + catList[i];
109 - }
110 - }
111 - self.find('input[name='+origname+']').val( str );
 72+ var $input = $container.find('input');
 73+ _insertCat( $input.val().trim() );
 74+ $input.val("");
 75+ }
11276
113 - };
11477 function _insertCat( cat ) {
115 - if ( _containsCat( cat ) ) { return; }
116 - var cat_esc = escape(cat).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/ /g,'+');
117 - self.find('ul').append('<li class="cat"><a class="cat" href="'+settings.linkformat + cat_esc+'">'+cat+'</a> <a href="javascript:void(0)" class="delete">'+settings.deletetext+'</a></li>');
118 - catList.push(cat);
119 - var v = self.find('input[name='+origname+']').val();
120 - if (v != '') { v = v + settings.delimitter; }
121 - self.find('input[name='+origname+']').val( v + cat );
122 - };
 78+ if ( mw.isEmpty( cat ) || _containsCat( cat ) ) {
 79+ return;
 80+ }
 81+ var href = _catLink( cat );
 82+ var $li = $j( '<li class="cat"></li>' );
 83+ $container.find('ul').append( $li );
 84+ $li.append( '<a class="cat" href="'+href+'">'+cat+'</a>' );
 85+ $li.append( $j.fn.removeCtrl( 'mwe-upwiz-category-remove', function() { $li.remove(); } ) );
 86+ }
 87+
 88+ function _catLink( cat ) {
 89+ return "XXX FIX LATER"; // XXX dummy
 90+ // var cat_esc = escape(cat).replace(/\+/g, '%2B').replace(/\"/g,'%22').replace(/\'/g, '%27').replace(/ /g,'+');
 91+ }
 92+
 93+ function _getCats() {
 94+ return $container.find('ul li a.cat').map( function() { return this.text; } );
 95+ }
 96+
12397 function _containsCat( cat ) {
124 - for (var i = 0; i < catList.length; i++) {
125 - if (cat == catList[i]) { return 1; }
126 - }
127 - return 0;
128 - };
 98+ return _getCats().filter( function() { return this == cat; } ).length !== 0;
 99+ }
 100+
 101+ function _fetchSuggestions( query ) {
 102+ var _this = this;
 103+ var request = $j.ajax( {
 104+ url: wgScriptPath + '/api.php',
 105+ data: {
 106+ 'action': 'query',
 107+ 'list': 'allpages',
 108+ 'apnamespace': wgNamespaceIds['category'],
 109+ 'apprefix': $j( this ).val(),
 110+ 'format': 'json'
 111+ },
 112+ dataType: 'json',
 113+ success: function( data ) {
 114+ // Process data.query.allpages into an array of titles
 115+ var pages = data.query.allpages;
 116+ var titleArr = [];
 117+
 118+ $j.each( pages, function( i, page ) {
 119+ var title = page.title.split( ':', 2 )[1];
 120+ titleArr.push( title );
 121+ } );
 122+
 123+ $j( _this ).suggestions( 'suggestions', titleArr );
 124+ }
 125+ } );
 126+
 127+ $j( _this ).data( 'request', request );
 128+ }
 129+
 130+
129131 }})(jQuery);
Index: trunk/extensions/UploadWizard/js/mw.UploadWizard.js
@@ -494,7 +494,7 @@
495495 var _this = this;
496496
497497 _this.filename = result.upload.filename;
498 - _this.title = mw.getConfig( 'fileNamespace' ) + ':' + _this.filename;
 498+ _this.title = wgFormattedNamespaces[wgNamespaceIds['file']] + ':' + _this.filename;
499499
500500 _this.extractImageInfo( result.upload.imageinfo );
501501
@@ -1239,11 +1239,11 @@
12401240 showAnim: 'slideDown',
12411241 altField: '#' + dateInputId,
12421242 altFormat: 'yy-mm-dd' } )
1243 - .click( function() { $j( this ).datepicker( 'show' ) } )
 1243+ .click( function() { $j( this ).datepicker( 'show' ); } )
12441244 .readonly();
12451245
12461246 _this.$form.find( '.mwe-date' )
1247 - .bind( 'change', function() { $j( this ).valid() } )
 1247+ .bind( 'change', function() { $j( this ).valid(); } )
12481248 .hide();
12491249
12501250 /* if the date is not valid, we need to pop open the "more options". How?
@@ -1254,8 +1254,10 @@
12551255 _this.addDescription( true, mw.getConfig('userLanguage') );
12561256 $j( containerDiv ).append( _this.div );
12571257
1258 - // make this a cool category picker
1259 - $categoriesDiv.find( '.mwe-upwiz-details-input' ).find( 'input' ).mwCoolCats();
 1258+ // make this a category picker
 1259+ $categoriesDiv.find( '.mwe-upwiz-details-input' )
 1260+ .find( 'input' )
 1261+ .mwCoolCats( { buttontext: gM( 'mwe-upwiz-categories-add' ) } );
12601262
12611263 };
12621264
@@ -1312,7 +1314,8 @@
13131315 */
13141316 setFilenameFromTitle: function() {
13151317 var _this = this;
1316 - _this.filename = mw.getConfig( 'fileNamespace' ) + ':' + _this.getFilenameFromTitle();
 1318+
 1319+ _this.filename = wgFormattedNamespaces[wgNamespaceIds['file']] + ':' + _this.getFilenameFromTitle();
13171320 $j( '#mwe-upwiz-details-filename' ).text( _this.filename );
13181321
13191322 },
@@ -2901,7 +2904,7 @@
29022905 var formValid = _this.$form.valid();
29032906 var licenseInputValid = _this.licenseInput.valid();
29042907 return formValid && licenseInputValid;
2905 - },
 2908+ }
29062909 } );
29072910 };
29082911
@@ -3159,7 +3162,7 @@
31603163 title = title.substr( 0, title.length - extension.length - 1 );
31613164 }
31623165 // usually File:
3163 - var namespace = mw.getConfig( 'fileNamespace' );
 3166+ var namespace = wgFormattedNamespaces[wgNamespaceIds['file']];
31643167 if ( title.indexOf( namespace + ':' ) === 0 ) {
31653168 title = title.substr( namespace.length + 1 );
31663169 }
Index: trunk/extensions/UploadWizard/UploadWizardPage.js
@@ -17,10 +17,6 @@
1818 var discussLink = discussListItem.getElementsByTagName( 'a' )[0];
1919 discussLink.setAttribute( 'target', 'usability_discussion' );
2020
21 - // The namespace for media files. We use this to look up what the URL prefix is on this MediaWiki, usually
22 - // 'Image:' or something like that. Defined in defines.php
23 - var NS_FILE = 6;
24 -
2521 mw.load( 'UploadWizard.UploadWizard', function () {
2622 mw.setConfig( 'debug', true );
2723
@@ -68,10 +64,6 @@
6965 { template: 'GFDL', messageKey: 'mwe-upwiz-license-gfdl', 'default': false }
7066 ],
7167
72 - // usually, but not always, File:
73 - fileNamespace: wgCanonicalNamespaceNames[NS_FILE],
74 -
75 -
7668 // XXX this is horribly confusing -- some file restrictions are client side, others are server side
7769 // the filename prefix blacklist is at least server side -- all this should be replaced with PHP regex config
7870 // or actually, in an ideal world, we'd have some way to reliably detect gibberish, rather than trying to

Status & tagging log