1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10: 11:
12: abstract class Horde_Vcs_File_Base
13: {
14: 15: 16: 17: 18:
19: protected $_driver;
20:
21: 22: 23: 24: 25:
26: protected $_dir;
27:
28: 29: 30: 31: 32:
33: protected $_name;
34:
35: 36: 37: 38: 39:
40: protected $_logs = array();
41:
42: 43: 44: 45: 46:
47: protected $_revs = array();
48:
49: 50: 51:
52: protected $_rep;
53:
54: 55: 56: 57: 58:
59: protected $_branch = null;
60:
61: 62: 63: 64: 65:
66: protected $_initialized = false;
67:
68: 69: 70: 71: 72: 73: 74:
75: public function __construct($filename, $opts = array())
76: {
77: $this->_name = basename($filename);
78: $this->_dir = dirname($filename);
79: if ($this->_dir == '.') {
80: $this->_dir = '';
81: }
82:
83: if (!empty($opts['branch'])) {
84: $this->_branch = $opts['branch'];
85: }
86: }
87:
88: 89: 90:
91: public function __sleep()
92: {
93: return array_diff(array_keys(get_object_vars($this)), array('_rep'));
94: }
95:
96: abstract protected function _init();
97:
98: protected function _ensureInitialized()
99: {
100: if (!$this->_initialized) {
101: $this->_initialized = true;
102: $this->_init();
103: }
104: }
105:
106: 107: 108:
109: public function setRepository($rep)
110: {
111: $this->_rep = $rep;
112: }
113:
114: 115: 116:
117: public function getBlob($revision)
118: {
119: return $this->_rep->checkout($this->getPath(), $revision);
120: }
121:
122: 123: 124: 125: 126:
127: public function isDeleted()
128: {
129: return false;
130: }
131:
132: 133: 134: 135: 136:
137: public function getFileName()
138: {
139: return $this->_name;
140: }
141:
142: 143: 144: 145: 146: 147:
148: public function getRevision()
149: {
150: $this->_ensureInitialized();
151: if (!isset($this->_revs[0])) {
152: throw new Horde_Vcs_Exception('No revisions');
153: }
154: return $this->_revs[0];
155: }
156:
157: 158: 159: 160: 161: 162: 163:
164: public function getPreviousRevision($rev)
165: {
166: $this->_ensureInitialized();
167: $key = array_search($rev, $this->_revs);
168: return (($key !== false) && isset($this->_revs[$key + 1]))
169: ? $this->_revs[$key + 1]
170: : null;
171: }
172:
173: 174: 175:
176: protected function _getLog($rev = null)
177: {
178: $class = 'Horde_Vcs_Log_' . $this->_driver;
179:
180: if (!is_null($rev) && !empty($this->_cache)) {
181: $cacheId = implode('|', array($class, $this->sourceroot, $this->getPath(), $rev, $this->_cacheVersion));
182:
183:
184: if ($this->_cache->exists($cacheId, 0)) {
185: $ob = unserialize($this->_cache->get($cacheId, 0));
186: }
187: }
188:
189: if (empty($ob) || !$ob) {
190: $ob = new $class($rev);
191:
192: }
193: $ob->setRepository($this->_rep);
194: $ob->setFile($this);
195:
196: if (!is_null($rev) && !empty($this->_cache)) {
197: $this->_cache->set($cacheId, serialize($ob));
198: }
199:
200: return $ob;
201: }
202:
203: 204: 205: 206: 207: 208:
209: abstract public function getLastLog();
210:
211: 212: 213: 214: 215: 216: 217:
218: public function applySort($how = Horde_Vcs::SORT_REV)
219: {
220: $this->_ensureInitialized();
221:
222: switch ($how) {
223: case Horde_Vcs::SORT_NAME:
224: $func = 'Name';
225: break;
226:
227: case Horde_Vcs::SORT_AGE:
228: $func = 'Age';
229: break;
230:
231: case Horde_Vcs::SORT_REV:
232: default:
233: $func = 'Revision';
234: break;
235: }
236:
237: uasort($this->_logs, array($this, 'sortBy' . $func));
238: return true;
239: }
240:
241: 242: 243:
244: public function sortByRevision($a, $b)
245: {
246: return $this->_rep->cmp($b->getRevision(), $a->getRevision());
247: }
248:
249: public function sortByAge($a, $b)
250: {
251: return $b->getDate() - $a->getDate();
252: }
253:
254: public function sortByName($a, $b)
255: {
256: return strcmp($a->getAuthor(), $b->getAuthor());
257: }
258:
259: 260: 261: 262: 263:
264: public function getSourcerootPath()
265: {
266: return ltrim($this->_dir . '/' . $this->_name, '/');
267: }
268:
269: 270: 271: 272: 273: 274:
275: public function getPath()
276: {
277: return $this->_rep->sourceroot . '/' . $this->getSourcerootPath();
278: }
279:
280: 281: 282:
283: public function getBranches()
284: {
285: return array();
286: }
287:
288: 289: 290:
291: public function getLog($rev = null)
292: {
293: $this->_ensureInitialized();
294: return is_null($rev)
295: ? $this->_logs
296: : (isset($this->_logs[$rev]) ? $this->_logs[$rev] : null);
297: }
298:
299: 300: 301:
302: public function revisionCount()
303: {
304: $this->_ensureInitialized();
305: return count($this->_revs);
306: }
307:
308: 309: 310:
311: public function getTags()
312: {
313: return array();
314: }
315: }
316: