From 336cb46167edf1049fb4309a682e98dd478beda2 Mon Sep 17 00:00:00 2001 From: Brion Vibber Date: Wed, 18 Jul 2007 19:19:50 +0000 Subject: [PATCH] Tweaks to the AJAX license preview: * Rename 'licence' to 'license' for consistency with the rest of the code. * The preview code was assuming simple templates with no parameters and no subst:s, which failed on most of the selections used now on Commons. Now doing a full pre-save transform and parse on {{$license}}, just as will be done on the final save. * When selecting 'None' again after another option, the preview pane is now cleared. A fun 'todo' might be to also pass the currently selected filename, if any, to the license preview. Some of the templates in use attempt to use the current-page-name variables to include a self-link. At the moment using a hardcoded 'Image:Sample.jpg' for the virtual title on the preview rendering. --- RELEASE-NOTES | 2 +- includes/DefaultSettings.php | 4 +-- includes/Setup.php | 4 +-- includes/SpecialUpload.php | 56 ++++++++++++------------------------ skins/common/upload.js | 35 +++++++++++----------- 5 files changed, 42 insertions(+), 59 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 66834b4ef9..8358681571 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -137,7 +137,7 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * (bug 10530) Introduce optional "sp-contributions-explain" message for additional explanation in Special:Contributions * (bug 10520) Preview licences during upload via AJAX (toggle with - $wgAjaxLicencePreview) + $wgAjaxLicensePreview) * New Parser::setTransparentTagHook for parser extension and template compatibility * Introduced 'ContributionsToolLinks' hook; see docs/hooks.txt for more diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 4f16073987..7d495e35b1 100644 --- a/includes/DefaultSettings.php +++ b/includes/DefaultSettings.php @@ -1201,7 +1201,7 @@ $wgCacheEpoch = '20030516000000'; * to ensure that client-side caches don't keep obsolete copies of global * styles. */ -$wgStyleVersion = '85'; +$wgStyleVersion = '86'; # Server-side caching: @@ -2563,7 +2563,7 @@ $wgAjaxUploadDestCheck = true; /** * Enable previewing licences via AJAX */ -$wgAjaxLicencePreview = true; +$wgAjaxLicensePreview = true; /** * Allow DISPLAYTITLE to change title display diff --git a/includes/Setup.php b/includes/Setup.php index a5e2048099..b3749afee7 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -258,8 +258,8 @@ $wgPostCommitUpdateList = array(); if ( $wgAjaxSearch ) $wgAjaxExportList[] = 'wfSajaxSearch'; if ( $wgAjaxWatch ) $wgAjaxExportList[] = 'wfAjaxWatch'; if ( $wgAjaxUploadDestCheck ) $wgAjaxExportList[] = 'UploadForm::ajaxGetExistsWarning'; -if( $wgAjaxLicencePreview ) - $wgAjaxExportList[] = 'UploadForm::ajaxGetLicencePreview'; +if( $wgAjaxLicensePreview ) + $wgAjaxExportList[] = 'UploadForm::ajaxGetLicensePreview'; wfSeedRandom(); diff --git a/includes/SpecialUpload.php b/includes/SpecialUpload.php index 83b3811350..18ebf0a532 100644 --- a/includes/SpecialUpload.php +++ b/includes/SpecialUpload.php @@ -559,40 +559,22 @@ class UploadForm { } /** - * Render a preview of a given licence for the AJAX preview on upload + * Render a preview of a given license for the AJAX preview on upload * - * @param string $licence + * @param string $license * @return string */ - public static function ajaxGetLicencePreview( $licence ) { - global $wgParser; - $licence = self::getLicenceTitle( $licence ); - if( $licence instanceof Title && $licence->exists() ) { - $title = SpecialPage::getTitleFor( 'Upload' ); - $revision = Revision::newFromTitle( $licence ); - $output = $wgParser->parse( $revision->getText(), $title, new ParserOptions() ); - return $output->getText(); - } - return wfMsgHtml( 'license-nopreview' ); - } - - /** - * Get the title of the page associated with a given licence - * string, i.e. do a quick resolution of {{$license}} without - * invoking the full parser - * - * @param string $licence - * @return Title - */ - private static function getLicenceTitle( $licence ) { - $template = substr( $licence, 0, 1 ) != ':'; - $title = Title::newFromText( ltrim( $licence, ':' ) ); - if( $title instanceof Title && $title->getNamespace() == NS_MAIN ) { - return $template - ? Title::makeTitle( NS_TEMPLATE, $title->getText() ) - : $title; - } - return $title; + public static function ajaxGetLicensePreview( $license ) { + global $wgParser, $wgUser; + $text = '{{' . $license . '}}'; + $title = Title::makeTitle( NS_IMAGE, 'Sample.jpg' ); + $options = ParserOptions::newFromUser( $wgUser ); + + // Expand subst: first, then live templates... + $text = $wgParser->preSaveTransform( $text, $title, $wgUser, $options ); + $output = $wgParser->parse( $text, $title, $options ); + + return $output->getText(); } /** @@ -750,19 +732,19 @@ class UploadForm { */ function mainUploadForm( $msg='' ) { global $wgOut, $wgUser; - global $wgUseCopyrightUpload, $wgUseAjax, $wgAjaxUploadDestCheck, $wgAjaxLicencePreview; + global $wgUseCopyrightUpload, $wgUseAjax, $wgAjaxUploadDestCheck, $wgAjaxLicensePreview; global $wgRequest, $wgAllowCopyUploads, $wgEnableAPI; global $wgStylePath, $wgStyleVersion; $useAjaxDestCheck = $wgUseAjax && $wgAjaxUploadDestCheck; - $useAjaxLicencePreview = $wgUseAjax && $wgAjaxLicencePreview; + $useAjaxLicensePreview = $wgUseAjax && $wgAjaxLicensePreview; $adc = wfBoolToStr( $useAjaxDestCheck ); - $alp = wfBoolToStr( $useAjaxLicencePreview ); + $alp = wfBoolToStr( $useAjaxLicensePreview ); $wgOut->addScript( " " ); @@ -889,10 +871,10 @@ EOT " ); - if( $useAjaxLicencePreview ) { + if( $useAjaxLicensePreview ) { $wgOut->addHtml( " - + " ); } diff --git a/skins/common/upload.js b/skins/common/upload.js index 69f81d0857..6477e12849 100644 --- a/skins/common/upload.js +++ b/skins/common/upload.js @@ -1,17 +1,16 @@ function licenseSelectorCheck() { var selector = document.getElementById( "wpLicense" ); + var selection = selector.options[selector.selectedIndex].value; if( selector.selectedIndex > 0 ) { - var selection = selector.options[selector.selectedIndex].value; if( selection == "" ) { // Option disabled, but browser is broken and doesn't respect this selector.selectedIndex = 0; - } else { - // We might show a preview - if( wgAjaxLicencePreview ) { - wgUploadLicenceObj.fetchPreview( selection ); - } } } + // We might show a preview + if( wgAjaxLicensePreview ) { + wgUploadLicenseObj.fetchPreview( selection ); + } } function licenseSelectorFixup() { @@ -138,31 +137,33 @@ function fillDestFilename(id) { } } -var wgUploadLicenceObj = { +var wgUploadLicenseObj = { 'responseCache' : { '' : '' }, - 'fetchPreview': function( licence ) { - if( licence in this.responseCache ) { - this.showPreview( this.responseCache[licence] ); + 'fetchPreview': function( license ) { + if( license == "" ) { + this.showPreview( "" ); + } else if( license in this.responseCache ) { + this.showPreview( this.responseCache[license] ); } else { - injectSpinner( document.getElementById( 'wpLicense' ), 'licence' ); - sajax_do_call( 'UploadForm::ajaxGetLicencePreview', [licence], + injectSpinner( document.getElementById( 'wpLicense' ), 'license' ); + sajax_do_call( 'UploadForm::ajaxGetLicensePreview', [license], function( result ) { - wgUploadLicenceObj.processResult( result, licence ); + wgUploadLicenseObj.processResult( result, license ); } ); } }, - 'processResult' : function( result, licence ) { - removeSpinner( 'licence' ); + 'processResult' : function( result, license ) { + removeSpinner( 'license' ); this.showPreview( result.responseText ); - this.responseCache[licence] = result.responseText; + this.responseCache[license] = result.responseText; }, 'showPreview' : function( preview ) { - var previewPanel = document.getElementById( 'mw-licence-preview' ); + var previewPanel = document.getElementById( 'mw-license-preview' ); if( previewPanel.innerHTML != preview ) previewPanel.innerHTML = preview; } -- 2.20.1