/** * [doScrapping description] * @param [type] $endpoint [description] * @return [type] [description] */ private function doScrapping($endpoint) { $event = null; $earthquake = null; Debugger::dump('endpoint: ' . $endpoint . ' _' . $_SERVER['HTTP_USER_AGENT']); Debugger::dump('***INICIANDO SCRAPPING****'); $content = $this->scrapper->getContent($endpoint); if (!$content) { Debugger::dump('***ERROR, NO SE OBTUBIERON DATOS'); return; } $this->scrapper->domLoad($content); $tableList = $this->scrapper->findInDom('table tbody tr'); foreach ($tableList as $table) { $earthquakeData = []; foreach ($table->find('td') as $key => $tableItem) { $earthquakeData[$key] = $tableItem->text(); } if (empty($earthquakeData)) { Debugger::dump('***NO HAY DATOS****'); continue; } $dateUTC = $earthquakeData[1]; $dateTs = DatesUtils::toTimestamp($dateUTC); $dateSQL = DatesUtils::toSQLDate($dateUTC); $eventData = ['lat' => $earthquakeData[2], 'lon' => $earthquakeData[3], 'ts' => $dateSQL, 'hash' => md5($dateTs)]; /* Evitar crear eventos duplicados que muestren erroneamente más de un evento siendo que se trata del mismo * pero actualizado. * Esto se hace debido a que el primer informe ante un evento, puede ser preliminar * y se pueden publicar actualizaciones de datos con cambios en magnitud o ubicación geográfica posteriormente. */ $eventExists = $this->Event->checkForExists($eventData, $this->dateBounds); if ($eventExists['exists']) { Debugger::dump('***EVENTO YA EXISTE ****'); $event = $eventExists; } else { Debugger::dump('***NO SE ENCONTRO EVENTO, CREANDO ****'); $this->Event->create(); $event = $this->Event->save($eventData); } if ($event) { $metadatum = ['event_id' => $event['Event']['id'], 'agency_id' => 1, 'lat' => $eventData['lat'], 'lon' => $eventData['lon'], 'ts' => $dateSQL, 'depth' => $earthquakeData[4], 'magnitude' => floatval($earthquakeData[5]), 'geo_reference' => $earthquakeData[6]]; if (!$eventExists['exists']) { Debugger::dump('***EVENTO NO EXISTE, SISMO TAMPOCO ****'); $this->EventMetadatum->create(); $earthquake = $this->EventMetadatum->save($metadatum); } else { $earthquakeExists = $this->EventMetadatum->checkForExists($metadatum, $this->dateBounds, $eventExists['Event']['id']); if ($earthquakeExists['exists']) { Debugger::dump('***EVENTO EXISTE, SISMO TAMBIEN ****'); } else { Debugger::dump('***EVENTO EXISTE, NUEVO SISMO NO. CREANDO NUEVO ASOCIADO A EVENTO****'); $this->EventMetadatum->create(); $earthquake = $this->EventMetadatum->save($metadatum); } } } } return true; }
/** * [doScrapping description] * @param [type] $endpoint [description] * @return [type] [description] */ private function doScrapping($endpoint) { $event = null; $earthquake = null; Debugger::dump('endpoint: ' . $endpoint . ' _' . $_SERVER['HTTP_USER_AGENT']); Debugger::dump('***INICIANDO SCRAPPING****'); $content = $this->scrapper->getContent($endpoint); $content = json_decode($content); foreach ($content->features as $earthquakeData) { if (empty($earthquakeData)) { Debugger::dump('***NO HAY DATOS****'); continue; } $dateUTC = DatesUtils::toReadableDate($earthquakeData->properties->time / 1000); $dateTs = $earthquakeData->properties->time; $dateSQL = DatesUtils::toSQLDate($dateUTC); $eventData = ['lat' => $earthquakeData->geometry->coordinates[1], 'lon' => $earthquakeData->geometry->coordinates[0], 'ts' => $dateSQL, 'hash' => md5($dateTs)]; /* Evitar crear eventos duplicados que muestren erroneamente más de un evento siendo que se trata del mismo * pero actualizado. * Esto se hace debido a que el primer informe ante un evento, puede ser preliminar * y se pueden publicar actualizaciones de datos con cambios en magnitud o ubicación geográfica posteriormente. */ $eventExists = $this->Event->checkForExists($eventData, $this->dateBounds); if ($eventExists['exists']) { Debugger::dump('***EVENTO YA EXISTE ****'); $event = $eventExists; } else { Debugger::dump('***NO SE ENCONTRO EVENTO, CREANDO ****'); $this->Event->create(); $event = $this->Event->save($eventData); } if ($event) { $metadatum = ['event_id' => $event['Event']['id'], 'agency_id' => 3, 'lat' => $eventData['lat'], 'lon' => $eventData['lon'], 'ts' => $dateSQL, 'depth' => $earthquakeData->geometry->coordinates[2], 'magnitude' => floatval($earthquakeData->properties->mag), 'geo_reference' => $earthquakeData->properties->place, 'magnitude_type' => $earthquakeData->properties->magType]; if (!$eventExists['exists']) { Debugger::dump('***EVENTO NO EXISTE, SISMO TAMPOCO ****'); $this->EventMetadatum->create(); $earthquake = $this->EventMetadatum->save($metadatum); } else { $earthquakeExists = $this->EventMetadatum->checkForExists($metadatum, $this->dateBounds, $eventExists['Event']['id']); if ($earthquakeExists['exists']) { Debugger::dump('***EVENTO EXISTE, SISMO TAMBIEN ****'); } else { Debugger::dump('***EVENTO EXISTE, NUEVO SISMO NO. CREANDO NUEVO ASOCIADO A EVENTO****'); $this->EventMetadatum->create(); $earthquake = $this->EventMetadatum->save($metadatum); } } } } }
/** * Test the display() function * * @return void */ public function testDisplay() { // Setup $content = 'anything'; // Test display() with all null params $this->handler = new RDFa(); $this->assertEquals($this->handler->display(), ''); // Test if the params are reseted after the display() function $this->handler->setType('Article')->content($content)->property('name')->fallback('Thing', 'url')->display(); $this->assertNull($this->handler->getFallbackProperty()); $this->assertNull($this->handler->getFallbackType()); $this->assertNull($this->handler->getProperty()); $this->assertNull($this->handler->getContent()); // Test for a simple display $response = $this->handler->property('url')->display(); $this->assertEquals($response, "property='url'"); // Test for a simple display with $content $response = $this->handler->property('url')->content($content)->display(); $this->assertEquals($response, "<span property='url'>{$content}</span>"); // Test for a simple display if the $content is empty '' $response = $this->handler->enable(true)->content('')->property('name')->display(); $this->assertEquals($response, "<span property='name'></span>"); // Test for a simple 'nested' display $response = $this->handler->property('author')->display(); $this->assertEquals($response, "property='author' vocab='https://schema.org' typeof='Organization'"); // Test for a 'nested' display with $content $response = $this->handler->property('author')->content($content)->display(); $this->assertEquals($response, "<span property='author' vocab='https://schema.org' typeof='Organization'>{$content}</span>"); // Test for a 'nested' display with $content and $Fallback $response = $this->handler->fallback('Person', 'name')->property('author')->content($content)->display(); $this->assertEquals($response, "<span property='author' vocab='https://schema.org' typeof='Person'><span property='name'>{$content}</span></span>"); // Test for a 'nested' display with $Fallback and without $content $response = $this->handler->fallback('Person', 'name')->property('author')->display(); $this->assertEquals($response, "property='author' vocab='https://schema.org' typeof='Person' property='name'"); // Test for a 'meta' display without $content $response = $this->handler->property('datePublished')->display(); $this->assertEquals($response, "property='datePublished'"); // Test for a 'meta' display with $content $content = '01 January 2011'; $response = $this->handler->property('datePublished')->content($content)->display(); $this->assertEquals($response, "<meta property='datePublished' content='{$content}'/>{$content}"); // Test for a 'meta' display with human $content and $machineContent $machineContent = "2011-01-01T00:00:00+00:00"; $response = $this->handler->property('datePublished')->content($content, $machineContent)->display(); $this->assertEquals($response, "<meta property='datePublished' content='{$machineContent}'/>{$content}"); // Test when if fallbacks that the library returns an empty string as specified $response = $this->handler->content('en-GB')->property('doesNotExist')->display('meta', true); $this->assertEquals($response, ''); // Test if the library is disabled $response = $this->handler->enable(false)->content($content)->fallback('Article', 'about')->property('datePublished')->display(); $this->assertEquals($response, $content); // Test if the library is disabled and if it have a $content it must return an empty string $response = $this->handler->enable(false)->content('en-GB')->property('inLanguage')->fallback('Language', 'name')->display('meta', true); $this->assertEquals($response, ''); // Test if the params are reseted after display(), if the library is disabled $this->assertNull($this->handler->getFallbackProperty()); $this->assertNull($this->handler->getFallbackType()); $this->assertNull($this->handler->getProperty()); $this->assertNull($this->handler->getContent()); }
/** * Extract values from an object * * @param object $object * * @return array */ public function extract($object) { if (!$object instanceof Post) { return array(); } return array('id' => $object->getId(), 'title' => $object->getTitle(), 'slug' => $object->getSlug(), 'content' => $object->getContent(), 'created' => $object->getCreated()); }
/** * @param object $section * * @return \DateTime */ public function sectionUpdateDate($section) { if (!$section instanceof OnePageSection) { return null; } $modified = $section->getUpdatedAt(); $modified = max($modified, SeoContentLastModifiedGuesser::recursiveBlockUpdateDate($section->getContent())); $modified = max($modified, SeoContentLastModifiedGuesser::recursiveBlockUpdateDate($section->getBlock())); return $modified; }
/** * On load filter callback. * * @param object $asset */ public function filterLoad($asset) { // has url? if (!$asset->getUrl()) { return; } // set base path self::$path = dirname($asset->getUrl()) . '/'; $asset->setContent(preg_replace_callback('/url\\(\\s*[\'"]?(?![a-z]+:|\\/+)([^\'")]+)[\'"]?\\s*\\)/i', array($this, 'rewrite'), $asset->getContent())); }
/** * On content filter callback. * * @param object $asset */ public function filterContent($asset) { $images = array(); $content = $asset->getContent(); // get images and the related path if (preg_match_all('/url\\(\\s*[\'"]?([^\'"]+)[\'"]?\\s*\\)/Ui', $asset->getContent(), $matches)) { foreach ($matches[0] as $i => $url) { if ($path = realpath($asset['base_path'] . '/' . ltrim(preg_replace('/' . preg_quote($asset['base_url'], '/') . '/', '', $matches[1][$i], 1), '/'))) { $images[$url] = $path; } } } // check if image exists and filesize < 10kb foreach ($images as $url => $path) { if (filesize($path) <= 10240 && preg_match('/\\.(gif|png|jpg)$/i', $path, $extension)) { $content = str_replace($url, sprintf('url(data:image/%s;base64,%s)', str_replace('jpg', 'jpeg', strtolower($extension[1])), base64_encode(file_get_contents($path))), $content); } } $asset->setContent($content); }
/** * On content filter callback. * * @param object $asset */ public function filterContent($asset) { $content = $asset->getContent(); // move unresolved @import rules to the top $regexp = '/@import[^;]+;/i'; if (preg_match_all($regexp, $content, $matches)) { $content = preg_replace($regexp, '', $content); $content = implode("\n", $matches[0]) . "\n" . $content; } $asset->setContent($content); }
/** * Check the standardised FPDI PDF getContent() method * From fpdi_pdf_parser.php * @since 6.1.0 */ public function test_get_content() { /* Set Page 1*/ $this->fpdi_parser->pageno = 0; /* Get contents */ $content = $this->fpdi_parser->getContent(); /* Check if contains specific text */ $this->assertNotSame(false, strpos($content, 'MAIN HEADING')); $this->assertNotSame(false, strpos($content, 'Secondary Heading')); $this->assertNotSame(false, strpos($content, 'Blue Liquid Designs')); $this->assertSame(false, strpos($content, 'String Not In PDF')); }
/** * Test the display() function * * @return void * * @since 3.2 */ public function testDisplay() { // Setup $content = 'anything'; // Test display() with all null params $this->handler = new JMicrodata(); $this->assertEquals($this->handler->display(), ''); // Test if the params are reseted after display() $this->handler->setType('Article')->content($content)->property('name')->fallback('Thing', 'url')->display(); $this->assertNull($this->handler->getFallbackProperty()); $this->assertNull($this->handler->getFallbackType()); $this->assertNull($this->handler->getProperty()); $this->assertEmpty($this->handler->getContent()); // Test for a simple display $responce = $this->handler->property('url')->display(); $this->assertEquals($responce, "itemprop='url'"); // Test for a simple display with content $responce = $this->handler->property('url')->content($content)->display(); $this->assertEquals($responce, "<span itemprop='url'>{$content}</span>"); // Test for a simple display if the content is empty '' $responce = $this->handler->enable(true)->content('')->property('name')->display(); $this->assertEquals($responce, "<span itemprop='name'></span>"); // Test for a simple nested display $responce = $this->handler->property('author')->display(); $this->assertEquals($responce, "itemprop='author' itemscope itemtype='https://schema.org/Organization'"); // Test for a nested display with content $responce = $this->handler->property('author')->content($content)->display(); $this->assertEquals($responce, "<span itemprop='author' itemscope itemtype='https://schema.org/Organization'>{$content}</span>"); // Test for a nested display with content and Fallback $responce = $this->handler->fallback('Person', 'name')->property('author')->content($content)->display(); $this->assertEquals($responce, "<span itemprop='author' itemscope itemtype='https://schema.org/Person'><span itemprop='name'>{$content}</span></span>"); // Test for a nested display with Fallback and without content $responce = $this->handler->fallback('Person', 'name')->property('author')->display(); $this->assertEquals($responce, "itemprop='author' itemscope itemtype='https://schema.org/Person' itemprop='name'"); // Test for a meta display without content $responce = $this->handler->property('datePublished')->display(); $this->assertEquals($responce, "itemprop='datePublished'"); // Test for a meta display with content $content = '01 January 2011'; $responce = $this->handler->property('datePublished')->content($content)->display(); $this->assertEquals($responce, "<meta itemprop='datePublished' content='{$content}'/>{$content}"); // Test if the JMicrodata is disabled $responce = $this->handler->enable(false)->content($content)->fallback('Article', 'about')->property('datePublished')->display(); $this->assertEquals($responce, $content); // Test if JMicrodata is disabled and have a $content it must return an empty string $responce = $this->handler->enable(false)->content('en-GB')->property('inLanguage')->fallback('Language', 'name')->display('meta', true); $this->assertEquals($responce, ''); // Test if the params are reseted after display(), if the library is disabled $this->assertNull($this->handler->getFallbackProperty()); $this->assertNull($this->handler->getFallbackType()); $this->assertNull($this->handler->getProperty()); $this->assertNull($this->handler->getContent()); }
/** * Assert against an api response * @param object $response * @param boolean $fail * @return array $data */ protected function assertResponse($response, $statusCode = 200) { // Check status code $this->assertEquals((int) $statusCode, $response->getStatusCode(), "Response: " . $response->getContent()); // Response should be json $data = json_decode($response->getContent()); $this->assertNotEmpty($data, "Response was not JSON: " . $response->getContent()); // If fail response if (strpos('40', (string) $response->getStatusCode())) { // Error responses should contain error(s) $this->assertTrue(!empty($data->errors) || !empty($data->error), "Error response should contain error(s). Response: " . print_r($data, 1)); } return $data; }
/** * Create an element from attributes, including a DocBlock * * @param string $pkg_dir * @param object $Obj * @param boolean $graph whether to include related data * * @throws \Exception */ public function create($pkg_dir, $Obj, $graph) { $array = $Obj->toArray('', false, false, $graph); $content = $Obj->getContent(); $attributes = $this->getObjAttributes($content, $this->dox_start, $this->dox_end); // TODO: getProperties? if (!isset($attributes[$this->objectname])) { $name_attribute = $this->objectname; $name = $Obj->get($name_attribute); } else { $name = $attributes[$this->objectname]; } $dir = $this->Repoman->get_core_path($pkg_dir) . rtrim($this->Repoman->get($this->dir_key), '/'); $filename = $dir . '/' . $name . $this->write_ext; if (file_exists($filename) && !$this->Repoman->get('overwrite')) { throw new \Exception('Element already exists. Overwrite not allowed. ' . $filename); } // Create DocBlock if it doesn't already exist if (empty($attributes)) { $docblock = $this->dox_start . "\n"; $docblock .= $this->dox_pad . '@' . $this->objectname . ' ' . $array[$this->objectname] . "\n"; $docblock .= $this->dox_pad . '@description ' . $array['description'] . "\n"; $docblock .= $this->extend_docblock($Obj); $docblock .= $this->dox_end . "\n"; $this->modx->log(\modX::LOG_LEVEL_DEBUG, "DocBlock generated:\n" . $docblock); $content = $docblock . $content; } // Create dir if doesn't exist if (!file_exists($dir) && false === mkdir($dir, $this->Repoman->get('dir_mode'), true)) { throw new \Exception('Could not create directory ' . $dir); } if (false === file_put_contents($filename, $content)) { throw new \Exception('Could not write to file ' . $filename); } else { $this->modx->log(\modX::LOG_LEVEL_INFO, 'Created static element at ' . $filename); } // Do you want to mess with the original object? Or just grab a snapshot of it? if ($this->Repoman->get('move')) { $Obj->set('static', true); $Obj->set('static_file', self::path_to_rel($filename, MODX_BASE_PATH)); $Obj->set('source', $this->getSource()); if (!$Obj->save()) { throw new \Exception('Problem saving ' . $this->classname . ' ' . $array[$this->objectname]); } $this->modx->log(\modX::LOG_LEVEL_INFO, 'Original ' . $this->classname . ' ' . $array[$this->objectname] . ' updated to new location.'); } else { $this->modx->log(\modX::LOG_LEVEL_DEBUG, 'Original ' . $this->classname . ' ' . $array[$this->objectname] . ' copied only.'); } }
/** * Runs when new ticket is created * @param object $ticket * @param object $customer [optional] * @return */ public function ticketNew($ticket, $customer = null) { // If email notifications are not disabled, notify admin $department = $ticket->getDepartment(); /*parse content for send into mail*/ $originalContent = $ticket->getContent(); $parsedContent = $this->_parseContent($ticket); $ticket->setContent(nl2br($parsedContent)); $ticket->setContent($this->_processContent($ticket->getContent(), array('ticket' => $ticket, 'department' => $department))); if ($department->getEnabled() && $department->getNotify()) { // Notify admin/department $tpl = $department->getToAdminNewEmail(); $sender = array('name' => $customer->getName(), 'email' => $customer->getEmail()); $recipientEmail = $department->getContact(); $recipientName = $department->getName(); $mailTemplate = $this->_getMailTemplate($ticket, $department); if (!is_null($mailTemplate)) { $this->log('Sending mail about new ticket ' . $ticket->getUid() . ' to department ' . $recipientName . '<' . $recipientEmail . '>'); $mailTemplate->setDesignConfig(array('area' => 'backend', 'store' => $ticket->getStoreId()))->sendTransactional($tpl, $sender, $recipientEmail, $recipientName, array('ticket' => $ticket, 'department' => $department), $ticket->getStoreId()); } } if ($customer) { $recipientEmail = is_string($customer) ? $customer : $customer->getEmail(); $recipientName = is_string($customer) ? $customer : $customer->getName(); } else { $recipientEmail = $ticket->getCustomerEmail(); $recipientName = $ticket->getCustomerName(); } if ($recipientEmail) { if ($ticket->getCreatedBy() == 'admin') { $tpl = $department->getNewFromAdminToCustomer(); } else { $tpl = $department->getToCustomerNewEmail(); } $sender = $department->getSender(); $mailTemplate = $this->_getMailTemplate($ticket, $department); if (isset($mailTemplate) && !is_null($mailTemplate)) { $this->log('Sending mail about new ticket ' . $ticket->getUid() . ' to customer ' . $recipientName . '<' . $recipientEmail . '>'); $mailTemplate->setDesignConfig(array('area' => 'frontend', 'store' => $ticket->getStoreId()))->sendTransactional($tpl, $sender, $recipientEmail, $recipientName, array('ticket' => $ticket, 'department' => $department), $ticket->getStoreId()); } } $ticket->setContent($originalContent); }
/** * prepare a deserialized copy of a json response * * @param object $response Response containing our return value as raw json * * @return \Symfony\Component\BrowserKit\Response response * * @todo use JMSSerializer for additional JSON validation */ protected function filterResponse($response) { $this->results = json_decode($response->getContent()); return parent::filterResponse($response); }
/** * Returns text-formatted content for message or ticket with "quot marks" * @param object $item * @return */ public function getContentAsQuot($item) { $item->getContentType() || $item->setContentType(self::DEFAULT_CONTENT_TYPE); $text = $item->getContent(); switch ($item->getContentType()) { case 'text/html': case 'html': $text = $this->html2text($text); break; case 'text/plain': break; } $text = trim($text); $text = preg_replace('/\\[quot\\s+name="(.*)"\\](.*)\\[\\/quot\\]/Usi', '', $text); $item->getAuthorName() || $item->setAuthorName(self::DEFAULT_QUOTE_TEXT); return "[quot name=\"" . $item->getAuthorName() . "\"]" . trim(htmlentities($text, null, self::DEFAULT_ENCODING)) . "[/quot]"; }
/** * @param object|\PHPUnit_Framework_MockObject_MockObject $file * * @return array<string,mixed> */ private function mockBase64PreviewData($file) { $preview = ['preview' => base64_encode($file->getContent()), 'mimetype' => 'image/png']; return $preview; }
/** * @access protected * @param object $cell * @param integer $x * @param integer $y * @param integer $xdep * @param integer $ydep * @param integer $xfin * @param integer $yfin * @param string|array $data * @param string|array $dataRef * @return void */ protected function _setSum($cell, $x, $y, $xdep, $ydep, $xfin, $yfin, $data, &$dataRef) { $dataRef += $cell->getContent() * 1; }
/** * Get content for element. * * @param object $element */ private function _getContentForElement($element) { foreach ($this->_fields as $field) { switch ($field->type) { case 'Assets': case 'Categories': case 'Entries': case 'Users': if (!isset($field->settings['sources'])) { continue; } $sourceKey = is_array($field->settings['sources']) ? implode('-', $field->settings['sources']) : $field->settings['sources']; $additionalCriteria = array(); switch ($field->type) { case 'Assets': $elementType = ElementType::Asset; // Update key and criteria? if (isset($field->settings['restrictFiles']) && $field->settings['restrictFiles']) { $additionalCriteria['kind'] = $field->settings['allowedKinds']; $sourceKey .= is_array($field->settings['allowedKinds']) ? implode('-', $field->settings['allowedKinds']) : $field->settings['allowedKinds']; } break; case 'Categories': $elementType = ElementType::Category; break; case 'Entries': $elementType = ElementType::Entry; break; case 'Users': $elementType = ElementType::User; break; } $element->getContent()->setAttribute($field->handle, array($this->_getRandomElement($elementType, $sourceKey, $additionalCriteria))); break; case 'Checkboxes': case 'Dropdown': case 'RadioButtons': if (!isset($field->settings['options']) || !count($field->settings['options'])) { continue; } $randomOption = $field->settings['options'][mt_rand(0, count($field->settings['options']) - 1)]['value']; if ($field->type == 'Checkboxes') { $randomOption = array($randomOption); } $element->getContent()->setAttribute($field->handle, $randomOption); break; case 'Date': $element->getContent()->setAttribute($field->handle, new DateTime(null, new \DateTimeZone(craft()->getTimeZone()))); break; case 'Number': $min = isset($field->settings['min']) ? $field->settings['min'] : 0; $max = isset($field->settings['max']) ? $field->settings['max'] : 1000; $element->getContent()->setAttribute($field->handle, mt_rand($min, $max)); break; case 'PlainText': if (isset($field->settings['multiline']) && $field->settings['multiline']) { $element->getContent()->setAttribute($field->handle, $this->_getRandomText()); } else { $element->getContent()->setAttribute($field->handle, $this->_getRandomText(1, 'sentences')); } break; default: $element->getContent()->setAttribute($field->handle, $this->_getRandomText()); break; } } }
/** * decode attached parts * @param object $part */ function decodePlain($part) { $content = $part->getContent(); if (isset($part->contentTransferEncoding)) { switch ($part->contentTransferEncoding) { case 'base64': $content = base64_decode($content); break; case 'quoted-printable': $content = quoted_printable_decode($content); break; } } return $content; }
/** * @param object|\PHPUnit_Framework_MockObject_MockObject $file * @param $filename * * @return array */ private function mockDownloadData($file, $filename) { $download = ['preview' => $file->getContent(), 'mimetype' => $file->getMimeType()]; if ($download) { if (is_null($filename)) { $filename = $file->getName(); } $download['name'] = $filename; } return $download; }
/** * Apply filters and cache a asset * * @param string $file * @param object $asset * @param array $filters * @param array $options * @return object */ public function cache($file, $asset, $filters = array(), $options = array()) { // init vars $hash = substr($asset->hash(serialize($filters)), 0, 8); $options = array_merge(array('Gzip' => false), $options); // copy gzip file, if not exists if ($options['Gzip'] && !$this['path']->path('cache:gzip.php')) { @copy($this['path']->path('warp:gzip/gzip.php'), rtrim($this['path']->path('cache:'), '/') . '/gzip.php'); } // append cache file suffix based on hash if ($extension = pathinfo($file, PATHINFO_EXTENSION)) { $file = preg_replace('/' . preg_quote('.' . $extension, '/') . '$/', sprintf('-%s.%s', $hash, $extension), $file, 1); } else { $file .= '-' . $hash; } // create cache file, if not exists if (!$this['path']->path('cache:' . $file)) { $content = $asset->getContent($this['assetfilter']->create($filters)); // move unresolved @import rules to the top if (in_array('CssImportResolver', $filters)) { $regexp = '/@import[^;]+;/i'; if (preg_match_all($regexp, $content, $matches)) { $content = preg_replace($regexp, '', $content); $content = implode("\n", $matches[0]) . "\n" . $content; } } @file_put_contents($this['path']->path('cache:') . '/' . ltrim($file, '/'), $content); } $asset->setUrl($this['path']->url(($options['Gzip'] && $this['path']->path('cache:gzip.php') ? 'cache:gzip.php?' : 'cache:') . $file)); return $asset; }
/** * [doScrapping description] * @param [type] $endpoint [description] * @return [type] [description] */ private function doScrapping($endpoint) { $event = null; $earthquake = null; $i = 0; Debugger::dump('endpoint: ' . $endpoint . ' _' . $_SERVER['HTTP_USER_AGENT']); Debugger::dump('***INICIANDO SCRAPPING****'); $content = $this->scrapper->getContent($endpoint); if ($content) { $this->scrapper->domLoad($content); $tableList = $this->scrapper->findInDom('table tbody tr'); } else { Debugger::dump('***ERROR, NO SE OBTUBIERON DATOS'); } //get each table node foreach ($tableList as $tableKey => $table) { $earthquakeData = array(); //get each data item $i = 0; foreach ($table->find('td') as $itemKey => $tableItem) { $earthquakeData[$itemKey] = $tableItem->text(); $i++; } if ($i < 8 or empty($earthquakeData)) { continue; } $latDMSArr = Coordinates::extractDMS($earthquakeData[2]); $lonDMSArr = Coordinates::extractDMS($earthquakeData[3]); $lat = Coordinates::DMStoDEC($latDMSArr['coordinates'][0], $latDMSArr['coordinates'][1], 0, $latDMSArr['geoDir']); $lon = Coordinates::DMStoDEC($lonDMSArr['coordinates'][0], $lonDMSArr['coordinates'][1], 0, $lonDMSArr['geoDir']); $dateUTC = $earthquakeData[0]; $dateTs = DatesUtils::toTimestamp($dateUTC); $dateSQL = DatesUtils::toSQLDate($dateUTC); $eventData = array('lat' => $lat, 'lon' => $lon, 'ts' => $dateSQL, 'hash' => md5($dateTs)); /* Evitar crear eventos duplicados que muestren erroneamente más de un evento siendo que se trata del mismo * pero actualizado. * Esto se hace debido a que el primer informe ante un evento, puede ser preliminar * y se pueden publicar actualizaciones de datos con cambios en magnitud o ubicación geográfica posteriormente. */ $eventExists = $this->Event->checkForExists($eventData, $this->dateBounds); if ($eventExists['exists']) { Debugger::dump('***EVENTO YA EXISTE ****'); //echo ('evento ya existe <br>'); $event = $eventExists; } else { Debugger::dump('***NO SE ENCONTRO EVENTO, CREANDO ****'); $this->Event->create(); $event = $this->Event->save($eventData); } if ($event) { $metadatum = array('event_id' => $event['Event']['id'], 'agency_id' => 2, 'lat' => $eventData['lat'], 'lon' => $eventData['lon'], 'ts' => $dateSQL, 'depth' => $earthquakeData[4], 'magnitude' => floatval($earthquakeData[1]), 'geo_reference' => $earthquakeData[7], 'magnitude_type' => $earthquakeData[1] > 3.5 ? 'Mw' : 'Ml'); if (!$eventExists['exists']) { Debugger::dump('***SISMO NO EXISTE, CREANDO ****'); $this->EventMetadatum->create(); $earthquake = $this->EventMetadatum->save($metadatum); } else { $earthquakeExists = $this->EventMetadatum->checkForExists($metadatum, $this->dateBounds, $eventExists['Event']['id']); if ($earthquakeExists['exists']) { Debugger::dump('***EVENTO EXISTE, SISMO TAMBIEN ****'); } else { Debugger::dump('***EVENTO EXISTE, NUEVO SISMO NO. CREANDO NUEVO ASOCIADO A EVENTO****'); $this->EventMetadatum->create(); $earthquake = $this->EventMetadatum->save($metadatum); } } } } }
/** * Getter for content. * * @return string */ public function &getContent() { return $this->_controller->getContent(); }
/** * Apply filters and cache a asset * * @param string $file * @param object $asset * @param array $filters * @param array $options * @return object */ public function cache($file, $asset, $filters = array(), $options = array()) { // init vars $hash = substr($asset->hash(serialize($filters)), 0, 8); $options = array_merge(array('Gzip' => false), $options); // copy gzip file, if not exists if ($options['Gzip'] && !$this['path']->path('cache:gzip.php')) { @copy($this['path']->path('warp:gzip/gzip.php'), rtrim($this['path']->path('cache:'), '/') . '/gzip.php'); } // append cache file suffix based on hash if ($extension = pathinfo($file, PATHINFO_EXTENSION)) { $file = preg_replace('/' . preg_quote('.' . $extension, '/') . '$/', sprintf('-%s.%s', $hash, $extension), $file, 1); } else { $file .= '-' . $hash; } // create cache file, if not exists if (!$this['path']->path('cache:' . $file)) { $content = $asset->getContent($this['assetfilter']->create($filters)); @file_put_contents($this['path']->path('cache:') . '/' . ltrim($file, '/'), $content); } $asset->setUrl($this['path']->url(($options['Gzip'] && $this['path']->path('cache:gzip.php') ? 'cache:gzip.php?' : 'cache:') . $file)); return $asset; }