Skip to content

Commit a7e711f

Browse files
committed
Refactored to use phpcr_cli helper
1 parent 02fb844 commit a7e711f

File tree

12 files changed

+322
-150
lines changed

12 files changed

+322
-150
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
namespace PHPCR\Util\Console\Command;
4+
5+
use Symfony\Component\Console\Command\Command;
6+
use Symfony\Component\Console\Input\InputInterface;
7+
use Symfony\Component\Console\Output\OutputInterface;
8+
use PHPCR\Util\Console\Helper\PhpcrCliHelper;
9+
10+
abstract class BaseCommand extends Command
11+
{
12+
protected function getPhpcrSession()
13+
{
14+
return $this->getHelper('phpcr')->getSession();
15+
}
16+
17+
protected function getPhpcrCliHelper()
18+
{
19+
$phpcrCliHelper = new PhpcrCliHelper($this->getPhpcrSession());
20+
return $phpcrCliHelper;
21+
}
22+
}

src/PHPCR/Util/Console/Command/NodeDumpCommand.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
namespace PHPCR\Util\Console\Command;
2323

2424
use PHPCR\Util\UUIDHelper;
25-
use Symfony\Component\Console\Command\Command;
2625
use PHPCR\ItemNotFoundException;
2726
use PHPCR\RepositoryException;
2827
use PHPCR\PathNotFoundException;
@@ -42,7 +41,7 @@
4241
* @author Daniel Barsotti <daniel.barsotti@liip.ch>
4342
* @author Daniel Leech <daniel@dantleech.com>
4443
*/
45-
class NodeDumpCommand extends Command
44+
class NodeDumpCommand extends BaseCommand
4645
{
4746
/**
4847
* Limit after which to cut lines when dumping properties
@@ -95,7 +94,7 @@ public function setDumpMaxLineLength($length)
9594
*/
9695
protected function execute(InputInterface $input, OutputInterface $output)
9796
{
98-
$session = $this->getHelper('phpcr')->getSession();
97+
$session = $this->getPhpcrSession();
9998
$dumperHelper = $this->getHelper('phpcr_console_dumper');
10099

101100
// node to dump

src/PHPCR/Util/Console/Command/NodeTouchCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
*
3737
* @author Daniel Leech <daniel@dantleech.com>
3838
*/
39-
class NodeTouchCommand extends Command
39+
class NodeTouchCommand extends BaseCommand
4040
{
4141
/**
4242
* {@inheritDoc}
@@ -102,8 +102,8 @@ protected function configure()
102102
*/
103103
protected function execute(InputInterface $input, OutputInterface $output)
104104
{
105-
$helper = $this->getHelper('phpcr');
106-
$session = $helper->getSession();
105+
$helper = $this->getPhpcrCliHelper();
106+
$session = $this->getPhpcrSession();
107107

108108
$path = $input->getArgument('path');
109109
$type = $input->getOption('type');

src/PHPCR/Util/Console/Command/NodeTypeListCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
*
3131
* @author Daniel Leech <daniel@dantleech.com>
3232
*/
33-
class NodeTypeListCommand extends Command
33+
class NodeTypeListCommand extends BaseCommand
3434
{
3535
/**
3636
* {@inheritDoc}
@@ -53,7 +53,7 @@ protected function configure()
5353
*/
5454
protected function execute(InputInterface $input, OutputInterface $output)
5555
{
56-
$session = $this->getHelper('phpcr')->getSession();
56+
$session = $this->getPhpcrSession();
5757
$ntm = $session->getWorkspace()->getNodeTypeManager();
5858

5959
$nodeTypes = $ntm->getAllNodeTypes();

src/PHPCR/Util/Console/Command/NodeTypeRegisterCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@
3939
*
4040
* @author Uwe Jäger <uwej711@googlemail.com>
4141
*/
42-
class NodeTypeRegisterCommand extends Command
42+
class NodeTypeRegisterCommand extends BaseCommand
4343
{
4444
/**
4545
* {@inheritDoc}
@@ -91,7 +91,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
9191

9292
$cnd = file_get_contents($cnd_file);
9393
$allowUpdate = $input->getOption('allow-update');
94-
$session = $this->getHelper('phpcr')->getSession();
94+
$session = $this->getPhpcrSession();
9595

9696
try {
9797
$this->updateFromCnd($output, $session, $cnd, $allowUpdate);

src/PHPCR/Util/Console/Command/WorkspaceNodeUpdateCommand.php renamed to src/PHPCR/Util/Console/Command/NodesUpdateCommand.php

Lines changed: 75 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,19 @@
2121

2222
namespace PHPCR\Util\Console\Command;
2323

24-
use Symfony\Component\Console\Command\Command;
2524
use Symfony\Component\Console\Input\InputOption;
26-
use Symfony\Component\Console\Input\InputArgument;
2725
use Symfony\Component\Console\Input\InputInterface;
2826
use Symfony\Component\Console\Output\OutputInterface;
2927
use Symfony\Component\Console\Helper\DialogHelper;
28+
use PHPCR\Util\Console\Command\BaseCommand;
3029

3130
/**
3231
* Command which can update the properties of nodes found
3332
* using the given JCR query.
3433
*
3534
* @author Daniel Leech <daniel@dantleech.com>
3635
*/
37-
class WorkspaceNodeUpdateCommand extends Command
36+
class NodesUpdateCommand extends BaseCommand
3837
{
3938
/**
4039
* {@inheritDoc}
@@ -43,19 +42,23 @@ protected function configure()
4342
{
4443
parent::configure();
4544

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'
45+
$this->setName('phpcr:nodes:update')
46+
->addOption(
47+
'type', 't',
48+
InputOption::VALUE_REQUIRED,
49+
'Update nodes of given node type, e.g. nt:unstructured'
5450
)
5551
->addOption(
56-
'language', 'l',
52+
'where', 'w',
53+
InputOption::VALUE_OPTIONAL,
54+
'Specify the update criteria in SQL, e.g. "foobar = \'foo\' AND barfoo = \'bar\'"'
55+
)
56+
->addOption(
57+
'query-language', 'l',
5758
InputOption::VALUE_OPTIONAL,
58-
'The query language (sql, jcr_sql2')
59+
'The query language (sql, jcr_sql2',
60+
'jcr_sql2'
61+
)
5962

6063
->addOption('set-prop', 'p',
6164
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
@@ -73,16 +76,22 @@ protected function configure()
7376
InputOption::VALUE_REQUIRED | InputOption::VALUE_IS_ARRAY,
7477
'Remove mixin from the nodes'
7578
)
79+
->addOption('force', null,
80+
InputOption::VALUE_NONE,
81+
'Use to bypass the confirmation dialog'
82+
)
7683
->setDescription('Command to manipulate the nodes in the workspace.')
7784
->setHelp(<<<HERE
78-
The <info>workspace:node:update</info> command updates properties of nodes found
79-
by the given JCR query.
85+
The <info>nodes:update</info> command updates properties of nodes of type x matching
86+
the given select criteria.
8087
81-
php bin/phpcr workspace:node:update "SELECT FROM nt:unstructured" --set-prop=foo=bar
88+
php bin/phpcr nodes:update --type="nt:unstructured" --where="foo='bar'" --set-prop=foo=bar
8289
8390
The options for manipulating nodes are the same as with the
8491
<info>node:touch</info> command and
8592
can be repeated to update multiple properties.
93+
94+
The <info>--where</info> option corresponds to the "where" part of a standard query.
8695
HERE
8796
);
8897
}
@@ -92,34 +101,45 @@ protected function configure()
92101
*/
93102
protected function execute(InputInterface $input, OutputInterface $output)
94103
{
95-
$sql = $input->getArgument('query');
96-
$language = strtoupper($input->getOption('language'));
104+
$type = $input->getOption('type');
105+
$where = $input->getOption('where');
106+
$queryLanguage = strtoupper($input->getOption('query-language'));
97107
$setProp = $input->getOption('set-prop');
98108
$removeProp = $input->getOption('remove-prop');
99109
$addMixins = $input->getOption('add-mixin');
100110
$removeMixins = $input->getOption('remove-mixin');
101-
$force = $input->getOption('force');
111+
$noInteraction = $input->getOption('no-interaction');
112+
$helper = $this->getPhpcrCliHelper();
113+
$session = $this->getPhpcrSession();
114+
$this->dialog = new DialogHelper();
115+
116+
if (!$type) {
117+
throw new \InvalidArgumentException('You must provide the "type" option, to select all nodes specify the type "nt:base"');
118+
}
102119

103-
$helper = $this->getHelper('phpcr');
104-
$session = $helper->getSession();
120+
if ($where) {
121+
$sql = sprintf('SELECT * FROM [%s] WHERE %s', $type, $where);
122+
} else {
123+
$sql = sprintf('SELECT * FROM [%s]', $type);
124+
}
105125

106-
$query = $helper->createQuery($language, $sql);
126+
$output->writeln($sql);
127+
$query = $helper->createQuery($queryLanguage, $sql);
107128

108129
$start = microtime(true);
109130
$result = $query->execute();
110131
$elapsed = microtime(true) - $start;
111132

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);
133+
if (!$noInteraction) {
134+
if (false === $this->getAction($output, $result)) {
135+
return 0;
136+
}
118137
}
119138

120139
foreach ($result as $i => $row) {
121140
$output->writeln(sprintf(
122-
"<info>Updating node</info> %s.",
141+
"<info>Updating node:</info>[%d] %s.",
142+
$i,
123143
$row->getPath()
124144
));
125145

@@ -137,4 +157,30 @@ protected function execute(InputInterface $input, OutputInterface $output)
137157

138158
return 0;
139159
}
160+
161+
protected function getAction($output, $result)
162+
{
163+
$response = strtoupper($this->dialog->ask($output, sprintf(
164+
'<question>About to update %d nodes. Enter "Y" to continue, "N" to cancel or "L" to list.</question>',
165+
count($result->getRows())
166+
), false));
167+
168+
if ($response == 'L') {
169+
foreach ($result as $i => $row) {
170+
$output->writeln(sprintf(' - [%d] %s', $i, $row->getPath()));
171+
}
172+
173+
return $this->getAction($output, $result);
174+
}
175+
176+
if ($response == 'N') {
177+
return false;
178+
}
179+
180+
if ($response == 'Y') {
181+
return true;
182+
}
183+
184+
return $this->getAction($output, $result);
185+
}
140186
}

src/PHPCR/Util/Console/Command/WorkspaceQueryCommand.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@
3232
* resulting nodes.
3333
*
3434
* @author Daniel Barsotti <daniel.barsotti@liip.ch>
35+
* @author Daniel Leech <daniel@dantleech.com>
3536
*/
36-
class WorkspaceQueryCommand extends Command
37+
class WorkspaceQueryCommand extends BaseCommand
3738
{
3839
/**
3940
* {@inheritDoc}
@@ -61,8 +62,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
6162
$limit = $input->getOption('limit');
6263
$offset = $input->getOption('offset');
6364

64-
$helper = $this->getHelper('phpcr');
65-
$session = $helper->getSession();
65+
$helper = $this->getPhpcrCliHelper();
66+
$session = $this->getPhpcrSession();
6667

6768
$query = $helper->createQuery($language, $sql);
6869

0 commit comments

Comments
 (0)