From d9f6b83ed6d8a4546e4119c64b639adda057d25e Mon Sep 17 00:00:00 2001 From: Christian Weiske Date: Fri, 13 Apr 2012 19:56:24 +0200 Subject: first tool supported: xmllint --- src/phorkie/File.php | 16 ++++++++++++-- src/phorkie/Tool/Info.php | 33 ++++++++++++++++++++++++++++ src/phorkie/Tool/Manager.php | 46 ++++++++++++++++++++++++++++++++++++++++ src/phorkie/Tool/Result.php | 9 ++++++++ src/phorkie/Tool/Result/Line.php | 34 +++++++++++++++++++++++++++++ src/phorkie/Tool/Xmllint.php | 44 ++++++++++++++++++++++++++++++++++++++ 6 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 src/phorkie/Tool/Info.php create mode 100644 src/phorkie/Tool/Manager.php create mode 100644 src/phorkie/Tool/Result.php create mode 100644 src/phorkie/Tool/Result/Line.php create mode 100644 src/phorkie/Tool/Xmllint.php (limited to 'src') diff --git a/src/phorkie/File.php b/src/phorkie/File.php index 87c3851..869aa80 100644 --- a/src/phorkie/File.php +++ b/src/phorkie/File.php @@ -75,14 +75,17 @@ class File * * @param string $type Link type. Supported are: * - "raw" - * - "display" + * - "tool" + * @param string $option * * @return string */ - public function getLink($type) + public function getLink($type, $option = null) { if ($type == 'raw') { return '/' . $this->repo->id . '/raw/' . $this->getFilename(); + } else if ($type == 'tool') { + return '/' . $this->repo->id . '/tool/' . $option . '/' . $this->getFilename(); } throw new Exception('Unknown type'); } @@ -95,6 +98,15 @@ class File } return $GLOBALS['phorkie']['languages'][$ext]['mime']; } + + /** + * @return array Array of Tool_Info objects + */ + public function getToolInfos() + { + $tm = new Tool_Manager(); + return $tm->getSuitable($this); + } } ?> \ No newline at end of file diff --git a/src/phorkie/Tool/Info.php b/src/phorkie/Tool/Info.php new file mode 100644 index 0000000..c1c3c69 --- /dev/null +++ b/src/phorkie/Tool/Info.php @@ -0,0 +1,33 @@ +class = $class; + } + + public function getLink(File $file) + { + return $file->getLink('tool', $this->stripPrefix($this->class)); + } + + public function getTitle() + { + return $this->stripPrefix($this->class); + } + + protected function stripPrefix($class) + { + $prefix = '\\phorkie\\Tool_'; + if (substr($class, 0, strlen($prefix)) === $prefix) { + return substr($class, strlen($prefix)); + } + return $class; + } +} + +?> diff --git a/src/phorkie/Tool/Manager.php b/src/phorkie/Tool/Manager.php new file mode 100644 index 0000000..3bcf750 --- /dev/null +++ b/src/phorkie/Tool/Manager.php @@ -0,0 +1,46 @@ +getExt(); + $suitables = array(); + foreach ($GLOBALS['phorkie']['tools'] as $class) { + if (array_search($ext, $class::$arSupportedExtensions) !== false) { + $suitables[] = new Tool_Info($class); + } + } + return $suitables; + } + + /** + * Returns the class name from a tool name + * + * @param string $name Full class name or short name without + * 'phorkie\\Tool_' prefix + * + * @return string Class name or NULL if not found + */ + public function getClass($name) + { + if (strpos($name, '\\') === false && strpos($name, '_') === false) { + return '\\phorkie\\Tool_' . $name; + } + return $name; + } + + public function loadTool($name) + { + $class = $this->getClass($name); + if (!class_exists($class, true)) { + throw new Exception('Tool does not exist: ' . $class); + } + + return new $class(); + } +} + +?> \ No newline at end of file diff --git a/src/phorkie/Tool/Result.php b/src/phorkie/Tool/Result.php new file mode 100644 index 0000000..22ea273 --- /dev/null +++ b/src/phorkie/Tool/Result.php @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/src/phorkie/Tool/Result/Line.php b/src/phorkie/Tool/Result/Line.php new file mode 100644 index 0000000..a788db6 --- /dev/null +++ b/src/phorkie/Tool/Result/Line.php @@ -0,0 +1,34 @@ +message = $message; + $this->setLevel($level); + } + + public function setLevel($level) + { + if ($level !== 'ok' && $level !== 'error' && $level !== 'warning') { + throw new Exception('Invalid result line level: ' . $level); + } + $this->level = $level; + } + + public function getAlertLevel() + { + static $map = array( + 'error' => 'alert-error', + 'ok' => 'alert-success', + 'warning' => '', + ); + return $map[$this->level]; + } +} + +?> \ No newline at end of file diff --git a/src/phorkie/Tool/Xmllint.php b/src/phorkie/Tool/Xmllint.php new file mode 100644 index 0000000..b9917f5 --- /dev/null +++ b/src/phorkie/Tool/Xmllint.php @@ -0,0 +1,44 @@ +getPath(); + $fpathlen = strlen($fpath); + + $res = new Tool_Result(); + $cmd = 'xmllint --noout ' . escapeshellarg($fpath) . ' 2>&1'; + exec($cmd, $output, $retval); + if ($retval == 0) { + $res->annotations['general'][] = new Tool_Result_Line( + 'XML is well-formed', 'ok' + ); + return $res; + } + + for ($i = 0; $i < count($output); $i += 3) { + $line = $output[$i]; + if (substr($line, 0, $fpathlen) != $fpath) { + throw new Exception('xmllint does not behave as expected: ' . $line); + } + list($line, $msg) = explode(':', substr($line, $fpathlen + 1), 2); + $res->annotations[$line][] = new Tool_Result_Line( + $msg, 'error' + ); + } + + $res->annotations['general'][] = new Tool_Result_Line( + 'XML is not well-formed', 'error' + ); + + return $res; + } +} + +?> \ No newline at end of file -- cgit v1.2.3