Overview

Packages

  • None
  • Vcs

Classes

  • Horde_Vcs
  • Horde_Vcs_Base
  • Horde_Vcs_Cvs
  • Horde_Vcs_Directory_Base
  • Horde_Vcs_Directory_Cvs
  • Horde_Vcs_Directory_Git
  • Horde_Vcs_Directory_Rcs
  • Horde_Vcs_Directory_Svn
  • Horde_Vcs_File_Base
  • Horde_Vcs_File_Cvs
  • Horde_Vcs_File_Git
  • Horde_Vcs_File_Rcs
  • Horde_Vcs_File_Svn
  • Horde_Vcs_Git
  • Horde_Vcs_Log_Base
  • Horde_Vcs_Log_Cvs
  • Horde_Vcs_Log_Git
  • Horde_Vcs_Log_Rcs
  • Horde_Vcs_Log_Svn
  • Horde_Vcs_Patchset
  • Horde_Vcs_Patchset_Base
  • Horde_Vcs_Patchset_Cvs
  • Horde_Vcs_Patchset_Git
  • Horde_Vcs_Patchset_Svn
  • Horde_Vcs_QuickLog_Base
  • Horde_Vcs_QuickLog_Cvs
  • Horde_Vcs_QuickLog_Git
  • Horde_Vcs_QuickLog_Rcs
  • Horde_Vcs_QuickLog_Svn
  • Horde_Vcs_Rcs
  • Horde_Vcs_Svn
  • Overview
  • Package
  • Class
  • Tree
  1: <?php
  2: /**
  3:  * Base file class.
  4:  *
  5:  * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
  6:  *
  7:  * See the enclosed file COPYING for license information (LGPL). If you
  8:  * did not receive this file, see http://www.horde.org/licenses/lgpl21.
  9:  *
 10:  * @package Vcs
 11:  */
 12: abstract class Horde_Vcs_File_Base
 13: {
 14:     /**
 15:      * The current driver.
 16:      *
 17:      * @var string
 18:      */
 19:     protected $_driver;
 20: 
 21:     /**
 22:      * The directory of this file.
 23:      *
 24:      * @var string
 25:      */
 26:     protected $_dir;
 27: 
 28:     /**
 29:      * The name of this file.
 30:      *
 31:      * @var string
 32:      */
 33:     protected $_name;
 34: 
 35:     /**
 36:      * TODO
 37:      *
 38:      * @var array
 39:      */
 40:     protected $_logs = array();
 41: 
 42:     /**
 43:      * TODO
 44:      *
 45:      * @var array
 46:      */
 47:     protected $_revs = array();
 48: 
 49:     /**
 50:      * TODO
 51:      */
 52:     protected $_rep;
 53: 
 54:     /**
 55:      * TODO
 56:      *
 57:      * @var string
 58:      */
 59:     protected $_branch = null;
 60: 
 61:     /**
 62:      * Have we initalized logs and revisions?
 63:      *
 64:      * @var boolean
 65:      */
 66:     protected $_initialized = false;
 67: 
 68:     /**
 69:      * Constructor.
 70:      *
 71:      * @param string $filename  Full path (inside the source root) to this file.
 72:      * @param array $opts       Additional parameters:
 73:      *                          - 'branch': (string)
 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:      * When serializing, don't return the repository object
 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:      * TODO
108:      */
109:     public function setRepository($rep)
110:     {
111:         $this->_rep = $rep;
112:     }
113: 
114:     /**
115:      * TODO - better name, wrap an object around this?
116:      */
117:     public function getBlob($revision)
118:     {
119:         return $this->_rep->checkout($this->getPath(), $revision);
120:     }
121: 
122:     /**
123:      * Has the file been deleted?
124:      *
125:      * @return boolean  Is this file deleted?
126:      */
127:     public function isDeleted()
128:     {
129:         return false;
130:     }
131: 
132:     /**
133:      * Returns name of the current file without the repository extensions.
134:      *
135:      * @return string  Filename without repository extension
136:      */
137:     public function getFileName()
138:     {
139:         return $this->_name;
140:     }
141: 
142:     /**
143:      * Returns the last revision of the current file on the HEAD branch.
144:      *
145:      * @return string  Last revision of the current file.
146:      * @throws Horde_Vcs_Exception
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:      * Returns the revision before the specified revision.
159:      *
160:      * @param string $rev  A revision.
161:      *
162:      * @return string  The previous revision or null if the first revision.
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:      * @param string $rev  The revision identifier.
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:             // Individual revisions can be cached forever
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:      * Returns a log object for the most recent log entry of this file.
205:      *
206:      * @return Horde_Vcs_QuickLog  Log object of the last entry in the file.
207:      * @throws Horde_Vcs_Exception
208:      */
209:     abstract public function getLastLog();
210: 
211:     /**
212:      * Sort the list of Horde_Vcs_Log objects that this file contains.
213:      *
214:      * @param integer $how  Horde_Vcs::SORT_REV (sort by revision),
215:      *                      Horde_Vcs::SORT_NAME (sort by author name), or
216:      *                      Horde_Vcs::SORT_AGE (sort by commit date).
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:      * The sortBy*() functions are internally used by applySort.
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:      * Return the filename relative to its sourceroot.
261:      *
262:      * @return string  Pathname relative to the sourceroot.
263:      */
264:     public function getSourcerootPath()
265:     {
266:         return ltrim($this->_dir . '/' . $this->_name, '/');
267:     }
268: 
269:     /**
270:      * Return the "base" filename (i.e. the filename needed by the various
271:      * command line utilities).
272:      *
273:      * @return string  A filename.
274:      */
275:     public function getPath()
276:     {
277:         return $this->_rep->sourceroot . '/' . $this->getSourcerootPath();
278:     }
279: 
280:     /**
281:      * TODO
282:      */
283:     public function getBranches()
284:     {
285:         return array();
286:     }
287: 
288:     /**
289:      * TODO
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:      * TODO
301:      */
302:     public function revisionCount()
303:     {
304:         $this->_ensureInitialized();
305:         return count($this->_revs);
306:     }
307: 
308:     /**
309:      * TODO
310:      */
311:     public function getTags()
312:     {
313:         return array();
314:     }
315: }
316: 
API documentation generated by ApiGen