9 namespace Fossology\Monk\UI;
20 use Symfony\Component\HttpFoundation\File\UploadedFile;
21 use Symfony\Component\HttpFoundation\Request;
22 use Symfony\Component\HttpFoundation\Response;
26 const NAME =
"oneshot-monk";
29 private $highlightProcessor;
31 private $highlightRenderer;
33 private $textRenderer;
35 function __construct()
37 parent::__construct(self::NAME, array(
38 self::TITLE =>
"One-Shot Monk",
39 self::MENU_LIST =>
"Upload::One-Shot Monk Analysis",
41 self::REQUIRES_LOGIN =>
true
44 $this->highlightProcessor = $this->
getObject(
'view.highlight_processor');
45 $this->highlightRenderer = $this->
getObject(
'view.highlight_renderer');
46 $this->textRenderer = $this->
getObject(
'view.text_renderer');
53 protected function handle(Request $request)
56 $uploadFile = $request->files->get(
'file_input');
57 if ($uploadFile ===
null) {
60 $fullpath = $uploadFile->getPath().
'/'.$uploadFile->getFilename();
62 list($licenseIds, $rendered) = $this->scanMonkFileRendered($fullpath);
63 $vars = $this->mergeWithDefault(array(
'content' => $this->renderLicenseList($licenseIds).$rendered));
64 $vars[
'styles'] .=
"<link rel='stylesheet' href='css/highlights.css'>\n";
65 return $this->
render(
'include/base.html.twig', $vars);
68 public function scanMonkRendered($text, $fromRest =
false)
70 $tmpFileName = tempnam(
"/tmp",
"monk");
72 throw new \Exception(
"cannot create temporary file");
74 $handle = fopen($tmpFileName,
"w");
75 fwrite($handle, $text);
77 list($licenseIds, $highlights) = $this->scanMonk($tmpFileName);
80 $this->highlightProcessor->addReferenceTexts($highlights);
82 return array($licenseIds, $highlights);
85 $splitPositions = $this->highlightProcessor->calculateSplitPositions($highlights);
88 $rendered = $this->textRenderer->renderText($textFragment, $splitPositions);
89 return array($licenseIds, $rendered);
93 public function scanMonkFileRendered($tmpfname)
95 list($licenseIds, $highlights) = $this->scanMonk($tmpfname);
97 $text = file_get_contents($tmpfname);
99 $this->highlightProcessor->addReferenceTexts($highlights);
100 $splitPositions = $this->highlightProcessor->calculateSplitPositions($highlights);
103 $rendered = $this->textRenderer->renderText($textFragment, $splitPositions);
105 return array($licenseIds, $rendered);
109 public function scanMonk($fileName)
112 $cmd = dirname(__DIR__).
'/agent/monk -c '.$SYSCONFDIR.
' '.$fileName;
113 exec($cmd, $output, $returnVar);
114 if ($returnVar != 0) {
115 throw new \Exception(
"scan failed with $returnVar");
118 $qFileName = preg_quote($fileName,
"/");
119 $licenseIds = array();
120 $highlights = array();
121 foreach ($output as $line) {
122 $lineMatches = array();
123 if (preg_match(
'/found diff match between "'.$qFileName.
'" and "[^"]*" \(rf_pk=(?P<rf>[0-9]+)\); rank (?P<rank>[0-9]{1,3}); diffs: \{(?P<diff>[st\[\]0-9, MR+-]+)}/', $line, $lineMatches)) {
124 $licenseId = $lineMatches[
'rf'];
125 $licenseIds[] = $licenseId;
126 $this->addDiffsToHighlights($licenseId, $lineMatches, $highlights);
128 if (preg_match(
'/found full match between "'.$qFileName.
'" and "[^"]*" \(rf_pk=(?P<rf>[0-9]+)\); matched: (?P<start>[0-9]*)\+?(?P<len>[0-9]*)?/', $line, $lineMatches)) {
129 $licenseId = $lineMatches[
'rf'];
130 $licenseIds[] = $licenseId;
132 $start = $lineMatches[
'start'];
133 $end = $start + $lineMatches[
'len'];
135 $type = Highlight::MATCH;
137 $highlight =
new Highlight($start, $end, $type);
138 $highlight->setLicenseId($licenseId);
140 $highlights[] = $highlight;
144 return array($licenseIds, $highlights);
147 private function addDiffsToHighlights($licenseId, $lineMatches, &$highlights)
149 foreach (explode(
',', $lineMatches[
'diff']) as $diff) {
151 if (preg_match(
'/t\[(?P<start>[0-9]*)\+?(?P<len>[0-9]*)?\] M(?P<type>.?) s\[(?P<rf_start>[0-9]*)\+?(?P<rf_len>[0-9]*)?\]/', $diff, $diffMatches)) {
152 $start = intval($diffMatches[
'start']);
153 $end = $start + intval($diffMatches[
'len']);
154 $rfStart = intval($diffMatches[
'rf_start']);
155 $rfEnd = $rfStart + intval($diffMatches[
'rf_len']);
157 switch ($diffMatches[
'type']) {
159 $type = Highlight::MATCH;
162 $type = Highlight::CHANGED;
165 $type = Highlight::DELETED;
168 $type = Highlight::ADDED;
171 throw new \Exception(
'unrecognized diff type');
173 $highlight =
new Highlight($start, $end, $type, $rfStart, $rfEnd);
174 $highlight->setLicenseId($licenseId);
176 $highlights[] = $highlight;
178 throw new \Exception(
'failed parsing diff element: '.$diff);
184 public function renderLicenseList($licenseIds)
189 $licenseDao = $container->get(
'dao.license');
190 $isLoggedIn = $this->isLoggedIn();
191 foreach ($licenseIds as $licenseId) {
193 $license = $licenseDao->getLicenseById($licenseId);
195 $js =
"javascript:window.open('?mod=popup-license&rf=" . $license->getId() .
"','License text','width=600,height=400,toolbar=no,scrollbars=yes,resizable=yes');";
196 $content .=
'<li><a onclick="' . $js .
'" href="javascript:;">' . $license->getShortName() .
'</a></li>';
198 $content .=
'<li>' . $license->getShortName() .
'</li>';
201 return $content ? _(
'Possible licenses').
":<ul>$content</ul>" : _(
'No match found').
'<hr/>';
205 register_plugin(
new OneShot());
Contains the constants and helpers for authentication of user.
render($templateName, $vars=null, $headers=null)