2008-07-28 18:31:23 +00:00
|
|
|
<?php
|
|
|
|
/* -*- tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/*
|
|
|
|
# ***** BEGIN LICENSE BLOCK *****
|
|
|
|
# This file is part of InDefero, an open source project management application.
|
|
|
|
# Copyright (C) 2008 Céondo Ltd and contributors.
|
|
|
|
#
|
|
|
|
# InDefero is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# InDefero is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
|
|
|
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
|
|
|
#
|
|
|
|
# ***** END LICENSE BLOCK ***** */
|
|
|
|
|
|
|
|
Pluf::loadFunction('Pluf_HTTP_URL_urlForView');
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Make the links to issues and commits.
|
|
|
|
*/
|
|
|
|
class IDF_Template_IssueComment extends Pluf_Template_Tag
|
|
|
|
{
|
|
|
|
private $project = null;
|
2008-08-07 13:35:03 +00:00
|
|
|
private $request = null;
|
2008-09-01 19:42:18 +00:00
|
|
|
private $scm = null;
|
2008-07-28 18:31:23 +00:00
|
|
|
|
2009-02-16 17:12:21 +00:00
|
|
|
function start($text, $request, $echo=true, $wordwrap=true, $esc=true, $autolink=true, $nl2br=false)
|
2008-07-28 18:31:23 +00:00
|
|
|
{
|
2008-08-07 13:35:03 +00:00
|
|
|
$this->project = $request->project;
|
|
|
|
$this->request = $request;
|
2008-12-05 10:34:02 +00:00
|
|
|
$this->scm = IDF_Scm::get($request->project);
|
2008-11-22 22:51:23 +00:00
|
|
|
if ($esc) $text = Pluf_esc($text);
|
2008-11-23 13:58:43 +00:00
|
|
|
if ($autolink) {
|
2008-12-04 13:29:45 +00:00
|
|
|
$text = preg_replace('#([a-z]+://[^\s\(\)]+)#i',
|
|
|
|
'<a href="\1">\1</a>', $text);
|
2008-11-23 13:58:43 +00:00
|
|
|
}
|
2008-08-07 13:35:03 +00:00
|
|
|
if ($request->rights['hasIssuesAccess']) {
|
2009-06-19 14:01:17 +00:00
|
|
|
$text = preg_replace_callback('#(issues?|bugs?|tickets?)\s+(\d+)(\#ic\d*){0,1}((\s+and|\s+or|,)\s+(\d+)(\#ic\d*){0,1}){0,}#im',
|
2008-08-07 13:35:03 +00:00
|
|
|
array($this, 'callbackIssues'), $text);
|
|
|
|
}
|
|
|
|
if ($request->rights['hasSourceAccess']) {
|
2009-04-19 12:33:12 +00:00
|
|
|
$text = preg_replace_callback('#(commits?\s+)([0-9a-f]{1,40}(?:(?:\s+and|\s+or|,)\s+[0-9a-f]{1,40})*)\b#i',
|
|
|
|
array($this, 'callbackCommits'), $text);
|
2009-02-02 21:55:45 +00:00
|
|
|
$text = preg_replace_callback('#(src:)([^\s\(\)]+)#im',
|
|
|
|
array($this, 'callbackSource'), $text);
|
2008-08-07 13:35:03 +00:00
|
|
|
}
|
2009-06-26 11:12:26 +00:00
|
|
|
if ($wordwrap) $text = Pluf_Text::wrapHtml($text, 69, "\n");
|
2009-02-16 17:12:21 +00:00
|
|
|
if ($nl2br) $text = nl2br($text);
|
2008-11-14 14:41:51 +00:00
|
|
|
if ($echo) {
|
|
|
|
echo $text;
|
|
|
|
} else {
|
|
|
|
return $text;
|
|
|
|
}
|
2008-07-28 18:31:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* General call back for the issues.
|
|
|
|
*/
|
|
|
|
function callbackIssues($m)
|
|
|
|
{
|
2009-06-19 14:01:17 +00:00
|
|
|
if (count($m) == 3 || count($m) == 4) {
|
2008-07-28 18:31:23 +00:00
|
|
|
$issue = new IDF_Issue($m[2]);
|
|
|
|
if ($issue->id > 0 and $issue->project == $this->project->id) {
|
2009-06-19 14:01:17 +00:00
|
|
|
if (count($m) == 3) {
|
|
|
|
return $this->linkIssue($issue, $m[1].' '.$m[2]);
|
|
|
|
} else {
|
|
|
|
return $this->linkIssue($issue, $m[1].' '.$m[2], $m[3]);
|
|
|
|
}
|
2008-07-28 18:31:23 +00:00
|
|
|
} 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.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-19 12:33:12 +00:00
|
|
|
/**
|
|
|
|
* General call back to convert commits to HTML links.
|
|
|
|
*
|
|
|
|
* @param array $m Single regex match.
|
|
|
|
* @return string Content with converted commits.
|
|
|
|
*/
|
|
|
|
function callbackCommits($m)
|
|
|
|
{
|
|
|
|
$keyword = rtrim($m[1]);
|
|
|
|
if ('commits' === $keyword) {
|
|
|
|
// Multiple commits like 'commits 6e030e6, a25bfc1 and
|
|
|
|
// 3c094f8'.
|
|
|
|
return $m[1].preg_replace_callback('#\b[0-9a-f]{4,40}\b#i', array($this, 'callbackCommit'), $m[2]);
|
|
|
|
} else if ('commit' === $keyword) {
|
|
|
|
// Single commit like 'commit 6e030e6'.
|
|
|
|
return $m[1].call_user_func(array($this, 'callbackCommit'), array($m[2]));
|
|
|
|
}
|
|
|
|
return $m[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Convert plaintext commit to HTML link. Called from callbackCommits.
|
|
|
|
*
|
|
|
|
* Regex callback for {@link IDF_Template_IssueComment::callbackCommits()}.
|
|
|
|
*
|
|
|
|
* @param array Single regex match.
|
|
|
|
* @return string HTML A element with commit.
|
|
|
|
*/
|
2008-07-28 18:31:23 +00:00
|
|
|
function callbackCommit($m)
|
|
|
|
{
|
2009-04-19 12:33:12 +00:00
|
|
|
$co = $this->scm->getCommit($m[0]);
|
2009-05-26 19:47:54 +00:00
|
|
|
if (!$co) {
|
|
|
|
return $m[0]; // not a commit.
|
|
|
|
}
|
2009-04-19 12:33:12 +00:00
|
|
|
return '<a href="'
|
|
|
|
.Pluf_HTTP_URL_urlForView('IDF_Views_Source::commit', array($this->project->shortname, $co->commit))
|
|
|
|
.'">'.$m[0].'</a>';
|
2008-07-28 18:31:23 +00:00
|
|
|
}
|
|
|
|
|
2009-02-02 21:55:45 +00:00
|
|
|
function callbackSource($m)
|
|
|
|
{
|
2009-05-26 19:47:54 +00:00
|
|
|
if (!$this->scm->isAvailable()) return $m[0];
|
2009-02-02 21:55:45 +00:00
|
|
|
$file = $m[2];
|
2009-05-26 19:47:54 +00:00
|
|
|
$request_file_info = $this->scm->getPathInfo($file);
|
2009-02-02 21:55:45 +00:00
|
|
|
if (!$request_file_info) {
|
|
|
|
return $m[0];
|
|
|
|
}
|
|
|
|
if ($request_file_info->type != 'tree') {
|
2009-05-26 19:47:54 +00:00
|
|
|
return $m[1].'<a href="'.Pluf_HTTP_URL_urlForView('IDF_Views_Source::tree', array($this->project->shortname, $this->scm->getMainBranch(), $file)).'">'.$m[2].'</a>';
|
2009-02-02 21:55:45 +00:00
|
|
|
}
|
|
|
|
return $m[0];
|
|
|
|
}
|
|
|
|
|
2008-07-28 18:31:23 +00:00
|
|
|
/**
|
|
|
|
* Generate the link to an issue.
|
|
|
|
*
|
|
|
|
* @param IDF_Issue Issue.
|
|
|
|
* @param string Name of the link.
|
|
|
|
* @return string Linked issue.
|
|
|
|
*/
|
2009-06-19 14:01:17 +00:00
|
|
|
public function linkIssue($issue, $title, $anchor='')
|
2008-07-28 18:31:23 +00:00
|
|
|
{
|
|
|
|
$ic = (in_array($issue->status, $this->project->getTagIdsByStatus('closed'))) ? 'issue-c' : 'issue-o';
|
|
|
|
return '<a href="'.Pluf_HTTP_URL_urlForView('IDF_Views_Issue::view',
|
2009-06-19 14:01:17 +00:00
|
|
|
array($this->project->shortname, $issue->id)).$anchor.'" class="'.$ic.'" title="'.Pluf_esc($issue->summary).'">'.Pluf_esc($title).'</a>';
|
2008-07-28 18:31:23 +00:00
|
|
|
}
|
|
|
|
}
|