From: Christian Weiske Date: Fri, 13 Apr 2012 17:56:24 +0000 (+0200) Subject: first tool supported: xmllint X-Git-Tag: v0.1.0~42 X-Git-Url: https://git.cweiske.de/phorkie.git/commitdiff_plain/d9f6b83ed6d8a4546e4119c64b639adda057d25e first tool supported: xmllint --- diff --git a/data/config.default.php b/data/config.default.php index 804ff5e..443328d 100644 --- a/data/config.default.php +++ b/data/config.default.php @@ -6,6 +6,9 @@ $GLOBALS['phorkie']['cfg'] = array( 'tpl' => __DIR__ . '/templates/', 'css' => 'http://twitter.github.com/bootstrap/assets/css/bootstrap.css', ); +$GLOBALS['phorkie']['tools'] = array( + '\\phorkie\\Tool_Xmllint' +); /** * Array of supported file types / languages. * Key is the file extension diff --git a/data/templates/display-file.htm b/data/templates/display-file.htm new file mode 100644 index 0000000..d06c365 --- /dev/null +++ b/data/templates/display-file.htm @@ -0,0 +1,12 @@ +
+
+ raw + {% for toolinfo in file.getToolInfos %} + {{toolinfo.getTitle}} + {% endfor %} +

{{file.getFilename}}

+
+
+ {{file.getHighlightedContent|raw}} +
+
diff --git a/data/templates/display-foot.htm b/data/templates/display-foot.htm new file mode 100644 index 0000000..9f2451f --- /dev/null +++ b/data/templates/display-foot.htm @@ -0,0 +1,7 @@ +
+ +
diff --git a/data/templates/display-head.htm b/data/templates/display-head.htm new file mode 100644 index 0000000..7ce5160 --- /dev/null +++ b/data/templates/display-head.htm @@ -0,0 +1,35 @@ +

{{repo.getDescription}}

+
+
+ edit +
+
+

Paste #{{repo.id}}

+
+
+
+ +
+
+
+ +{% if repo.getCloneURL(true) or repo.getCloneURL(false) %} +
+ {% if repo.getCloneURL(true) %} +
+
Public clone URL
+ +
+ {% endif %} + {% if repo.getCloneURL(false) %} +
+
Private clone URL
+ +
+ {% endif %} +
+{% endif %} diff --git a/data/templates/display.htm b/data/templates/display.htm index da75cd9..e021296 100644 --- a/data/templates/display.htm +++ b/data/templates/display.htm @@ -8,61 +8,13 @@ {% endblock %} {% block content %} -

{{repo.getDescription}}

-
-
- edit -
-
-

Paste #{{repo.id}}

-
-
-
- -
-
-
- -{% if repo.getCloneURL(true) or repo.getCloneURL(false) %} -
- {% if repo.getCloneURL(true) %} -
-
Public clone URL
- -
- {% endif %} - {% if repo.getCloneURL(false) %} -
-
Private clone URL
- -
- {% endif %} -
-{% endif %} + {% include 'display-head.htm' %} {% for file in repo.getFiles %} -
-
- raw -

{{file.getFilename}}

-
-
- {{file.getHighlightedContent|raw}} -
-
+ {% include 'display-file.htm' %} {% endfor %} -
- -
+ {% include 'display-foot.htm' %} {% endblock %} {% block sidebar %} diff --git a/data/templates/tool.htm b/data/templates/tool.htm new file mode 100644 index 0000000..ef8e5e0 --- /dev/null +++ b/data/templates/tool.htm @@ -0,0 +1,44 @@ +{% extends "base.htm" %} +{% block title %} + Tool results: + {%if repo.getDescription %} + {{repo.getDescription}} + {%else%} + {{repo.id}} + {%endif%} +{% endblock %} + +{% block content %} +

Tool results: {{repo.getDescription}}

+
+
+ edit + back +
+
+

Paste #{{repo.id}}

+
+
+ + {% for line in toolres.annotations.general %} +
+ {{line.message}} +
+ {% endfor %} + + {% include 'display-file.htm' %} + + {% for number,lineinfos in toolres.annotations if number != 'general' %} + {% for line in lineinfos %} +
+ Line #{{number}}: {{line.message}} +
+ {% endfor %} + {% endfor %} + + {% include 'display-foot.htm' %} +{% endblock %} + +{% block sidebar %} +sidebar FIXME +{% endblock %} 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 diff --git a/www/.htaccess b/www/.htaccess index 32701be..10833c7 100644 --- a/www/.htaccess +++ b/www/.htaccess @@ -8,6 +8,7 @@ RewriteRule ^([0-9]+)/delete/confirm$ /delete.php?id=$1&confirm=1 RewriteRule ^([0-9]+)/edit$ /edit.php?id=$1 RewriteRule ^([0-9]+)/fork$ /fork.php?id=$1 RewriteRule ^([0-9]+)/raw/(.+)$ /raw.php?id=$1&file=$2 +RewriteRule ^([0-9]+)/tool/([^/]+)/(.+)$ /tool.php?id=$1&tool=$2&file=$3 RewriteRule ^list$ /list.php RewriteRule ^list/([0-9]+)$ /list.php?page=$1 diff --git a/www/phorkie.css b/www/phorkie.css index 882a2f7..3e4b061 100644 --- a/www/phorkie.css +++ b/www/phorkie.css @@ -28,9 +28,12 @@ a.anchorlink { color: #FFA; } -.file { - margin-top: 2em; +h1 { + margin-bottom: 0.5ex; +} +.repo-info { + margin-bottom: 2em; } .file .header { padding: 1.0ex; diff --git a/www/tool.php b/www/tool.php new file mode 100644 index 0000000..d42954c --- /dev/null +++ b/www/tool.php @@ -0,0 +1,33 @@ +loadFromRequest(); + +if (!isset($_GET['file']) || $_GET['file'] == '') { + throw new Exception_Input('File name missing'); +} +$file = $repo->getFileByName($_GET['file']); + +if (!isset($_GET['tool']) || $_GET['tool'] == '') { + throw new Exception_Input('Tool name missing'); +} + +$tm = new Tool_Manager(); +$tool = $tm->loadTool($_GET['tool']); + +$res = $tool->run($file); + +render( + 'tool', + array( + 'repo' => $repo, + 'file' => $file, + 'toolres' => $res, + ) +); + +?> \ No newline at end of file