Tweaks to the AJAX license preview:
authorBrion Vibber <brion@users.mediawiki.org>
Wed, 18 Jul 2007 19:19:50 +0000 (19:19 +0000)
committerBrion Vibber <brion@users.mediawiki.org>
Wed, 18 Jul 2007 19:19:50 +0000 (19:19 +0000)
* 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
includes/DefaultSettings.php
includes/Setup.php
includes/SpecialUpload.php
skins/common/upload.js

index 66834b4..8358681 100644 (file)
@@ -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
index 4f16073..7d495e3 100644 (file)
@@ -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
index a5e2048..b3749af 100644 (file)
@@ -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();
 
index 83b3811..18ebf0a 100644 (file)
@@ -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( "<script type=\"text/javascript\">
 wgAjaxUploadDestCheck = {$adc};
-wgAjaxLicencePreview = {$alp};
+wgAjaxLicensePreview = {$alp};
 </script>
 <script type=\"text/javascript\" src=\"{$wgStylePath}/common/upload.js?{$wgStyleVersion}\"></script>
                " );
@@ -889,10 +871,10 @@ EOT
                        </td>
                        </tr>
                        <tr>" );
-                       if( $useAjaxLicencePreview ) {
+                       if( $useAjaxLicensePreview ) {
                                $wgOut->addHtml( "
                                        <td></td>
-                                       <td id=\"mw-licence-preview\"></td>
+                                       <td id=\"mw-license-preview\"></td>
                                </tr>
                                <tr>" );
                        }
index 69f81d0..6477e12 100644 (file)
@@ -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;
        }