r16158 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r16157‎ | r16158 | r16159 >
Date:10:08, 22 August 2006
Author:magnusmanske
Status:old
Tags:
Comment:
Option: Allow upload from URL. Set $wgAllowCopyUploads = true ;
Modified paths:
  • /trunk/phase3/RELEASE-NOTES (modified) (history)
  • /trunk/phase3/includes/DefaultSettings.php (modified) (history)
  • /trunk/phase3/includes/SpecialUpload.php (modified) (history)
  • /trunk/phase3/languages/MessagesEn.php (modified) (history)

Diff [purge]

Index: trunk/phase3/includes/SpecialUpload.php
@@ -30,7 +30,7 @@
3131 var $mUploadFile, $mUploadDescription, $mLicense ,$mIgnoreWarning, $mUploadError;
3232 var $mUploadSaveName, $mUploadTempName, $mUploadSize, $mUploadOldVersion;
3333 var $mUploadCopyStatus, $mUploadSource, $mReUpload, $mAction, $mUpload;
34 - var $mOname, $mSessionKey, $mStashed, $mDestFile, $mRemoveTempFile;
 34+ var $mOname, $mSessionKey, $mStashed, $mDestFile, $mRemoveTempFile, $mSourceType;
3535 /**#@-*/
3636
3737 /**
@@ -39,6 +39,7 @@
4040 * @param $request Data posted.
4141 */
4242 function UploadForm( &$request ) {
 43+ global $wgAllowCopyUploads;
4344 $this->mDestFile = $request->getText( 'wpDestFile' );
4445
4546 if( !$request->wasPosted() ) {
@@ -55,6 +56,7 @@
5657 $this->mUploadCopyStatus = $request->getText( 'wpUploadCopyStatus' );
5758 $this->mUploadSource = $request->getText( 'wpUploadSource' );
5859 $this->mWatchthis = $request->getBool( 'wpWatchthis' );
 60+ $this->mSourceType = $request->getBool( 'wpSourceType' );
5961 wfDebug( "UploadForm: watchthis is: '$this->mWatchthis'\n" );
6062
6163 $this->mAction = $request->getVal( 'action' );
@@ -79,17 +81,50 @@
8082 /**
8183 *Check for a newly uploaded file.
8284 */
83 - $this->mUploadTempName = $request->getFileTempName( 'wpUploadFile' );
84 - $this->mUploadSize = $request->getFileSize( 'wpUploadFile' );
85 - $this->mOname = $request->getFileName( 'wpUploadFile' );
86 - $this->mUploadError = $request->getUploadError( 'wpUploadFile' );
87 - $this->mSessionKey = false;
88 - $this->mStashed = false;
89 - $this->mRemoveTempFile = false; // PHP will handle this
 85+ if( $wgAllowCopyUploads && $this->mSourceType == 'web' ) {
 86+ $this->initialize_web_file( $request );
 87+ } else {
 88+ $this->initialize_php_file( $request );
 89+ }
9090 }
9191 }
9292
9393 /**
 94+ * Initialize the uploaded file from PHP data
 95+ * @access private
 96+ */
 97+ function initialize_php_file( &$request ) {
 98+ $this->mUploadTempName = $request->getFileTempName( 'wpUploadFile' );
 99+ $this->mUploadSize = $request->getFileSize( 'wpUploadFile' );
 100+ $this->mOname = $request->getFileName( 'wpUploadFile' );
 101+ $this->mUploadError = $request->getUploadError( 'wpUploadFile' );
 102+ $this->mSessionKey = false;
 103+ $this->mStashed = false;
 104+ $this->mRemoveTempFile = false; // PHP will handle this
 105+ }
 106+
 107+ /**
 108+ * Copy a web file to a temporary file
 109+ * @access private
 110+ */
 111+ function initialize_web_file( &$request ) {
 112+ global $wgTmpDirectory;
 113+ $url = $request->getText( 'wpUploadFile' );
 114+ $local_file = tempnam( $wgTmpDirectory, 'WEBUPLOAD' );
 115+
 116+ # Maybe check for filesize($url) first?
 117+ $error = !@copy( $url, $local_file );
 118+
 119+ $this->mUploadTempName = $local_file;
 120+ $this->mUploadSize = filesize( $local_file );
 121+ $this->mOname = array_pop( explode( '/', $url ) );
 122+ $this->mUploadError = $error;
 123+ $this->mSessionKey = false;
 124+ $this->mStashed = false;
 125+ $this->mRemoveTempFile = false; // PHP will *not* handle this
 126+ }
 127+
 128+ /**
94129 * Start doing stuff
95130 * @access public
96131 */
@@ -126,17 +161,17 @@
127162 }
128163
129164 /** Check if the image directory is writeable, this is a common mistake */
130 - if ( !is_writeable( $wgUploadDirectory ) ) {
 165+ if( !is_writeable( $wgUploadDirectory ) ) {
131166 $wgOut->addWikiText( wfMsg( 'upload_directory_read_only', $wgUploadDirectory ) );
132167 return;
133168 }
134169
135170 if( $this->mReUpload ) {
136 - if ( !$this->unsaveUploadedFile() ) {
 171+ if( !$this->unsaveUploadedFile() ) {
137172 return;
138173 }
139174 $this->mainUploadForm();
140 - } else if ( 'submit' == $this->mAction || $this->mUpload ) {
 175+ } else if( 'submit' == $this->mAction || $this->mUpload ) {
141176 $this->processUpload();
142177 } else {
143178 $this->mainUploadForm();
@@ -156,7 +191,7 @@
157192 global $wgUser, $wgOut;
158193
159194 /* Check for PHP error if any, requires php 4.2 or newer */
160 - if ( $this->mUploadError == 1/*UPLOAD_ERR_INI_SIZE*/ ) {
 195+ if( $this->mUploadError == 1/*UPLOAD_ERR_INI_SIZE*/ ) {
161196 $this->mainUploadForm( wfMsgHtml( 'largefileserver' ) );
162197 return;
163198 }
@@ -170,7 +205,7 @@
171206 }
172207
173208 # Chop off any directories in the given filename
174 - if ( $this->mDestFile ) {
 209+ if( $this->mDestFile ) {
175210 $basename = wfBaseName( $this->mDestFile );
176211 } else {
177212 $basename = wfBaseName( $this->mOname );
@@ -196,7 +231,7 @@
197232 $partname .= '.' . $ext[$i];
198233 }
199234
200 - if ( strlen( $partname ) < 3 ) {
 235+ if( strlen( $partname ) < 3 ) {
201236 $this->mainUploadForm( wfMsgHtml( 'minlength' ) );
202237 return;
203238 }
@@ -363,7 +398,9 @@
364399 * is a PHP-managed upload temporary
365400 */
366401 function saveUploadedFile( $saveName, $tempName, $useRename = false ) {
367 - global $wgOut;
 402+ global $wgOut, $wgAllowCopyUploads;
 403+
 404+ if ( !$useRename AND $wgAllowCopyUploads AND $this->mSourceType == 'web' ) $useRename = true;
368405
369406 $fname= "SpecialUpload::saveUploadedFile";
370407
@@ -586,6 +623,7 @@
587624 function mainUploadForm( $msg='' ) {
588625 global $wgOut, $wgUser;
589626 global $wgUseCopyrightUpload;
 627+ global $wgRequest, $wgAllowCopyUploads;
590628
591629 $cols = intval($wgUser->getOption( 'cols' ));
592630 $ew = $wgUser->getOption( 'editwidth' );
@@ -623,6 +661,14 @@
624662 $watchChecked = $wgUser->getOption( 'watchdefault' )
625663 ? 'checked="checked"'
626664 : '';
 665+
 666+ if ( $wgAllowCopyUploads AND $wgRequest->getText('source') == 'web' ) {
 667+ $sourcetype = 'text';
 668+ $source_comment = '<input type="hidden" name="wpSourceType" value="web"/>' . wfMsgHtml( 'upload_source_url' );
 669+ } else {
 670+ $sourcetype = 'file';
 671+ $source_comment = '';
 672+ }
627673
628674 $wgOut->addHTML( "
629675 <form id='upload' method='post' enctype='multipart/form-data' action=\"$action\">
@@ -630,7 +676,7 @@
631677 <tr>
632678 <td align='right'><label for='wpUploadFile'>{$sourcefilename}:</label></td>
633679 <td align='left'>
634 - <input tabindex='1' type='file' name='wpUploadFile' id='wpUploadFile' " . ($this->mDestFile?"":"onchange='fillDestFilename()' ") . "size='40' />
 680+ <input tabindex='1' type='{$sourcetype}' name='wpUploadFile' id='wpUploadFile' " . ($this->mDestFile?"":"onchange='fillDestFilename()' ") . "size='40' />{$source_comment}
635681 </td>
636682 </tr>
637683 <tr>
Index: trunk/phase3/includes/DefaultSettings.php
@@ -337,6 +337,8 @@
338338 $wgSharedUploadDBprefix = '';
339339 /** Cache shared metadata in memcached. Don't do this if the commons wiki is in a different memcached domain */
340340 $wgCacheSharedUploads = true;
 341+/** Allow for upload to be copied from an URL. Requires Special:Upload?source=web */
 342+$wgAllowCopyUploads = false;
341343
342344 /**
343345 * Point the upload navigation link to an external URL
Index: trunk/phase3/languages/MessagesEn.php
@@ -1149,6 +1149,7 @@
11501150 'license' => 'Licensing',
11511151 'nolicense' => 'None selected',
11521152 'licenses' => '-', # Don't duplicate this in translations
 1153+'upload_source_url' => ' (vaild, publicy accessible URL)',
11531154
11541155 # Image list
11551156 #
Index: trunk/phase3/RELEASE-NOTES
@@ -147,6 +147,7 @@
148148 * (bug 7044) Introduce "padleft" and "padright" colon functions
149149 * Pass page title as parameters to "linkshere" and "nolinkshere" and update
150150 default message text
 151+* Allows to upload from publicy accessible URL. Set $wgAllowCopyUploads = true ; in LocalSettings.php
151152
152153 == Languages updated ==
153154