public function execute($request)
 {
     if (!isset($request->limit)) {
         $request->limit = sfConfig::get('app_hits_per_page');
     }
     $this->resource = $this->getRoute()->resource;
     // Check that this isn't the root
     if (!isset($this->resource->parent)) {
         $this->forward404();
     }
     $query = QubitSearch::getInstance()->addTerm($this->resource->id, 'parentId');
     if (isset($request->query)) {
         $query = $request->query;
     }
     $query = QubitAcl::searchFilterByRepository($query, 'read');
     $query = QubitAcl::searchFilterDrafts($query);
     $this->pager = new QubitArrayPager();
     $this->pager->hits = QubitSearch::getInstance()->getEngine()->getIndex()->find($query);
     $this->pager->setMaxPerPage($request->limit);
     $this->pager->setPage($request->page);
     $ids = array();
     foreach ($this->pager->getResults() as $hit) {
         $ids[] = $hit->getDocument()->id;
     }
     $criteria = new Criteria();
     $criteria->add(QubitInformationObject::ID, $ids, Criteria::IN);
     $this->informationObjects = QubitInformationObject::get($criteria);
 }
 public function execute($request)
 {
     if (!isset($request->limit)) {
         $request->limit = sfConfig::get('app_hits_per_page');
     }
     if (isset($request->subquery)) {
         try {
             // Parse query string
             $query = QubitSearch::getInstance()->parse($request->subquery);
         } catch (Exception $e) {
             $this->error = $e->getMessage();
             return;
         }
     } else {
         $this->redirect(array('module' => 'actor', 'action' => 'browse'));
     }
     $query->addSubquery(QubitSearch::getInstance()->addTerm('QubitActor', 'className'), true);
     $query = QubitAcl::searchFilterByResource($query, QubitActor::getById(QubitActor::ROOT_ID));
     $this->pager = new QubitArrayPager();
     try {
         $this->pager->hits = QubitSearch::getInstance()->getEngine()->getIndex()->find($query);
     } catch (Exception $e) {
         $this->error = $e->getMessage();
         return;
     }
     $this->pager->setMaxPerPage($request->limit);
     $this->pager->setPage($request->page);
     $ids = array();
     foreach ($this->pager->getResults() as $hit) {
         $ids[] = $hit->getDocument()->id;
     }
     $criteria = new Criteria();
     $criteria->add(QubitActor::ID, $ids, Criteria::IN);
     $this->actors = QubitActor::get($criteria);
 }
 public function execute($request)
 {
     // Use existing symfony task
     $cacheClear = new sfCacheClearTask(sfContext::getInstance()->getEventDispatcher(), new sfFormatter());
     $cacheClear->run();
     // Clear the search index
     QubitSearch::getInstance()->getEngine()->erase();
     QubitSearch::getInstance()->optimize();
     $this->redirect(array('module' => 'sfInstallPlugin', 'action' => 'finishInstall'));
 }
 public function filterQuery($query)
 {
     // Limit search to current culture and info. objects
     $query->addSubquery(QubitSearch::getInstance()->addTerm('QubitInformationObject', 'className'), true);
     $query->addSubquery(QubitSearch::getInstance()->addTerm($this->context->user->getCulture(), 'culture'), true);
     $query = QubitAcl::searchFilterByRepository($query, 'read');
     $query = QubitAcl::searchFilterDrafts($query);
     // Limit to a repository if in context
     if (isset($this->getRoute()->resource) && $this->getRoute()->resource instanceof QubitRepository) {
         $query->addSubquery(QubitSearch::getInstance()->addTerm($this->getRoute()->resource->id, 'repositoryId'), true);
         $this->title .= $this->context->i18n->__(' in %1%', array('%1%' => $this->getRoute()->resource->authorizedFormOfName));
     }
     return $query;
 }
 public function execute($request)
 {
     parent::execute($request);
     $this->addFields();
     $this->title = $this->context->i18n->__('Global search/replace');
     if ($request->isMethod('post')) {
         // Make sure we have required information for search/replace
         if (empty($request->pattern) || empty($request->replacement)) {
             $this->error = $this->context->i18n->__('Both source and replacement fields are required.');
             return;
         } else {
             if (!isset($request->confirm)) {
                 $this->title = $this->context->i18n->__('Are you sure you want to replace "%1%" with "%2%" in %3%?', array('%1%' => $request->pattern, '%2%' => $request->replacement, '%3%' => sfInflector::humanize(sfInflector::underscore($request->column))));
                 return;
             }
         }
         // Process replacement on each IO
         // NB: could this be made faster by reading a batch of IDs?
         foreach ($this->pager->hits as $hit) {
             $io = QubitInformationObject::getById($hit->getDocument()->id);
             if (isset($request->allowRegex)) {
                 $pattern = '/' . strtr($request->pattern, array('/' => '\\/')) . '/';
                 if (!isset($request->caseSensitive)) {
                     $pattern .= 'i';
                 }
                 $replacement = strtr($request->replacement, array('/' => '\\/'));
                 $replaced = preg_replace($pattern, $replacement, $io->__get($request->column));
             } elseif (isset($request->caseSensitive)) {
                 $replaced = str_replace($request->pattern, $request->replacement, $io->__get($request->column));
             } else {
                 $replaced = str_ireplace($request->pattern, $request->replacement, $io->__get($request->column));
             }
             $io->__set($request->column, $replaced);
             $io->save();
         }
         // force refresh of index to keep sync
         QubitSearch::getInstance()->optimize();
         // When complete, redirect to GSR home
         $this->redirect(array('module' => 'search', 'action' => 'globalReplace'));
     }
 }
 public function execute($request)
 {
     if (!$this->context->user->hasCredential(array('contributor', 'editor', 'administrator'), false)) {
         QubitAcl::forwardUnauthorized();
     }
     if (!isset($request->limit)) {
         $request->limit = sfConfig::get('app_hits_per_page');
     }
     if (isset($request->subquery)) {
         try {
             // Parse query string
             $query = QubitSearch::getInstance()->parse($request->subquery);
         } catch (Exception $e) {
             $this->error = $e->getMessage();
             return;
         }
     } else {
         $this->redirect(array('module' => 'donor', 'action' => 'browse'));
     }
     $query->addSubquery(QubitSearch::getInstance()->addTerm('QubitDonor', 'className'), true);
     $this->pager = new QubitArrayPager();
     try {
         $this->pager->hits = QubitSearch::getInstance()->getEngine()->getIndex()->find($query);
     } catch (Exception $e) {
         $this->error = $e->getMessage();
         return;
     }
     $this->pager->setMaxPerPage($request->limit);
     $this->pager->setPage($request->page);
     $ids = array();
     foreach ($this->pager->getResults() as $hit) {
         $ids[] = $hit->getDocument()->id;
     }
     $criteria = new Criteria();
     $criteria->add(QubitDonor::ID, $ids, Criteria::IN);
     $this->donors = QubitDonor::get($criteria);
 }
