(bug 10520) Preview licences during upload via AJAX
authorRob Church <robchurch@users.mediawiki.org>
Sat, 14 Jul 2007 22:06:01 +0000 (22:06 +0000)
committerRob Church <robchurch@users.mediawiki.org>
Sat, 14 Jul 2007 22:06:01 +0000 (22:06 +0000)
RELEASE-NOTES
includes/DefaultSettings.php
includes/GlobalFunctions.php
includes/Setup.php
includes/SpecialUpload.php
skins/common/upload.js

index 915fda3..e77c500 100644 (file)
@@ -136,6 +136,8 @@ it from source control: http://www.mediawiki.org/wiki/Download_from_SVN
 * (bug 1962) Allow HTML attributes on <math>
 * (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 ==
 
index 7a9c4f9..3d9e0e8 100644 (file)
@@ -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
  */
index 81aa111..abf101f 100644 (file)
@@ -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
index 5a20b86..a5e2048 100644 (file)
@@ -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();
 
index a187f65..75a0f6a 100644 (file)
@@ -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( 
-                       "<script type='text/javascript'>wgAjaxUploadDestCheck = " . 
-                               ($useAjax ? 'true' : 'false' ) . ";</script>\n" . 
-                       "<script type='text/javascript' src=\"$wgStylePath/common/upload.js?1\"></script>\n" 
-               );
+               $useAjaxDestCheck = $wgUseAjax && $wgAjaxUploadDestCheck;
+               $useAjaxLicencePreview = $wgUseAjax && $wgAjaxLicencePreview;
+               
+               $adc = wfBoolToStr( $useAjaxDestCheck );
+               $alp = wfBoolToStr( $useAjaxLicencePreview );
+               
+               $wgOut->addScript( "<script type=\"text/javascript\">
+wgAjaxUploadDestCheck = {$adc};
+wgAjaxLicencePreview = {$alp};
+</script>
+<script type=\"text/javascript\" src=\"{$wgStylePath}/common/upload.js?{$wgStyleVersion}\"></script>
+               " );
 
                if( !wfRunHooks( 'UploadForm:initial', array( &$this ) ) )
                {
@@ -794,7 +836,7 @@ class UploadForm {
                                "size='40' />" .
                                "<input type='hidden' name='wpSourceType' value='file' />" ;
                }
-               if ( $useAjax ) {
+               if ( $useAjaxDestCheck ) {
                        $warningRow = "<tr><td colspan='2' id='wpDestFile-warning'>&nbsp</td></tr>";
                        $destOnkeyup = 'onkeyup="wgUploadWarningObj.keypress();"';
                } else {
@@ -845,8 +887,13 @@ EOT
                                </select>
                        </td>
                        </tr>
-                       <tr>
-               ");
+                       <tr>" );
+                       if( $useAjaxLicencePreview ) {
+                               $wgOut->addHtml( "
+                                       <td id=\"mw-licence-preview\" colspan=\"2\"></td>
+                               </tr>
+                               <tr>" );
+                       }
                }
 
                if ( $wgUseCopyrightUpload ) {
index 7c66e6d..3fc5fa0 100644 (file)
@@ -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 <option>
-               selector.selectedIndex = 0;
+       var selector = document.getElementById( "wpLicense" );
+       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 );
+                       }
+               }
        }
 }
 
@@ -132,4 +139,34 @@ function fillDestFilename(id) {
        }
 }
 
-addOnloadHook(licenseSelectorFixup);
+var wgUploadLicenceObj = {
+       
+       'responseCache' : { '' : '' },
+
+       'fetchPreview': function( licence ) {
+               if( licence in this.responseCache ) {
+                       this.showPreview( this.responseCache[licence] );
+               } else {
+                       sajax_do_call( 'UploadForm::ajaxGetLicencePreview', [licence],
+                               function( result ) {
+                                       wgUploadLicenceObj.processResult( result, licence );
+                               }
+                       );
+               }
+       },
+
+       'processResult' : function( result, licence ) {
+               this.showPreview( result.responseText );
+               this.responseCache[licence] = result.responseText;
+       },
+
+       'showPreview' : function( preview ) {
+               var previewPanel = document.getElementById( 'mw-licence-preview' );
+               if( previewPanel.innerHTML != preview ) {
+                       previewPanel.innerHTML = preview;
+               }
+       }
+
+}
+
+addOnloadHook( licenseSelectorFixup );
\ No newline at end of file