Rillke has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/195230

Change subject: Upload form: Show or hide form controls dependent on existence 
of destination
......................................................................

Upload form: Show or hide form controls dependent on existence of destination

The status before this patch is that said form controls are absent or not
solely dependent on whether the `wpForReupload` parameter is set. This works
fine as long as the user does have an outdated link in their bookmarks, opens
a file description page which is deleted inbetween and presses "upload a new
version of this file" or enters a destination at which a file description
page exists.

In these cases the upload form previously has shown or hidden form controls
relevant to the creation of a new file description page which either let to
confusion on the user's end:
- Why should I pick a license, my work is already licensed?
- Why didn't my new license choice during re-upload change the file
  description page?)

Or on the administrator's end:
- How could this user upload a file without specifying a license?

Minor cleanup:
- Use `$license` variable instead of querying the same again.
- Documenting methods touched by this change.

Bug: T91889
Change-Id: I5baf02165af7cd753c3dd75b93159b6b117bc942
---
M includes/api/ApiQueryImageInfo.php
M includes/specials/SpecialUpload.php
M resources/src/mediawiki.special/mediawiki.special.upload.js
3 files changed, 87 insertions(+), 9 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/core 
refs/changes/30/195230/1

diff --git a/includes/api/ApiQueryImageInfo.php 
b/includes/api/ApiQueryImageInfo.php
index c4ca5d6..13fffd3 100644
--- a/includes/api/ApiQueryImageInfo.php
+++ b/includes/api/ApiQueryImageInfo.php
@@ -465,7 +465,18 @@
                $uploadwarning = isset( $prop['uploadwarning'] );
 
                if ( $uploadwarning ) {
-                       $vals['html'] = SpecialUpload::getExistsWarning( 
UploadBase::getExistsWarning( $file ) );
+                       $existsWarning = UploadBase::getExistsWarning( $file );
+                       $vals['uploadwarning'] = array(
+                               'code' => $existsWarning['warning'],
+                               'html' => SpecialUpload::getExistsWarning( 
$existsWarning ),
+                       );
+                       // Title::exists() allows hooks to alter its result;
+                       // UploadBase::getExistsWarning returns one warning 
only and
+                       // considers the existence of a file (not the 
description page)
+                       // more important
+                       if ( $file->getTitle()->getArticleID() ) {
+                               $vals['uploadwarning']['page-exists'] = '';
+                       }
                }
 
                if ( $file->isDeleted( File::DELETED_FILE ) ) {
diff --git a/includes/specials/SpecialUpload.php 
b/includes/specials/SpecialUpload.php
index 72d02e0..f05312bb 100644
--- a/includes/specials/SpecialUpload.php
+++ b/includes/specials/SpecialUpload.php
@@ -460,8 +460,16 @@
                        }
                }
 
-               // Get the page text if this is not a reupload
-               if ( !$this->mForReUpload ) {
+               // Get the page text if the file description page does not exist
+               $title = Title::makeTitleSafe( NS_FILE, $this->mDesiredDestName 
);
+               $descPageExists = $title instanceof Title && $title->exists();
+
+               // It might be possible that a file description has been 
deleted since
+               // we displayed the upload form; in this case some fields are 
likely
+               // empty but we still generate a file description skeleton which
+               // makes it easier to the user to complete it compared to 
creating
+               // a totally new file description page.
+               if ( !$descPageExists ) {
                        $pageText = self::getInitialPageText( $this->mComment, 
$this->mLicense,
                                $this->mCopyrightStatus, 
$this->mCopyrightSource, $this->getConfig() );
                } else {
@@ -793,6 +801,7 @@
                $this->mHideIgnoreWarning = !empty( 
$options['hideignorewarning'] );
                $this->mDestWarningAck = !empty( $options['destwarningack'] );
                $this->mDestFile = isset( $options['destfile'] ) ? 
$options['destfile'] : '';
+               $this->mDestDescPageTitle = Title::makeTitleSafe( NS_FILE, 
$this->mDestFile );
 
                $this->mComment = isset( $options['description'] ) ?
                        $options['description'] : '';
@@ -1046,9 +1055,21 @@
                        )
                );
 
