project = $request->project; $this->request = $request; $this->scm = IDF_Scm::get($request->project); if ($wordwrap) $text = wordwrap($text, 69, "\n", true); if ($esc) $text = Pluf_esc($text); if ($autolink) { $text = preg_replace('#([a-z]+://[^\s\(\)]+)#i', '\1', $text); } if ($request->rights['hasIssuesAccess']) { $text = preg_replace_callback('#(issues?|bugs?|tickets?)\s+(\d+)((\s+and|\s+or|,)\s+(\d+)){0,}#im', array($this, 'callbackIssues'), $text); } if ($request->rights['hasSourceAccess']) { $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); $text = preg_replace_callback('#(src:)([^\s\(\)]+)#im', array($this, 'callbackSource'), $text); } if ($nl2br) $text = nl2br($text); if ($echo) { echo $text; } else { return $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. } } /** * 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. */ function callbackCommit($m) { try { if ('commit' != $this->scm->testHash($m[0])) { return $m[0]; // not a commit. } } catch (IDF_Scm_Exception $e) { return $m[0]; // commit not found. } $co = $this->scm->getCommit($m[0]); return ''.$m[0].''; } function callbackSource($m) { $branches = $this->scm->getBranches(); if (count($branches) == 0) return $m[0]; $file = $m[2]; if ('commit' != $this->scm->testHash($branches[0], $file)) { return $m[0]; } $request_file_info = $this->scm->getFileInfo($file, $branches[0]); if (!$request_file_info) { return $m[0]; } if ($request_file_info->type != 'tree') { return $m[1].''.$m[2].''; } return $m[0]; } /** * 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).''; } }