protected function reindexAll() { $nq = new NodeQuery(); $nq->setParameter('Elements.in', $this->Index->getElements()); $nq->setParameter('NodeRefs.only', true); $all = $this->NodeService->findAll($nq)->getResults(); foreach ($all as $nodeRef) { $this->Index->reindex($nodeRef); } }
protected function migratePrimaryImage() { $elements = $this->ElementService->findAllWithAspect('@mixin-primary-image'); $this->TransactionManager->commit(); $step = 200; foreach ($elements as $element) { echo "Migrate element [{$element->Slug}]\n"; for (;;) { $q = new NodeQuery(); $q->setParameter('Elements.in', $element->Slug)->setParameter('OutTags.exist', '#primary-media')->setParameter('OutTags.select', '#primary-media')->setLimit($step); $q = $this->NodeService->findAll($q); if (!$q->hasResults()) { echo "No more results\n"; break; } $this->TransactionManager->begin(); foreach ($q->getResults() as $node) { $mediaNode = $node->getOutTag('#primary-media')->TagLinkNode; $node->removeOutTags('#primary-media'); if (!$mediaNode->Element->hasAspect('@images')) { echo "Linked node does not have @images.\n"; continue; } else { $node->addOutTag(new Tag($mediaNode->NodeRef, null, '#primary-image')); } echo "Updating node [{$node->NodeRef}]\n"; $this->NodeService->edit($node); } $this->TransactionManager->commit(); } } echo "Done.\n"; }
public function preEdit(NodeRef $nodeRef, Node $node) { // node has no file if (!($originalFileTag = $node->getOutTag('#original'))) { return; } // node already has thumbnails that are going to be added if ($node->hasOutTags('#thumbnails')) { return; } $q = new NodeQuery(); $q->setParameter('Elements.in', $nodeRef->Element->Slug)->setParameter('Slugs.in', $nodeRef->Slug)->setParameter('OutTags.exist', '#thumbnails')->setParameter('Count.only', true); // already has thumbnails if ($this->NodeService->findAll($q)->getTotalRecords()) { return; } $file = $this->FileService->retrieveFileFromNode($nodeRef->Element, new NodeRef($this->ElementService->getBySlug($originalFileTag->TagElement), $originalFileTag->TagSlug)); if (!$file) { throw new ThumbnailsException('Could not retrieve original file!'); } $originalFilePath = $file->getLocalPath(); $thumbnailFileNodes = $this->ImageService->createInitialThumbnails($node, $originalFilePath); $newThumbnailTags = array(); foreach ($thumbnailFileNodes as $size => $thumbnailFileNode) { $tag = new Tag($thumbnailFileNode->getElement()->getSlug(), $thumbnailFileNode->Slug, '#thumbnails', $size, $size); $newThumbnailTags[] = $tag; } $node->replaceOutTags('#thumbnails', $newThumbnailTags); }
protected function go() { if (!isset($this->Index)) { throw new Exception('Missing property Index on node search controller'); } $dto = new NodeQuery(); if ($this->getTemplateVariable('Site.all') == null) { if ($this->getTemplateVariable('Sites.in') == null) { $site = $this->RequestContext->getSite(); $dto->setParameter('Sites.in', $site->getSlug()); } $this->passthruTemplateVariable($dto, 'Sites.in'); // $this->passthruTemplateVariable($dto, 'SiteIDs.in'); } if ($this->getTemplateVariable('Elements.in') == null) { $dto->setParameter('Elements.in', $this->Index->getElements()); } $this->passthruTemplateVariable($dto, 'Elements.in'); $dto->setParameter('Status.isActive', true); $this->passthruTemplateVariable($dto, 'SearchKeywords'); $this->passthruTemplateVariable($dto, 'SearchThreshold'); $this->passthruTemplateVariable($dto, 'Meta.select'); $this->passthruTemplateVariable($dto, 'OutTags.select'); $this->passthruTemplateVariable($dto, 'InTags.select'); $this->passthruTemplateVariable($dto, 'ActiveDate.before'); $this->passthruTemplateVariable($dto, 'ActiveDate.after'); $this->passthruTemplateVariable($dto, 'ActiveDate.start'); $this->passthruTemplateVariable($dto, 'ActiveDate.end'); $this->passthruTemplateVariable($dto, 'CreationDate.before'); $this->passthruTemplateVariable($dto, 'CreationDate.after'); $this->passthruTemplateVariable($dto, 'CreationDate.start'); $this->passthruTemplateVariable($dto, 'CreationDate.end'); $this->passthruTemplateVariable($dto, 'OutTags.exist'); $this->passthruTemplateVariable($dto, 'InTags.exist'); $this->passthruTemplateVariable($dto, 'Meta.exist'); return $this->readNodeQuery($this->Index->search($dto)); }
protected function fetchMaxChildTreeID(Node $myNode, TagDef $treeOriginTagDef, TagPartial $treeOriginPartial, $parentTreeID = '') { $nq = new NodeQuery(); $nq->setParameter('Status.all', true); $nq->setParameter('TreeID.childOf', $parentTreeID); $nq->setParameter('Elements.in', $myNode->getNodeRef()->getElement()->getSlug()); if ($treeOriginTagDef->Direction == 'out') { $nq->setParameter('OutTags.exist', $treeOriginPartial->toString()); } else { $nq->setParameter('InTags.exist', $treeOriginPartial->toString()); } $nq->setOrderBy('TreeID', 'DESC'); $nq->setLimit(1); $result = $this->NodeFindAllDAO->findAll($nq, true)->getResult(); if (!empty($result)) { $lastNodeTreeID = $result['TreeID']; // if on the same level, return blank, in order to append as first child if (strcmp($lastNodeTreeID, $parentTreeID) === 0) { // error_log('No children, same level'); return ''; } else { if (empty($parentTreeID)) { return substr($lastNodeTreeID, 0, 4); } } // error_log('Last TreeID = '.$lastNodeTreeID); return $lastNodeTreeID; } return ''; }
protected function elementHasRecords($slug) { $nq = new NodeQuery(); $nq->setParameter('Elements.in', $slug); // $nq->setParameter('Status.all', true); $nq->setLimit(1); $count = $this->NodeService->findAll($nq)->getResult(); return $count !== null; }
/** * sets the meta where params that exist in the calling * template on the node query. * * @param NodeQuery $nq */ private function setMetaWhereParams(NodeQuery $nq) { foreach ($this->templateVars as $name => $value) { if (strpos($name, '#') === 0) { $nq->setParameter($name, $value); } } }
public function tagGalleryPhotos() { $galleryNodeRef = $this->NodeRefService->parseFromString('media-gallery:test-gallery'); $interval = 25; $offset = 0; $nq = new NodeQuery(); $nq->setParameter('Elements.in', 'image'); $nq->setParameter('NodeRefs.only', true); $nq->setOrderBy('ActiveDate', 'DESC'); $nq->setLimit($interval); $nq->setOffset($offset); $nq = $this->NodeService->findAll($nq, true); $nodes = $nq->getResults(); while (count($nodes) > 0) { foreach ($nodes as $node) { $tag = new Tag($node->getElement()->getSlug(), $node->getSlug(), '#media-gallery-items'); try { $this->NodeService->addOutTag($galleryNodeRef, $tag); echo $node->Slug . "\n"; } catch (Exception $e) { echo $node->Slug . " error: " . $e->getMessage() . "\n"; } } $offset = $offset + $interval; unset($nodes); $nq->setLimit($interval); $nq->setOffset($offset); $nq->clearResults(); $nq = $this->NodeService->findAll($nq, true); $nodes = $nq->getResults(); } }
protected function single() { $dto = new NodeQuery(); $dto->setLimit(1); if ($this->getTemplateVariable('Site.all') == null) { if ($this->getTemplateVariable('Sites.in') == null) { $site = $this->RequestContext->getSite(); $dto->setParameter('Sites.in', $site->getSlug()); } $this->passthruTemplateVariable($dto, 'Sites.in'); //$this->passthruTemplateVariable($dto, 'SiteIDs.in'); } $this->passthruTemplateVariable($dto, 'Elements.in'); // $this->passthruTemplateVariable($dto, 'Slugs.in'); $this->passthruTemplateVariable($dto, 'Meta.select'); $this->passthruTemplateVariable($dto, 'OutTags.select'); $this->passthruTemplateVariable($dto, 'InTags.select'); // $this->passthruTemplateVariable($dto, 'Sections.select'); // $this->passthruTemplateVariable($dto, 'Title.like'); // $this->passthruTemplateVariable($dto, 'Title.ieq'); // $this->passthruTemplateVariable($dto, 'Title.eq'); // $this->passthruTemplateVariable($dto, 'Title.firstChar'); // // if(!$this->Permissions->checkPermission('cms-view')) { // $dto->setParameter('Status.isActive', true); // } // $this->passthruTemplateVariable($dto, 'ActiveDate.before'); // $this->passthruTemplateVariable($dto, 'ActiveDate.after'); // $this->passthruTemplateVariable($dto, 'ActiveDate.start'); // $this->passthruTemplateVariable($dto, 'ActiveDate.end'); // // $this->passthruTemplateVariable($dto, 'CreationDate.before'); // $this->passthruTemplateVariable($dto, 'CreationDate.after'); // $this->passthruTemplateVariable($dto, 'CreationDate.start'); // $this->passthruTemplateVariable($dto, 'CreationDate.end'); // // $this->passthruTemplateVariable($dto, 'OutTags.exist'); // $this->passthruTemplateVariable($dto, 'InTags.exist'); // $this->passthruTemplateVariable($dto, 'Meta.exist'); // $this->passthruTemplateVariable($dto, 'Sections.exist'); // // foreach($this->templateVars as $name => $value) // { // if(strpos($name, '#') === 0) // $dto->setParameter($name, $value); // } $slug = $this->getTemplateVariable('Slugs.in'); if ($slug != null) { $dto->setParameter('Slugs.in', $slug); $dto = $this->NodeRefService->normalizeNodeQuery($dto); $nodeRefs = $dto->getParameter('NodeRefs.normalized'); $nodePartials = $dto->getParameter('NodePartials.eq'); $allFullyQualified = $dto->getParameter('NodeRefs.fullyQualified'); if (!$allFullyQualified || count($nodeRefs) > 1) { $this->Logger->debug('node-single query is not fully-qualified NodeRef or more than 1 node is being returned'); $dto->setResults(array()); } else { $row = $this->RegulatedNodeService->getByNodeRef(current($nodeRefs), $nodePartials, $this->getTemplateVariable('ForceReadWrite') != null ? StringUtils::strToBool($this->getTemplateVariable('ForceReadWrite')) : false); $showPending = StringUtils::strToBool($this->getTemplateVariable('Status.allowInactive')) || $this->Permissions->checkPermission('cms-view'); //Allow draft records to be viewed if exact URL is known. if (empty($row)) { $this->Logger->debug('Row not found'); $dto->setResults(array()); } else { if ($row->Status == 'deleted') { $this->Logger->debug('Node status is deleted'); $dto->setResults(array()); } else { if (!$row->isIsActive() && !$showPending) { $this->Logger->debug('Record is not active (and user is not permitted to see pending status)'); $dto->setResults(array()); } else { $this->Logger->debug('Found node [' . $row->getNodeRef() . ']'); $this->Events->trigger('NodeWebController.single', $this->templateVars, $row); foreach ((array) $row->getNodeRef()->getElement()->getAspects() as $aspect) { $this->Events->trigger('NodeWebController' . '.@' . $aspect->Slug . '.' . 'single', $this->templateVars, $row); } $dto->setResults(array($row)); } } } } } else { $dto->setResults(array()); } return $this->readNodeQuery($dto); }
protected function interactive() { echo "\nWelcome to the Crowd Fusion Node Database Monitor. Commands end with ; or newline.\n"; echo "Server version: " . $this->VersionService->getCrowdFusionVersion() . "\n"; echo "\n"; echo "Type 'help' for help. Type 'exit' to quit.\n"; $codeBuffer = null; $history = array(); do { echo "\ncf> "; /* $ans = strtolower( trim( `bash -c "read -n3 KEY; echo -n \\\$KEY | grep '\[A'"` ) ); $ans = bin2hex($ans); if($ans == '1b5b61') echo "up arrow\n\n"; //print_r(bin2hex($ans)); exit; $ch = fgetc(STDIN); var_dump(ord($ch)); exit; */ $input = trim(fgets(STDIN)); $input = rtrim($input, ';'); try { if ($input != 'exit' && $input != 'quit') { $parts = preg_split('/\\s+/', $input); $action = strtolower($parts[0]); //********************************************* //*** RUN ************************************* //********************************************* if ($action == 'run') { if (empty($history)) { $input = 'hist'; } else { $input = $history[0]; if (isset($parts[1])) { if (isset($history[intval($parts[1]) - 1])) { $input = $history[intval($parts[1]) - 1]; } else { $input = 'hist'; } } } $parts = preg_split('/\\s+/', $input); $action = strtolower($parts[0]); } //********************************************* //*** HISTORY ********************************* //********************************************* if ($action == 'hist' || $action == 'history') { if (empty($history)) { echo "Empty history\n"; } else { $num = 10; if (isset($parts[1]) && is_numeric($parts[1])) { $num = intval($parts[1]); } for ($i = 0; $i < $num; $i++) { if (isset($history[$i])) { echo " " . ($i + 1) . ") " . $history[$i] . "\n"; } } } } else { if ($action == 'show') { $target = isset($parts[1]) ? strtolower($parts[1]) : null; //************ //* ELEMENTS * //************ if ($target == 'elements') { $start = microtime(true); $elements = $this->ElementService->findAll()->getResults(); $data = array(); foreach ($elements as $element) { $data['Name'][] = $element->Name; $data['Slug'][] = $element->Slug; $data['Description'][] = $element->Description; } $end = microtime(true); $this->printTable($data, $end - $start); } else { if ($target == 'aspects') { //*************** //* FOR ELEMENT * //*************** if (isset($parts[2])) { if (strtolower($parts[2]) != 'for') { throw new Exception('Syntax Error. Expected "for [element slug]"'); } if (!isset($parts[3])) { throw new Exception('Syntax Error. Expected "[element slug]"'); } $target = strtolower($parts[3]); $start = microtime(true); $target = $this->ElementService->getBySlug($target); $aspects = $target->getAspects(); $data = array(); foreach ($aspects as $aspect) { $data['Name'][] = $aspect->Name; $data['Slug'][] = '@' . $aspect->Slug; } $end = microtime(true); $this->printTable($data, $end - $start); } else { $start = microtime(true); $aspects = $this->AspectService->findAll()->getResults(); $data = array(); foreach ($aspects as $aspect) { $data['Name'][] = $aspect->Name; $data['Slug'][] = '@' . $aspect->Slug; $data['Description'][] = $aspect->Description; } $end = microtime(true); $this->printTable($data, $end - $start); } } else { if ($target == 'code') { if (empty($codeBuffer)) { echo "Code snippet buffer is empty. Please execute a query then type 'show code'.\n"; } else { echo "\n"; if (is_array($codeBuffer)) { echo implode("\n", $codeBuffer); } else { echo $codeBuffer; } echo "\n"; } } else { throw new Exception('Syntax Error. Expected "elements" or "aspects"'); } } } } else { if ($action == 'help') { echo "help is coming...\n"; } else { if ($action == 'describe' || $action == 'desc') { $target = strtolower($parts[1]); if (substr($target, 0, 1) == '@') { $start = microtime(true); $target = $this->AspectService->getBySlug(ltrim($target, '@')); $schema = $target->Schema; $data = array(); $metaDefs = $schema->getMetaDefs(); foreach ($metaDefs as $metaDef) { $validation = $metaDef->Validation->getValidationArray(); $data['Type'][] = 'Meta'; $data['Role'][] = '#' . $metaDef->Id; $data['Title'][] = $metaDef->Title; $data['Datatype'][] = $metaDef->Datatype; $data['Default'][] = $metaDef->Default; $data['Nullable'][] = $validation['nullable'] ? 'true' : 'false'; $data['Match'][] = $validation['match']; $data['Min'][] = $validation['min']; $data['Max'][] = $validation['max']; $data['Sortable'][] = ''; $data['QuickAdd'][] = ''; $data['Multiple'][] = ''; $data['Fieldlike'][] = ''; } //sortable,quickadd,filter,fieldlike $tagDefs = $schema->getTagDefs(); foreach ($tagDefs as $tagDef) { //$validation = $metaDef->Validation->getValidationArray(); $data['Type'][] = ucfirst($tagDef->Direction); $data['Role'][] = '#' . $tagDef->Id; $data['Title'][] = $tagDef->Title; $data['Datatype'][] = $tagDef->Partial; $data['Default'][] = ''; $data['Nullable'][] = ''; $data['Match'][] = ''; $data['Min'][] = ''; $data['Max'][] = ''; $data['Sortable'][] = $tagDef->Sortable ? 'true' : 'false'; $data['QuickAdd'][] = $tagDef->QuickAdd ? 'true' : 'false'; $data['Multiple'][] = $tagDef->Multiple ? 'true' : 'false'; $data['Fieldlike'][] = $tagDef->Fieldlike ? 'true' : 'false'; } //print_r($schema); $end = microtime(true); $this->printTable($data, $end - $start); } else { $start = microtime(true); $target = $this->ElementService->getBySlug($target); $data = array('Name' => array($target->Name), 'Slug' => array($target->Slug), 'Description' => array($target->Description), 'Base URL' => array($target->BaseURL), 'Default Order' => array($target->DefaultOrder), 'Allow Slug Slashes' => array($target->AllowSlugSlashes ? "true" : "false"), 'Anchored Site' => array($target->AnchoredSiteSlug)); $end = microtime(true); $this->printTable($data, $end - $start); } } else { if ($action == 'select') { $codeBuffer = array(); $m = array(); if (preg_match('/^(select)\\s+(?P<fields>.+)\\s+(from)\\s+(?P<element>[@a-z0-9\\-]+)(?P<conditions>.+)?/i', $input, $m)) { $start = microtime(true); $selectedFields = array(); $selectedMeta = array(); if (strtolower($m['fields']) == 'count') { $fields = 'count'; } else { $fields = preg_split('/\\s*,\\s*/', $m['fields']); $validFields = array('Title', 'Slug', 'ActiveDate', 'Status', 'CreationDate', 'ModifiedDate', 'SortOrder', 'NodeRef'); $validMeta = array(); $validFieldsLower = array(); foreach ($validFields as $f) { $validFieldsLower[] = strtolower($f); } if (substr($m['element'], 0, 1) == '@') { $target = $this->AspectService->getBySlug(ltrim($m['element'], '@')); if ($target == null) { throw new Exception('Aspect not found: ' . $m['element']); } $schema = $target->Schema; $metaDefs = $schema->getMetaDefs(); foreach ($metaDefs as $metaDef) { $validFieldsLower[] = '#' . strtolower($metaDef->Id); $validMeta[] = '#' . strtolower($metaDef->Id); } } else { $target = $this->ElementService->getBySlug($m['element']); if ($target == null) { throw new Exception('Element not found: ' . $m['element']); } $schema = $target->Schema; $metaDefs = $schema->getMetaDefs(); foreach ($metaDefs as $metaDef) { $validFieldsLower[] = '#' . strtolower($metaDef->Id); $validMeta[] = '#' . strtolower($metaDef->Id); } } $validFieldsLowerFlipped = array_flip($validFieldsLower); foreach ($fields as $field) { if (!in_array(strtolower($field), $validFieldsLower)) { throw new Exception("Invalid field '{$field}'. Permitted fields: " . implode(array_merge($validFields, $validMeta), ', ')); } if (in_array(strtolower($field), $validMeta)) { $selectedMeta[] = strtolower($field); } else { $selectedFields[] = $validFields[$validFieldsLowerFlipped[strtolower($field)]]; } } } $nq = new NodeQuery(); $codeBuffer[] = '$nq = new NodeQuery();'; $nq->setParameter('Elements.in', $m['element']); $codeBuffer[] = '$nq->setParameter("Elements.in","' . $m['element'] . '");'; $limit = null; $offset = 0; if ($fields == 'count') { $nq->setParameter('Count.only', true); $codeBuffer[] = '$nq->setParameter("Count.only",true);'; } else { if (!empty($selectedMeta)) { $nq->setParameter('Meta.select', implode($selectedMeta, ',')); $codeBuffer[] = '$nq->setParameter("Meta.select","' . implode($selectedMeta, ',') . '");'; } $conditions = $m['conditions']; //PROCESS LIMIT if (preg_match('/(limit)\\s+(?P<limit>\\d+)/i', $conditions, $limitMatch)) { $limit = intval($limitMatch['limit']); $codeBuffer[] = '$nq->setLimit(' . $limit . ');'; } //PROCESS OFFSET if (preg_match('/(offset)\\s+(?P<offset>\\d+)/i', $conditions, $offsetMatch)) { $offset = intval($offsetMatch['offset']); $codeBuffer[] = '$nq->setOffset(' . $offset . ');'; } //todo: process where //todo: process order by //todo: process as json } if ($fields == 'count') { $total = $this->NodeService->findAll($nq)->getTotalRecords(); $codeBuffer[] = '$total = $this->NodeService->findAll($nq)->getTotalRecords()'; $data = array('Total' => array($total)); } else { //if no limit set or limit is > 1000, loop through in chunks of 1000 until no results or requested limit is reached $count = 0; $nq->setOffset($offset); $nq->setLimit($limit == null || $limit > 1000 ? 1000 : $limit); $results = $this->NodeService->findAll($nq)->getResults(); $codeBuffer[] = '$results = $this->NodeService->findAll($nq)->getResults();'; $data = array(); while (!empty($results)) { if (!empty($results)) { foreach ($results as $r) { foreach ($selectedFields as $field) { $data[$field][] = $r->{$field}; } foreach ($selectedMeta as $field) { $data[$field][] = $r->getMetaValue($field); } $count++; } } $results = null; if ($limit != null && $count >= $limit) { break; } if ($limit == null || $limit > 1000) { $offset += 1000; $nq->clearResults(); $nq->setOffset($offset); $results = $this->NodeService->findAll($nq)->getResults(); } } } $end = microtime(true); $this->printTable($data, $end - $start); if (isset($history[0]) && $history[0] != $input || empty($history)) { array_unshift($history, $input); } } else { if ($action == 'update') { throw new Exception('you wish!'); } else { if ($action == 'delete') { throw new Exception('you wish!'); } else { throw new Exception("shut yo mouth"); } } } } } } } } //print_r($parts); } } catch (Exception $e) { echo "ERROR: " . $e->getMessage() . "\n"; } } while ($input != 'exit' && $input != 'quit'); echo "Bye\n"; }
public function populateDefaults($elements = 'all') { $log = ''; if ($elements == 'all') { $elements = $this->ElementService->findAll()->getResults(); } else { $elementSlugs = StringUtils::smartExplode($elements); $elements = array(); foreach ($elementSlugs as $eSlug) { $elements[] = $this->ElementService->getBySlug($eSlug); } } foreach ($elements as $element) { $offset = 0; $nq = new NodeQuery(); $nq->setParameter('Elements.in', $element->getSlug()); $nq->setParameter('Meta.select', 'all'); $nq->setLimit(1000); $nq->setOffset($offset); $nq->asObjects(); $nq->isRetrieveTotalRecords(true); $nq = $this->NodeService->findAll($nq, true); $nodes = $nq->getResults(); $tCount = $nq->getTotalRecords(); $count = 0; while (count($nodes) > 0) { foreach ($nodes as $node) { $this->NodeMapper->defaultsOnNode($node); $this->NodeService->edit($node); ++$count; } $offset += 1000; $nq = new NodeQuery(); $nq->setParameter('Elements.in', $element->getSlug()); $nq->setParameter('Meta.select', 'all'); $nq->setLimit(1000); $nq->setOffset($offset); $nq->asObjects(); $nodes = $this->NodeService->findAll($nq)->getResults(); } $log .= 'Updated defaults on ' . $count . ' of ' . $tCount . ' ' . $element->getSlug() . " nodes.\n"; } return $log; }