http://www.mediawiki.org/wiki/Special:Code/MediaWiki/88699
Revision: 88699 Author: janpaul123 Date: 2011-05-23 22:45:23 +0000 (Mon, 23 May 2011) Log Message: ----------- All kinds of fixes per https://secure.wikimedia.org/wikipedia/mediawiki/wiki/User:Catrope/Extension_review/WikiLove and myself Modified Paths: -------------- trunk/extensions/WikiLove/WikiLove.api.php trunk/extensions/WikiLove/WikiLove.i18n.php trunk/extensions/WikiLove/WikiLove.php trunk/extensions/WikiLove/defaultTypes.js trunk/extensions/WikiLove/patches/WikiLoveLog.sql trunk/extensions/WikiLove/wikiLove.js Modified: trunk/extensions/WikiLove/WikiLove.api.php =================================================================== --- trunk/extensions/WikiLove/WikiLove.api.php 2011-05-23 22:44:35 UTC (rev 88698) +++ trunk/extensions/WikiLove/WikiLove.api.php 2011-05-23 22:45:23 UTC (rev 88699) @@ -7,16 +7,16 @@ $title = Title::newFromText( $params['title'] ); if ( is_null( $title ) ) { - $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) ); + $this->dieUsageMsg( array( 'invaliduser', $params['title'] ) ); } $talk = WikiLoveHooks::getUserTalkPage( $title ); if ( is_null( $talk ) ) { - $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) ); + $this->dieUsageMsg( array( 'invaliduser', $params['title'] ) ); } if ( $wgWikiLoveLogging ) { - $this->saveInDb( $talk, $params['subject'], $params['text'], $params['type'], $params['template'] ); + $this->saveInDb( $talk, $params['subject'], $params['text'], $params['type'], isset( $params['email'] ) ? 1 : 0 ); } $api = new ApiMain( new FauxRequest( array( @@ -28,15 +28,19 @@ 'summary' => $params['subject'], 'notminor' => true, ), false, array( 'wsEditToken' => $wgRequest->getSessionData( 'wsEditToken' ) ) ), true ); + + if ( isset( $params['email'] ) ) { + $this->emailUser( $talk, $params['subject'], $params['email'] ); + } $api->execute(); $result = $api->getResult(); $data = $result->getData(); - $talk->setFragment( '#' . $params['subject'] ); $this->getResult()->addValue( 'redirect', 'pageName', $talk->getPrefixedDBkey() ); - $this->getResult()->addValue( 'redirect', 'fragment', $talk->getFragmentForURL() ); + $this->getResult()->addValue( 'redirect', 'fragment', Title::escapeFragmentForURL( $params['subject'] ) ); + // note that we cannot use Title::makeTitle here as it doesn't sanitize the fragment } /** @@ -46,25 +50,38 @@ * @param $type * @return void */ - private function saveInDb( $talk, $subject, $text, $type, $template ) { + private function saveInDb( $talk, $subject, $text, $type, $email ) { global $wgUser; $dbw = wfGetDB( DB_MASTER ); $values = array( - 'wl_timestamp' => $dbw->timestamp(), - 'wl_sender_id' => $wgUser->getId(), - 'wl_receiver_id' => User::newFromName( $talk->getSubjectPage()->getBaseText() )->getId(), - 'wl_type' => $type, - 'wl_subject' => $subject, - 'wl_message' => $text, - 'wl_email' => 0, + 'wll_timestamp' => $dbw->timestamp(), + 'wll_sender' => $wgUser->getId(), + 'wll_receiver' => User::newFromName( $talk->getSubjectPage()->getBaseText() )->getId(), + 'wll_type' => $type, + 'wll_subject' => $subject, + 'wll_message' => $text, + 'wll_email' => $email, ); try{ $dbw->insert( 'wikilove_log', $values, __METHOD__ ); } catch( DBQueryError $dbqe ) { - $this->dieUsage( 'Warning: action was not logged!', 'nologging' ); - return false; + $this->setWarning( 'Action was not logged' ); } } + + private function emailUser( $talk, $subject, $text ) { + $api = new ApiMain( new FauxRequest( array( + 'action' => 'emailuser', + 'target' => User::newFromName( $talk->getSubjectPage()->getBaseText() )->getName(), + 'subject' => $subject, + 'text' => $text, + ), false, array( 'wsEditToken' => $wgRequest->getSessionData( 'wsEditToken' ) ) ), true ); + try{ + $api->execute(); + } catch( DBQueryError $dbqe ) { + $this->setWarning( 'E-mail was not sent' ); + } + } public function getAllowedParams() { return array( @@ -87,6 +104,9 @@ 'type' => array( ApiBase::PARAM_TYPE => 'string', ), + 'email' => array( + ApiBase::PARAM_TYPE => 'string', + ), ); } @@ -125,7 +145,7 @@ public function getExamples() { return array( - 'api.php?action=wikiLove&title=User:Dummy&text=Love&subject=Hi&token=%2B\\', + 'api.php?action=wikilove&title=User:Dummy&text=Love&subject=Hi&token=%2B\\', ); } Modified: trunk/extensions/WikiLove/WikiLove.i18n.php =================================================================== --- trunk/extensions/WikiLove/WikiLove.i18n.php 2011-05-23 22:44:35 UTC (rev 88698) +++ trunk/extensions/WikiLove/WikiLove.i18n.php 2011-05-23 22:45:23 UTC (rev 88699) @@ -31,6 +31,7 @@ 'wikilove-omit-sig' => '(without a signature)', 'wikilove-button-preview' => 'Preview', 'wikilove-preview' => 'Preview', + 'wikilove-notify' => 'Notify the user by e-mail', 'wikilove-button-send' => 'Send WikiLove', 'wikilove-type-makeyourown' => 'Make your own', 'wikilove-err-header' => 'Please enter a header.', @@ -38,9 +39,6 @@ 'wikilove-err-msg' => 'Please enter a message.', 'wikilove-err-image' => 'Please select an image.', 'wikilove-err-sig' => 'Please do not include a signature in the message.', - - // messages for default types - 'wikilove-barnstar-header' => 'A barnstar for you!', ); /** Message documentation (Message documentation) Modified: trunk/extensions/WikiLove/WikiLove.php =================================================================== --- trunk/extensions/WikiLove/WikiLove.php 2011-05-23 22:44:35 UTC (rev 88698) +++ trunk/extensions/WikiLove/WikiLove.php 2011-05-23 22:45:23 UTC (rev 88699) @@ -45,6 +45,11 @@ 'descriptionmsg' => 'wikilove-desc', ); +// default user options +$wgWikiLoveGlobal = false; // enable the extension for all users, removing the user preference +$wgWikiLoveTabIcon = true; // use an icon for skins that support them (i.e. Vector) +$wgWikiLoveLogging = false; // enable logging of giving of WikiLove + // current directory including trailing slash $dir = dirname( __FILE__ ) . '/'; @@ -60,17 +65,11 @@ $wgHooks['SkinTemplateNavigation'][] = 'WikiLoveHooks::skinTemplateNavigation'; $wgHooks['SkinTemplateTabs'][] = 'WikiLoveHooks::skinTemplateTabs'; $wgHooks['BeforePageDisplay'][] = 'WikiLoveHooks::beforePageDisplay'; -//$wgHooks['LoadExtensionSchemaUpdates'][] = 'WikiLoveHooks::loadExtensionSchemaUpdates'; -// Not a final schema, please apply patches/WikiLoveLog.sql manually for now! +$wgHooks['LoadExtensionSchemaUpdates'][] = 'WikiLoveHooks::loadExtensionSchemaUpdates'; // api modules -$wgAPIModules['wikiLove'] = 'WikiLoveApi'; +$wgAPIModules['wikilove'] = 'WikiLoveApi'; -// default user options -$wgWikiLoveGlobal = false; -$wgWikiLoveTabIcon = true; -$wgWikiLoveLogging = false; - // resources $wikiLoveTpl = array( 'localBasePath' => dirname( __FILE__ ), @@ -100,6 +99,7 @@ 'wikilove-omit-sig', 'wikilove-button-preview', 'wikilove-preview', + 'wikilove-notify', 'wikilove-button-send', 'wikilove-type-makeyourown', 'wikilove-err-header', @@ -113,6 +113,7 @@ 'jquery.ui.dialog', 'jquery.ui.button', 'jquery.elastic', + 'jquery.localize', ), ), 'jquery.elastic' => $wikiLoveTpl + array( Modified: trunk/extensions/WikiLove/defaultTypes.js =================================================================== --- trunk/extensions/WikiLove/defaultTypes.js 2011-05-23 22:44:35 UTC (rev 88698) +++ trunk/extensions/WikiLove/defaultTypes.js 2011-05-23 22:45:23 UTC (rev 88699) @@ -2,44 +2,45 @@ // example type, could be removed later (also no i18n) 'barnstar': { name: 'Barnstar', // name of the type (appears in the types menu) - fields: [ 'header', 'title', 'image' ], // fields to ask for in form if not specified in config select: 'Select a barnstar:', // subtype select label subtypes: { // some different subtypes // note that when not using subtypes you should use these subtype options // for the top-level type 'original': { + fields: [ 'notify' ], // fields to ask for in form option: 'Original barnstar', // option listed in the select list descr: 'This barnstar is given to recognize particularly fine contributions to Wikipedia, to let people know that their hard work is seen and appreciated.', // description - header: mw.msg( 'wikilove-barnstar-header' ), // header that appears at the top of the talk page post (optional) + header: 'A barnstar for you!', // header that appears at the top of the talk page post (optional) title: 'The Original Barnstar', // title that appears inside the award box (optional) image: 'Original Barnstar Hires.png', // image for the award mail: 'Hello $7!\n\nI just awarded you a barnstar.' // message to use in email notification; $7 is replaced by the recipient's username }, 'editors': { + fields: [ 'notify' ], option: 'Editor\'s barnstar', descr: 'The Editor\'s Barnstar is awarded to individuals who display particularly fine decisions in general editing.', - header: mw.msg( 'wikilove-barnstar-header' ), + header: 'A barnstar for you!', title: 'The Editor\'s Barnstar', image: 'Editors Barnstar Hires.png', mail: 'Hello $7!\n\nI just awarded you a barnstar.' }, 'tireless': { + fields: [ 'title', 'notify' ], option: 'Tireless contributor barnstar', descr: 'The Tireless Contributor Barnstar is awarded to especially tireless Wikipedians who contribute an especially large body of work without sacrificing quality.', - header: mw.msg( 'wikilove-barnstar-header' ), - title: 'The Editor\'s Barnstar', + header: 'A barnstar for you!', + title: 'Tireless contributor barnstar', image: 'Tireless Contributor Barnstar Hires.gif', mail: 'Hello $7!\n\nI just awarded you a barnstar.' } }, - showNotify: true, // add email notices as an option for each award of this type icon: mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/extensions/WikiLove/images/icons/wikilove-icon-barnstar.png' // icon for left-side menu }, 'cats': { name: 'Cat', fields: [ 'header' ], header: 'A kitten for you!', - text: '[[$3|left|150px]]\n$1\n\n~~~~\n<br style="clear: both"/>', // $3 is the image filename + text: '[[$3|left|150px]]\n$1\n\n~~~~\n<br style="clear: both"/>', // custom text gallery: { // right now we can only query the local wiki (not e.g. commons) category: 'Category:Cats', @@ -51,7 +52,7 @@ // default type, nice to leave this one in place when adding other types 'makeyourown': { name: mw.msg( 'wikilove-type-makeyourown' ), - fields: [ 'header', 'title', 'image' ], + fields: [ 'header', 'title', 'image', 'notify' ], imageSize: '150px' } } \ No newline at end of file Modified: trunk/extensions/WikiLove/patches/WikiLoveLog.sql =================================================================== --- trunk/extensions/WikiLove/patches/WikiLoveLog.sql 2011-05-23 22:44:35 UTC (rev 88698) +++ trunk/extensions/WikiLove/patches/WikiLoveLog.sql 2011-05-23 22:45:23 UTC (rev 88699) @@ -1,15 +1,24 @@ -- -- WikiLove logging schema +-- Logs all actions of giving WikiLove. -- Not final, please apply this patch manually for now! -- CREATE TABLE IF NOT EXISTS /*_*/wikilove_log ( - `wl_id` int NOT NULL PRIMARY KEY auto_increment, - `wl_timestamp` binary(14) NOT NULL, - `wl_sender_id` int(11) NOT NULL, - `wl_receiver_id` int(11) NOT NULL, - `wl_type` varchar(64) NOT NULL, - `wl_subject` varchar(255) NOT NULL, - `wl_message` blob NOT NULL, - `wl_email` bool NOT NULL default '0' + wll_id int NOT NULL PRIMARY KEY auto_increment, -- unique id + wll_timestamp binary(14) NOT NULL, -- timestamp + wll_sender int(11) NOT NULL, -- user id of the sender + wll_receiver int(11) NOT NULL, -- user id of the receiver + wll_type varchar(64) NOT NULL, -- type (and subtype) of message + wll_subject varchar(255) NOT NULL, -- subject line + wll_message blob NOT NULL, -- actual message + wll_email bool NOT NULL default '0' -- whether or not a notification mail has been sent ) /*$wgDBTableOptions*/; + +CREATE INDEX /*i*/wll_timestamp ON /*_*/wikilove_log (wll_timestamp); +CREATE INDEX /*i*/wll_type_time ON /*_*/wikilove_log (wll_type, wll_timestamp); +CREATE INDEX /*i*/wll_sender_time ON /*_*/wikilove_log (wll_sender, wll_timestamp); +CREATE INDEX /*i*/wll_receiver_time ON /*_*/wikilove_log (wll_receiver, wll_timestamp); +-- ASSUMPTION: once narrowed down to a single user (sender/receiver), we can afford a filesort +-- as a single users will have only limited WikiLove messages from or to him/her. It's not worth +-- the memory of extra indexes to cover all the combinations (sender/receiver/type => 8 indexes) Modified: trunk/extensions/WikiLove/wikiLove.js =================================================================== --- trunk/extensions/WikiLove/wikiLove.js 2011-05-23 22:44:35 UTC (rev 88698) +++ trunk/extensions/WikiLove/wikiLove.js 2011-05-23 22:45:23 UTC (rev 88699) @@ -1,3 +1,4 @@ +var mw; ( function( $ ) { $.wikiLove = { $dialog: null, // dialog jQuery object @@ -24,112 +25,116 @@ if ( $.wikiLove.$dialog === null ) { // Load local configuration var wikiLoveConfigUrl = wgServer + wgScript + '?' + $.param( { 'title': 'MediaWiki:WikiLove.js', 'action': 'raw', 'ctype': 'text/javascript' } ); - mw.loader.load( wikiLoveConfigUrl ); - - // Find out if we can email the user - $.wikiLove.getEmailable(); - - // Reusable spinner string - var spinner = '<img class="wlSpinner" src="' + mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) - + '/extensions/WikiLove/images/spinner.gif"/>'; - - // Build a type list like this: - var $typeList = $( '<ul id="wlTypes"></ul>' ); - for( var typeId in $.wikiLove.types ) { - $button = $( '<a href="#"></a>' ); - $buttonInside = $( '<div class="wlInside"></div>' ); + $.getScript( wikiLoveConfigUrl, function() { + // Test to see if the 'E-mail this user' link exists + $.wikiLove.emailable = $( '#t-emailuser' ).length ? true : false; - if( typeof $.wikiLove.types[typeId].icon == 'string' ) { - $buttonInside.append( '<div class="wlIconBox"><img src="' - + $.wikiLove.types[typeId].icon + '"/></div>' ); + // Reusable spinner string + var spinner = '<img class="wlSpinner" src="' + mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/extensions/WikiLove/images/spinner.gif' + '"/>'; + + // Build a type list like this: + var $typeList = $( '<ul id="wlTypes"></ul>' ); + for( var typeId in $.wikiLove.types ) { + var $button = $( '<a href="#"></a>' ); + var $buttonInside = $( '<div class="wlInside"></div>' ); + + if( typeof $.wikiLove.types[typeId].icon == 'string' ) { + $buttonInside.append( '<div class="wlIconBox"><img src="' + + mw.html.escape( $.wikiLove.types[typeId].icon ) + '"/></div>' ); + } + else { + $buttonInside.addClass( 'wlNoIcon' ); + } + + $buttonInside.append( '<div class="wlLinkText">' + $.wikiLove.types[typeId].name + '</div>' ); + + $button.append( '<div class="wlLeftCap"></div>'); + $button.append( $buttonInside ); + $button.append( '<div class="wlRightCap"></div>'); + $button.data( 'typeId', typeId ); + $typeList.append( $( '<li tabindex="0"></li>' ).append( $button ) ); } - else { - $buttonInside.addClass( 'wlNoIcon' ); - } - $buttonInside.append( '<div class="wlLinkText">' + $.wikiLove.types[typeId].name + '</div>' ); + $.wikiLove.$dialog = $( '\ +<div id="wikiLoveDialog">\ + <div id="wlSelectType">\ + <span class="wlNumber">1</span>\ + <h3><html:msg key="wikilove-select-type"/></h3>\ + <ul id="wlTypes"></ul>\ + </div>\ + <div id="wlGetStarted">\ + <h2><html:msg key="wikilove-get-started-header"/></h2>\ + <ol>\ + <li><html:msg key="wikilove-get-started-list-1"/></li>\ + <li><html:msg key="wikilove-get-started-list-2"/></li>\ + <li><html:msg key="wikilove-get-started-list-3"/></li>\ + </ol>\ + </div>\ + <div id="wlAddDetails">\ + <span class="wlNumber">2</span>\ + <h3><html:msg key="wikilove-add-details"/></h3>\ + <form id="wlPreviewForm">\ + <label for="wlSubtype" id="wlSubtypeLabel"></label>\ + <select id="wlSubtype"></select>\ + <div id="wlSubtypeDescription"></div>\ + <label id="wlGalleryLabel"><html:msg key="wikilove-image"/></label>\ + <div id="wlGallerySpinner">' + spinner + '</div>\ + <div id="wlGallery"></div>\ + <label for="wlHeader" id="wlHeaderLabel"><html:msg key="wikilove-header"/></label>\ + <input type="text" class="text" id="wlHeader"/>\ + <label for="wlTitle" id="wlTitleLabel"><html:msg key="wikilove-title"/></label>\ + <input type="text" class="text" id="wlTitle"/>\ + <label for="wlImage" id="wlImageLabel"><html:msg key="wikilove-image"/></label>\ + <input type="text" class="text" id="wlImage"/>\ + <label for="wlMessage" id="wlMessageLabel"><html:msg key="wikilove-enter-message"/></label>\ + <span class="wlNote"><html:msg key="wikilove-omit-sig"/></span>\ + <textarea id="wlMessage"></textarea>\ + <div id="wlNotify">\ + <input type="checkbox" id="wlNotifyCheckbox" name="notify"/>\ + <label for="wlNotifyCheckbox"><html:msg key="wikilove-notify"/></label>\ + </div>\ + <button class="submit" id="wlButtonPreview" type="submit"></button>\ + ' + spinner + '\ + </form>\ + </div>\ + <div id="wlPreview">\ + <span class="wlNumber">3</span>\ + <h3><html:msg key="wikilove-preview"/></h3>\ + <div id="wlPreviewArea"></div>\ + <form id="wlSendForm">\ + <button class="submit" id="wlButtonSend" type="submit"></button>\ + ' + spinner + '\ + </form>\ + </div>\ +</div>' ); + $.wikiLove.$dialog.localize(); - $button.append( '<div class="wlLeftCap"></div>'); - $button.append( $buttonInside ); - $button.append( '<div class="wlRightCap"></div>'); - $button.data( 'typeId', typeId ); - $typeList.append( $( '<li tabindex="0"></li>' ).append( $button ) ); - } - - // Build the left menu for selecting a type: - var $selectType = $( '<div id="wlSelectType"></div>' ) - .append( '<span class="wlNumber">1</span>' ) - .append( '<h3>' + mw.msg( 'wikilove-select-type' ) + '</h3>' ) - .append( $typeList ); + $.wikiLove.$dialog.dialog({ + width: 800, + position: ['center', 80], + autoOpen: false, + title: mw.msg( 'wikilove-dialog-title' ), + modal: true, + resizable: false + }); - var $getStarted = $( '<div id="wlGetStarted"></div>' ) - .append( '<h2>' + mw.msg( 'wikilove-get-started-header' ) + '</h2>' ) - .append( $( '<ol></ol>' ) - .append( '<li>' + mw.msg( 'wikilove-get-started-list-1' ) + '</li>' ) - .append( '<li>' + mw.msg( 'wikilove-get-started-list-2' ) + '</li>' ) - .append( '<li>' + mw.msg( 'wikilove-get-started-list-3' ) + '</li>' ) - ); - - // Build the right top section for selecting a subtype and entering a title (optional) and message - var $addDetails = $( '<div id="wlAddDetails"></div>' ) - .append( '<span class="wlNumber">2</span>' ) - .append( '<h3>' + mw.msg( 'wikilove-add-details' ) + '</h3>' ) - .append( $( '<form id="wlPreviewForm"></form>' ) - .append( '<label for="wlSubtype" id="wlSubtypeLabel"></label>' ) - .append( '<select id="wlSubtype"></select>' ) - .append( '<div id="wlSubtypeDescription"/>' ) - .append( '<label id="wlGalleryLabel">' + mw.msg( 'wikilove-image' ) + '</label>' ) - .append( '<div id="wlGallerySpinner">' + spinner + '</div>' ) - .append( '<div id="wlGallery"/>' ) - .append( '<label for="wlHeader" id="wlHeaderLabel">' + mw.msg( 'wikilove-header' ) + '</label>' ) - .append( '<input type="text" class="text" id="wlHeader"/>' ) - .append( '<label for="wlTitle" id="wlTitleLabel">' + mw.msg( 'wikilove-title' ) + '</label>' ) - .append( '<input type="text" class="text" id="wlTitle"/>' ) - .append( '<label for="wlImage" id="wlImageLabel">' + mw.msg( 'wikilove-image' ) + '</label>' ) - .append( '<input type="text" class="text" id="wlImage"/>' ) - .append( '<label for="wlMessage" id="wlMessageLabel">' + mw.msg( 'wikilove-enter-message' ) + '</label>' ) - .append( '<span class="wlNote">' + mw.msg( 'wikilove-omit-sig' ) + '</span>' ) - .append( '<textarea id="wlMessage"></textarea>' ) - .append( $('<div id="wlNotify"></div>').html('<input type="checkbox" id="wlNotifyCheckbox" name="notify"/> <label for="wlNotifyCheckbox">Notify user by email</label>') ) - .append( $('<button class="submit" id="wlButtonPreview" type="submit"></button>').button({ label: mw.msg( 'wikilove-button-preview' ), icons: { primary:'ui-icon-search' } }) ) - .append( spinner ) - ) - .hide(); - - // Build the right bottom preview section - var $preview = $( '<div id="wlPreview"></div>' ) - .append( '<span class="wlNumber">3</span>' ) - .append( '<h3>' + mw.msg( 'wikilove-preview' ) + '</h3>' ) - .append( '<div id="wlPreviewArea"></div>' ) - .append( $( '<form id="wlSendForm"></form>' ) - .append( $('<button class="submit" id="wlButtonSend" type="submit"></button>').button({ label: mw.msg( 'wikilove-button-send' ) }) ) - .append( spinner ) - ) - .hide(); - - // Build a modal, hidden dialog with the 3 different sections - $.wikiLove.$dialog = $( '<div id="wikiLoveDialog"></div>' ) - .append( $selectType ) - .append( $getStarted ) - .append( $addDetails ) - .append( $preview ) - .dialog({ - width: 800, - position: ['center', 80], - autoOpen: false, - title: mw.msg( 'wikilove-dialog-title' ), - modal: true, - resizable: false - }); - - $( '#wlTypes a' ).click( $.wikiLove.clickType ); - $( '#wlSubtype' ).change( $.wikiLove.changeSubtype ); - $( '#wlPreviewForm' ).submit( $.wikiLove.submitPreview ); - $( '#wlSendForm' ).click( $.wikiLove.submitSend ); - $( '#wlMessage' ).elastic(); // have the message textarea grow automatically + $( '#wlButtonPreview' ).button( { label: mw.msg( 'wikilove-button-preview' ), icons: { primary:'ui-icon-search' } } ); + $( '#wlButtonSend' ).button( { label: mw.msg( 'wikilove-button-send' ) } ); + $( '#wlAddDetails' ).hide(); + $( '#wlPreview' ).hide(); + $( '#wlTypes' ).replaceWith( $typeList ); + + $( '#wlTypes a' ).click( $.wikiLove.clickType ); + $( '#wlSubtype' ).change( $.wikiLove.changeSubtype ); + $( '#wlPreviewForm' ).submit( $.wikiLove.submitPreview ); + $( '#wlSendForm' ).click( $.wikiLove.submitSend ); + $( '#wlMessage' ).elastic(); // have the message textarea grow automatically + + $.wikiLove.$dialog.dialog( 'open' ); + }); + } else { + $.wikiLove.$dialog.dialog( 'open' ); } - - $.wikiLove.$dialog.dialog( 'open' ); }, /* @@ -157,13 +162,13 @@ // add all the subtypes to the menu while setting their subtype ids in jQuery data var subtype = $.wikiLove.types[$.wikiLove.currentTypeId].subtypes[subtypeId]; $( '#wlSubtype' ).append( - $( '<option>' + subtype.option + '</option>' ).data( 'subtypeId', subtypeId ) + $( '<option></option>' ).text( subtype.option ).data( 'subtypeId', subtypeId ) ); } $( '#wlSubtype' ).show(); // change and show the subtype label depending on the type - $( '#wlSubtypeLabel' ).text( $.wikiLove.types[$.wikiLove.currentTypeId].select ); + $( '#wlSubtypeLabel' ).text( $.wikiLove.types[$.wikiLove.currentTypeId].select || mw.msg( 'wikilove-select-type' ) ); $( '#wlSubtypeLabel' ).show(); $.wikiLove.changeSubtype(); // update controls depending on the currently selected (i.e. first) subtype } @@ -200,33 +205,6 @@ }, /* - * Find out whether we can e-mail this user. Probably needs to be moved to the API. - */ - getEmailable: function() { - // Test to see if the 'E-mail this user' link exists - $.wikiLove.emailable = $('#t-emailuser').length ? true : false; - }, - - /* - * Actually send the notification e-mail. Probably needs to be moved to the API. - */ - sendEmail: function( subject, text ) { - $.ajax({ - url: mw.util.wikiScript( 'api' ), - data: { - 'action': 'emailuser', - 'target': wgTitle, - 'subject': subject, - 'text': text, - 'format': 'json', - 'token': mw.user.tokens.get( 'editToken' ) - }, - dataType: 'json', - type: 'POST' - }); - }, - - /* * Called when type or subtype changes, updates controls. */ updateAllDetails: function() { @@ -240,40 +218,34 @@ } // show or hide header label and textbox depending on whether a predefined header exists - var needsHeader = $.inArray( 'header', $.wikiLove.types[$.wikiLove.currentTypeId].fields ); - if( typeof $.wikiLove.currentTypeOrSubtype.header == 'string' || needsHeader == -1 ) { - $( '#wlHeaderLabel').hide(); - $( '#wlHeader' ).hide(); - $( '#wlHeader' ).val( $.wikiLove.currentTypeOrSubtype.header ); - } else if ( needsHeader != -1 ) { + if( $.inArray( 'header', $.wikiLove.currentTypeOrSubtype.fields ) >= 0 ) { $( '#wlHeaderLabel').show(); $( '#wlHeader' ).show(); - $( '#wlHeader' ).val( '' ); + } else { + $( '#wlHeaderLabel').hide(); + $( '#wlHeader' ).hide(); } + $( '#wlHeader' ).val( $.wikiLove.currentTypeOrSubtype.header || '' ); // show or hide title label and textbox depending on whether a predefined title exists - var needsTitle = $.inArray( 'title', $.wikiLove.types[$.wikiLove.currentTypeId].fields ); - if( typeof $.wikiLove.currentTypeOrSubtype.title == 'string' || needsTitle == -1 ) { - $( '#wlTitleLabel').hide(); - $( '#wlTitle' ).hide(); - $( '#wlTitle' ).val( $.wikiLove.currentTypeOrSubtype.title ); - } else if ( needsTitle != -1 ) { + if( $.inArray( 'title', $.wikiLove.currentTypeOrSubtype.fields ) >= 0 ) { $( '#wlTitleLabel').show(); $( '#wlTitle' ).show(); - $( '#wlTitle' ).val( '' ); + } else { + $( '#wlTitleLabel').hide(); + $( '#wlTitle' ).hide(); } + $( '#wlTitle' ).val( $.wikiLove.currentTypeOrSubtype.title || '' ); // show or hide image label and textbox depending on whether a predefined image exists - var needsTitle = $.inArray( 'image', $.wikiLove.types[$.wikiLove.currentTypeId].fields ); - if( typeof $.wikiLove.currentTypeOrSubtype.image == 'string' || needsTitle == -1 ) { - $( '#wlImageLabel').hide(); - $( '#wlImage' ).hide(); - $( '#wlImage' ).val( $.wikiLove.currentTypeOrSubtype.image ); - } else if ( needsTitle != -1 ) { + if( $.inArray( 'image', $.wikiLove.currentTypeOrSubtype.fields ) >= 0 ) { $( '#wlImageLabel').show(); $( '#wlImage' ).show(); - $( '#wlImage' ).val( '' ); + } else { + $( '#wlImageLabel').hide(); + $( '#wlImage' ).hide(); } + $( '#wlImage' ).val( $.wikiLove.currentTypeOrSubtype.image || '' ); if( typeof $.wikiLove.currentTypeOrSubtype.gallery == 'object' ) { $( '#wlGalleryLabel' ).show(); @@ -287,7 +259,7 @@ $( '#wlGallerySpinner' ).hide(); } - if( $.wikiLove.types[$.wikiLove.currentTypeId].showNotify ) { + if( $.inArray( 'notify', $.wikiLove.currentTypeOrSubtype.fields ) >= 0 && $.wikiLove.emailable ) { $( '#wlNotify' ).show(); } else { $( '#wlNotify' ).hide(); @@ -320,13 +292,8 @@ $.wikiLove.showError( 'wikilove-err-sig' ); return false; } - if ( $.wikiLove.currentTypeOrSubtype.text ) { - var text = $.wikiLove.currentTypeOrSubtype.text; - } else { - var text = $.wikiLove.defaultText; - } var msg = $.wikiLove.prepareMsg( - text, + $.wikiLove.currentTypeOrSubtype.text || $.wikiLove.defaultText, $.wikiLove.currentTypeOrSubtype.imageSize, $.wikiLove.currentTypeOrSubtype.backgroundColor, $.wikiLove.currentTypeOrSubtype.borderColor @@ -337,14 +304,17 @@ 'header': $( '#wlHeader' ).val(), 'msg': msg, 'type': $.wikiLove.currentTypeId - + ($.wikiLove.currentSubtypeId != null ? '-' + $.wikiLove.currentSubtypeId : ''), - 'notify': $( '#wlNotifyCheckbox:checked' ).val() + + ($.wikiLove.currentSubtypeId !== null ? '-' + $.wikiLove.currentSubtypeId : ''), }; + + if ( $( '#wlNotifyCheckbox:checked' ).val() && $.wikiLove.emailable ) { + $.wikiLove.previewData.mail = $.wikiLove.prepareMsg( $.wikiLove.currentTypeOrSubtype.mail ); + } return false; }, showError: function( errmsg ) { - $( '#wlAddDetails' ).append( $( '<div class="wlError"></div>' ).html( mw.msg( errmsg ) ) ); + $( '#wlAddDetails' ).append( $( '<div class="wlError"></div>' ).text( mw.msg( errmsg ) ) ); }, /* @@ -363,21 +333,10 @@ msg = msg.replace( '$2', $( '#wlTitle' ).val() ); // replace the title msg = msg.replace( '$3', $( '#wlImage' ).val() ); // replace the image - if ( imageSize ) { - var myImageSize = imageSize; - } else { - var myImageSize = $.wikiLove.defaultImageSize; - } - if ( backgroundColor ) { - var myBackgroundColor = backgroundColor; - } else { - var myBackgroundColor = $.wikiLove.defaultBackgroundColor; - } - if ( borderColor ) { - var myBorderColor = borderColor; - } else { - var myBorderColor = $.wikiLove.defaultBorderColor; - } + var myImageSize = imageSize || $.wikiLove.defaultImageSize; + var myBackgroundColor = backgroundColor || $.wikiLove.defaultBackgroundColor; + var myBorderColor = borderColor || $.wikiLove.defaultBorderColor; + msg = msg.replace( '$4', myImageSize ); // replace the image size msg = msg.replace( '$5', myBackgroundColor ); // replace the background color msg = msg.replace( '$6', myBorderColor ); // replace the border color @@ -433,33 +392,33 @@ /* * Fires the final AJAX request and then redirects to the talk page where the content is added. */ - doSend: function( subject, wikitext, type, notify ) { + doSend: function( subject, wikitext, type, mail ) { $( '#wlPreview .wlSpinner' ).fadeIn( 200 ); + + var sendData = { + 'action': 'wikilove', + 'format': 'json', + 'title': mw.config.get( 'wgPageName' ), + 'type': type, + 'text': wikitext, + 'subject': subject, + 'token': mw.user.tokens.get( 'editToken' ) + }; + + if ( mail ) { + sendData.email = mail; + } + $.ajax({ url: mw.config.get( 'wgServer' ) + mw.config.get( 'wgScriptPath' ) + '/api.php?', - data: { - 'action': 'wikiLove', - 'format': 'json', - 'title': mw.config.get( 'wgPageName' ), - 'type': type, - 'text': wikitext, - 'subject': subject, - 'token': mw.user.tokens.get( 'editToken' ) - }, + data: sendData, dataType: 'json', type: 'POST', success: function( data ) { - if ( notify && $.wikiLove.emailable && typeof $.wikiLove.currentTypeOrSubtype.mail != 'undefined' ) { - $.wikiLove.sendEmail( - $.wikiLove.currentTypeOrSubtype.title, - $.wikiLove.prepareMsg( $.wikiLove.currentTypeOrSubtype.mail ) - ); - } - $( '#wlPreview .wlSpinner' ).fadeOut( 200 ); if ( typeof data.error !== 'undefined' ) { - $( '#wlPreview' ).append( '<div class="wlError">' + data.error.info + '<div>' ); + $( '#wlPreview' ).append( '<div class="wlError">' + mw.html.escape( data.error.info ) + '<div>' ); return; } @@ -471,8 +430,8 @@ window.location.reload(); } else { - window.location = mw.config.get( 'wgArticlePath' ).replace('$1', data.redirect.pageName) - + data.redirect.fragment; + window.location = mw.config.get( 'wgArticlePath' ).replace( '$1', data.redirect.pageName ) + + '#' + data.redirect.fragment; } } }); @@ -530,7 +489,7 @@ // only add the image if it's actually an image if( page.imageinfo[0].mime.substr(0,5) == 'image' ) { // build an image tag with the correct url and width - $img = $( '<img/>' ) + var $img = $( '<img/>' ) .attr( 'src', page.imageinfo[0].url ) .attr( 'width', $.wikiLove.currentTypeOrSubtype.gallery.width ) .hide() @@ -578,5 +537,6 @@ }); } }; -$.wikiLove.init(); + +$( document ).ready( $.wikiLove.init ); } ) ( jQuery ); _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs