function ingest($db, $r, $fetch_enclosures = false) { $user = $r->getUser(); $coll_ascii_id = $this->getAsciiId(); $count = $this->getItemCount(); $collection_name = $this->getTitle(); $ascii_id = $this->getAsciiId(); $c = new Dase_DBO_Collection($db); $c->collection_name = $collection_name; if (Dase_DBO_Collection::get($db, $ascii_id) || $c->findOne()) { //$r->renderError(409,'collection already exists'); Dase_Log::info(LOG_FILE, 'collection exists ' . $c->collection_name); return; } $c->ascii_id = $ascii_id; $c->is_public = 0; $c->created = date(DATE_ATOM); $c->updated = date(DATE_ATOM); if ($c->insert()) { $cache = $r->getCache(); $cache->expire('app_data'); Dase_Log::info(LOG_FILE, 'created collection ' . $c->collection_name); $coll_media_dir = MEDIA_DIR . '/' . $ascii_id; if (file_exists($coll_media_dir)) { //$r->renderError(409,'collection media archive exists'); Dase_Log::info(LOG_FILE, 'collection media archive exists'); } else { if (mkdir("{$coll_media_dir}")) { chmod("{$coll_media_dir}", 0775); foreach (Dase_Media::$sizes as $size => $access_level) { mkdir("{$coll_media_dir}/{$size}"); Dase_Log::info(LOG_FILE, 'created directory ' . $coll_media_dir . '/' . $size); chmod("{$coll_media_dir}/{$size}", 0775); } //todo: compat only! symlink($coll_media_dir, $coll_media_dir . '_collection'); } } foreach ($this->getEntries() as $entry) { if ('item' == $entry->getEntryType()) { $r->set('collection_ascii_id', $c->ascii_id); $entry->insert($db, $r, $fetch_enclosures); } } } }
public function getHandlerObject() { $classname = $this->initModule($this->config); if (!$classname) { $classname = 'Dase_Handler_' . Dase_Util::camelize($this->handler); } if (class_exists($classname, true)) { return new $classname($this->db, $this->config); } else { Dase_Log::info(LOG_FILE, 'no such handler class ' . $classname . ' redirecting'); $this->renderRedirect($this->default_handler); } }
public function expunge() { if (!$this->id || !$this->ascii_id) { throw new Exception('cannot delete unspecified type'); } $ait = new Dase_DBO_AttributeItemType($this->db); $ait->item_type_id = $this->id; foreach ($ait->find() as $doomed) { Dase_Log::info(LOG_FILE, 'deleted attribute_item_type ' . $doomed->id); $doomed->delete(); } $this->delete(); }
public function redirect($path = '', $params = null, $code = 303) { //SHOULD use 303 (redirect after put,post,delete) //OR 307 -- no go -- look here //NOTE that this redirect may be innapropriate when //client expect something OTHER than html (e.g., json,text,xml) //format should be passed in params $query_array = array(); if (isset($params) && is_array($params)) { foreach ($params as $key => $val) { $query_array[] = urlencode($key) . '=' . urlencode($val); } } $app_root = $this->request->app_root; if ('http' != substr($path, 0, 4)) { $redirect_path = trim($app_root, '/') . "/" . trim($path, '/'); } else { $redirect_path = $path; } if (count($query_array)) { //since path is allowed to have some query params already if (false !== strpos($path, '?')) { $redirect_path .= '&' . join("&", $query_array); } else { $redirect_path .= '?' . join("&", $query_array); } } Dase_Log::info(LOG_FILE, 'redirecting to ' . $redirect_path); header("Location:" . $redirect_path, TRUE, $code); exit; }
public function getResultsAsAtom() { $app_root = $this->request->app_root; //use XMLReader for speed $total = 0; $coll_tallies = array(); $ids = array(); $reader = new XMLReader(); if (false === $reader->XML($this->_getSearchResults())) { Dase_Log::debug(LOG_FILE, 'SOLR ERROR : error reading search engine xml'); } while ($reader->read()) { //get total number found if ($reader->localName == "result" && $reader->nodeType == XMLReader::ELEMENT) { $total = $reader->getAttribute('numFound'); } //get entries if ($reader->localName == "str" && $reader->nodeType == XMLReader::ELEMENT) { if ('_id' == $reader->getAttribute('name')) { $reader->read(); $ids[] = $reader->value; } } //get collection tallies if ($reader->localName == "lst" && $reader->nodeType == XMLReader::ELEMENT) { if ('c' == $reader->getAttribute('name')) { while ($reader->read()) { if ($reader->localName == "int" && $reader->nodeType == XMLReader::ELEMENT) { $tally['coll'] = $reader->getAttribute('name'); @($tally['collection_name'] = $GLOBALS['app_data']['collections'][$tally['coll']]); //advance reader $reader->read(); $tally['count'] = $reader->value; if ($tally['count']) { $coll_tallies[] = $tally; } $tally = array(); } } } } } $reader->close(); $url = $this->_cleanUpUrl($this->request->getUrl()); $grid_url = $url . '&start=' . $this->start . '&max=' . $this->max . '&display=grid&sort=' . $this->sort; $list_url = $url . '&start=' . $this->start . '&max=' . $this->max . '&display=list&sort=' . $this->sort; $id = $app_root . '/search/' . md5($url); $updated = date(DATE_ATOM); //todo: probably the q param //note: bug -- this chops off last part of query //echo if it contains an ampersand //$query = ''; preg_match('/(\\?|&|&)q=([^&]+)/i', urldecode($this->solr_search_url), $matches); if (isset($matches[2])) { $query = htmlspecialchars(urlencode($matches[2])); } if (!$total) { Dase_Log::info(FAILED_SEARCH_LOG, $query); } $feed = <<<EOD <feed xmlns="http://www.w3.org/2005/Atom" \t xmlns:thr="http://purl.org/syndication/thread/1.0"> <author> \t<name>DASe (Digital Archive Services)</name> \t<uri>http://daseproject.org</uri> \t<email>admin@daseproject.org</email> </author> <title>DASe Search Result</title> <link rel="alternate" title="Search Result" href="{$url}" type="text/html"/> <link rel="related" title="grid" href="{$grid_url}" type="text/html"/> <link rel="related" title="list" href="{$list_url}" type="text/html"/> <updated>{$updated}</updated> <category term="search" scheme="http://daseproject.org/category/feedtype"/> <id>{$id}</id> <totalResults xmlns="http://a9.com/-/spec/opensearch/1.1/">{$total}</totalResults> <startIndex xmlns="http://a9.com/-/spec/opensearch/1.1/">{$this->start}</startIndex> <itemsPerPage xmlns="http://a9.com/-/spec/opensearch/1.1/">{$this->max}</itemsPerPage> <Query xmlns="http://a9.com/-/spec/opensearch/1.1/" role="request" searchTerms="{$query}"/> EOD; //next link $next = $this->start + $this->max; if ($next <= $total) { $next_url = $url . '&start=' . $next . '&max=' . $this->max . '&sort=' . $this->sort; $feed .= "\n <link rel=\"next\" href=\"{$next_url}\"/>"; } //previous link $previous = $this->start - $this->max; if ($previous >= 0) { $previous_url = $url . '&start=' . $previous . '&max=' . $this->max . '&sort=' . $this->sort; $feed .= "\n <link rel=\"previous\" href=\"{$previous_url}\"/>"; } //collection fq //this will allow us to create search filters on page forms foreach ($this->coll_filters as $c) { $feed .= "\n <category term=\"{$c}\" scheme=\"http://daseproject.org/category/collection_filter\"/>\n"; } $tallied = array(); foreach ($coll_tallies as $tally) { $count = $tally['count']; $cname = $tally['collection_name']; $cname_specialchars = htmlspecialchars($cname); $coll = $tally['coll']; $encoded_query = $query . '&c=' . $coll; $feed .= " <link rel=\"http://daseproject.org/relation/single_collection_search\" title=\"{$cname_specialchars}\" thr:count=\"{$count}\" href=\"q={$encoded_query}\"/>\n"; } if (1 == count($coll_tallies)) { $feed .= " <link rel=\"http://daseproject.org/relation/collection\" title=\"{$cname_specialchars}\" thr:count=\"{$count}\" href=\"{$app_root}/collection/{$coll}\"/>\n"; $feed .= " <link rel=\"http://daseproject.org/relation/collection/attributes\" title=\"{$cname_specialchars} attributes\" href=\"{$app_root}/collection/{$coll}/attributes.json\"/>\n"; } //this prevents a 'search/item' becoming 'search/item/item': $item_request_url = str_replace('search/item', 'search', $this->request->url); $item_request_url = preg_replace('/search/', 'search/item', $item_request_url, 1); //omit format param $item_request_url = preg_replace('/(\\?|&|&)format=\\w*/i', '', $item_request_url); $item_request_url = htmlspecialchars($item_request_url); $num = 0; //foreach ($entries as $entry_txt) { foreach ($ids as $unique_id) { $doc = new Dase_DBO_ItemAtom($this->db); $doc->unique_id = $unique_id; $doc->findOne(); $num++; $setnum = $num + $this->start; //$entry = Dase_Util::unhtmlspecialchars($entry_txt); $entry = $doc->doc; $added = <<<EOD <category term="{$setnum}" scheme="http://daseproject.org/category/position"/> <link rel="http://daseproject.org/relation/search-item" href="{$item_request_url}&num={$setnum}&uid={$unique_id}"/> EOD; $entry = str_replace('<author>', $added . "\n <author>", $entry); $feed .= $entry; } $feed .= "</feed>"; $feed = str_replace('{APP_ROOT}', $app_root, $feed); return $feed; }
function createNewItem($serial_number = null, $eid = null) { if (!$eid) { $eid = '_dase'; } $item = new Dase_DBO_Item($this->db); $item->collection_id = $this->id; if ($serial_number) { $item->serial_number = $serial_number; if ($item->findOne()) { Dase_Log::info(LOG_FILE, "duplicate serial number: " . $serial_number); throw new Dase_Exception('duplicate serial number!'); return; } $item->status = 'public'; $item->item_type_id = 0; $item->item_type_ascii_id = 'default'; $item->item_type_name = 'default'; $item->created = date(DATE_ATOM); $item->updated = date(DATE_ATOM); $item->created_by_eid = $eid; $item->p_collection_ascii_id = $this->ascii_id; $item->p_remote_media_host = $this->remote_media_host; $item->collection_name = $this->collection_name; $item->insert(); $this->updateItemCount(); return $item; } else { $item->status = 'public'; $item->item_type_id = 0; $item->item_type_ascii_id = 'default'; $item->item_type_name = 'default'; $item->created = date(DATE_ATOM); $item->created_by_eid = $eid; $item->p_collection_ascii_id = $this->ascii_id; $item->p_remote_media_host = $this->remote_media_host; $item->collection_name = $this->collection_name; $item->insert(); //after move to mysql to avoid collisions w/ old sernums //replace first '0' w/ '1' //todo: better way to generate unique sernum. // (do NOT forget to enforce uniqueness in DB) //$item->serial_number = sprintf("%09d",$item->id); $item->serial_number = '1' . sprintf("%08d", $item->id); $item->updated = date(DATE_ATOM); $item->update(); $this->updateItemCount(); return $item; } }
/** will be invoked by subclass */ function makeViewitem($item, $path_to_media) { $size = $this->size; $collection = $item->getCollection(); $target = $path_to_media . '/' . $collection->ascii_id . '/viewitem/' . $size . '.jpg'; if (!file_exists($target)) { copy($this->base_path . '/www/images/thumb_icons/' . $size . '.jpg', $target); } $media_file = new Dase_DBO_MediaFile($this->db); $media_file->item_id = $item->id; $media_file->filename = $size . '.jpg'; $media_file->width = 80; $media_file->height = 80; $media_file->mime_type = 'image/jpeg'; $media_file->size = 'viewitem'; $media_file->file_size = filesize($target); $media_file->p_collection_ascii_id = $collection->ascii_id; $media_file->p_serial_number = $item->serial_number; $media_file->insert(); Dase_Log::info(LOG_FILE, "created {$media_file->size} {$media_file->filename}"); }
function makeSizes($item, $path_to_media, $rotate) { $collection = $item->getCollection(); $image_properties = array('small' => array('geometry' => '640x480', 'max_height' => '480', 'size_tag' => '_640'), 'medium' => array('geometry' => '800x600', 'max_height' => '600', 'size_tag' => '_800'), 'large' => array('geometry' => '1024x768', 'max_height' => '768', 'size_tag' => '_1024'), 'full' => array('geometry' => '3600x2700', 'max_height' => '2700', 'size_tag' => '_3600')); $last_width = ''; $last_height = ''; $subdir = Dase_Util::getSubdir($item->serial_number); foreach ($image_properties as $size => $size_info) { $newimage = $path_to_media . '/' . $collection->ascii_id . '/' . $size . '/' . $subdir . '/' . $item->serial_number . $size_info['size_tag'] . '.jpg'; $subdir_path = $path_to_media . '/' . $collection->ascii_id . '/' . $size . '/' . $subdir; if (!file_exists($subdir_path)) { mkdir($subdir_path); } $command = CONVERT . " \"{$this->filepath}\" -format jpeg -rotate {$rotate} -resize '{$size_info['geometry']} >' -colorspace RGB {$newimage}"; $exec_output = array(); $results = exec($command, $exec_output); if (!file_exists($newimage)) { Dase_Log::debug(LOG_FILE, "failed to write {$size} image"); Dase_Log::debug(LOG_FILE, "UNSUCCESSFUL: {$command}"); } $file_info = getimagesize($newimage); //create the media_file entry $media_file = new Dase_DBO_MediaFile($this->db); $media_file->item_id = $item->id; $media_file->filename = $item->serial_number . $size_info['size_tag'] . ".jpg"; if ($file_info) { $media_file->width = $file_info[0]; $media_file->height = $file_info[1]; } if ($media_file->width <= $last_width && $media_file->height <= $last_height) { return; } $last_width = $media_file->width; $last_height = $media_file->height; $media_file->mime_type = 'image/jpeg'; $media_file->size = $size; $media_file->md5 = md5_file($newimage); $media_file->updated = date(DATE_ATOM); $media_file->file_size = filesize($newimage); $media_file->p_collection_ascii_id = $collection->ascii_id; $media_file->p_serial_number = $item->serial_number; $media_file->insert(); Dase_Log::info(LOG_FILE, "created {$media_file->size} {$media_file->filename}"); } return; }
public function postToUploader($r) { //form can use any 'name' it wishes $filecount = 0; foreach ($r->_files as $k => $v) { $input_name = $k; if ($input_name && is_file($r->_files[$input_name]['tmp_name'])) { $name = $r->_files[$input_name]['name']; $path = $r->_files[$input_name]['tmp_name']; $type = $r->_files[$input_name]['type']; if (!Dase_Media::isAcceptable($type)) { Dase_Log::debug(LOG_FILE, $type . ' is not a supported media type'); //$r->renderError(415,'unsupported media type: '.$type); continue; } if (!is_uploaded_file($path)) { //$r->renderError(400,'no go upload'); continue; } Dase_Log::info(LOG_FILE, 'uploading file ' . $name . ' type: ' . $type); try { //this'll create thumbnail, viewitem, and any derivatives $file = Dase_File::newFile($this->db, $path, $type, $name, BASE_PATH); } catch (Exception $e) { Dase_Log::debug(LOG_FILE, 'add to collection error: ' . $e->getMessage()); //$r->renderError(409,$e->getMessage()); continue; } $item = $this->collection->createNewItem(null, $this->user->eid); if ($r->has('title')) { $item->setValue('title', $r->get('title')); } else { $item->setValue('title', $name); } try { $media_file = $file->addToCollection($item, true, MEDIA_DIR); //true means tets for dups } catch (Exception $e) { Dase_Log::debug(LOG_FILE, 'add to collection error: ' . $e->getMessage()); //$r->renderError(409,$e->getMessage()); continue; } $item->setItemType($r->get('item_type')); //here's where we map admin_att to real att $item->mapConfiguredAdminAtts(); //delay search building?? $item->buildSearchIndex(); $filecount++; } } $r->renderResponse('uploaded ' . $filecount . ' files'); //$r->renderRedirect('manage/'.$this->collection->ascii_id.'/uploader'); }
function testLogWritesToLogFile() { Dase_Log::info(LOG_FILE, 'test'); $val = substr(Dase_Log::readLastLine(LOG_FILE), -4); $this->assertTrue('test' === $val); }
function asAtom($app_root, $authorized_links = false) { $this->user || $this->getUser(); $feed = new Dase_Atom_Feed(); $feed->setTitle($this->name); if ($this->description) { $feed->setSubtitle($this->description); } $feed->setId($app_root . '/tag/' . $this->user->eid . '/' . $this->ascii_id); $feed->setUpdated($this->updated); $feed->addAuthor($this->user->eid); $feed->setFeedType('tag'); $feed->addLink($app_root . '/tag/' . $this->user->eid . '/' . $this->ascii_id . '.atom', 'self'); $feed->addLink($app_root . '/tag/' . $this->user->eid . '/' . $this->ascii_id, 'alternate'); $feed->addLink($app_root . '/tag/' . $this->user->eid . '/' . $this->ascii_id . '/list', 'alternate', 'text/html', '', 'list'); $feed->addLink($app_root . '/tag/' . $this->user->eid . '/' . $this->ascii_id . '/grid', 'alternate', 'text/html', '', 'grid'); $feed->addLink($app_root . '/tag/' . $this->user->eid . '/' . $this->ascii_id . '.json', 'alternate', 'application/json', '', 'slideshow'); $feed->addCategory($this->type, "http://daseproject.org/category/tag_type", $this->type); if ($this->is_public) { $pub = "public"; } else { $pub = "private"; } $feed->addCategory($pub, "http://daseproject.org/category/visibility"); $feed->addCategory($this->background, "http://daseproject.org/category/background"); /* TO DO categories: admin_coll_id, updated, created, master_item, etc */ $setnum = 0; $collections_array = array(); foreach ($this->getTagItems() as $tag_item) { $tag_item->persist(true); $item_unique = $tag_item->p_collection_ascii_id . '/' . $tag_item->p_serial_number; //lets us determine if tag includes items in only one collection $collections_array[$tag_item->p_collection_ascii_id] = 1; if ($authorized_links) { //fresh, not from cache $item = $tag_item->getItem(); $entry = $feed->addEntry(); $entry = $item->injectAtomEntryData($entry, $app_root, true); } else { $entry = $feed->addItemEntryByItemUnique($this->db, $item_unique, $app_root); } if ($entry) { $setnum++; $entry->addCategory($setnum, 'http://daseproject.org/category/position'); $entry->addCategory($tag_item->id, 'http://daseproject.org/category/tag_item_id'); $entry->addLink($app_root . '/tag/' . $this->user->eid . '/' . $this->ascii_id . '/' . $tag_item->id, "http://daseproject.org/relation/search-item"); $entry->addLink($app_root . '/tag/' . $this->user->eid . '/' . $this->ascii_id . '/' . $tag_item->id . '/annotation', "http://daseproject.org/relation/edit-annotation"); if ($tag_item->annotation) { $entry->setSummary($tag_item->annotation); } } else { //remove tag_item $log_text = "SMOKING GUN Ann Johns mystery: tried removing {$item_unique} from set {$this->eid}/{$this->ascii_id}"; Dase_Log::info(LOG_FILE, $log_text); //$tag_item->delete();; //$this->resortTagItems(); //$this->updateItemCount(); } } if (1 == count($collections_array)) { $coll = array_pop(array_keys($collections_array)); $feed->addCategory($coll, "http://daseproject.org/category/collection"); } return $feed->asXml(); }
function create($db, $r) { $atom_author = $this->getAuthorName(); $user = $r->getUser('http'); $collection_name = $this->getTitle(); if (!$collection_name) { $r->renderError(400, 'no title'); } $c = new Dase_DBO_Collection($db); $c->collection_name = $collection_name; if ($r->has('ascii_id')) { $ascii_id = $r->get('ascii_id'); //set in handler based on Slug } else { $ascii_id = $this->getAsciiId(); } if (!$ascii_id) { $ascii_id = $c->createAscii(); } if (Dase_DBO_Collection::get($db, $ascii_id) || $c->findOne()) { $r->renderError(409, 'collection already exists'); } $c->ascii_id = $ascii_id; $coll_media_dir = MEDIA_DIR . '/' . $ascii_id; if (file_exists($coll_media_dir)) { //todo: think about this... //$r->renderError(409,'collection media archive exists'); } $c->is_public = 0; $c->created = date(DATE_ATOM); $c->updated = date(DATE_ATOM); $content = $this->getContent(); if ($content) { $c->description = $content; } $summary = $this->getSummary(); if ($summary) { $c->admin_notes = $summary; } if ($c->insert()) { $cache = $r->getCache(); $cache->expire('app_data'); Dase_Log::info(LOG_FILE, 'created collection ' . $c->collection_name); if (mkdir("{$coll_media_dir}")) { chmod("{$coll_media_dir}", 0775); foreach (Dase_Media::$sizes as $size => $access_level) { mkdir("{$coll_media_dir}/{$size}"); Dase_Log::info(LOG_FILE, 'created directory ' . $coll_media_dir . '/' . $size); chmod("{$coll_media_dir}/{$size}", 0775); } symlink($coll_media_dir, $coll_media_dir . '_collection'); } foreach (array('title', 'description', 'keyword', 'rights') as $att) { $a = new Dase_DBO_Attribute($db); $a->ascii_id = $att; $a->attribute_name = ucfirst($att); $a->collection_id = $c->id; $a->in_basic_search = true; $a->is_on_list_display = true; $a->is_public = true; $a->html_input_type = 'text'; if ('description' == $att) { $a->html_input_type = 'textarea'; } $a->updated = date(DATE_ATOM); if ($a->insert()) { Dase_Log::debug(LOG_FILE, 'created att ' . $att); } else { Dase_Log::debug(LOG_FILE, 'problem creating ' . $att); } } $cm = new Dase_DBO_CollectionManager($db); $cm->collection_ascii_id = $ascii_id; $cm->dase_user_eid = $user->eid; $cm->auth_level = 'admin'; $cm->created = date(DATE_ATOM); $cm->created_by_eid = $user->eid; if ($cm->insert()) { Dase_Log::info(LOG_FILE, 'created admin user ' . $ascii_id . '::' . $user->eid); } else { Dase_Log::info(LOG_FILE, 'could not create admin user'); } return $ascii_id; } else { return false; } }
public function deleteTagItems($r) { //move some of this into model $tag = $this->tag; $u = $r->getUser(); Dase_Log::info(LOG_FILE, "{$u->eid} ({$u->name}) is fixing to delete a bunch of items from {$tag->ascii_id}"); $u->expireDataCache($r->getCache()); if (!$u->can('write', $tag)) { $r->renderError(401, 'user does not have write privileges'); } $item_uniques_array = explode(',', $r->get('uniques')); $num = count($item_uniques_array); foreach ($item_uniques_array as $item_unique) { $tag->removeItem($item_unique); } $tag->resortTagItems(); $tag->updateItemCount(); $r->response_mime_type = 'text/plain'; $r->renderResponse("removed {$num} items from {$tag->name}"); }