예제 #7
0
 /**
  * Filter search query by resource specific ACL
  *
  * @param $query
  * @param mixed $root - root object for list
  * @return query
  */
 public static function searchFilterByResource($query, $root)
 {
     $user = sfContext::getInstance()->user;
     $permissions = self::getUserPermissionsByAction($user, get_class($root), 'read');
     // Build access control list
     $grants = 0;
     if (0 < count($permissions)) {
         foreach ($permissions as $permission) {
             if (!isset($resourceAccess[$permission->objectId])) {
                 $resourceAccess[$permission->objectId] = self::isAllowed($user, $permission->objectId, 'read');
                 if ($resourceAccess[$permission->objectId]) {
                     $grants++;
                 }
             }
         }
     }
     // If no grants then user can't see anything
     if (0 == $grants) {
         self::forwardUnauthorized();
     } else {
         if (!self::isAllowed($user, $root->id, 'read')) {
             $allows = array_keys($resourceAccess, true, true);
             while ($resourceId = array_shift($allows)) {
                 $query->addSubquery(QubitSearch::getInstance()->addTerm($resourceId, 'id'), true);
             }
         } else {
             $bans = array_keys($resourceAccess, false, true);
             while ($resourceId = array_shift($bans)) {
                 $query->addSubquery(QubitSearch::getInstance()->addTerm($resourceId, 'id'), false);
             }
         }
     }
     return $query;
 }
 public function execute($request)
 {
     // Default items per page
     if (!isset($request->limit)) {
         $request->limit = sfConfig::get('app_hits_per_page');
     }
     $this->form = new sfForm();
     $this->resource = $this->getRoute()->resource;
     // Check that the object exists and that it is not the root
     if (!isset($this->resource) || !isset($this->resource->parent)) {
         $this->forward404();
     }
     // Check authorization
     if (!QubitAcl::check($this->resource, 'update')) {
         QubitAcl::forwardUnauthorized();
     }
     // "parent" form field
     $this->form->setValidator('parent', new sfValidatorString(array('required' => true)));
     $this->form->setWidget('parent', new sfWidgetFormInputHidden());
     // Root is default parent
     if ($this->resource instanceof QubitInformationObject) {
         $this->form->bind($request->getGetParameters() + array('parent' => $this->context->routing->generate(null, array(QubitInformationObject::getById(QubitInformationObject::ROOT_ID), 'module' => 'informationobject'))));
     } else {
         if ($this->resource instanceof QubitTerm) {
             $this->form->bind($request->getGetParameters() + array('parent' => $this->context->routing->generate(null, array(QubitTerm::getById(QubitTerm::ROOT_ID), 'module' => 'term'))));
         }
     }
     if ($request->isMethod('post')) {
         $this->form->bind($request->getPostParameters());
         if ($this->form->isValid()) {
             $params = $this->context->routing->parse(Qubit::pathInfo($this->form->parent->getValue()));
             // In term treeview, root node links (href) to taxonomy, but it represents the term root object
             if ($this->resource instanceof QubitTerm && QubitObject::getById($params['_sf_route']->resource->id) instanceof QubitTaxonomy) {
                 $this->resource->parentId = QubitTerm::ROOT_ID;
             } else {
                 $this->resource->parentId = $params['_sf_route']->resource->id;
             }
             $this->resource->save();
             if ($request->isXmlHttpRequest()) {
                 return $this->renderText('');
             } else {
                 if ($this->resource instanceof QubitInformationObject) {
                     $this->redirect(array($this->resource, 'module' => 'informationobject'));
                 } else {
                     if ($this->resource instanceof QubitTerm) {
                         $this->redirect(array($this->resource, 'module' => 'term'));
                     }
                 }
             }
         }
     }
     $params = $this->context->routing->parse(Qubit::pathInfo($this->form->parent->getValue()));
     $this->parent = QubitObject::getById($params['_sf_route']->resource->id);
     $query = QubitSearch::getInstance()->addTerm($this->parent->slug, 'parent');
     if (isset($request->query)) {
         $query = $request->query;
     }
     $this->pager = new QubitArrayPager();
     $this->pager->hits = QubitSearch::getInstance()->getEngine()->getIndex()->find($query);
     $this->pager->setMaxPerPage($request->limit);
     $this->pager->setPage($request->page);
     $ids = array();
     foreach ($this->pager->getResults() as $hit) {
         $ids[] = $hit->getDocument()->id;
     }
     $criteria = new Criteria();
     $criteria->add(QubitObject::ID, $ids, Criteria::IN);
     $this->results = QubitObject::get($criteria);
 }
 public function filterQuery($query)
 {
     // limit to a repository if selected
     if (!empty($this->request->repository)) {
         $query->addSubquery(QubitSearch::getInstance()->addTerm($this->request->repository, 'repositoryId'), true);
         $this->queryTerms[] = array('term' => $this->getContext()->i18n->__('Repository') . ': ' . QubitRepository::getById($this->request->repository)->__toString(), 'operator' => 'and');
     }
     // digital object filters
     if ('true' == $this->request->hasDigitalObject) {
         $query->addSubquery(QubitSearch::getInstance()->addTerm('true', 'hasDigitalObject'), true);
         $this->queryTerms[] = array('term' => $this->getContext()->i18n->__('Digital object is available'), 'operator' => 'and');
     } else {
         if ('false' == $this->request->hasDigitalObject) {
             $query->addSubquery(QubitSearch::getInstance()->addTerm('false', 'hasDigitalObject'), true);
             $this->queryTerms[] = array('term' => $this->getContext()->i18n->__('No digital object is available'), 'operator' => 'and');
         }
     }
     if (!empty($this->request->mediatype)) {
         $query->addSubquery(QubitSearch::getInstance()->addTerm($this->request->mediatype, 'do_mediaTypeId'), true);
         $this->queryTerms[] = array('term' => 'mediatype: ' . QubitTerm::getById($this->request->mediatyp)->__toString(), 'operator' => 'and');
     }
     $query = parent::filterQuery($query);
     return $query;
 }
 protected function execute($arguments = array(), $options = array())
 {
     $timer = new QubitTimer();
     // overall timing
     $context = sfContext::createInstance($this->configuration);
     if (empty($arguments['folder']) || !file_exists($arguments['folder'])) {
         throw new sfException('You must specify a valid import folder or file');
     }
     // Set indexing preference
     if ($options['noindex']) {
         QubitSearch::getInstance()->disabled = true;
     } else {
         QubitSearch::getInstance()->getEngine()->enableBatchMode();
     }
     if (is_dir($arguments['folder'])) {
         // Recurse into the import folder
         $files = $this->dir_tree(rtrim($arguments['folder'], '/'));
     } else {
         $files = array($arguments['folder']);
     }
     // TODO: Add some colour
     $this->log("Importing " . count($files) . " files from " . $arguments['folder'] . " (indexing is " . ($options['noindex'] ? "DISABLED" : "ENABLED") . ") ...\n");
     $count = 0;
     $total = count($files);
     foreach ($files as $file) {
         $start = microtime(true);
         // Choose import type based on file extension, eg. csv, xml
         if ('csv' == pathinfo($file, PATHINFO_EXTENSION)) {
             $importer = new QubitCsvImport();
             $importer->import($file, $options);
         } elseif ('xml' == pathinfo($file, PATHINFO_EXTENSION)) {
             $importer = new QubitXmlImport();
             $importer->import($file, array('strictXmlParsing' => false));
         } else {
             // Move on to the next file
             continue;
         }
         // Try to free up memory
         unset($importer);
         $count++;
         $split = microtime(true) - $start;
         // Store details if output is specified
         if ($options['output']) {
             $rows[] = array($count, $split, memory_get_usage());
         }
         if ($options['v']) {
             $this->log(basename($file) . " imported (" . round($split, 2) . " s) (" . $count . "/" . $total . ")");
         }
     }
     // Create/open output file if specified
     if ($options['output']) {
         $fh = fopen($options['output'], 'w+');
         foreach ($rows as $row) {
             fputcsv($fh, $row);
         }
         fputcsv($fh, array('', $timer->elapsed(), memory_get_peak_usage()));
         fclose($fh);
     }
     // Optimize index if enabled
     if (!$options['noindex']) {
         QubitSearch::getInstance()->getEngine()->optimize();
     }
     $this->log("\nSuccessfully imported " . $count . " XML/CSV files in " . $timer->elapsed() . " s. " . memory_get_peak_usage() . " bytes used.");
 }
 public function execute($request)
 {
     $this->timer = new QubitTimer();
     $file = $request->getFiles('file');
     // if we got here without a file upload, go to file selection
     if (!isset($file)) {
         $this->redirect(array('module' => 'object', 'action' => 'importSelect'));
     }
     // set indexing preference
     if ($request->getParameter('noindex')) {
         QubitSearch::getInstance()->disabled = true;
     } else {
         QubitSearch::getInstance()->getEngine()->enableBatchMode();
     }
     try {
         // choose import type based on file extension, eg. csv, xml
         if ('csv' == pathinfo($file['name'], PATHINFO_EXTENSION)) {
             $importer = new QubitCsvImport();
             $importer->import($file['tmp_name'], array('schema' => $request->getParameter('schema')));
         } elseif ('xml' == pathinfo($file['name'], PATHINFO_EXTENSION)) {
             $importer = new QubitXmlImport();
             $importer->import($file['tmp_name'], array('strictXmlParsing' => false));
         } elseif ('zip' == pathinfo($file['name'], PATHINFO_EXTENSION) && class_exists('ZipArchive')) {
             $zipFolder = $file['tmp_name'] . '-zip';
             if (!file_exists($zipFolder)) {
                 mkdir($zipFolder, 0755);
             }
             // extract the zip archive into the temporary folder
             // TODO: need some error handling here
             $zip = new ZipArchive();
             $zip->open($file['tmp_name']);
             $zip->extractTo($zipFolder);
             $zip->close();
             $files = $this->dir_tree($zipFolder);
             // this code is from lib/importBulkTask.class.php
             foreach ($files as $import_file) {
                 // try to free up memory
                 unset($importer);
                 // choose import type based on file extension, eg. csv, xml
                 if ('csv' == pathinfo($import_file, PATHINFO_EXTENSION)) {
                     $importer = new QubitCsvImport();
                     $importer->import($import_file, array('schema'));
                 } elseif ('xml' == pathinfo($import_file, PATHINFO_EXTENSION)) {
                     $importer = new QubitXmlImport();
                     $importer->import($import_file, array('strictXmlParsing' => false));
                 } else {
                     // move on to the next file
                     continue;
                 }
             }
         } else {
             $this->context->user->setFlash('error', $this->context->i18n->__('Unable to import selected file'));
             $this->redirect(array('module' => 'object', 'action' => 'importSelect'));
         }
     } catch (sfException $e) {
         $this->context->user->setFlash('error', $e->getMessage());
         $this->redirect(array('module' => 'object', 'action' => 'importSelect'));
     }
     // optimize index if enabled
     if (!$request->getParameter('noindex')) {
         QubitSearch::getInstance()->getEngine()->optimize();
     }
     $this->errors = $importer->getErrors();
     $this->rootObject = $importer->getRootObject();
     $this->objectType = strtr(get_class($this->rootObject), array('Qubit' => ''));
 }