r80667 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r80666‎ | r80667 | r80668 >
Date:00:17, 21 January 2011
Author:krinkle
Status:deferred
Tags:
Comment:
Nicing up some loose ends + Storing title instead of fullpagename (BREAKING CHANGE)
* Using WP_PLUGIN_URL to make sure it works with subsubdirectory or root installations as well (no longer depend on the folder being named 'wordpress' and in the root)
* Using Special:FilePath (r80381)
* BREAKING CHANGE: Storing filetitle instead of fullpagename (ie. without 'File:' prefix)
* Typo in label for attribute corrected (Follow-up r80390)
* Using jQuery 1.4's new callback function in val() setter. Get and set in one call.
* Removing linebreak after shortcode (WordPress made a paragraph because of this which caused there to be be an empty line between top of image and top of text (if image was floated to left or right)
Modified paths:
  • /trunk/tools/wp-photocommons/inc/class-photocommons.php (modified) (history)
  • /trunk/tools/wp-photocommons/js/admin.js (modified) (history)
  • /trunk/tools/wp-photocommons/js/search.js (modified) (history)

Diff [purge]

Index: trunk/tools/wp-photocommons/inc/class-photocommons.php
@@ -1,11 +1,11 @@
22 <?php
3 -class Photocommons {
4 - // TODO: ugly
5 - const PLUGIN_PATH = "/wp-content/plugins/wp-photocommons/";
6 - const RESIZE_URL = "http://commons.wikimedia.org/w/api.php?action=query&titles=%s&prop=imageinfo&iiprop=url&iiurlwidth=%s&format=php";
 3+class PhotoCommons {
 4+ const PLUGIN_PATH = '/wp-photocommons/';
 5+ const FILEPATH_PATTERN = 'http://commons.wikimedia.org/w/index.php?title=Special:FilePath&file=%s&width=%s';
 6+ const FILEPAGE_PATTERN = 'http://commons.wikimedia.org/w/index.php?title=File:%s';
77
88 function __construct() {
9 - if (is_admin()) {
 9+ if ( is_admin() ) {
1010 $this->initAdmin();
1111 } else {
1212 $this->initFrontend();
@@ -14,39 +14,26 @@
1515
1616 public function addShortcode($args) {
1717 $filename = $args['file'];
18 - $size = $args['size'];
 18+ $width = $args['width'];
1919 $align = empty($args['align']) ? 'alignright' : 'align' . $args['align'];
20 - $d = $this->doApiThumbResizeRequest($filename, $size);
 20+ $thumb = $this->getThumbUrl($filename, $width);
 21+ $filepage = $this->getPageUrl($filename, $width);
2122
2223 return sprintf(
23 - '<a href="%s" title="%s">' .
24 - '<img src="%s" title="%s" alt="%s" class="%s" width="%s" height="%s" />' .
 24+ '<a href="%s" title="%s" class="wp-photocommons-thumb">' .
 25+ '<img src="%s" title="%s via Wikimedia Commons" alt="%s" class="%s" width="%s" />' .
2526 '</a>',
26 - $d['url'], $d['title'], $d['src'], $d['title'], $d['title'],
27 - $align,
28 - $d['width'], $d['height']
 27+ $filepage, $filename,
 28+ $thumb, $filename, $filename, $align, $width
2929 );
3030 }
3131
32 - private function doApiThumbResizeRequest($filename, $size) {
33 - ini_set('user_agent', 'Photocommons/1.0');
34 - $url = $this->getResizeUrl($filename, $size);
35 - $result = unserialize(file_get_contents($url));
36 - $data = array_pop($result['query']['pages']);
37 -
38 - $image = $data['imageinfo'][0];
39 -
40 - return array(
41 - "url" => $image['descriptionurl'],
42 - "src" => $image['thumburl'],
43 - "width" => $image['thumbwidth'],
44 - "height" => $image['thumbheight'],
45 - "title" => $data['title']
46 - );
 32+ private function getThumbUrl($file, $width) {
 33+ return sprintf(self::FILEPATH_PATTERN, rawurlencode($file), rawurlencode($width));
4734 }
4835
49 - private function getResizeUrl($file, $size) {
50 - return sprintf(self::RESIZE_URL, rawurlencode($file), rawurlencode($size));
 36+ private function getPageUrl($file, $width) {
 37+ return sprintf(self::FILEPAGE_PATTERN, rawurlencode($file));
5138 }
5239
5340 private function initAdmin() {
@@ -56,9 +43,9 @@
5744
5845 private function enqueueScripts() {
5946 // Register some of our own scripts
60 - wp_register_script('admin', self::PLUGIN_PATH . 'js/admin.js');
61 - wp_register_script('search', self::PLUGIN_PATH . 'js/search.js');
62 - wp_register_script('suggestions', self::PLUGIN_PATH . 'js/jquery.suggestions.js');
 47+ wp_register_script('admin', plugins_url() . self::PLUGIN_PATH . 'js/admin.js');
 48+ wp_register_script('search', plugins_url() . self::PLUGIN_PATH . 'js/search.js');
 49+ wp_register_script('suggestions', plugins_url() . self::PLUGIN_PATH . 'js/jquery.suggestions.js');
6350
6451 // Enqueue external libraries
6552 wp_enqueue_script('jquery');
@@ -74,8 +61,8 @@
7562 private function enqueueStyles() {
7663 // Register our own styles and enqueue
7764 wp_register_style('jquid_jquery_blog_stylesheet', 'http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.6/themes/redmond/jquery-ui.css');
78 - wp_register_style('suggestions', self::PLUGIN_PATH . "css/jquery.suggestions.css");
79 - wp_register_style('search', self::PLUGIN_PATH . "css/search.css");
 65+ wp_register_style('suggestions', plugins_url() . self::PLUGIN_PATH . 'css/jquery.suggestions.css');
 66+ wp_register_style('search', plugins_url() . self::PLUGIN_PATH . 'css/search.css');
8067
8168 wp_enqueue_style('jquid_jquery_blog_stylesheet');
8269 wp_enqueue_style('suggestions');
@@ -84,6 +71,6 @@
8572 }
8673
8774 private function initFrontend() {
88 - add_shortcode("photocommons", array($this, "addShortcode"));
 75+ add_shortcode('photocommons', array($this, 'addShortcode'));
8976 }
9077 }
\ No newline at end of file
Index: trunk/tools/wp-photocommons/js/search.js
@@ -1,20 +1,15 @@
2 -// usage: log('inside coolFunc',this,arguments);
3 -// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
4 -window.log = function(){
5 - log.history = log.history || []; // store logs to an array for reference
6 - log.history.push(arguments);
7 - if(this.console){
8 - console.log( Array.prototype.slice.call(arguments) );
9 - }
 2+// Debug
 3+window.log = function( a, b ) {
 4+ //console.log( a, b );
105 };
116
12 -if ( !window.Photocommons ) {
13 - window.Photocommons = {};
 7+if ( !window.PhotoCommons ) {
 8+ window.PhotoCommons = {};
149 }
1510
1611 (function($){
1712
18 - $.extend( Photocommons, {
 13+ $.extend( PhotoCommons, {
1914
2015 getQueryUrl: function( type, args ) {
2116 var queries = {
@@ -54,7 +49,7 @@
5550 throw new Error( 'Unknown query type' );
5651 }
5752
58 - return Photocommons.makeUrl(queries[type](args));
 53+ return PhotoCommons.makeUrl(queries[type](args));
5954 },
6055
6156 makeUrl: function( args ) {
@@ -90,7 +85,7 @@
9186 /* jQuery suggestions */
9287 $( '#wp-photocommons-search' ).suggestions( {
9388 fetch: function( query ) {
94 - var url = Photocommons.getQueryUrl( 'pagesearch', {
 89+ var url = PhotoCommons.getQueryUrl( 'pagesearch', {
9590 'search' : query
9691 });
9792 $.getJSON( url, function( data ) {
@@ -104,7 +99,7 @@
105100 result: {
106101 select: function( $result ) {
107102 var value = $result.val(),
108 - url = Photocommons.getQueryUrl( 'pageimages', {
 103+ url = PhotoCommons.getQueryUrl( 'pageimages', {
109104 'title' : value,
110105 'width' : '200'
111106 });
@@ -117,11 +112,15 @@
118113 $( '#wp-photocommons-images' ).html( 'No images found :(' );
119114 } else {
120115 $.each( data.query.pageids, function( key, pageid ) {
121 - var img = data.query.pages[pageid];
 116+ var img = data.query.pages[pageid],
 117+ pagetitle;
122118 if ( img.imageinfo && img.imageinfo[0] ) {
 119+ pagetitle = img.title.split(':');
 120+ pagetitle.shift();
 121+ pagetitle = pagetitle.join(':');
123122 $('<div class="image">').attr({
124123 'style': "background-image:url('" + img.imageinfo[0].thumburl + "');",
125 - 'data-filename': img.title
 124+ 'data-filename': pagetitle
126125 }).appendTo('#wp-photocommons-images');
127126
128127 }
@@ -140,6 +139,6 @@
141140
142141 // Init
143142 // FIXME
144 - $( document ).ready( Photocommons.init );
 143+ $( document ).ready( PhotoCommons.init );
145144
146145 })(jQuery);
\ No newline at end of file
Index: trunk/tools/wp-photocommons/js/admin.js
@@ -1,38 +1,39 @@
22 (function($) {
3 - var PATH = "../wp-content/plugins/wp-photocommons";
 3+ var PATH = '../wp-content/plugins/wp-photocommons';
44
55 function addButtons() {
6 - $("#media-buttons").append(''.concat(
 6+ $('#media-buttons').append(''.concat(
77 '<a id="photocommons-add" title="Afbeeldingen invoegen van Wikimedia Commons" style="padding-left:4px;">',
88 '<img src="' + PATH + '/img/button.png"/>',
99 '</a>'
1010 ));
1111
12 - $("#photocommons-add").live('click', function(e) {
 12+ $('#photocommons-add').live('click', function(e) {
1313 e.preventDefault();
1414
15 - $("body").prepend('<div id="photocommons-dialog"></div>');
16 - $("#photocommons-dialog").load(PATH + "/search.php?standalone=1", function() {
17 - Photocommons.init();
 15+ $('body').prepend('<div id="photocommons-dialog"></div>');
 16+ $('#photocommons-dialog').load(PATH + '/search.php?standalone=1', function() {
 17+ PhotoCommons.init();
1818
19 - var $self = $("#photocommons-dialog");
 19+ var $self = $('#photocommons-dialog');
2020
2121 $self.dialog({
22 - title : 'Photocommons - Afbeelingen invoegen van Wikimedia Commons',
 22+ title : 'PhotoCommons - Afbeelingen invoegen van Wikimedia Commons',
2323 width : 800,
2424 height : 500
2525 });
2626
27 - $("#wp-photocommons-images .image").live('click', function() {
 27+ $('#wp-photocommons-images .image').live('click', function() {
2828 var file = $(this).attr('data-filename'),
29 - shortcode = '[photocommons file="' + file + '" size="300"]' + "\n";
 29+ shortcode = '[photocommons file="' + file + '" width="300"] ';
3030
3131 // Depending on whether we are in Wysiwyg or HTML mode we
3232 // do a different insert
33 - if ($("#edButtonHTML").hasClass('active')) {
 33+ if ($('#edButtonHTML').hasClass('active')) {
3434 // HTML editor
35 - cnt = $("#content").val();
36 - $("#content").val( + cnt);
 35+ $('#content').val( function(i,val){
 36+ return shortcode + val;
 37+ });
3738 } else {
3839 // Wysiwyg
3940 tinyMCE.execCommand('mceInsertContent', false, shortcode);

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r80381Add width parameter to Special:Filepath to allow getting the file path of a t...catrope11:20, 15 January 2011
r80390TAG - last changes before presentationhusky12:30, 15 January 2011

Status & tagging log