protected function uploadImage() { global $wgImageMagickConvertCommand, $wgServer; $request = $this->getRequest(); $result = array(); $fromPage = $request->getVal('viapage'); // sanity check on the page to link to $title = Title::newFromText($fromPage, NS_MAIN); if (!$title->exists()) { $result['error'] = "Error: No article {$fromPage} found to link image."; return $result; } // try to get a unique file name by appending a suffix and the current time to the save name here $dateTime = new DateTime(); $webUpload = $request->getUpload('wpUploadImage'); $info = new SplFileInfo($webUpload->getName()); $ext = $info->getExtension(); $info = new SplFileInfo($request->getVal("name")); for ($i = 0; $i < 100; $i++) { $saveName = "User Completed Image {$fromPage} {$dateTime->format('Y.m.d H.i.s')}.{$i}.{$ext}"; $title = Title::newFromText($saveName, NS_IMAGE); if (!$title->getArticleID()) { break; } } // if the title still exists, show an error if ($title->getArticleID()) { $result['error'] = 'file with this name already exists'; return $result; } $upload = new UploadFromFile(); $upload->initialize($saveName, $webUpload); $verification = $upload->verifyUpload(); if ($verification['status'] !== UploadBase::OK) { $result['error'] = "verification error: " . $verification['status']; return $result; } $warnings = $upload->checkWarnings(); if ($warnings) { $result['warnings'] = $warnings; // todo this should be toggled on off for testings perhaps // since it might get kind of annoying if ($warnings['duplicate']) { $result['debug'][] = $warnings['duplicate-archive']; $result['error'] = "this file was already uploaded"; return $result; } } $comment = '{{Self}}'; $status = $upload->performUpload($comment, '', true, $this->getUser()); if (!$status->isGood()) { $error = $status->getErrorsArray(); $result['error'] = 'perform upload error: ' . $error; return $result; } $upload->cleanupTempFile(); // todo - do this part after the single file upload // Image orientation is a bit wonky on some mobile devices; use ImageMagick's auto-orient to try fixing it. //$tempFilePath = $temp_file->getPath(); //$cmd = $wgImageMagickConvertCommand . ' ' . $tempFilePath . ' -auto-orient ' . $tempFilePath; //exec($cmd); $file = $upload->getLocalFile(); $thumb = $file->getThumbnail(200, -1, true, true); if (!$thumb) { $result['error'] = 'file thumbnail does not exist'; $file->delete(''); return $result; } $fileTitle = $file->getTitle(); $result['titleText'] = $fileTitle->getText(); $result['titleDBkey'] = substr($fileTitle->getDBkey(), 21); // Only keep important info $result['titlePreText'] = '/' . $fileTitle->getPrefixedText(); $result['titleArtID'] = $fileTitle->getArticleID(); $result['timestamp'] = wfTimestamp(TS_MW); $result['fromPage'] = $request->getVal('viapage'); $result['thumbURL'] = $thumb->getUrl(); $result['fileURL'] = $wgServer . $file->getUrl(); $this->addToDB($result); return $result; }
/** * @static * @return AjaxResponse * @throws MWException */ public static function uploadImage() { global $wgRequest, $wgUser, $wgFileExtensions, $wgLang; if ($wgRequest->wasPosted()) { $ret = array(); $upload = new UploadFromFile(); $upload->initializeFromRequest($wgRequest); $permErrors = $upload->verifyPermissions($wgUser); if ($permErrors !== true) { $ret['error'] = true; $ret['message'] = wfMsg('badaccess'); } else { $details = $upload->verifyUpload(); if ($details['status'] != UploadBase::OK) { $ret['error'] = true; switch ($details['status']) { /** Statuses that only require name changing **/ case UploadBase::MIN_LENGTH_PARTNAME: $ret['message'] = wfMsgHtml('minlength1'); break; case UploadBase::ILLEGAL_FILENAME: $ret['message'] = wfMsgExt('illegalfilename', 'parseinline', $details['filtered']); break; case UploadBase::OVERWRITE_EXISTING_FILE: $ret['message'] = wfMsgExt($details['overwrite'], 'parseinline'); break; case UploadBase::FILETYPE_MISSING: $ret['message'] = wfMsgExt('filetype-missing', 'parseinline'); break; case UploadBase::EMPTY_FILE: $ret['message'] = wfMsgHtml('emptyfile'); break; case UploadBase::FILETYPE_BADTYPE: $finalExt = $details['finalExt']; $ret['message'] = wfMsgExt('filetype-banned-type', array('parseinline'), htmlspecialchars($finalExt), implode(wfMsgExt('comma-separator', array('escapenoentities')), $wgFileExtensions), $wgLang->formatNum(count($wgFileExtensions))); break; case UploadBase::VERIFICATION_ERROR: unset($details['status']); $code = array_shift($details['details']); $ret['message'] = wfMsgExt($code, 'parseinline', $details['details']); break; case UploadBase::HOOK_ABORTED: if (is_array($details['error'])) { # allow hooks to return error details in an array $args = $details['error']; $error = array_shift($args); } else { $error = $details['error']; $args = null; } $ret['message'] = wfMsgExt($error, 'parseinline', $args); break; default: throw new MWException(__METHOD__ . ": Unknown value `{$details['status']}`"); } } else { $warnings = $upload->checkWarnings(); if ($warnings) { if (!empty($warnings['exists']) || !empty($warnings['duplicate']) || !empty($warnings['duplicate-archive'])) { $ret['conflict'] = true; $ret['message'] = wfMsg('toplists-error-image-already-exists'); } else { $ret['error'] = true; $ret['message'] = ''; foreach ($warnings as $warning => $args) { if ($args === true) { $args = array(); } elseif (!is_array($args)) { $args = array($args); } $ret['message'] .= wfMsgExt($warning, 'parseinline', $args) . "/n"; } } } else { $status = $upload->performUpload('/* comment */', '/* page text */', false, $wgUser); if (!$status->isGood()) { $ret['error'] = true; $ret['message'] = wfMsg('toplists-upload-error-unknown'); } else { $ret['success'] = true; $file = $upload->getLocalFile(); if (!empty($file)) { $thumb = $file->transform(array('width' => 120), 0); $pictureName = $upload->getTitle()->getText(); $pictureUrl = $thumb->getUrl(); } $ret['name'] = $pictureName; $ret['url'] = $pictureUrl; } } } } $response = new AjaxResponse('<script type="text/javascript">window.document.responseContent = ' . json_encode($ret) . ';</script>'); $response->setContentType('text/html; charset=utf-8'); return $response; } }
} else { $error = "badaccess-groups"; } } // Use comment as initial page text by default if (is_null($text)) { $text = $comment; } #wfFiUploadComplete($mUpload); // No errors, no warnings: do the upload $status = $mUpload->performUpload($comment, $text, $watch, $wgUser); if (!$status->isGood()) { $error = $status->getErrorsArray(); $error = "An internal error occurred"; } $file = $mUpload->getLocalFile(); #$result['imageinfo'] = $mUpload->getImageInfo( $this->getResult() ); #$path_parts = pathinfo($file->getName()); #$extension = $path_parts['extension']; #if (in_array($extension, $wgPictureExt)){ #$extension = "pic"; #} $mUpload->cleanupTempFile(); if ($error) { $return = array('status' => '0', 'error' => $error); } else { $return = array('status' => '1', 'name' => $file->getName(), 'fi' => $sCreateIndexFilepath); } if (isset($_REQUEST['response']) && $_REQUEST['response'] == 'xml') { // header('Content-type: text/xml'); // Really dirty, use DOM and CDATA section!