Add "upload" type to API
[lhc/web/wiklou.git] / includes / api / ApiBase.php
index 4a6dad3..480404c 100644 (file)
@@ -468,6 +468,9 @@ abstract class ApiBase extends ContextSource {
                                                                        $desc .= $paramPrefix . $intRangeStr;
                                                                }
                                                                break;
+                                                       case 'upload':
+                                                               $desc .= $paramPrefix . "Must be posted as a file upload using multipart/form-data";
+                                                               break;
                                                }
                                        }
 
@@ -917,6 +920,29 @@ abstract class ApiBase extends ContextSource {
                        }
 
                        $value = $this->getMain()->getCheck( $encParamName );
+               } elseif ( $type == 'upload' ) {
+                       if ( isset( $default ) ) {
+                               // Having a default value is not allowed
+                               ApiBase::dieDebug( __METHOD__, "File upload param $encParamName's default is set to '$default'. File upload parameters may not have a default." );
+                       }
+                       if ( $multi ) {
+                               ApiBase::dieDebug( __METHOD__, "Multi-values not supported for $encParamName" );
+                       }
+                       $value = $this->getMain()->getUpload( $encParamName );
+                       if ( !$value->exists() ) {
+                               // This will get the value without trying to normalize it
+                               // (because trying to normalize a large binary file
+                               // accidentally uploaded as a field fails spectacularly)
+                               $value = $this->getMain()->getRequest()->unsetVal( $encParamName );
+                               if ( $value !== null ) {
+                                       $this->dieUsage(
+                                               "File upload param $encParamName is not a file upload; " .
+                                               "be sure to use multipart/form-data for your POST and include " .
+                                               "a filename in the Content-Disposition header.",
+                                               "badupload_{$encParamName}"
+                                       );
+                               }
+                       }
                } else {
                        $value = $this->getMain()->getVal( $encParamName, $default );
 
@@ -1013,6 +1039,8 @@ abstract class ApiBase extends ContextSource {
                                                        $value = $value[0];
                                                }
                                                break;
+                                       case 'upload': // nothing to do
+                                               break;
                                        default:
                                                ApiBase::dieDebug( __METHOD__, "Param $encParamName's type is unknown - $type" );
                                }