var $mDestName, $mTempPath, $mFileSize, $mFileProps;
var $mCopyrightStatus, $mCopyrightSource, $mReUpload, $mAction, $mUploadClicked;
var $mSrcName, $mSessionKey, $mStashed, $mDesiredDestName, $mRemoveTempFile, $mSourceType;
- var $mCurlDestHandle;
+ var $mDestWarningAck, $mCurlDestHandle;
var $mLocalFile;
# Placeholders for text injection by hooks (must be HTML)
$this->mCopyrightSource = $request->getText( 'wpUploadSource' );
$this->mWatchthis = $request->getBool( 'wpWatchthis' );
$this->mSourceType = $request->getText( 'wpSourceType' );
- wfDebug( "UploadForm: watchthis is: '$this->mWatchthis'\n" );
+ $this->mDestWarningAck = $request->getText( 'wpDestFileWarningAck' );
$this->mAction = $request->getVal( 'action' );
$warning .= '<li>'.wfMsgHtml( 'emptyfile' ).'</li>';
}
- global $wgUser;
- $sk = $wgUser->getSkin();
-
- // Check for uppercase extension. We allow these filenames but check if an image
- // with lowercase extension exists already
- if ( $finalExt != strtolower( $finalExt ) ) {
- $nt_lc = Title::newFromText( $partname . '.' . strtolower( $finalExt ) );
- $image_lc = wfLocalFile( $nt_lc );
- }
-
- if( $this->mLocalFile->exists() ) {
- $dlink = $sk->makeKnownLinkObj( $nt );
- if ( $this->mLocalFile->allowInlineDisplay() ) {
- $dlink2 = $sk->makeImageLinkObj( $nt, wfMsgExt( 'fileexists-thumb', 'parseinline', $dlink ),
- $nt->getText(), 'right', array(), false, true );
- } elseif ( !$this->mLocalFile->allowInlineDisplay() && $this->mLocalFile->isSafeFile() ) {
- $icon = $this->mLocalFile->iconThumb();
- $dlink2 = '<div style="float:right" id="mw-media-icon"><a href="' . $this->mLocalFile->getURL() . '">' .
- $icon->toHtml() . '</a><br />' . $dlink . '</div>';
- } else {
- $dlink2 = '';
- }
-
- $warning .= '<li>' . wfMsgExt( 'fileexists', 'parseline', $dlink ) . '</li>' . $dlink2;
-
- } elseif ( isset( $image_lc) && $image_lc->exists() ) {
- # Check if image with lowercase extension exists.
- # It's not forbidden but in 99% it makes no sense to upload the same filename with uppercase extension
- $dlink = $sk->makeKnownLinkObj( $nt_lc );
- if ( $image_lc->allowInlineDisplay() ) {
- $dlink2 = $sk->makeImageLinkObj( $nt_lc, wfMsgExt( 'fileexists-thumb', 'parseinline', $dlink ),
- $nt_lc->getText(), 'right', array(), false, true );
- } elseif ( !$image_lc->allowInlineDisplay() && $image_lc->isSafeFile() ) {
- $icon = $image_lc->iconThumb();
- $dlink2 = '<div style="float:right" id="mw-media-icon"><a href="' . $image_lc->getURL() . '">' .
- $icon->toHtml() . '</a><br />' . $dlink . '</div>';
- } else {
- $dlink2 = '';
- }
-
- $warning .= '<li>' . wfMsgExt( 'fileexists-extension', 'parsemag' , $partname . '.'
- . $finalExt , $dlink ) . '</li>' . $dlink2;
-
- } elseif ( ( substr( $partname , 3, 3 ) == 'px-' || substr( $partname , 2, 3 ) == 'px-' )
- && ereg( "[0-9]{2}" , substr( $partname , 0, 2) ) )
- {
- # Check for filenames like 50px- or 180px-, these are mostly thumbnails
- $nt_thb = Title::newFromText( substr( $partname , strpos( $partname , '-' ) +1 ) . '.' . $finalExt );
- $image_thb = wfLocalFile( $nt_thb );
- if ($image_thb->exists() ) {
- # Check if an image without leading '180px-' (or similiar) exists
- $dlink = $sk->makeKnownLinkObj( $nt_thb);
- if ( $image_thb->allowInlineDisplay() ) {
- $dlink2 = $sk->makeImageLinkObj( $nt_thb,
- wfMsgExt( 'fileexists-thumb', 'parseinline', $dlink ),
- $nt_thb->getText(), 'right', array(), false, true );
- } elseif ( !$image_thb->allowInlineDisplay() && $image_thb->isSafeFile() ) {
- $icon = $image_thb->iconThumb();
- $dlink2 = '<div style="float:right" id="mw-media-icon"><a href="' .
- $image_thb->getURL() . '">' . $icon->toHtml() . '</a><br />' .
- $dlink . '</div>';
- } else {
- $dlink2 = '';
- }
-
- $warning .= '<li>' . wfMsgExt( 'fileexists-thumbnail-yes', 'parsemag', $dlink ) .
- '</li>' . $dlink2;
- } else {
- # Image w/o '180px-' does not exists, but we do not like these filenames
- $warning .= '<li>' . wfMsgExt( 'file-thumbnail-no', 'parseinline' ,
- substr( $partname , 0, strpos( $partname , '-' ) +1 ) ) . '</li>';
- }
- }
- if ( $this->mLocalFile->wasDeleted() ) {
- # If the file existed before and was deleted, warn the user of this
- # Don't bother doing so if the image exists now, however
- $ltitle = SpecialPage::getTitleFor( 'Log' );
- $llink = $sk->makeKnownLinkObj( $ltitle, wfMsgHtml( 'deletionlog' ),
- 'type=delete&page=' . $nt->getPrefixedUrl() );
- $warning .= wfOpenElement( 'li' ) . wfMsgWikiHtml( 'filewasdeleted', $llink ) .
- wfCloseElement( 'li' );
+ if ( !$this->mDestWarningAck ) {
+ $warning .= self::getExistsWarning( $this->mLocalFile );
}
-
if( $warning != '' ) {
/**
* Stash the file in a temporary location; the user can choose
global $wgUser;
$wgUser->addWatch( $this->mLocalFile->getTitle() );
}
- if ( $status === '' ) {
- // New upload, redirect to description page
- $wgOut->redirect( $this->mLocalFile->getTitle()->getFullURL() );
+ // Success, redirect to description page
+ $wgOut->redirect( $this->mLocalFile->getTitle()->getFullURL() );
+ wfRunHooks( 'UploadComplete', array( &$img ) );
+ }
+ }
+
+ /**
+ * Do existence checks on a file and produce a warning
+ * This check is static and can be done pre-upload via AJAX
+ * Returns an HTML fragment consisting of one or more LI elements if there is a warning
+ * Returns an empty string if there is no warning
+ */
+ static function getExistsWarning( $file ) {
+ global $wgUser;
+ // Check for uppercase extension. We allow these filenames but check if an image
+ // with lowercase extension exists already
+ $warning = '';
+ $ext = $file->getExtension();
+ $sk = $wgUser->getSkin();
+ if ( $ext !== '' ) {
+ $partname = substr( $file->getName(), 0, -strlen( $ext ) - 1 );
+ } else {
+ $partname = $file->getName();
+ }
+
+ if ( $ext != strtolower( $ext ) ) {
+ $nt_lc = Title::newFromText( $partname . '.' . strtolower( $ext ) );
+ $file_lc = wfLocalFile( $nt_lc );
+ } else {
+ $file_lc = false;
+ }
+
+ if( $file->exists() ) {
+ $dlink = $sk->makeKnownLinkObj( $file->getTitle() );
+ if ( $file->allowInlineDisplay() ) {
+ $dlink2 = $sk->makeImageLinkObj( $file->getTitle(), wfMsgExt( 'fileexists-thumb', 'parseinline', $dlink ),
+ $file->getName(), 'right', array(), false, true );
+ } elseif ( !$file->allowInlineDisplay() && $file->isSafeFile() ) {
+ $icon = $file->iconThumb();
+ $dlink2 = '<div style="float:right" id="mw-media-icon"><a href="' . $file->getURL() . '">' .
+ $icon->toHtml() . '</a><br />' . $dlink . '</div>';
} else {
- // Reupload, show success page
- $this->showSuccess();
+ $dlink2 = '';
+ }
+
+ $warning .= '<li>' . wfMsgExt( 'fileexists', 'parseline', $dlink ) . '</li>' . $dlink2;
+
+ } elseif ( $file_lc && $file_lc->exists() ) {
+ # Check if image with lowercase extension exists.
+ # It's not forbidden but in 99% it makes no sense to upload the same filename with uppercase extension
+ $dlink = $sk->makeKnownLinkObj( $nt_lc );
+ if ( $file_lc->allowInlineDisplay() ) {
+ $dlink2 = $sk->makeImageLinkObj( $nt_lc, wfMsgExt( 'fileexists-thumb', 'parseinline', $dlink ),
+ $nt_lc->getText(), 'right', array(), false, true );
+ } elseif ( !$file_lc->allowInlineDisplay() && $file_lc->isSafeFile() ) {
+ $icon = $file_lc->iconThumb();
+ $dlink2 = '<div style="float:right" id="mw-media-icon"><a href="' . $file_lc->getURL() . '">' .
+ $icon->toHtml() . '</a><br />' . $dlink . '</div>';
+ } else {
+ $dlink2 = '';
+ }
+
+ $warning .= '<li>' . wfMsgExt( 'fileexists-extension', 'parsemag' , $partname . '.'
+ . $ext , $dlink ) . '</li>' . $dlink2;
+
+ } elseif ( ( substr( $partname , 3, 3 ) == 'px-' || substr( $partname , 2, 3 ) == 'px-' )
+ && ereg( "[0-9]{2}" , substr( $partname , 0, 2) ) )
+ {
+ # Check for filenames like 50px- or 180px-, these are mostly thumbnails
+ $nt_thb = Title::newFromText( substr( $partname , strpos( $partname , '-' ) +1 ) . '.' . $ext );
+ $file_thb = wfLocalFile( $nt_thb );
+ if ($file_thb->exists() ) {
+ # Check if an image without leading '180px-' (or similiar) exists
+ $dlink = $sk->makeKnownLinkObj( $nt_thb);
+ if ( $file_thb->allowInlineDisplay() ) {
+ $dlink2 = $sk->makeImageLinkObj( $nt_thb,
+ wfMsgExt( 'fileexists-thumb', 'parseinline', $dlink ),
+ $nt_thb->getText(), 'right', array(), false, true );
+ } elseif ( !$file_thb->allowInlineDisplay() && $file_thb->isSafeFile() ) {
+ $icon = $file_thb->iconThumb();
+ $dlink2 = '<div style="float:right" id="mw-media-icon"><a href="' .
+ $file_thb->getURL() . '">' . $icon->toHtml() . '</a><br />' .
+ $dlink . '</div>';
+ } else {
+ $dlink2 = '';
+ }
+
+ $warning .= '<li>' . wfMsgExt( 'fileexists-thumbnail-yes', 'parsemag', $dlink ) .
+ '</li>' . $dlink2;
+ } else {
+ # Image w/o '180px-' does not exists, but we do not like these filenames
+ $warning .= '<li>' . wfMsgExt( 'file-thumbnail-no', 'parseinline' ,
+ substr( $partname , 0, strpos( $partname , '-' ) +1 ) ) . '</li>';
}
- wfRunHooks( 'UploadComplete', array( &$img ) );
}
+ if ( $file->wasDeleted() ) {
+ # If the file existed before and was deleted, warn the user of this
+ # Don't bother doing so if the image exists now, however
+ $ltitle = SpecialPage::getTitleFor( 'Log' );
+ $llink = $sk->makeKnownLinkObj( $ltitle, wfMsgHtml( 'deletionlog' ),
+ 'type=delete&page=' . $file->getTitle()->getPrefixedUrl() );
+ $warning .= '<li>' . wfMsgWikiHtml( 'filewasdeleted', $llink ) . '</li>';
+ }
+ return $warning;
+ }
+
+ static function ajaxGetExistsWarning( $filename ) {
+ $file = wfFindFile( $filename );
+ $s = ' ';
+ if ( $file ) {
+ $warning = self::getExistsWarning( $file );
+ if ( $warning !== '' ) {
+ $s = "<ul>$warning</ul>";
+ }
+ }
+ return $s;
}
/**
/* -------------------------------------------------------------- */
- /**
- * Show some text and linkage on successful upload.
- * @access private
- */
- function showSuccess() {
- global $wgUser, $wgOut, $wgContLang;
-
- $sk = $wgUser->getSkin();
- $ilink = $sk->makeMediaLinkObj( $this->mLocalFile->getTitle() );
- $dname = $wgContLang->getNsText( NS_IMAGE ) . ':'.$this->mDestName;
- $dlink = $sk->makeKnownLink( $dname, $dname );
-
- $wgOut->addHTML( '<h2>' . wfMsgHtml( 'successfulupload' ) . "</h2>\n" );
- $text = wfMsgWikiHtml( 'fileuploaded', $ilink, $dlink );
- $wgOut->addHTML( $text );
- $wgOut->returnToMain( false );
- }
-
/**
* @param string $error as HTML
* @access private
*/
function mainUploadForm( $msg='' ) {
global $wgOut, $wgUser;
- global $wgUseCopyrightUpload;
- global $wgRequest, $wgAllowCopyUploads;
+ global $wgUseCopyrightUpload, $wgAjaxUploadDestCheck;
+ global $wgRequest, $wgAllowCopyUploads, $wgEnableAPI;
+ global $wgStylePath;
+
+ $wgOut->addScript(
+ "<script type='text/javascript'>wgAjaxUploadDestCheck = " .
+ ($wgAjaxUploadDestCheck ? 'true' : 'false' ) . ";</script>\n" .
+ "<script type='text/javascript' src=\"$wgStylePath/common/upload.js?1\"></script>\n"
+ );
if( !wfRunHooks( 'UploadForm:initial', array( &$this ) ) )
{
"size='40' />" .
"<input type='hidden' name='wpSourceType' value='file' />" ;
}
+ if ( $wgAjaxUploadDestCheck ) {
+ $warningRow = "<tr><td colspan='2' id='wpDestFile-warning'> </td></tr>";
+ $destOnkeyup = 'onkeyup="wgUploadWarningObj.keypress();"';
+ } else {
+ $warningRow = '';
+ $destOnkeyup = '';
+ }
+
$encComment = htmlspecialchars( $this->mComment );
$wgOut->addHTML( <<<EOT
<tr>
<td align='right'><label for='wpDestFile'>{$destfilename}:</label></td>
<td align='left'>
- <input tabindex='2' type='text' name='wpDestFile' id='wpDestFile' size='40' value="$encDestName" onkeyup="checkFileExists();" />
+ <input tabindex='2' type='text' name='wpDestFile' id='wpDestFile' size='40'
+ value="$encDestName" $destOnkeyup />
</td>
</tr>
<tr>
$wgOut->addHTML( "
<td align='right'><label for='wpLicense'>$license:</label></td>
<td align='left'>
- <script type='text/javascript' src=\"$wgStylePath/common/upload.js\"></script>
<select name='wpLicense' id='wpLicense' tabindex='4'
onchange='licenseSelectorCheck()'>
<option value=''>$nolicense</option>
");
}
-
$wgOut->addHtml( "
<td></td>
<td>
<label for='wpIgnoreWarning'>" . wfMsgHtml( 'ignorewarnings' ) . "</label>
</td>
</tr>
+ $warningRow
<tr>
<td></td>
<td align='left'><input tabindex='9' type='submit' name='wpUpload' value=\"{$ulb}\" /></td>
</tr>
-
<tr>
<td></td>
<td align='left'>
</tr>
</table>
+ <input type='hidden' name='wpDestFileWarningAck' id='wpDestFileWarningAck' value=''/>
</form>" );
}
// for MSIE/Mac; non-breaking spaces cause the <option> not to render
// but, for some reason, setting the text to itself works
var selector = document.getElementById("wpLicense");
- var ua = navigator.userAgent;
- var isMacIe = (ua.indexOf("MSIE") != -1) && (ua.indexOf("Mac") != -1);
- if (isMacIe) {
- for (var i = 0; i < selector.options.length; i++) {
- selector.options[i].text = selector.options[i].text;
+ if (selector) {
+ var ua = navigator.userAgent;
+ var isMacIe = (ua.indexOf("MSIE") != -1) && (ua.indexOf("Mac") != -1);
+ if (isMacIe) {
+ for (var i = 0; i < selector.options.length; i++) {
+ selector.options[i].text = selector.options[i].text;
+ }
+ }
+ }
+}
+
+var wgUploadWarningObj = {
+ 'responseCache' : { '' : ' ' },
+ 'nameToCheck' : '',
+ 'typing': false,
+ 'delay': 500, // ms
+ 'timeoutID': false,
+
+ 'keypress': function () {
+ // Find file to upload
+ var destFile = document.getElementById('wpDestFile');
+ var warningElt = document.getElementById( 'wpDestFile-warning' );
+ if ( !destFile || !warningElt ) return ;
+
+ this.nameToCheck = destFile.value ;
+
+ // Clear timer
+ if ( this.timeoutID ) {
+ window.clearTimeout( this.timeoutID );
+ }
+ // Check response cache
+ if ( this.nameToCheck in this.responseCache ) {
+ this.setWarning(this.responseCache[this.nameToCheck]);
+ return;
+ }
+
+ this.setInnerHTML(warningElt, '..'); // TODO: pretty animated GIF
+ this.timeoutID = window.setTimeout( 'wgUploadWarningObj.timeout()', this.delay );
+ },
+
+ 'checkNow': function (fname) {
+ if ( this.timeoutID ) {
+ window.clearTimeout( this.timeoutID );
+ }
+ this.nameToCheck = fname;
+ this.timeout();
+ },
+
+ 'timeout' : function() {
+ var warningElt = document.getElementById( 'wpDestFile-warning' );
+ this.setInnerHTML(warningElt, '....'); // TODO: pretty animated GIF
+
+ // Get variables into local scope so that they will be preserved for the
+ // anonymous callback. fileName is copied so that multiple overlapping
+ // ajax requests can be supported.
+ var obj = this;
+ var fileName = this.nameToCheck;
+ sajax_do_call( 'UploadForm::ajaxGetExistsWarning', [this.nameToCheck],
+ function (result) {
+ obj.processResult(result, fileName)
+ }
+ );
+ },
+
+ 'processResult' : function (result, fileName) {
+ this.setWarning(result.responseText);
+ this.responseCache[fileName] = result.responseText;
+ },
+
+ 'setWarning' : function (warning) {
+ var warningElt = document.getElementById( 'wpDestFile-warning' );
+ var ackElt = document.getElementById( 'wpDestFileWarningAck' );
+ this.setInnerHTML(warningElt, warning);
+
+ // Set a value in the form indicating that the warning is acknowledged and
+ // doesn't need to be redisplayed post-upload
+ if ( warning == '' || warning == ' ' ) {
+ ackElt.value = '';
+ } else {
+ ackElt.value = '1';
+ }
+ },
+
+ 'setInnerHTML' : function (element, text) {
+ // Check for no change to avoid flicker in IE 7
+ if (element.innerHTML != text) {
+ element.innerHTML = text;
+ }
+ }
+}
+
+function fillDestFilename(id) {
+ if (!document.getElementById) {
+ return;
+ }
+ var path = document.getElementById(id).value;
+ // Find trailing part
+ var slash = path.lastIndexOf('/');
+ var backslash = path.lastIndexOf('\\');
+ var fname;
+ if (slash == -1 && backslash == -1) {
+ fname = path;
+ } else if (slash > backslash) {
+ fname = path.substring(slash+1, 10000);
+ } else {
+ fname = path.substring(backslash+1, 10000);
+ }
+
+ // Capitalise first letter and replace spaces by underscores
+ fname = fname.charAt(0).toUpperCase().concat(fname.substring(1,10000)).replace(/ /g, '_');
+
+ // Output result
+ var destFile = document.getElementById('wpDestFile');
+ if (destFile) {
+ destFile.value = fname;
+ if ( wgAjaxUploadDestCheck ) {
+ wgUploadWarningObj.checkNow(fname) ;
}
}
}