From 1ec212b6127bbd63474c3225289060a43266f75e Mon Sep 17 00:00:00 2001 From: Rob Church Date: Sat, 14 Jul 2007 22:06:01 +0000 Subject: [PATCH] (bug 10520) Preview licences during upload via AJAX --- RELEASE-NOTES | 2 + includes/DefaultSettings.php | 7 +++- includes/GlobalFunctions.php | 11 ++++++ includes/Setup.php | 2 + includes/SpecialUpload.php | 71 ++++++++++++++++++++++++++++++------ skins/common/upload.js | 49 ++++++++++++++++++++++--- 6 files changed, 123 insertions(+), 19 deletions(-) diff --git a/RELEASE-NOTES b/RELEASE-NOTES index 915fda3f86..e77c50089a 100644 --- a/RELEASE-NOTES +++ b/RELEASE-NOTES @@ -136,6 +136,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN * (bug 1962) Allow HTML attributes on * (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) == Bugfixes since 1.10 == diff --git a/includes/DefaultSettings.php b/includes/DefaultSettings.php index 7a9c4f9727..3d9e0e819f 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 = '82'; +$wgStyleVersion = '83'; # Server-side caching: @@ -2559,6 +2559,11 @@ $wgAjaxWatch = true; */ $wgAjaxUploadDestCheck = true; +/** + * Enable previewing licences via AJAX + */ +$wgAjaxLicencePreview = true; + /** * Allow DISPLAYTITLE to change title display */ diff --git a/includes/GlobalFunctions.php b/includes/GlobalFunctions.php index 81aa1112d0..abf101f3d0 100644 --- a/includes/GlobalFunctions.php +++ b/includes/GlobalFunctions.php @@ -2285,4 +2285,15 @@ function wfQueriesMustScale() { function wfScript( $script = 'index' ) { global $wgScriptPath, $wgScriptExtension; return "{$wgScriptPath}/{$script}{$wgScriptExtension}"; +} + +/** + * Convenience function converts boolean values into "true" + * or "false" (string) values + * + * @param bool $value + * @return string + */ +function wfBoolToStr( $value ) { + return $value ? 'true' : 'false'; } \ No newline at end of file diff --git a/includes/Setup.php b/includes/Setup.php index 5a20b860c2..a5e2048099 100644 --- a/includes/Setup.php +++ b/includes/Setup.php @@ -258,6 +258,8 @@ $wgPostCommitUpdateList = array(); if ( $wgAjaxSearch ) $wgAjaxExportList[] = 'wfSajaxSearch'; if ( $wgAjaxWatch ) $wgAjaxExportList[] = 'wfAjaxWatch'; if ( $wgAjaxUploadDestCheck ) $wgAjaxExportList[] = 'UploadForm::ajaxGetExistsWarning'; +if( $wgAjaxLicencePreview ) + $wgAjaxExportList[] = 'UploadForm::ajaxGetLicencePreview'; wfSeedRandom(); diff --git a/includes/SpecialUpload.php b/includes/SpecialUpload.php index a187f65d3b..75a0f6a6ca 100644 --- a/includes/SpecialUpload.php +++ b/includes/SpecialUpload.php @@ -556,6 +556,43 @@ class UploadForm { } return $s; } + + /** + * Render a preview of a given licence for the AJAX preview on upload + * + * @param string $licence + * @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 ''; + } + + /** + * 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; + } /** * Stash a file in a temporary directory for later processing @@ -712,17 +749,22 @@ class UploadForm { */ function mainUploadForm( $msg='' ) { global $wgOut, $wgUser; - global $wgUseCopyrightUpload, $wgAjaxUploadDestCheck, $wgUseAjax; + global $wgUseCopyrightUpload, $wgUseAjax, $wgAjaxUploadDestCheck, $wgAjaxLicencePreview; global $wgRequest, $wgAllowCopyUploads, $wgEnableAPI; - global $wgStylePath; - - $useAjax = $wgAjaxUploadDestCheck && $wgUseAjax; + global $wgStylePath, $wgStyleVersion; - $wgOut->addScript( - "\n" . - "\n" - ); + $useAjaxDestCheck = $wgUseAjax && $wgAjaxUploadDestCheck; + $useAjaxLicencePreview = $wgUseAjax && $wgAjaxLicencePreview; + + $adc = wfBoolToStr( $useAjaxDestCheck ); + $alp = wfBoolToStr( $useAjaxLicencePreview ); + + $wgOut->addScript( " + + " ); if( !wfRunHooks( 'UploadForm:initial', array( &$this ) ) ) { @@ -794,7 +836,7 @@ class UploadForm { "size='40' />" . "" ; } - if ( $useAjax ) { + if ( $useAjaxDestCheck ) { $warningRow = "
 
"; $destOnkeyup = 'onkeyup="wgUploadWarningObj.keypress();"'; } else { @@ -845,8 +887,13 @@ EOT -
- "); +
" ); + if( $useAjaxLicencePreview ) { + $wgOut->addHtml( " + +
+
" ); + } } if ( $wgUseCopyrightUpload ) { diff --git a/skins/common/upload.js b/skins/common/upload.js index 7c66e6d832..3fc5fa0a47 100644 --- a/skins/common/upload.js +++ b/skins/common/upload.js @@ -1,9 +1,16 @@ function licenseSelectorCheck() { - var selector = document.getElementById("wpLicense"); - if (selector.selectedIndex > 0 && - selector.options[selector.selectedIndex].value == "" ) { - // Browser is broken, doesn't respect disabled attribute on