Change access of paths to a static method
[viewsvn.git] / includes / svncommon.php
1 <?php
2 /*=====================================================================*\
3 || ###################################################################
4 || # ViewSVN [#]version[#]
5 || # Copyright ©2002-[#]year[#] Iris Studios, Inc.
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 * Common functions that aren't Xquery-related and advanced query systems
24 *
25 * @package ViewSVN
26 */
27
28 /**
29 * This is a utility class that should be accessed in a static fashion. It
30 * is responsbile for various parsing mechanisms, mainly.
31 *
32 * @package ViewSVN
33 * @version $Id$
34 */
35 class SVNCommon
36 {
37 // ###################################################################
38 /**
39 * Prepares data for output
40 *
41 * @access public
42 *
43 * @param string Standard data
44 *
45 * @return string Output-ready data
46 */
47 function format($string)
48 {
49 // convert entities
50 $string = htmlspecialchars($string);
51
52 // tabs to 5 spaces
53 $string = str_replace("\t", ' ', $string);
54
55 // spaces to nbsp
56 if (true)
57 {
58 $string = preg_replace('#( +)#e', 'SVNCommon::format_spaces("\1")', $string);
59 }
60 // no word wrap
61 else
62 {
63 $string = str_replace(' ', '&nbsp;', $string);
64 }
65
66 // convert advanced diff
67 $string = str_replace(array('{@+' . '+}', '{@-' . '-}'), array('<span class="diff_add_bit">', '<span class="diff_del_bit">'), $string);
68 $string = str_replace(array('{/@+' . '+}', '{/@-' . '-}'), '</span>', $string);
69
70 // nl2br
71 $string = nl2br($string);
72
73 return $string;
74 }
75
76 // ###################################################################
77 /**
78 * Formats a SVN log message
79 *
80 * @access public
81 *
82 * @param string Unformatted log message
83 *
84 * @return string Output-ready log message
85 */
86 function format_log_message($message)
87 {
88 global $viewsvn;
89
90 $message = $viewsvn->entity_encode($message);
91
92 $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);
93
94 $list = false;
95 $lines = explode("\n", $message);
96 $message = '';
97 foreach ($lines AS $line)
98 {
99 if (preg_match('#^\s*?(\*|\-)\s?(.*)#', $line, $matches))
100 {
101 if ($list)
102 {
103 $message .= '<li>' . $matches[2] . '</li>';
104 }
105 else
106 {
107 $message .= '<ul class="list">';
108 $message .= '<li>' . $matches[2] . '</li>';
109 }
110 $list = true;
111 }
112 else
113 {
114 if ($list)
115 {
116 $message .= '</ul>';
117 $message .= $line;
118 }
119 else
120 {
121 $message .= $line;
122 $message .= '<br />';
123 }
124 $list = false;
125 }
126
127 $message .= "\n";
128 }
129
130 if ($list)
131 {
132 $message .= '</ul>';
133 }
134
135 $message = preg_replace('#(<br />)*$#', '', $message);
136
137 return $message;
138 }
139
140 // ###################################################################
141 /**
142 * Parses a date from Xquery XML outut
143 *
144 * @access public
145 *
146 * @param string Date string
147 *
148 * @return string Formatted and readable date string
149 */
150 function format_date_string($string)
151 {
152 // 2005-01-23T20:42:53.703057Z
153 return preg_replace('#(....)\-(..)\-(..)T(..):(..):(..).(.*)Z#e', 'gmdate("r", mktime(\4, \5, \6, \2, \3, \1))', $string);
154 }
155
156 // ###################################################################
157 /**
158 * Counts the spaces and replaces two or more ones
159 *
160 * @access private
161 *
162 * @param string Spaced string
163 *
164 * @return string &nbsp;'d string
165 */
166 function format_spaces($thestring)
167 {
168 if (strlen($thestring) >= 2)
169 {
170 $thestring = str_replace(' ', '&nbsp;', $thestring);
171 }
172
173 return $thestring;
174 }
175
176 // ###################################################################
177 /**
178 * Prints the file changed list
179 *
180 * @access public
181 *
182 * @public array List of file changes
183 * @public string The repository
184 * @public integer Current revision
185 *
186 * @return string Processed HTML
187 */
188 function construct_file_changes($changes, $repos, $revision)
189 {
190 global $viewsvn;
191
192 $files = '';
193
194 foreach ($changes AS $file)
195 {
196 switch ($file['action'])
197 {
198 case 'A':
199 $class = 'file_add';
200 $tooltip = $viewsvn->lang->string('Added');
201 break;
202 case 'D':
203 $class = 'file_delete';
204 $tooltip = $viewsvn->lang->string('Deleted');
205 break;
206 case 'M':
207 $class = 'file_modify';
208 $tooltip = $viewsvn->lang->string('Modified');
209 break;
210 case 'R':
211 $class = 'file_replace';
212 $tooltip = $viewsvn->lang->string('Replaced');
213 break;
214 }
215
216 $show['from'] = (bool)$file['from'];
217
218 if ($file['from'])
219 {
220 $class = 'file_move';
221 $tooltip = 'Moved/Copied';
222 preg_match('#(.*):([0-9]+)#', $file['from'], $matches);
223 $link['from'] = $viewsvn->path . '/view.php/' . $repos . '/' . $matches[1] . Paths::fetch_rev_str(false, $matches[2]);
224 }
225
226 $link['file'] = $viewsvn->path . '/view.php/' . $repos . $file['value'] . Paths::fetch_rev_str(false, $revision);
227
228 eval('$files .= "' . $viewsvn->template->fetch('file_change') . '";');
229 }
230
231 return $files;
232 }
233
234 // ###################################################################
235 /**
236 * Generates a clean revision number
237 *
238 * @access public
239 *
240 * @param integer Revision number
241 *
242 * @return mixed Cleaned revision or HEAD
243 */
244 function rev($revision)
245 {
246 if (($revision = intval($revision)) < 1)
247 {
248 $revision = 'HEAD';
249 }
250 return $revision;
251 }
252 }
253
254 /*=====================================================================*\
255 || ###################################################################
256 || # $HeadURL$
257 || # $Id$
258 || ###################################################################
259 \*=====================================================================*/
260 ?>