From 3fb47562ce0453af4481b1b470581f5bcbbc6b3b Mon Sep 17 00:00:00 2001 From: Loic d'Anterroches Date: Mon, 28 Jul 2008 20:31:23 +0200 Subject: [PATCH] Automatically create links in the issue description and comments. Link to issues but also commits. --- src/IDF/Project.php | 12 +++- src/IDF/Template/IssueComment.php | 107 +++++++++++++++++++++++++++++ src/IDF/templates/issues/view.html | 2 +- www/media/idf/css/style.css | 9 ++- 4 files changed, 126 insertions(+), 4 deletions(-) create mode 100644 src/IDF/Template/IssueComment.php diff --git a/src/IDF/Project.php b/src/IDF/Project.php index b63a302..e656123 100644 --- a/src/IDF/Project.php +++ b/src/IDF/Project.php @@ -30,9 +30,11 @@ class IDF_Project extends Pluf_Model { public $_model = __CLASS__; + public $_extra_cache = array(); function init() { + $this->_extra_cache = array(); $this->_a['table'] = 'idf_projects'; $this->_a['model'] = __CLASS__; $this->_a['cols'] = array( @@ -147,11 +149,18 @@ class IDF_Project extends Pluf_Model * Get the open/closed tag ids as they are often used when doing * listings. * + * As this can be often used, the info are cached. + * * @param string Status ('open') or 'closed' + * @param bool Force cache refresh (false) * @return array Ids of the open/closed tags */ - public function getTagIdsByStatus($status='open') + public function getTagIdsByStatus($status='open', $cache_refresh=false) { + if (!$cache_refresh + and isset($this->_extra_cache['getTagIdsByStatus-'.$status])) { + return $this->_extra_cache['getTagIdsByStatus-'.$status]; + } switch ($status) { case 'open': $key = 'labels_issue_open'; @@ -167,6 +176,7 @@ class IDF_Project extends Pluf_Model foreach ($this->getTagsFromConfig($key, $default, 'Status') as $tag) { $tags[] = (int) $tag->id; } + $this->_extra_cache['getTagIdsByStatus-'.$status] = $tags; return $tags; } diff --git a/src/IDF/Template/IssueComment.php b/src/IDF/Template/IssueComment.php new file mode 100644 index 0000000..223b917 --- /dev/null +++ b/src/IDF/Template/IssueComment.php @@ -0,0 +1,107 @@ +project = $project; + $this->git = new IDF_Git(Pluf::f('git_repository')); + $text = wordwrap($text, 80, "\n", true); + $text = Pluf_esc($text); + $text = ereg_replace('[[:alpha:]]+://[^<>[:space:]]+[[:alnum:]/]', + '\\0', + $text); + $text = preg_replace_callback('#(issues?|bugs?|tickets?)\s+(\d+)((\s+and|\s+or|,)\s+(\d+)){0,}#im', + array($this, 'callbackIssues'), $text); + $text = preg_replace_callback('#(commit\s+)([0-9a-f]{5,40})#im', + array($this, 'callbackCommit'), $text); + echo $text; + } + + /** + * General call back for the issues. + */ + function callbackIssues($m) + { + if (count($m) == 3) { + $issue = new IDF_Issue($m[2]); + if ($issue->id > 0 and $issue->project == $this->project->id) { + return $this->linkIssue($issue, $m[1].' '.$m[2]); + } else { + return $m[0]; // not existing issue. + } + } else { + return preg_replace_callback('/(\d+)/', + array($this, 'callbackIssue'), + $m[0]); + } + } + + /** + * Call back for the case of multiple issues like 'issues 1, 2 and 3'. + * + * Called from callbackIssues, it is linking only the number of + * the issues. + */ + function callbackIssue($m) + { + $issue = new IDF_Issue($m[1]); + if ($issue->id > 0 and $issue->project == $this->project->id) { + return $this->linkIssue($issue, $m[1]); + } else { + return $m[0]; // not existing issue. + } + } + + function callbackCommit($m) + { + if ($this->git->testHash($m[2]) != 'commit') { + return $m[0]; + } + $co = $this->git->getCommit($m[2]); + return ''.$m[1].$m[2].''; + } + + /** + * Generate the link to an issue. + * + * @param IDF_Issue Issue. + * @param string Name of the link. + * @return string Linked issue. + */ + public function linkIssue($issue, $title) + { + $ic = (in_array($issue->status, $this->project->getTagIdsByStatus('closed'))) ? 'issue-c' : 'issue-o'; + return ''.Pluf_esc($title).''; + } +} diff --git a/src/IDF/templates/issues/view.html b/src/IDF/templates/issues/view.html index 14cab4a..8477084 100644 --- a/src/IDF/templates/issues/view.html +++ b/src/IDF/templates/issues/view.html @@ -13,7 +13,7 @@

{blocktrans}Comment {$i} by {$who}, {$c.creation_dtime|date}{/blocktrans}

{/if} -
{if strlen($c.content) > 0}{$c.content|issuetext}{else}{trans '(No comments were given for this change.)'}{/if}
+
{if strlen($c.content) > 0}{issuetext $c.content, $project}{else}{trans '(No comments were given for this change.)'}{/if}
{if $i> 0 and $c.changedIssue()}
diff --git a/www/media/idf/css/style.css b/www/media/idf/css/style.css index c5473be..9f12582 100644 --- a/www/media/idf/css/style.css +++ b/www/media/idf/css/style.css @@ -123,8 +123,13 @@ span.px-header-title { /** * Issue */ -p.issue-comment-text { - font-family: monospace; +a.issue-c { + text-decoration: line-through; +} + +pre.issue-comment-text { + font-family: monospace; + line-height: 1.2; /* to be nice also with links */ } div.issue-comment {