/** * Answer an array of slots that have sites and whose shortnames match a search string. * Use '*' as the wildcard. * * @param string $searchCriteria * @return array * @access public * @since 10/9/08 */ public function getSlotsWithSitesBySearch($searchCriteria) { if (!strlen($searchCriteria)) { return array(); } $searchCriteria = str_replace('*', '%', $searchCriteria); $query = new SelectQuery(); $query->addTable('segue_slot'); $query->addTable('segue_slot_owner AS all_owners', LEFT_JOIN, 'segue_slot.shortname = all_owners.shortname'); $query->addColumn('segue_slot.shortname', 'shortname'); $query->addColumn('segue_slot.site_id', 'site_id'); $query->addColumn('segue_slot.alias_target', 'alias_target'); $query->addColumn('segue_slot.type', 'type'); $query->addColumn('segue_slot.location_category', 'location_category'); $query->addColumn('segue_slot.media_quota', 'media_quota'); $query->addColumn('all_owners.owner_id', 'owner_id'); $query->addColumn('all_owners.removed', 'removed'); $query->addWhereLike('segue_slot.shortname', $searchCriteria); $query->addWhereNotEqual('segue_slot.site_id', ''); // printpre($query->asString()); $dbc = Services::getService('DBHandler'); $result = $dbc->query($query, IMPORTER_CONNECTION); return $this->getSlotsFromQueryResult($result); }
/** * Execute * * @return void * @access public * @since 3/26/08 */ public function execute() { if (!$this->isAuthorizedToExecute()) { throw new PermissionDeniedException('This command can only be run by admins or from the command-line.'); } header("Content-Type: text/plain"); if (RequestContext::value('help') || RequestContext::value('h') || RequestContext::value('?')) { throw new HelpRequestedException($this->usage); } $outDir = RequestContext::value('d'); if (empty($outDir)) { throw new InvalidArgumentException("An output directory must be specified.\n\n" . $this->usage); } if (!is_dir($outDir) || !is_writable($outDir)) { throw new InvalidArgumentException("The output directory doesn't exist or is not writeable.\n\n" . $this->usage); } foreach (SlotAbstract::getLocationCategories() as $category) { $baseUrl = SiteDispatcher::getBaseUrlForLocationCategory($category); if (!preg_match('/^https?:\\/\\/.+/', $baseUrl)) { throw new ConfigurationErrorException('Please set a base URL for the \'' . $category . '\' category with SiteDispatcher::setBaseUrlForLocationCategory($category, $url); in config/slots.conf.php'); } } while (ob_get_level()) { ob_end_flush(); } flush(); /********************************************************* * Check for a running export *********************************************************/ $dbc = Services::getService('DatabaseManager'); $query = new SelectQuery(); $query->addColumn('slot'); $query->addColumn('pid'); $query->addTable('site_export_queue'); $query->addWhereNotEqual('pid', 0); $result = $dbc->query($query); // If we are exporting, check the status of the export process if ($result->hasMoreRows()) { // Don't start a new export if one is running. if ($this->isRunning($result->field('pid'))) { print "An export is already running\n"; exit; } else { $query = new UpdateQuery(); $query->setTable('site_export_queue'); $query->addValue('status', 'DIED'); $query->addRawValue('pid', 'NULL'); $query->addValue('info', 'Process ' . $result->field('pid') . ' has died.'); $query->addWhereEqual('slot', $result->field('slot')); $query->addWhereEqual('pid', $result->field('pid')); $dbc->query($query); } } /********************************************************* * If there aren't any other exports happening, run our export *********************************************************/ // Find the next slot to update $query = new SelectQuery(); $query->addColumn('slot'); $query->addTable('site_export_queue', NO_JOIN, '', 'q'); $query->addTable('segue_slot', INNER_JOIN, 'q.slot = s.shortname', 's'); $query->addWhereNull('pid'); $query->addWhereNull('status'); $query->addWhereNull('alias_target'); $query->addWhereNotEqual('site_id', ''); $query->addOrderBy('priority', DESCENDING); $query->addOrderBy('slot', ASCENDING); $result = $dbc->query($query); // Exit if there is nothing to do. if (!$result->hasMoreRows()) { print "The queue is empty\n"; exit; } $slot = $result->field('slot'); $slotMgr = SlotManager::instance(); $slotObj = $slotMgr->getSlotByShortname($slot); $baseUrl = SiteDispatcher::getBaseUrlForLocationCategory($slotObj->getLocationCategory()); // Mark that we are running $query = new UpdateQuery(); $query->setTable('site_export_queue'); $query->addValue('pid', strval(getmypid())); $query->addWhereEqual('slot', $slot); $dbc->query($query); // Run the export $start = microtime(true); try { $exportDirname = $slot . "-html"; $exportDir = $outDir . "/" . $exportDirname; $archivePath = $outDir . '/' . $exportDirname . ".zip"; if (file_exists($exportDir)) { $this->deleteRecursive($exportDir); } mkdir($exportDir); if (file_exists($archivePath)) { unlink($archivePath); } // Set the user to be an admin. $idMgr = Services::getService("Id"); $authType = new Type("Authentication", "edu.middlebury.harmoni", "Harmoni DB"); $_SESSION['__AuthenticatedAgents']['Authentication::edu.middlebury.harmoni::Harmoni DB'] = $idMgr->getId('17008'); $authZ = Services::getService("AuthZ"); $isAuthorizedCache = $authZ->getIsAuthorizedCache(); $isAuthorizedCache->dirtyUser(); // Close the session. If we don't, a lock on the session file will // cause the request initiated via wget to hang. session_write_close(); // Do the export $urlParts = parse_url($baseUrl); $urlPrefix = rtrim($urlParts['path'], '/'); $include = array($urlPrefix . '/gui2', $urlPrefix . '/images', $urlPrefix . '/javascript', $urlPrefix . '/polyphony', $urlPrefix . '/repository', $urlPrefix . '/plugin_manager', $urlPrefix . '/rss', $urlPrefix . '/dataport/html/site/' . $slot); if (defined('WGET_PATH')) { $wget = WGET_PATH; } else { $wget = 'wget'; } if (defined('WGET_OPTIONS')) { $wgetOptions = WGET_OPTIONS; } else { $wgetOptions = ''; } $command = $wget . " " . $wgetOptions . " -r --page-requisites --html-extension --convert-links --no-directories -e robots=off " . "--directory-prefix=" . escapeshellarg($exportDir . '/content') . " " . "--include=" . escapeshellarg(implode(',', $include)) . " " . "--header=" . escapeshellarg("Cookie: " . session_name() . "=" . session_id()) . " " . escapeshellarg($baseUrl . '/dataport/html/site/' . $slot); print "Cookie: " . session_name() . "=" . session_id() . "\n"; // throw new Exception($command); exec($command, $output, $exitCode); if ($exitCode) { throw new Exception('Wget Failed. ' . implode("\n", $output)); } // Copy the main HTML file to index.html copy($exportDir . '/content/' . $slot . '.html', $exportDir . '/content/index.html'); // Copy the index.html file up a level to make it easy to find file_put_contents($exportDir . '/index.html', preg_replace('/(src|href)=([\'"])([^\'"\\/]+)([\'"])/', '$1=$2content/$3$4', file_get_contents($exportDir . '/content/index.html'))); // Zip up the result $archive = new ZipArchive(); if ($archive->open($archivePath, ZIPARCHIVE::CREATE) !== TRUE) { throw new Exception("Could not create zip archive."); } $this->addDirectoryToZip($archive, $exportDir, $exportDirname); $archive->close(); // Remove the directory $this->deleteRecursive($exportDir); // Mark our success $query = new UpdateQuery(); $query->setTable('site_export_queue'); $query->addRawValue('pid', 'NULL'); $query->addValue('status', 'SUCCESS'); $query->addValue('running_time', strval(round(microtime(true) - $start, 2))); $query->addWhereEqual('slot', $slot); $dbc->query($query); } catch (Exception $e) { $this->deleteRecursive($exportDir); if (file_exists($archivePath)) { unlink($archivePath); } // Mark our failure $query = new UpdateQuery(); $query->setTable('site_export_queue'); $query->addRawValue('pid', 'NULL'); $query->addValue('status', 'EXCEPTION'); $query->addValue('info', $e->getMessage()); $query->addValue('running_time', strval(round(microtime(true) - $start, 2))); $query->addWhereEqual('slot', $slot); $dbc->query($query); throw $e; } exit; }