1 : <?php
2 : /**
3 : * Roman de Renart
4 : *
5 : * PHP version 5
6 : *
7 : * @category Rdr
8 : * @package Edit
9 : * @author Michel Corne <mcorne@yahoo.com>
10 : * @copyright 2010 Michel Corne
11 : * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
12 : * @link http://roman-de-renart.blogspot.com/
13 : * @version SVN: $Id$
14 : */
15 :
16 : require_once 'InputOutput.php';
17 : require_once 'TableContents.php';
18 : require_once 'Worksheet.php';
19 : require_once 'Episode.php';
20 : require_once 'Load.php';
21 : require_once 'Read.php';
22 : require_once 'Fix.php';
23 : require_once 'Differences.php';
24 : require_once 'Mapping.php';
25 : require_once 'Questions.php';
26 : require_once 'Write.php';
27 :
28 : /**
29 : * Editing action processor
30 : *
31 : * @category Rdr
32 : * @package Edit
33 : * @author Michel Corne <mcorne@yahoo.com>
34 : * @copyright 2010 Michel Corne
35 : * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
36 : */
37 :
38 : class Controller extends InputOutput
39 : {
40 : /**
41 : * The error message reported when the omde of operation is not CGI
42 : */
43 : const ERR_INVALID_MODE = 'Invalid mode (CGI only)!';
44 :
45 : /**
46 : * The error message reported when an option is invalid
47 : */
48 : const ERR_INVALID_OPTION = 'Invalid option! Try option "h" to get the list of options.';
49 :
50 : /**
51 : * Description for const
52 : */
53 : const RESULT_FILE = 'result.txt';
54 :
55 : /**
56 : * The name of the file containing the result
57 : */
58 : const RESULT_FMT = "*** OPTION %s ***\n\n%s";
59 :
60 : /**
61 : * The help text
62 : */
63 : const HELP_TEXT =
64 : '
65 : Usage: edition [option] [param]
66 :
67 : l -- list episodes or display table of contents
68 : s -- [ID] select episode (new episode by default)
69 : g -- get selected episode
70 : t -- load, read and display table of contents
71 : e -- load and read episode
72 : f -- clean, fix, compute differences, questions and mapping
73 : u -- update episode: all options from load to verify
74 :
75 : el -- load episode
76 : er -- read episode
77 : ec -- clean Méon
78 : ef -- fix Méon
79 : ed -- find differences between Méon-Martin-FRO
80 : em -- compute mapping between Méon-Martin-FRO
81 : eq -- complete questions
82 : ew -- write episode
83 : ev -- verify changes
84 : et -- truncate episode
85 :
86 : tl -- load table of contents
87 : tr -- read table of contents
88 : td -- display table of contents
89 : tw -- write table of contents
90 :
91 : h -- list options (help)
92 : ';
93 :
94 : /**
95 : * The configuration directives
96 : * @var array
97 : */
98 : protected $config = array();
99 :
100 : /**
101 : * The option passed in the command line
102 : * @var string
103 : */
104 : protected $option;
105 :
106 : /**
107 : * The option details
108 : * @var array
109 : */
110 : protected $options = array(
111 : 'e' => array('arrayMap', 'processOption', array('el', 'er')),
112 : 'ec' => array('execAction', 'Fix', array(Episode::COL_MEON_TEXT_ORIG, Episode::COL_MEON_TEXT_CLEAN, Fix::MEON_CLEANING_LIST)),
113 : 'ed' => array('execAction', 'Differences', array(), 'findDifferences'),
114 : 'ef' => array('execAction', 'Fix', array(Episode::COL_MEON_TEXT_CLEAN, Episode::COL_MEON_TEXT_FIXED, Fix::MEON_FIXES_LIST)),
115 : 'el' => array('execAction', 'Load'),
116 : 'em' => array('execAction', 'Mapping'),
117 : 'eq' => array('execAction', 'Questions', array(), 'makeQuestions'),
118 : 'er' => array('execAction', 'Read'),
119 : 'et' => array('execAction', 'Episode', array(), 'truncateSheet'),
120 : 'ev' => array('execAction', 'Read', array(), 'compareInAndOut'),
121 : 'ew' => array('execAction', 'Write'),
122 : 'f' => array('arrayMap', 'processOption', array('ec', 'ef', 'ed', 'eq', 'em')),
123 : 'g' => array('execAction', 'Episode', array(), 'get'),
124 : 'h' => array('getHelp'),
125 : 'l' => array('processOption', 'td'),
126 : 's' => array('execAction', 'Episode', array(false), 'select', true),
127 : 't' => array('arrayMap', 'processOption', array('tl', 'tr', 'td')),
128 : 'td' => array('execAction', 'TableContents', array(), 'display'),
129 : 'tl' => array('execAction', 'TableContents', array(false), 'load'),
130 : 'tr' => array('execAction', 'TableContents', array(false), 'read'),
131 : 'tw' => array('execAction', 'TableContents', array(), 'write'),
132 : 'u' => array('arrayMap', 'processOption', array('e', 'f', 'ew', 'ev')),
133 : );
134 :
135 : /**
136 : * The value passed in the command line
137 : * @var string
138 : */
139 : protected $value = '';
140 :
141 : /**
142 : * Constructor
143 : *
144 : * @return void
145 : */
146 : public function __construct($configFile = null)
147 : {
148 6 : $configFile and $this->loadConfig($configFile);
149 6 : }
150 :
151 : /**
152 : * Executes an editing action
153 : *
154 : * @param string $class the name of the class to instanciate
155 : * @param array $objArgs the arguments of the class constructor
156 : * @param string $method the class method to call
157 : * @param bool $addValue adds the option value if true, ignores it if false
158 : * @return mixed the data returned by the method
159 : */
160 : public function execAction($class, $objArgs = array(), $method = null, $addValue = false)
161 : {
162 3 : settype($objArgs, 'array');
163 : // adds the option value to argument list of the method
164 3 : $methodArgs = $addValue? array($this->value) : array();
165 :
166 : // defaults the method name to <class-name>Episode
167 3 : $method or $method = strtolower($class) . 'Episode';
168 :
169 : // prepends the configuration directives to the argument list
170 3 : array_unshift($objArgs, $this->config);
171 : // instanciate the class
172 3 : $reflection = new ReflectionClass($class);
173 3 : $obj = $reflection->newInstanceArgs($objArgs);
174 :
175 : // calls the method
176 3 : return call_user_func_array(array($obj, $method), $methodArgs);
177 : }
178 :
179 : /**
180 : * Returns the help text
181 : *
182 : * @return string the help text
183 : */
184 : public function getHelp()
185 : {
186 1 : return trim(self::HELP_TEXT);
187 : }
188 :
189 : /**
190 : * Extracts the option and the value from the command line arguments
191 : *
192 : * @return the option and the value
193 : * @throws Exception an exception is thrown by abort() if the mode of operation is not CGI
194 : */
195 : public function getParams()
196 : {
197 2 : global $argv;
198 :
199 2 : $this->isCgi() or $this->abort(self::ERR_INVALID_MODE);
200 :
201 2 : $this->option = isset($argv[1])? str_replace('-', '', $argv[1]) : '';
202 2 : $this->value = isset($argv[2])? $argv[2] : '';
203 :
204 : // returns the option and the value
205 : // as a convenience for testing purposes
206 2 : return array($this->option, $this->value);
207 : }
208 :
209 : /**
210 : * Loads the configuration directives
211 : *
212 : * @return the configuration directives
213 : */
214 : public function loadConfig($configFile)
215 : {
216 4 : $this->config = $this->includeFile($configFile);
217 :
218 : // returns the configuration directives
219 : // as a convenience for testing purposes
220 4 : return $this->config;
221 : }
222 :
223 : /**
224 : * Processes an option
225 : *
226 : * @param string $option the name of the option of process
227 : * @param string $value the value passed with the option
228 : * @return string the result of the processing of the option
229 : */
230 : public function processOption($option = null)
231 : {
232 : // defaults the option to the option passed in the command line
233 2 : $option or $option = $this->option;
234 :
235 2 : if (isset($this->options[$option])) {
236 : // the option is valid, calls the method to process the option
237 2 : $params = $this->options[$option];
238 2 : $method = array_shift($params);
239 2 : $result = call_user_func_array(array($this, $method), $params);
240 2 : } else {
241 : // the option is invalid
242 1 : $result = self::ERR_INVALID_OPTION;
243 : }
244 :
245 2 : if (is_array($result)) {
246 1 : $result = $this->arrayToString($result);
247 1 : } else {
248 2 : $result = sprintf(self::RESULT_FMT, $option, $result);
249 : }
250 :
251 2 : return $result;
252 : }
253 :
254 : /**
255 : * Runs the editing action (entry point)
256 : *
257 : * @return string the result of the action
258 : */
259 : public function run()
260 : {
261 : try {
262 : // extracts the command line parameters
263 1 : $this->getParams();
264 : // processes the option
265 1 : $result = $this->processOption();
266 :
267 1 : } catch (Exception $e) {
268 1 : $result = $e->getMessage();
269 : }
270 :
271 : // stores the result in a file
272 1 : $fileName = $this->makeFilePath(self::RESULT_FILE, 'wip-dir');
273 1 : $this->writeFile($fileName, $result);
274 :
275 : // returns the result (for displaying)
276 1 : return "\n" . $this->decodeString($result) . "\n";
277 : }
278 :
279 : /**
280 : * Sets the options
281 : *
282 : * @param array $options the options
283 : * @return void
284 : */
285 : public function setOptions($options)
286 : {
287 : // as a convenience for testing purposes
288 2 : $this->options = $options;
289 2 : }
|