1: <?php
2: /**
3: * Base directory class that stores information about the files in a single
4: * directory in the repository.
5: *
6: * Copyright 2008-2012 Horde LLC (http://www.horde.org/)
7: *
8: * See the enclosed file COPYING for license information (LGPL). If you
9: * did not receive this file, see http://www.horde.org/licenses/lgpl21.
10: *
11: * @author Anil Madhavapeddy <anil@recoil.org>
12: * @author Michael Slusarz <slusarz@horde.org>
13: * @author Jan Schneider <jan@horde.org>
14: * @package Vcs
15: */
16: abstract class Horde_Vcs_Directory_Base
17: {
18: /**
19: * The directory's repository object.
20: *
21: * @var Horde_Vcs_Base
22: */
23: protected $_rep;
24:
25: /**
26: * The directory's path inside the repository.
27: *
28: * @var string
29: */
30: protected $_dirName;
31:
32: /**
33: * A list of Horde_Vcs_File_Base objects representing all files inside this
34: * directory.
35: *
36: * @var array
37: */
38: protected $_files = array();
39:
40: /**
41: * A (string) list of directories inside this directory.
42: *
43: * @var array
44: */
45: protected $_dirs = array();
46:
47: /**
48: * Constructor.
49: *
50: * @param Horde_Vcs_Base $rep A repository object.
51: * @param string $dn Path to the directory.
52: * @param array $opts Any additional options:
53: *
54: * @throws Horde_Vcs_Exception
55: */
56: public function __construct(Horde_Vcs_Base $rep, $dn, $opts = array())
57: {
58: $this->_rep = $rep;
59: $this->_dirName = '/' . ltrim($dn, '/');
60: }
61:
62: /**
63: * Returns a list of directories inside this directory.
64: *
65: * return array A (string) list of directories.
66: */
67: public function getDirectories()
68: {
69: return $this->_dirs;
70: }
71:
72: /**
73: * Returns a list of all files inside this directory.
74: *
75: * @return array A list of Horde_Vcs_File_Base objects.
76: */
77: public function getFiles($showdeleted = false)
78: {
79: return $this->_files;
80: }
81:
82: /**
83: * Sorts the the directory contents.
84: *
85: * @param integer $how A Horde_Vcs::SORT_* constant where * can be:
86: * NONE, NAME, AGE, REV for sorting by name, age or
87: * revision.
88: * @param integer $dir A Horde_Vcs::SORT_* constant where * can be:
89: * ASCENDING, DESCENDING for the order of the sort.
90: */
91: public function applySort($how = Horde_Vcs::SORT_NONE,
92: $dir = Horde_Vcs::SORT_ASCENDING)
93: {
94: // Always sort directories by name.
95: natcasesort($this->_dirs);
96:
97: $this->_doFileSort($this->_files, $how);
98:
99: if ($dir == Horde_Vcs::SORT_DESCENDING) {
100: $this->_dirs = array_reverse($this->_dirs);
101: $this->_files = array_reverse($this->_files);
102: }
103: }
104:
105: /**
106: * Sorts a list files.
107: *
108: * @see applySort()
109: *
110: * @param array $fileList A list of files.
111: * @param integer $how A Horde_Vcs::SORT_* constant.
112: */
113: protected function _doFileSort(&$fileList, $how = Horde_Vcs::SORT_NONE)
114: {
115: switch ($how) {
116: case Horde_Vcs::SORT_AGE:
117: usort($fileList, array($this, '_fileAgeSort'));
118: break;
119:
120: case Horde_Vcs::SORT_NAME:
121: usort($fileList, array($this, '_fileNameSort'));
122: break;
123:
124: case Horde_Vcs::SORT_AUTHOR:
125: usort($fileList, array($this, '_fileAuthorSort'));
126: break;
127:
128: case Horde_Vcs::SORT_REV:
129: usort($fileList, array($this, '_fileRevSort'));
130: break;
131:
132: case Horde_Vcs::SORT_NONE:
133: default:
134: break;
135: }
136: }
137:
138: /**
139: * Sort function for ascending age.
140: */
141: public function _fileAgeSort($a, $b)
142: {
143: $aa = $a->getLastLog();
144: $bb = $b->getLastLog();
145: return ($aa->getDate() == $bb->getDate())
146: ? 0
147: : (($aa->getDate() < $bb->getDate()) ? 1 : -1);
148: }
149:
150: /**
151: * Sort function by author name.
152: */
153: public function _fileAuthorSort($a, $b)
154: {
155: $aa = $a->getLastLog();
156: $bb = $b->getLastLog();
157: return ($aa->getAuthor() == $bb->getAuthor())
158: ? 0
159: : (($aa->getAuthor() > $bb->getAuthor()) ? 1 : -1);
160: }
161:
162: /**
163: * Sort function for ascending filename.
164: */
165: public function _fileNameSort($a, $b)
166: {
167: return strcasecmp($a->getFileName(), $b->getFileName());
168: }
169:
170: /**
171: * Sort function for ascending revision.
172: */
173: public function _fileRevSort($a, $b)
174: {
175: return $this->_rep->cmp($a->getRevision(), $b->getRevision());
176: }
177:
178: /**
179: * Returns a list of all branches in this directory.
180: *
181: * @return array A branch list.
182: */
183: public function getBranches()
184: {
185: return array();
186: }
187: }
188: