r45852 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r45851‎ | r45852 | r45853 >
Date:22:53, 17 January 2009
Author:btongminh
Status:deferred
Tags:
Comment:
Add a preliminary version of the ApiUpload module. This code is untested!
Modified paths:
  • /branches/new-upload/phase3/includes/AutoLoader.php (modified) (history)
  • /branches/new-upload/phase3/includes/api/ApiQueryImageInfo.php (modified) (history)
  • /branches/new-upload/phase3/includes/api/ApiUpload.php (added) (history)

Diff [purge]

Index: branches/new-upload/phase3/includes/api/ApiQueryImageInfo.php
@@ -155,12 +155,8 @@
156156 return $vals;
157157 }
158158
159 - public function getAllowedParams() {
 159+ public static function allProps() {
160160 return array (
161 - 'prop' => array (
162 - ApiBase :: PARAM_ISMULTI => true,
163 - ApiBase :: PARAM_DFLT => 'timestamp|user',
164 - ApiBase :: PARAM_TYPE => array (
165161 'timestamp',
166162 'user',
167163 'comment',
@@ -171,7 +167,14 @@
172168 'metadata',
173169 'archivename',
174170 'bitdepth',
175 - )
 171+ );
 172+ }
 173+ public function getAllowedParams() {
 174+ return array (
 175+ 'prop' => array (
 176+ ApiBase :: PARAM_ISMULTI => true,
 177+ ApiBase :: PARAM_DFLT => 'timestamp|user',
 178+ ApiBase :: PARAM_TYPE => self::allProps()
176179 ),
177180 'limit' => array(
178181 ApiBase :: PARAM_TYPE => 'limit',
Index: branches/new-upload/phase3/includes/api/ApiUpload.php
@@ -0,0 +1,243 @@
 2+<?php
 3+
 4+/*
 5+ * Created on Aug 21, 2008
 6+ * API for MediaWiki 1.8+
 7+ *
 8+ * Copyright (C) 2008 - 2009 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
 9+ *
 10+ * This program is free software; you can redistribute it and/or modify
 11+ * it under the terms of the GNU General Public License as published by
 12+ * the Free Software Foundation; either version 2 of the License, or
 13+ * (at your option) any later version.
 14+ *
 15+ * This program is distributed in the hope that it will be useful,
 16+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 18+ * GNU General Public License for more details.
 19+ *
 20+ * You should have received a copy of the GNU General Public License along
 21+ * with this program; if not, write to the Free Software Foundation, Inc.,
 22+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 23+ * http://www.gnu.org/copyleft/gpl.html
 24+ */
 25+
 26+if (!defined('MEDIAWIKI')) {
 27+ // Eclipse helper - will be ignored in production
 28+ require_once ("ApiBase.php");
 29+}
 30+
 31+
 32+/**
 33+ * @ingroup API
 34+ */
 35+class ApiUpload extends ApiBase {
 36+
 37+ public function __construct($main, $action) {
 38+ parent :: __construct($main, $action);
 39+ }
 40+
 41+ public function execute() {
 42+ global $wgUser;
 43+ $this->getMain()->requestWriteMode();
 44+ $this->mParams = $this->extractRequestParams();
 45+ // Add the uploaded file to the params array
 46+ $this->mParams['file'] = $this->getMain()->getRequest()->getFileName('file');
 47+
 48+ // Check whether upload is enabled
 49+ if( !UploadFromBase::isEnabled() )
 50+ $this->dieUsageMsg( array( 'uploaddisabled' ) );
 51+
 52+ // One and only one of the following parameters is needed
 53+ $this->requireOnlyOneParameter( $this->mParams,
 54+ 'sessionkey', 'file', 'url' );
 55+
 56+ if( $this->mParams['sessionkey'] ) {
 57+ // Stashed upload
 58+
 59+ $this->mUpload = new UploadFromStash();
 60+ $this->mUpload->initialize( $this->mParams['sessionkey'] );
 61+ } else {
 62+ // Upload from url or file
 63+
 64+ // Parameter filename is required
 65+ if( !isset( $this->mParams['filename'] ) )
 66+ $this->dieUsageMsg( array( 'missingparam', 'filename' ) );
 67+ // Parameter comment defaults to ''
 68+ if( !isset( $this->mParams['comment'] ) )
 69+ $this->mParams['comment'] = '';
 70+
 71+ // Initialize $this->mUpload
 72+ if( isset( $this->mParams['file'] ) ) {
 73+ $request = $this->getMain()->getRequest();
 74+ $this->mUpload = new UploadFromUpload();
 75+ $this->mUpload->initialize(
 76+ $request->getFileTempName( 'file' ),
 77+ $request->getFileSize( 'file' ),
 78+ $request->getFileName( 'file' )
 79+ );
 80+ } elseif( isset( $this->mParams['url'] ) ) {
 81+ $this->mUpload = new UploadFromUrl();
 82+ $this->mUpload->initialize( $this->mParams['filename'], $this->mParams['url'] );
 83+ }
 84+ }
 85+
 86+ // Check whether the user has the appropriate permissions to upload anyway
 87+ $permission = $this->mUpload->isAllowed( $wgUser );
 88+ if( $permission !== true ) {
 89+ if( !$wgUser->isLoggedIn() )
 90+ $this->dieUsageMsg( array( 'mustbeloggedin', 'upload' ) );
 91+ else
 92+ $this->dieUsageMsg( array( 'badaccess-groups' ) );
 93+ }
 94+
 95+ // Perform the upload
 96+ $result = $this->performUpload();
 97+
 98+ // Cleanup any temporary mess
 99+ $this->mUpload->cleanupTempFile();
 100+
 101+ $this->getResult()->addValue(null, $this->getModuleName(), $result);
 102+ }
 103+
 104+ private function performUpload() {
 105+ global $wgUser;
 106+ $result = array();
 107+ $resultDetails = null;
 108+
 109+ $permErrors = $this->mUpload->verifyPermissions( $wgUser );
 110+ if( $permErrors !== true ) {
 111+ $result['result'] = 'Failure';
 112+ $result['error'] = 'permission-denied';
 113+ return $result;
 114+ }
 115+
 116+ $verification = $this->mUpload->verifyUpload( $resultDetails );
 117+ if( $verification != UploadFromBase::OK ) {
 118+ $result['result'] = 'Failure';
 119+ switch( $verification ) {
 120+ case UploadFromBase::EMPTY_FILE:
 121+ $result['error'] = 'empty-file';
 122+ break;
 123+ case UploadFromBase::FILETYPE_MISSING:
 124+ $result['error'] = 'filetype-missing';
 125+ break;
 126+ case UploadFromBase::FILETYPE_BADTYPE:
 127+ global $wgFileExtensions;
 128+ $result['error'] = 'filetype-banned';
 129+ $result['filetype'] = $resultDetails['finalExt'];
 130+ $result['allowed-filetypes'] = $wgFileExtensions;
 131+ break;
 132+ case UploadFromBase::MIN_LENGHT_PARTNAME:
 133+ $result['error'] = 'filename-tooshort';
 134+ break;
 135+ case UploadFromBase::ILLEGAL_FILENAME:
 136+ $result['error'] = 'illegal-filename';
 137+ $result['filename'] = $resultDetails['filtered'];
 138+ break;
 139+ case UploadFromBase::OVERWRITE_EXISTING_FILE:
 140+ $result['error'] = 'overwrite';
 141+ break;
 142+ case UploadFromBase::VERIFICATION_ERROR:
 143+ $result['error'] = 'verification-error';
 144+ $args = $resultDetails['veri'];
 145+ $code = array_shift($args);
 146+ $result['verification-error'] = $code;
 147+ $result['args'] = $args;
 148+ $this->getResult()->setIndexedTagName( $result['args'], 'arg' );
 149+ break;
 150+ case UploadFromBase::UPLOAD_VERIFICATION_ERROR:
 151+ $result['error'] = 'upload-verification-error';
 152+ $result['upload-verification-error'] = $resultDetails['error'];
 153+ break;
 154+ default:
 155+ $result['error'] = 'unknown-error';
 156+ $result['code'] = $verification;
 157+ break;
 158+ }
 159+ return $result;
 160+ }
 161+
 162+ if( !$this->mParams['ignorewarnings'] ) {
 163+ $warnings = $this->mUpload->checkWarnings();
 164+ if( $warnings ) {
 165+ $this->getResult()->setIndexedTagName( $warnings, 'warning' );
 166+
 167+ $result['result'] = 'Warning';
 168+ $result['warnings'] = $warnings;
 169+ if( isset( $result['filewasdeleted'] ) )
 170+ $result['filewasdeleted'] = $result['filewasdeleted']->getDBkey();
 171+
 172+ $sessionKey = $this->mUpload->stashSession();
 173+ if( $sessionKey )
 174+ $result['sessionkey'] = $sessionKey;
 175+ return $result;
 176+ }
 177+ }
 178+
 179+ $status = $this->mUpload->performUpload( $this->mParams['comment'],
 180+ $this->mParams['comment'], $this->mParams['watch'], $wgUser);
 181+
 182+ if( !$status->isGood() ) {
 183+ $result['result'] = 'Failure';
 184+ $result['error'] = 'internal-error';
 185+ $result['details'] = $status->getErrorsArray();
 186+ $this->getResult()->setIndexedTagName( $result['details'], 'error' );
 187+ return $result;
 188+ }
 189+
 190+ $file = $this->mUpload->getLocalFile();
 191+ $result['result'] = 'Success';
 192+ $result['filename'] = $file->getName();
 193+
 194+ // Append imageinfo to the result
 195+ $result['imageinfo'] = ApiQueryImageInfo::getInfo( $file,
 196+ array_flip( ApiQueryImageInfo::allProps() ),
 197+ $this->getResult() );
 198+
 199+ return $result;
 200+ }
 201+
 202+ public function mustBePosted() { return true; }
 203+
 204+ public function getAllowedParams() {
 205+ return array (
 206+ 'filename' => null,
 207+ 'file' => null,
 208+ 'url' => null,
 209+ 'comment' => null,
 210+ 'watch' => false,
 211+ 'ignorewarnings' => false,
 212+ 'sessionkey' => null,
 213+ );
 214+ }
 215+
 216+ public function getParamDescription() {
 217+ return array (
 218+ 'filename' => 'Target filename',
 219+ 'file' => 'File contents',
 220+ 'url' => 'Url to upload from',
 221+ 'comment' => 'Upload comment or initial page text',
 222+ 'watch' => 'Watch the page',
 223+ 'ignorewarnings' => 'Ignore any warnings',
 224+ 'sessionkey' => 'Session key in case there were any warnings'
 225+ );
 226+ }
 227+
 228+ public function getDescription() {
 229+ return array(
 230+ 'Upload an image'
 231+ );
 232+ }
 233+
 234+ protected function getExamples() {
 235+ return array (
 236+ 'api.php?action=upload&filename=Wiki.png&url=http%3A//upload.wikimedia.org/wikipedia/en/b/bc/Wiki.png&ignorewarnings'
 237+ );
 238+ }
 239+
 240+ public function getVersion() {
 241+ return __CLASS__ . ': $Id: APiUpload.php 35619 2008-05-30 19:59:47Z btongminh $';
 242+ }
 243+}
 244+
Property changes on: branches/new-upload/phase3/includes/api/ApiUpload.php
___________________________________________________________________
Name: svn:eol-style
1245 + native
Index: branches/new-upload/phase3/includes/AutoLoader.php
@@ -286,6 +286,7 @@
287287 'ApiRollback' => 'includes/api/ApiRollback.php',
288288 'ApiUnblock' => 'includes/api/ApiUnblock.php',
289289 'ApiUndelete' => 'includes/api/ApiUndelete.php',
 290+ 'ApiUpload' => 'includes/api/ApiUpload.php',
290291 'ApiWatch' => 'includes/api/ApiWatch.php',
291292 'Services_JSON' => 'includes/api/ApiFormatJson_json.php',
292293 'Services_JSON_Error' => 'includes/api/ApiFormatJson_json.php',

Status & tagging log