- Blame now works
[viewsvn.git] / includes / functions.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # ViewSVN [#]version[#]
5 || # Copyright ©2002-[#]year[#] Blue Static
6 || #
7 || # This program is free software; you can redistribute it and/or modify
8 || # it under the terms of the GNU General Public License as published by
9 || # the Free Software Foundation; version [#]gpl[#] of the License.
10 || #
11 || # This program is distributed in the hope that it will be useful, but
12 || # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 || # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
14 || # more details.
15 || #
16 || # You should have received a copy of the GNU General Public License along
17 || # with this program; if not, write to the Free Software Foundation, Inc.,
18 || # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
19 || ###################################################################
20 \*=====================================================================*/
21
22 // ###################################################################
23 /**
24 * Constructs the navbar from the given path elememnts. The current
25 * element is never selectable
26 *
27 * @return string Compiled HTML navigation bar
28 */
29 function ConstructNavbar()
30 {
31 $path = preg_split('#/#', BSRegister::Get('input')->in['path'], -1, PREG_SPLIT_NO_EMPTY);
32 $input =& BSRegister::Get('input');
33
34 $html = '[<a href="' . ConstructLink('browse', $input->in['repos'], null, $input->in['rev']) . '">' . $input->in['repos'] . '</a>]: ';
35
36 if (empty($path))
37 {
38 return $html . '<strong>/</strong>';
39 }
40
41 $build = '';
42 foreach ($path AS $index => $part)
43 {
44 $build .= '/' . $part;
45 if (sizeof($path) - 1 == $index)
46 {
47 $html .= '/ <strong>' . $part . '</strong>';
48 }
49 else
50 {
51 $html .= '/ <a href="' . ConstructLink('browse', $input->in['repos'], $build, $input->in['rev']) . '"><strong>' . $part . '</strong></a> ';
52 }
53 }
54
55 return $html;
56 }
57
58 // ###################################################################
59 /**
60 * Formats an array of properties into a compiled HTML template
61 *
62 * @param array Array of properties
63 *
64 * @return string Compiled HTML property list
65 */
66 function FormatPropList($props)
67 {
68 if (sizeof($props) < 0)
69 {
70 return '';
71 }
72 foreach ($props AS $propname => $propval)
73 {
74 $data = sprintf(_('Property <strong>%1$s</strong> set to <em>%2$s</em>'), $propname, $propval);
75 eval('$proplist .= "' . BSRegister::Get('template')->fetch('property') . '";');
76 }
77 return $proplist;
78 }
79
80 // ###################################################################
81 /**
82 * Formats a SVN log message
83 *
84 * @param string Unformatted log message
85 *
86 * @return string Output-ready log message
87 */
88 function FormatLogMessage($message)
89 {
90 global $viewsvn;
91
92 $message = BSRegister::Get('input')->entityEncode($message);
93
94 // TODO - fix the revision links
95 // $message = preg_replace('#r([0-9]+)#e', '"<a href=\"" . $viewsvn->maincontrol->href_struct("diff.php" . Paths::fetch_rev_str(true, "\1", 0), "/") . "\">r\1</a>"', $message);
96
97 $list = false;
98 $lines = explode("\n", $message);
99 $message = '';
100 foreach ($lines AS $line)
101 {
102 if (preg_match('#^\s*?(\*|\-)\s?(.*)#', $line, $matches))
103 {
104 if ($list)
105 {
106 $message .= '<li>' . $matches[2] . '</li>';
107 }
108 else
109 {
110 $message .= '<ul class="list">';
111 $message .= '<li>' . $matches[2] . '</li>';
112 }
113 $list = true;
114 }
115 else
116 {
117 if ($list)
118 {
119 $message .= '</ul>';
120 $message .= $line;
121 }
122 else
123 {
124 $message .= $line;
125 $message .= '<br />';
126 }
127 $list = false;
128 }
129
130 $message .= "\n";
131 }
132
133 if ($list)
134 {
135 $message .= '</ul>';
136 }
137
138 $message = preg_replace('#(<br />)*$#', '', $message);
139
140 return $message;
141 }
142
143 // ###################################################################
144 /**
145 * Parses a date from SVN into something more human-friendly
146 *
147 * @param string Date string
148 *
149 * @return string Formatted and readable date string
150 */
151 function FormatSvnDate($string)
152 {
153 // 2005-01-23T20:42:53.703057Z
154 return preg_replace('#(....)\-(..)\-(..)T(..):(..):(..).(.*)Z#e', 'gmdate("r", mktime(\4, \5, \6, \2, \3, \1))', $string);
155 }
156
157 // ###################################################################
158 /**
159 * Generates an absolute-pathed link if you just pass it the four
160 * paramaters
161 *
162 * @param string Action/script
163 * @param string Repository name
164 * @param string Subpath in repository
165 * @param integer Revision
166 *
167 * @return string Link (not wrapped in <a>)
168 */
169 function ConstructLink($action, $repos, $path, $rev)
170 {
171 return BSRegister::Get('webpath') . '/' . $action . '/' . $repos . ':' . $path . ($rev > 0 ? '@' . $rev : '');
172 }
173
174 // ###################################################################
175 /**
176 * Prepares data for output
177 *
178 * @param string Standard data
179 *
180 * @return string Output-ready data
181 */
182 function FormatCode($string)
183 {
184 // convert entities
185 $string = htmlspecialchars($string);
186
187 // tabs to 5 spaces
188 $string = str_replace("\t", ' ', $string);
189
190 // spaces to nbsp
191 if (true)
192 {
193 $string = preg_replace('#( +)#e', '_FormatSpaces("\1")', $string);
194 }
195 // no word wrap
196 else
197 {
198 $string = str_replace(' ', '&nbsp;', $string);
199 }
200
201 // convert advanced diff
202 $string = str_replace(array('{@+' . '+}', '{@-' . '-}'), array('<span class="diff_add_bit">', '<span class="diff_del_bit">'), $string);
203 $string = str_replace(array('{/@+' . '+}', '{/@-' . '-}'), '</span>', $string);
204
205 // nl2br
206 $string = nl2br($string);
207
208 return $string;
209 }
210
211 // ###################################################################
212 /**
213 * Counts the spaces and replaces two or more ones
214 *
215 * @param string Spaced string
216 *
217 * @return string &nbsp;'d string
218 */
219 function _FormatSpaces($thestring)
220 {
221 if (strlen($thestring) >= 2)
222 {
223 $thestring = str_replace(' ', '&nbsp;', $thestring);
224 }
225
226 return $thestring;
227 }
228
229 /*=====================================================================*\
230 || ###################################################################
231 || # $HeadURL$
232 || # $Id$
233 || ###################################################################
234 \*=====================================================================*/
235 ?>