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; }
} #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! echo '<response>'; foreach ($return as $key => $value) { echo "<{$key}><![CDATA[{$value}]]></{$key}>"; } echo '</response>'; } else { // header('Content-type: application/json');