+               # Indicates that overwriting an existing file is desired
                if ( $this->mForReUpload ) {
                        $descriptor['DestFile']['readonly'] = true;
-               } else {
+               }
+
+               $descPageExists = $this->mDestDescPageTitle instanceof Title
+                       && $this->mDestDescPageTitle->exists();
+
+               # Show license input if the target can be amended by the user or
+               # if there is no file description page at the specified target
+               # because if there is a file description page, the selection 
will
+               # be silently discarded.
+               # For the first case: JavaScript will check if destination 
exists
+               # and hide non-applicable form elements on demand.
+               if ( !$this->mForReUpload || !$descPageExists ) {
                        $descriptor['License'] = array(
                                'type' => 'select',
                                'class' => 'Licenses',
@@ -1058,7 +1079,9 @@
                        );
                }
 
-               if ( $config->get( 'UseCopyrightUpload' ) ) {
+               if ( $config->get( 'UseCopyrightUpload' )
+                       && ( !$this->mForReUpload || !$descPageExists )
+               ) {
                        $descriptor['UploadCopyStatus'] = array(
                                'type' => 'text',
                                'section' => 'description',
diff --git a/resources/src/mediawiki.special/mediawiki.special.upload.js 
b/resources/src/mediawiki.special/mediawiki.special.upload.js
index eeccda5..8ce3f71 100644
--- a/resources/src/mediawiki.special/mediawiki.special.upload.js
+++ b/resources/src/mediawiki.special/mediawiki.special.upload.js
@@ -16,6 +16,7 @@
                typing: false,
                delay: 500, // ms
                timeoutID: false,
+               initialPageDescriptorRows: null,
 
                keypress: function () {
                        if ( !ajaxUploadDestCheck ) {
@@ -35,7 +36,7 @@
                        }
                        // Check response cache
                        if ( this.responseCache.hasOwnProperty( 
this.nameToCheck ) ) {
-                               this.setWarning( 
this.responseCache[this.nameToCheck] );
+                               this.processResult( 
this.responseCache[this.nameToCheck], this.nameToCheck );
                                return;
                        }
 
@@ -78,9 +79,52 @@
                        } );
                },
 
+               /**
+                * Returns all rows that are exclusively used
+                * upon creation of a new file description page
+                *
+                * @return jQuery
+                */
+               getInitialPageDescriptorRows: function() {
+                       this.initialPageDescriptorRows =
+                               this.initialPageDescriptorRows || $license
+                                       .add( '#wpUploadCopyStatus' )
+                                       .add( '#wpUploadSource' )
+                                       .closest( 'tr' );
+
+                       return this.initialPageDescriptorRows;
+               },
+
+               /**
+                * Idempotent method that processes the API result;
+                * either originating API or the result cache
+                *
+                * @param {Object} result
+                * @param {string} fileName
+                */
                processResult: function ( result, fileName ) {
-                       this.setWarning( result.html );
-                       this.responseCache[fileName] = result.html;
+                       var uploadwarning = result.uploadwarning;
+
+                       this.responseCache[fileName] = uploadwarning;
+                       this.setWarning( uploadwarning.html );
+                       this.toggleInitialPageDescriptorRows(
+                               uploadwarning['page-exists'] !== undefined );
+               },
+
+               /**
+                * Dependent on whether the destination file description page
+                * exists toggles visibility of form controls whose values
+                * are used for the creation of the initial file description
+                * page
+                *
+                * @param {string} pageExists
+                */
+               toggleInitialPageDescriptorRows: function( pageExists ) {
+                       if ( pageExists ) {
+                               this.getInitialPageDescriptorRows().hide();
+                       } else {
+                               this.getInitialPageDescriptorRows().show();
+                       }
                },
 
                setWarning: function ( warning ) {
@@ -111,7 +155,7 @@
                                return;
                        }
 
-                       $spinnerLicense = $.createSpinner().insertAfter( 
'#wpLicense' );
+                       $spinnerLicense = $.createSpinner().insertAfter( 
$license );
 
                        ( new mw.Api() ).get( {
                                action: 'parse',

-- 
To view, visit https://gerrit.wikimedia.org/r/195230
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5baf02165af7cd753c3dd75b93159b6b117bc942
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/core
Gerrit-Branch: master
Gerrit-Owner: Rillke <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to