|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * This file is part of the PHPCR Utils |
| 5 | + * |
| 6 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | + * you may not use this file except in compliance with the License. |
| 8 | + * You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + * |
| 18 | + * @license http://www.apache.org/licenses/LICENSE-2.0 Apache Software License 2.0 |
| 19 | + * @link http://phpcr.github.com/ |
| 20 | + */ |
| 21 | + |
| 22 | +namespace PHPCR\Util\Console\Command; |
| 23 | + |
| 24 | +use Symfony\Component\Console\Command\Command; |
| 25 | +use Symfony\Component\Console\Input\InputOption; |
| 26 | +use Symfony\Component\Console\Input\InputArgument; |
| 27 | +use Symfony\Component\Console\Input\InputInterface; |
| 28 | +use Symfony\Component\Console\Output\OutputInterface; |
| 29 | +use Symfony\Component\Console\Helper\DialogHelper; |
| 30 | + |
| 31 | +/** |
| 32 | + * Command which can update the properties of nodes found |
| 33 | + * using the given JCR query. |
| 34 | + * |
| 35 | + * @author Daniel Leech <daniel@dantleech.com> |
| 36 | + */ |
| 37 | +class WorkspaceNodeUpdateCommand extends Command |
| 38 | +{ |
| 39 | + /** |
| 40 | + * {@inheritDoc} |
| 41 | + */ |
| 42 | + protected function configure() |
| 43 | + { |
| 44 | + parent::configure(); |
| 45 | + |
| 46 | + $this->setName('phpcr:workspace:node:update') |
| 47 | + ->addArgument( |
| 48 | + 'query', |
| 49 | + InputArgument::REQUIRED, |
| 50 | + 'A query statement to execute') |
| 51 | + ->addOption('force', null, |
| 52 | + InputOption::VALUE_NONE, |
| 53 | + 'Use to bypass the confirmation dialog' |
| 54 | + ) |
| 55 | + ->addOption( |
| 56 | + 'language', 'l', |
| 57 | + InputOption::VALUE_OPTIONAL, |
| 58 | + 'The query language (sql, jcr_sql2') |
| 59 | + |
| 60 | + ->addOption('set-prop', 'p', |
| 61 | + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
| 62 | + 'Set node property on nodes use foo=bar' |
| 63 | + ) |
| 64 | + ->addOption('remove-prop', 'r', |
| 65 | + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
| 66 | + 'Remove property from nodes' |
| 67 | + ) |
| 68 | + ->addOption('add-mixin', null, |
| 69 | + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
| 70 | + 'Add a mixin to the nodes' |
| 71 | + ) |
| 72 | + ->addOption('remove-mixin', null, |
| 73 | + InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY, |
| 74 | + 'Remove mixin from the nodes' |
| 75 | + ) |
| 76 | + ->setDescription('Command to manipulate the nodes in the workspace.') |
| 77 | + ->setHelp(<<<HERE |
| 78 | +The <info>workspace:node:update</info> command updates properties of nodes found |
| 79 | +by the given JCR query. |
| 80 | +
|
| 81 | + php bin/phpcr workspace:node:update "SELECT FROM nt:unstructured" --set-prop=foo=bar |
| 82 | +
|
| 83 | +The options for manipulating nodes are the same as with the |
| 84 | +<info>node:touch</info> command and |
| 85 | +can be repeated to update multiple properties. |
| 86 | +HERE |
| 87 | +); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * {@inheritDoc} |
| 92 | + */ |
| 93 | + protected function execute(InputInterface $input, OutputInterface $output) |
| 94 | + { |
| 95 | + $sql = $input->getArgument('query'); |
| 96 | + $language = strtoupper($input->getOption('language')); |
| 97 | + $setProp = $input->getOption('set-prop'); |
| 98 | + $removeProp = $input->getOption('remove-prop'); |
| 99 | + $addMixins = $input->getOption('add-mixin'); |
| 100 | + $removeMixins = $input->getOption('remove-mixin'); |
| 101 | + $force = $input->getOption('force'); |
| 102 | + |
| 103 | + $helper = $this->getHelper('phpcr'); |
| 104 | + $session = $helper->getSession(); |
| 105 | + |
| 106 | + $query = $helper->createQuery($language, $sql); |
| 107 | + |
| 108 | + $start = microtime(true); |
| 109 | + $result = $query->execute(); |
| 110 | + $elapsed = microtime(true) - $start; |
| 111 | + |
| 112 | + if (!$force) { |
| 113 | + $dialog = new DialogHelper(); |
| 114 | + $force = $dialog->askConfirmation($output, sprintf( |
| 115 | + '<question>About to update %d nodes, do you want to continue Y/N ?</question>', |
| 116 | + count($result) |
| 117 | + ), false); |
| 118 | + } |
| 119 | + |
| 120 | + foreach ($result as $i => $row) { |
| 121 | + $output->writeln(sprintf( |
| 122 | + "<info>Updating node</info> %s.", |
| 123 | + $row->getPath() |
| 124 | + )); |
| 125 | + |
| 126 | + $node = $row->getNode(); |
| 127 | + |
| 128 | + $helper->processNode($output, $node, array( |
| 129 | + 'setProp' => $setProp, |
| 130 | + 'removeProp' => $removeProp, |
| 131 | + 'addMixins' => $addMixins, |
| 132 | + 'removeMixins' => $removeMixins, |
| 133 | + )); |
| 134 | + } |
| 135 | + |
| 136 | + $output->writeln(sprintf('<info>%.2f seconds</info>', $elapsed)); |
| 137 | + |
| 138 | + return 0; |
| 139 | + } |
| 140 | +} |
0 commit comments