/** * Insert an image upload into the mediawiki database tables. If the * image insert was successful, a page showing the wiki text for their * image is shown. Otherwise, if the image file name already exists in * the database, a conflict page is returned to the user. * * @param $type string with either 'overwrite' or blank -- specifies * whether to force-overwrite an existing image * @param $name filename chosen by user for uploaded image * @param $mwname filename of the file in mediawiki DB * @param $fromIIA true iff source of call is Special:IntroImageAdder * @param $image_comment a comment attached to the image upload (only * used if $fromIIA == true) * @return outputs either a wikitext results page (if image filename * didn't exist or force overwrite was selected) or a conflict page. * Returns an error string or empty string if no error. */ private function insertImage($type, $name, $mwname, $fromIIA = false, $image_comment = '') { global $wgRequest, $wgUser, $wgOut, $wgFileExtensions; if (!$fromIIA) { $license = $wgRequest->getVal('wpLicense', ''); if (!empty($license)) { $attrib = $wgRequest->getVal('attribution'); $comment = '{{' . $license . (!empty($attrib) ? '|' . $attrib : '') . '}}'; if ($license != '') { $wgUser->setOption('image_license', $license); $wgUser->saveSettings(); } } else { $comment = $wgRequest->getVal('ImageAttribution', ''); } } else { $comment = $image_comment; } if (wfReadOnly()) { return wfMsg('eiu-readonly'); } if (!empty($mwname) && !empty($name)) { $name = urldecode($name); $name = preg_replace('/[^' . Title::legalChars() . ']|[:\\/\\\\]|\\?/', '-', $name); $name = preg_replace('@&@', '&', $name); $name = trim($name); // did they give no extension at all when they changed the name? list($first, $ext) = self::splitFilenameExt($name); $ext = strtolower($ext); $title = Title::makeTitleSafe(NS_IMAGE, $name); if (is_null($title) || !in_array($ext, $wgFileExtensions)) { return wfMsg('eiu-filetype-incorrect'); } $newFile = true; $titleExists = $title->exists(); if (!$titleExists || $fromIIA) { // // DB entry for file doesn't exist. User renamed their // upload or it never existed. // if ($titleExists) { $suggestedName = self::generateNewFilename($name); $title = Title::makeTitleSafe(NS_IMAGE, $suggestedName); } // is the target protected? $permErrors = $title->getUserPermissionsErrors('edit', $wgUser); $permErrorsUpload = $title->getUserPermissionsErrors('upload', $wgUser); if ($permErrors || $permErrorsUpload) { return wfMsg('This image is protected'); } $temp_file = new LocalFile(Title::newFromText($mwname, NS_IMAGE), RepoGroup::singleton()->getLocalRepo()); $file = new LocalFile($title, RepoGroup::singleton()->getLocalRepo()); $file->upload($temp_file->getPath(), $comment, $comment); $temp_file->delete(''); } elseif ($type == 'overwrite') { // // DB entry exists and user selected to overwrite it // $title = Title::newFromText($name, NS_IMAGE); // is the target protected? $permErrors = $title->getUserPermissionsErrors('edit', $wgUser); $permErrorsUpload = $title->getUserPermissionsErrors('upload', $wgUser); $permErrorsCreate = $title->exists() ? array() : $title->getUserPermissionsErrors('create', $wgUser); if ($permErrors || $permErrorsUpload || $permErrorsCreate) { return wfMsg('This image is protected'); } $file_name = new LocalFile($title, RepoGroup::singleton()->getLocalRepo()); $file_mwname = new TempLocalFile(Title::newFromText($mwname, NS_IMAGE), RepoGroup::singleton()->getLocalRepo()); $file_name->upload($file_mwname->getPath(), $comment, $comment); $file_mwname->delete(''); $newFile = false; } elseif ($type == 'existing') { // // DB entry exists and user doesn't want to overwrite or // rename, so they use the existing file from the DB. // $title = Title::newFromText($name, NS_IMAGE); } else { // // There was a conflict with an existing file in the // DB. Title exists and overwrite action not taken yet. // $data = array('wpUpload' => 1, 'wpSourceType' => 'web', 'wpUploadFileURL' => ''); $form = new UploadForm(new FauxRequest($data, true)); // generate title if current one is taken $suggestedName = self::generateNewFilename($name); // extensions check list($first, $ext) = self::splitFilenameExt($suggestedName); $title = Title::newFromText($name, NS_IMAGE); $file = wfFindFile($title); $vars = array('suggestedFirstPart' => $first, 'extension' => strtolower($ext), 'name' => $name, 'mwname' => $mwname, 'file' => $file, 'image_comment' => $comment); $wgOut->setStatusCode(200); $wgOut->addHTML(EasyTemplate::html('eiu_conflict.tmpl.php', $vars)); // return no error return ''; } // add watch to file is user needs it if ($wgUser->getOption('watchdefault') || $newFile && $wgUser->getOption('watchcreations')) { $wgUser->addWatch($title); } $db =& wfGetDB(DB_MASTER); $db->commit(); } elseif (empty($mwname)) { $title = Title::makeTitleSafe(NS_IMAGE, $name); } elseif ($name !== null) { return WfMsg('eiu-warn3'); } else { // name === null $title = Title::newFromText($mwname, NS_IMAGE); } $file = wfFindFile($title); if (!is_object($file)) { return wfMsg('File not found'); } $details = self::splitValuePairs($wgRequest->getVal('image-details')); $tag = self::makeImageWikiTag($title, $file, $details); $vars = array('tag' => $tag, 'file' => $file, 'width' => $details['chosen-width'], 'height' => $details['chosen-height'], 'imageFilename' => $title->getText()); if (!$fromIIA) { $vars['details'] = $details; $html = EasyTemplate::html('eiu_upload_summary.tmpl.php', $vars); } else { $html = IntroImageAdder::addIntroImage($vars); } $wgOut->setStatusCode(200); $wgOut->addHTML($html); // return no error return ''; }
public function getCount(&$dbr) { return IntroImageAdder::getArticleCount($dbr); }