function upload() { $details = null; $this->content = 'error'; $up = new UploadFromFile(); $up->initializeFromRequest($this->wg->request); $permErrors = $up->verifyPermissions($this->wg->user); if ($permErrors !== true) { $this->status = self::UPLOAD_PERMISSION_ERROR; $this->statusMessage = $this->uploadMessage($this->status, null); } else { if (empty($this->wg->EnableUploads)) { // BugId:6122 $this->statusMessage = wfMsg('uploaddisabled'); } else { $details = $up->verifyUpload(); $this->status = is_array($details) ? $details['status'] : UploadBase::UPLOAD_VERIFICATION_ERROR; $this->statusMessage = ''; if ($this->status > 0) { $this->statusMessage = $this->uploadMessage($this->status, $details); } else { $titleText = $this->request->getVal('title'); $sectionNumber = $this->request->getVal('section', 0); $this->status = $up->performUpload('', '', '', $this->wg->user); $mainArticle = new Article(Title::newFromText($titleText)); if ($sectionNumber == 0) { $mainArticle->updateArticle($this->getWikiText($up->getTitle()->getText(), self::LEFT) . $mainArticle->getRawText(), '', false, false); } else { $firstSectionText = $mainArticle->getSection($mainArticle->getRawText(), $sectionNumber); $matches = array(); if (preg_match('/={2,3}[^=]+={2,3}/', $firstSectionText, $matches)) { $firstSectionText = trim(str_replace($matches[0], '', $firstSectionText)); $newSectionText = $mainArticle->replaceSection($sectionNumber, $matches[0] . "\n" . $this->getWikiText($up->getTitle()->getText(), self::LEFT) . $firstSectionText); $mainArticle->updateArticle($newSectionText, '', false, false); } } $this->content = $this->renderImage($up->getTitle()->getText(), self::LEFT); } } } }
/** * @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; } }
/** * This method hacks the normal nirvana dispatcher chain because of AIM and application/json mimetype incompatibility * Talk to Hyun or Inez */ public function executeUpload($params) { wfProfileIn(__METHOD__); global $wgRequest, $wgUser; if(!$wgUser->isLoggedIn()) { echo 'Not logged in'; exit(); } $this->watchthis = $wgRequest->getBool('wpWatchthis') && $wgUser->isLoggedIn(); $this->license = $wgRequest->getText('wpLicense'); $this->copyrightstatus = $wgRequest->getText('wpUploadCopyStatus'); $this->copyrightsource = $wgRequest->getText('wpUploadSource'); $this->ignorewarning = $wgRequest->getCheck('wpIgnoreWarning'); $this->overwritefile = $wgRequest->getCheck('wpDestFileWarningAck'); $this->defaultcaption = $wgRequest->getText('wpUploadDescription'); $details = null; $up = new UploadFromFile(); $up->initializeFromRequest($wgRequest); $permErrors = $up->verifyPermissions($wgUser); if ( $permErrors !== true ) { $this->status = self::UPLOAD_PERMISSION_ERROR; $this->statusMessage = $this->uploadMessage( $this->status, null ); } else if (empty($this->wg->EnableUploads)) { // BugId:6122 $this->statusMessage = wfMsg('uploaddisabled'); } else { $details = $up->verifyUpload(); $this->status = (is_array($details) ? $details['status'] : UploadBase::UPLOAD_VERIFICATION_ERROR); $this->statusMessage = ''; if ($this->status > 0) { $this->statusMessage = $this->uploadMessage($this->status, $details); } else { $warnings = array(); if(!$this->ignorewarning) { $warnings = $up->checkWarnings(); // BugId:3325 - add handling for "Overwrite File" checkbox if ($this->overwritefile && !empty($warnings['exists'])) { unset($warnings['exists']); } if(!empty($warnings)) { $this->status = self::UPLOAD_WARNING; $this->statusMessage .= $this->uploadWarning($warnings); } } if(empty($warnings)) { $pageText = SpecialUpload::getInitialPageText( $this->defaultcaption, $this->license, $this->copyrightstatus, $this->copyrightsource ); $status = $up->performUpload( $this->defaultcaption, $pageText, $this->watchthis, $wgUser ); if ($status->isGood()) { $aPageProps = array ( 'default_caption' => $this->defaultcaption ); Wikia::setProps( $up->getTitle()->getArticleID(), $aPageProps ); } else { $this->statusMessage .= "something is wrong with upload"; } } } } echo json_encode($this->getResponse()->getData()); header('content-type: text/plain; charset=utf-8'); wfProfileOut(__METHOD__); exit(); //end hack }