/** * Get an associative array containing information about * a file with the given storage path. * * Resulting array fields include: * - fileExists * - size (filesize in bytes) * - mime (as major/minor) * - media_type (value to be used with the MEDIATYPE_xxx constants) * - metadata (handler specific) * - sha1 (in base 36) * - width * - height * - bits (bitrate) * - file-mime * - major_mime * - minor_mime * * @param string $path Filesystem path to a file * @param string|bool $ext The file extension, or true to extract it from the filename. * Set it to false to ignore the extension. * @return array * @since 1.28 */ public function getPropsFromPath($path, $ext) { $fsFile = new FSFile($path); $info = $this->newPlaceholderProps(); $info['fileExists'] = $fsFile->exists(); if ($info['fileExists']) { $info['size'] = $fsFile->getSize(); // bytes $info['sha1'] = $fsFile->getSha1Base36(); # MIME type according to file contents $info['file-mime'] = $this->magic->guessMimeType($path, false); # Logical MIME type $ext = $ext === true ? FileBackend::extensionFromPath($path) : $ext; $info['mime'] = $this->magic->improveTypeFromExtension($info['file-mime'], $ext); list($info['major_mime'], $info['minor_mime']) = File::splitMime($info['mime']); $info['media_type'] = $this->magic->getMediaType($path, $info['mime']); # Height, width and metadata $handler = MediaHandler::getHandler($info['mime']); if ($handler) { $info['metadata'] = $handler->getMetadata($fsFile, $path); /** @noinspection PhpMethodParametersCountMismatchInspection */ $gis = $handler->getImageSize($fsFile, $path, $info['metadata']); if (is_array($gis)) { $info = $this->extractImageSizeInfo($gis) + $info; } } } return $info; }
function execute($par) { $mime = $par ? $par : $this->getRequest()->getText('mime'); $this->setHeaders(); $this->outputHeader(); $this->getOutput()->addHTML(Xml::openElement('form', array('id' => 'specialmimesearch', 'method' => 'get', 'action' => SpecialPage::getTitleFor('MIMEsearch')->getLocalUrl())) . Xml::openElement('fieldset') . Html::hidden('title', SpecialPage::getTitleFor('MIMEsearch')->getPrefixedText()) . Xml::element('legend', null, wfMsg('mimesearch')) . Xml::inputLabel(wfMsg('mimetype'), 'mime', 'mime', 20, $mime) . ' ' . Xml::submitButton(wfMsg('ilsubmit')) . Xml::closeElement('fieldset') . Xml::closeElement('form')); list($this->major, $this->minor) = File::splitMime($mime); if ($this->major == '' || $this->minor == '' || $this->minor == 'unknown' || !self::isValidType($this->major)) { return; } parent::execute($par); }
/** * @param ApiPageSet $resultPageSet * @return void */ private function run($resultPageSet = null) { $repo = $this->mRepo; if (!$repo instanceof LocalRepo) { $this->dieUsage('Local file repository does not support querying all images', 'unsupportedrepo'); } $prefix = $this->getModulePrefix(); $db = $this->getDB(); $params = $this->extractRequestParams(); // Table and return fields $this->addTables('image'); $prop = array_flip($params['prop']); $this->addFields(LocalFile::selectFields()); $ascendingOrder = true; if ($params['dir'] == 'descending' || $params['dir'] == 'older') { $ascendingOrder = false; } if ($params['sort'] == 'name') { // Check mutually exclusive params $disallowed = ['start', 'end', 'user']; foreach ($disallowed as $pname) { if (isset($params[$pname])) { $this->dieUsage("Parameter '{$prefix}{$pname}' can only be used with {$prefix}sort=timestamp", 'badparams'); } } if ($params['filterbots'] != 'all') { $this->dieUsage("Parameter '{$prefix}filterbots' can only be used with {$prefix}sort=timestamp", 'badparams'); } // Pagination if (!is_null($params['continue'])) { $cont = explode('|', $params['continue']); $this->dieContinueUsageIf(count($cont) != 1); $op = $ascendingOrder ? '>' : '<'; $continueFrom = $db->addQuotes($cont[0]); $this->addWhere("img_name {$op}= {$continueFrom}"); } // Image filters $from = $params['from'] === null ? null : $this->titlePartToKey($params['from'], NS_FILE); $to = $params['to'] === null ? null : $this->titlePartToKey($params['to'], NS_FILE); $this->addWhereRange('img_name', $ascendingOrder ? 'newer' : 'older', $from, $to); if (isset($params['prefix'])) { $this->addWhere('img_name' . $db->buildLike($this->titlePartToKey($params['prefix'], NS_FILE), $db->anyString())); } } else { // Check mutually exclusive params $disallowed = ['from', 'to', 'prefix']; foreach ($disallowed as $pname) { if (isset($params[$pname])) { $this->dieUsage("Parameter '{$prefix}{$pname}' can only be used with {$prefix}sort=name", 'badparams'); } } if (!is_null($params['user']) && $params['filterbots'] != 'all') { // Since filterbots checks if each user has the bot right, it // doesn't make sense to use it with user $this->dieUsage("Parameters '{$prefix}user' and '{$prefix}filterbots' cannot be used together", 'badparams'); } // Pagination $this->addTimestampWhereRange('img_timestamp', $ascendingOrder ? 'newer' : 'older', $params['start'], $params['end']); // Include in ORDER BY for uniqueness $this->addWhereRange('img_name', $ascendingOrder ? 'newer' : 'older', null, null); if (!is_null($params['continue'])) { $cont = explode('|', $params['continue']); $this->dieContinueUsageIf(count($cont) != 2); $op = $ascendingOrder ? '>' : '<'; $continueTimestamp = $db->addQuotes($db->timestamp($cont[0])); $continueName = $db->addQuotes($cont[1]); $this->addWhere("img_timestamp {$op} {$continueTimestamp} OR " . "(img_timestamp = {$continueTimestamp} AND " . "img_name {$op}= {$continueName})"); } // Image filters if (!is_null($params['user'])) { $this->addWhereFld('img_user_text', $params['user']); } if ($params['filterbots'] != 'all') { $this->addTables('user_groups'); $this->addJoinConds(['user_groups' => ['LEFT JOIN', ['ug_group' => User::getGroupsWithPermission('bot'), 'ug_user = img_user']]]); $groupCond = $params['filterbots'] == 'nobots' ? 'NULL' : 'NOT NULL'; $this->addWhere("ug_group IS {$groupCond}"); } } // Filters not depending on sort if (isset($params['minsize'])) { $this->addWhere('img_size>=' . intval($params['minsize'])); } if (isset($params['maxsize'])) { $this->addWhere('img_size<=' . intval($params['maxsize'])); } $sha1 = false; if (isset($params['sha1'])) { $sha1 = strtolower($params['sha1']); if (!$this->validateSha1Hash($sha1)) { $this->dieUsage('The SHA1 hash provided is not valid', 'invalidsha1hash'); } $sha1 = Wikimedia\base_convert($sha1, 16, 36, 31); } elseif (isset($params['sha1base36'])) { $sha1 = strtolower($params['sha1base36']); if (!$this->validateSha1Base36Hash($sha1)) { $this->dieUsage('The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash'); } } if ($sha1) { $this->addWhereFld('img_sha1', $sha1); } if (!is_null($params['mime'])) { if ($this->getConfig()->get('MiserMode')) { $this->dieUsage('MIME search disabled in Miser Mode', 'mimesearchdisabled'); } $mimeConds = []; foreach ($params['mime'] as $mime) { list($major, $minor) = File::splitMime($mime); $mimeConds[] = $db->makeList(['img_major_mime' => $major, 'img_minor_mime' => $minor], LIST_AND); } // safeguard against internal_api_error_DBQueryError if (count($mimeConds) > 0) { $this->addWhere($db->makeList($mimeConds, LIST_OR)); } else { // no MIME types, no files $this->getResult()->addValue('query', $this->getModuleName(), []); return; } } $limit = $params['limit']; $this->addOption('LIMIT', $limit + 1); $sortFlag = ''; if (!$ascendingOrder) { $sortFlag = ' DESC'; } if ($params['sort'] == 'timestamp') { $this->addOption('ORDER BY', 'img_timestamp' . $sortFlag); if (!is_null($params['user'])) { $this->addOption('USE INDEX', ['image' => 'img_usertext_timestamp']); } else { $this->addOption('USE INDEX', ['image' => 'img_timestamp']); } } else { $this->addOption('ORDER BY', 'img_name' . $sortFlag); } $res = $this->select(__METHOD__); $titles = []; $count = 0; $result = $this->getResult(); foreach ($res as $row) { if (++$count > $limit) { // We've reached the one extra which shows that there are // additional pages to be had. Stop here... if ($params['sort'] == 'name') { $this->setContinueEnumParameter('continue', $row->img_name); } else { $this->setContinueEnumParameter('continue', "{$row->img_timestamp}|{$row->img_name}"); } break; } if (is_null($resultPageSet)) { $file = $repo->newFileFromRow($row); $info = array_merge(['name' => $row->img_name], ApiQueryImageInfo::getInfo($file, $prop, $result)); self::addTitleInfo($info, $file->getTitle()); $fit = $result->addValue(['query', $this->getModuleName()], null, $info); if (!$fit) { if ($params['sort'] == 'name') { $this->setContinueEnumParameter('continue', $row->img_name); } else { $this->setContinueEnumParameter('continue', "{$row->img_timestamp}|{$row->img_name}"); } break; } } else { $titles[] = Title::makeTitle(NS_FILE, $row->img_name); } } if (is_null($resultPageSet)) { $result->addIndexedTagName(['query', $this->getModuleName()], 'img'); } else { $resultPageSet->populateFromTitles($titles); } }
public function execute($par) { $this->mime = $par ? $par : $this->getRequest()->getText('mime'); $this->mime = trim($this->mime); list($this->major, $this->minor) = File::splitMime($this->mime); if ($this->major == '' || $this->minor == '' || $this->minor == 'unknown' || !self::isValidType($this->major)) { $this->setHeaders(); $this->outputHeader(); $this->getPageHeader(); return; } parent::execute($par); }
/** * @param $dbw DatabaseBase * @return array */ function getConditions($dbw) { $conds = array(); $end = $this->getOption('end', false); $mime = $this->getOption('mime', false); $like = $this->getOption('metadata-contains', false); if ($end !== false) { $conds[] = 'img_name <= ' . $dbw->addQuotes($end); } if ($mime !== false) { list($major, $minor) = File::splitMime($mime); $conds['img_major_mime'] = $major; if ($minor !== '*') { $conds['img_minor_mime'] = $minor; } } if ($like) { $conds[] = 'img_metadata ' . $dbw->buildLike($dbw->anyString(), $like, $dbw->anyString()); } return $conds; }
/** * @param $resultPageSet ApiPageSet * @return void */ private function run($resultPageSet = null) { $repo = $this->mRepo; if (!$repo instanceof LocalRepo) { $this->dieUsage('Local file repository does not support querying all images', 'unsupportedrepo'); } $db = $this->getDB(); $params = $this->extractRequestParams(); if (!is_null($params['continue'])) { $cont = explode('|', $params['continue']); if (count($cont) != 1) { $this->dieUsage("Invalid continue param. You should pass the " . "original value returned by the previous query", "_badcontinue"); } $op = $params['dir'] == 'descending' ? '<' : '>'; $cont_from = $db->addQuotes($cont[0]); $this->addWhere("img_name {$op}= {$cont_from}"); } // Image filters $dir = $params['dir'] == 'descending' ? 'older' : 'newer'; $from = is_null($params['from']) ? null : $this->titlePartToKey($params['from']); $to = is_null($params['to']) ? null : $this->titlePartToKey($params['to']); $this->addWhereRange('img_name', $dir, $from, $to); if (isset($params['prefix'])) { $this->addWhere('img_name' . $db->buildLike($this->titlePartToKey($params['prefix']), $db->anyString())); } if (isset($params['minsize'])) { $this->addWhere('img_size>=' . intval($params['minsize'])); } if (isset($params['maxsize'])) { $this->addWhere('img_size<=' . intval($params['maxsize'])); } $sha1 = false; if (isset($params['sha1'])) { if (!$this->validateSha1Hash($params['sha1'])) { $this->dieUsage('The SHA1 hash provided is not valid', 'invalidsha1hash'); } $sha1 = wfBaseConvert($params['sha1'], 16, 36, 31); } elseif (isset($params['sha1base36'])) { $sha1 = $params['sha1base36']; if (!$this->validateSha1Base36Hash($sha1)) { $this->dieUsage('The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash'); } } if ($sha1) { $this->addWhereFld('img_sha1', $sha1); } if (!is_null($params['mime'])) { global $wgMiserMode; if ($wgMiserMode) { $this->dieUsage('MIME search disabled in Miser Mode', 'mimesearchdisabled'); } list($major, $minor) = File::splitMime($params['mime']); $this->addWhereFld('img_major_mime', $major); $this->addWhereFld('img_minor_mime', $minor); } $this->addTables('image'); $prop = array_flip($params['prop']); $this->addFields(LocalFile::selectFields()); $limit = $params['limit']; $this->addOption('LIMIT', $limit + 1); $sort = $params['dir'] == 'descending' ? ' DESC' : ''; $this->addOption('ORDER BY', 'img_name' . $sort); $res = $this->select(__METHOD__); $titles = array(); $count = 0; $result = $this->getResult(); foreach ($res as $row) { if (++$count > $limit) { // We've reached the one extra which shows that there are additional pages to be had. Stop here... $this->setContinueEnumParameter('continue', $row->img_name); break; } if (is_null($resultPageSet)) { $file = $repo->newFileFromRow($row); $info = array_merge(array('name' => $row->img_name), ApiQueryImageInfo::getInfo($file, $prop, $result)); self::addTitleInfo($info, $file->getTitle()); $fit = $result->addValue(array('query', $this->getModuleName()), null, $info); if (!$fit) { $this->setContinueEnumParameter('continue', $row->img_name); break; } } else { $titles[] = Title::makeTitle(NS_FILE, $row->img_name); } } if (is_null($resultPageSet)) { $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'img'); } else { $resultPageSet->populateFromTitles($titles); } }
/** * Get an associative array containing information about * a file with the given storage path. * * @param $ext Mixed: the file extension, or true to extract it from the filename. * Set it to false to ignore the extension. * * @return array */ public function getProps($ext = true) { wfProfileIn(__METHOD__); wfDebug(__METHOD__ . ": Getting file info for {$this->path}\n"); $info = self::placeholderProps(); $info['fileExists'] = $this->exists(); if ($info['fileExists']) { $magic = MimeMagic::singleton(); # get the file extension if ($ext === true) { $ext = self::extensionFromPath($this->path); } # mime type according to file contents $info['file-mime'] = $this->getMimeType(); # logical mime type $info['mime'] = $magic->improveTypeFromExtension($info['file-mime'], $ext); list($info['major_mime'], $info['minor_mime']) = File::splitMime($info['mime']); $info['media_type'] = $magic->getMediaType($this->path, $info['mime']); # Get size in bytes $info['size'] = $this->getSize(); # Height, width and metadata $handler = MediaHandler::getHandler($info['mime']); if ($handler) { $tempImage = (object) array(); $info['metadata'] = $handler->getMetadata($tempImage, $this->path); $gis = $handler->getImageSize($tempImage, $this->path, $info['metadata']); if (is_array($gis)) { $info = $this->extractImageSizeInfo($gis) + $info; } } $info['sha1'] = $this->getSha1Base36(); wfDebug(__METHOD__ . ": {$this->path} loaded, {$info['size']} bytes, {$info['mime']}.\n"); } else { wfDebug(__METHOD__ . ": {$this->path} NOT FOUND!\n"); } wfProfileOut(__METHOD__); return $info; }
/** * Return the appropriate im_XXX2vips handler for this file * @param File $file * @return mixed String or false */ public static function getVipsHandler( $file ) { list( $major, $minor ) = File::splitMime( $file->getMimeType() ); if ( $major == 'image' && in_array( $minor, array( 'jpeg', 'png', 'tiff' ) ) ) { return "im_{$minor}2vips"; } else { return false; } }
/** * @param $resultPageSet ApiPageSet * @return void */ private function run($resultPageSet = null) { $repo = $this->mRepo; if (!$repo instanceof LocalRepo) { $this->dieUsage('Local file repository does not support querying all images', 'unsupportedrepo'); } $prefix = $this->getModulePrefix(); $db = $this->getDB(); $params = $this->extractRequestParams(); // Table and return fields $this->addTables('image'); $prop = array_flip($params['prop']); $this->addFields(LocalFile::selectFields()); $dir = in_array($params['dir'], array('descending', 'older')) ? 'older' : 'newer'; if ($params['sort'] == 'name') { // Check mutually exclusive params $disallowed = array('start', 'end', 'user'); foreach ($disallowed as $pname) { if (isset($params[$pname])) { $this->dieUsage("Parameter '{$prefix}{$pname}' can only be used with {$prefix}sort=timestamp", 'badparams'); } } if ($params['filterbots'] != 'all') { $this->dieUsage("Parameter '{$prefix}filterbots' can only be used with {$prefix}sort=timestamp", 'badparams'); } // Pagination if (!is_null($params['continue'])) { $cont = explode('|', $params['continue']); if (count($cont) != 1) { $this->dieUsage('Invalid continue param. You should pass the ' . 'original value returned by the previous query', '_badcontinue'); } $op = $dir == 'older' ? '<' : '>'; $cont_from = $db->addQuotes($cont[0]); $this->addWhere("img_name {$op}= {$cont_from}"); } // Image filters $from = is_null($params['from']) ? null : $this->titlePartToKey($params['from']); $to = is_null($params['to']) ? null : $this->titlePartToKey($params['to']); $this->addWhereRange('img_name', $dir, $from, $to); if (isset($params['prefix'])) { $this->addWhere('img_name' . $db->buildLike($this->titlePartToKey($params['prefix']), $db->anyString())); } } else { // Check mutually exclusive params $disallowed = array('from', 'to', 'prefix'); foreach ($disallowed as $pname) { if (isset($params[$pname])) { $this->dieUsage("Parameter '{$prefix}{$pname}' can only be used with {$prefix}sort=name", 'badparams'); } } if (!is_null($params['user']) && $params['filterbots'] != 'all') { // Since filterbots checks if each user has the bot right, it doesn't make sense to use it with user $this->dieUsage("Parameters 'user' and 'filterbots' cannot be used together", 'badparams'); } // Pagination $this->addTimestampWhereRange('img_timestamp', $dir, $params['start'], $params['end']); // Image filters if (!is_null($params['user'])) { $this->addWhereFld('img_user_text', $params['user']); } if ($params['filterbots'] != 'all') { $this->addTables('user_groups'); $groupCond = $params['filterbots'] == 'nobots' ? 'NULL' : 'NOT NULL'; $this->addWhere("ug_group IS {$groupCond}"); $this->addJoinConds(array('user_groups' => array('LEFT JOIN', array('ug_group' => User::getGroupsWithPermission('bot'), 'ug_user = img_user')))); } } // Filters not depending on sort if (isset($params['minsize'])) { $this->addWhere('img_size>=' . intval($params['minsize'])); } if (isset($params['maxsize'])) { $this->addWhere('img_size<=' . intval($params['maxsize'])); } $sha1 = false; if (isset($params['sha1'])) { if (!$this->validateSha1Hash($params['sha1'])) { $this->dieUsage('The SHA1 hash provided is not valid', 'invalidsha1hash'); } $sha1 = wfBaseConvert($params['sha1'], 16, 36, 31); } elseif (isset($params['sha1base36'])) { $sha1 = $params['sha1base36']; if (!$this->validateSha1Base36Hash($sha1)) { $this->dieUsage('The SHA1Base36 hash provided is not valid', 'invalidsha1base36hash'); } } if ($sha1) { $this->addWhereFld('img_sha1', $sha1); } if (!is_null($params['mime'])) { global $wgMiserMode; if ($wgMiserMode) { $this->dieUsage('MIME search disabled in Miser Mode', 'mimesearchdisabled'); } list($major, $minor) = File::splitMime($params['mime']); $this->addWhereFld('img_major_mime', $major); $this->addWhereFld('img_minor_mime', $minor); } $limit = $params['limit']; $this->addOption('LIMIT', $limit + 1); $sort = $dir == 'older' ? ' DESC' : ''; if ($params['sort'] == 'timestamp') { $this->addOption('ORDER BY', 'img_timestamp' . $sort); if ($params['filterbots'] == 'all') { $this->addOption('USE INDEX', array('image' => 'img_timestamp')); } else { $this->addOption('USE INDEX', array('image' => 'img_usertext_timestamp')); } } else { $this->addOption('ORDER BY', 'img_name' . $sort); } $res = $this->select(__METHOD__); $titles = array(); $count = 0; $result = $this->getResult(); foreach ($res as $row) { if (++$count > $limit) { // We've reached the one extra which shows that there are additional pages to be had. Stop here... if ($params['sort'] == 'name') { $this->setContinueEnumParameter('continue', $row->img_name); } else { $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->img_timestamp)); } break; } if (is_null($resultPageSet)) { $file = $repo->newFileFromRow($row); $info = array_merge(array('name' => $row->img_name), ApiQueryImageInfo::getInfo($file, $prop, $result)); self::addTitleInfo($info, $file->getTitle()); $fit = $result->addValue(array('query', $this->getModuleName()), null, $info); if (!$fit) { if ($params['sort'] == 'name') { $this->setContinueEnumParameter('continue', $row->img_name); } else { $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->img_timestamp)); } break; } } else { $titles[] = Title::makeTitle(NS_FILE, $row->img_name); } } if (is_null($resultPageSet)) { $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'img'); } else { $resultPageSet->populateFromTitles($titles); } }