1<?php
2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project
3//
4// All Rights Reserved. See copyright.txt for details and a complete list of authors.
5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details.
6// $Id$
7
8namespace Tiki\Command;
9
10use Symfony\Component\Console\Command\Command;
11use Symfony\Component\Console\Input\InputArgument;
12use Symfony\Component\Console\Input\InputInterface;
13use Symfony\Component\Console\Input\InputOption;
14use Symfony\Component\Console\Output\OutputInterface;
15
16use TikiDb;
17
18class ProfileBaselineCommand extends Command
19{
20	protected function configure()
21	{
22		$this
23			->setName('profile:baseline')
24			->setDescription('Generate the SQL patch to assign profile symbols for an existing installation.')
25			->addArgument(
26				'repository',
27				InputArgument::OPTIONAL,
28				'Repository',
29				'file://profiles'
30			)
31			->addArgument(
32				'profile',
33				InputArgument::OPTIONAL,
34				'Repository',
35				'Baseline'
36			)
37			->addOption(
38				'categories',
39				null,
40				InputOption::VALUE_NONE,
41				'Include categories'
42			)
43			->addOption(
44				'file-galleries',
45				null,
46				InputOption::VALUE_NONE,
47				'Include file galleries'
48			)
49			->addOption(
50				'trackers',
51				null,
52				InputOption::VALUE_NONE,
53				'Include trackers'
54			)
55			->addOption(
56				'tracker-fields',
57				null,
58				InputOption::VALUE_NONE,
59				'Include tracker fields'
60			)
61			;
62	}
63
64	protected function execute(InputInterface $input, OutputInterface $output)
65	{
66		$profile = $input->getArgument('profile');
67		$repository = $input->getArgument('repository');
68
69		$db = TikiDb::get();
70		$writer = function ($type, $id, $name) use ($output, $db, $profile, $repository) {
71			$name = $db->qstr($this->generateSymbol($type, $name));
72
73			$profile = $db->qstr($profile);
74			$repository = $db->qstr($repository);
75			$type = $db->qstr($type);
76			$id = $db->qstr($id);
77
78			$output->writeln("REPLACE INTO `tiki_profile_symbols` (`domain`, `profile`, `object`, `type`, `value`, `named`) VALUES ($repository, $profile, $name, $type, $id, 'y');");
79		};
80
81		$output->writeln("-- This file was automatically generated by the profile baseline generator");
82		$output->writeln("-- Date: " . date('Y-m-d H:i:s'));
83
84		if ($input->getOption('categories')) {
85			$output->writeln("");
86			$output->writeln("-- Categories");
87			$table = $db->table('tiki_categories');
88			foreach ($table->fetchMap('categId', 'name', []) as $id => $name) {
89				$writer('category', $id, $name);
90			}
91		}
92
93		if ($input->getOption('file-galleries')) {
94			$output->writeln("");
95			$output->writeln("-- File Galleries");
96			$table = $db->table('tiki_file_galleries');
97			foreach ($table->fetchMap('galleryId', 'name', []) as $id => $name) {
98				$writer('file_gallery', $id, $name);
99			}
100		}
101
102		if ($input->getOption('trackers')) {
103			$output->writeln("");
104			$output->writeln("-- Trackers");
105			$table = $db->table('tiki_trackers');
106			foreach ($table->fetchMap('trackerId', 'name', []) as $id => $name) {
107				$writer('tracker', $id, $name);
108			}
109		}
110
111		if ($input->getOption('tracker-fields')) {
112			$output->writeln("");
113			$output->writeln("-- Tracker Fields");
114			$table = $db->table('tiki_tracker_fields');
115			foreach ($table->fetchMap('fieldId', 'name', []) as $id => $name) {
116				$writer('tracker_field', $id, $name);
117			}
118		}
119
120		$output->writeln("");
121		$output->writeln("-- Dump completed");
122	}
123
124	private function generateSymbol($type, $name)
125	{
126		static $memory = [];
127
128		$basename = preg_replace('/\W+/', '_', strtolower($name));
129		$candidate = $type . '_' . $basename;
130
131		if (! isset($memory[$candidate])) {
132			$memory[$candidate] = 0;
133			return $candidate;
134		} else {
135			return $candidate . '_' . ++$memory[$candidate];
136		}
137	}
138}
139