00001 <?php
00002
00015 abstract class SnapBulkAction extends SObject {
00017 const DEPTH_IMMEDIATE = 0;
00019 const DEPTH_ALL = 1;
00020
00021 private static $VERSION_ACTIONS = array(
00022 SnapVersion::STATUS_PRIVATE => array(
00023 SnapVersion::STATUS_PENDING => 'submit',
00024 SnapVersion::STATUS_DEFUNCT => 'defunct'
00025 ),
00026 SnapVersion::STATUS_PENDING => array(
00027 SnapVersion::STATUS_DEV => 'approveForDev',
00028 SnapVersion::STATUS_LIVE => 'approveForLive',
00029 SnapVersion::STATUS_PRIVATE => 'deny',
00030 SnapVersion::STATUS_DEFUNCT => 'defunct'
00031 ),
00032 SnapVersion::STATUS_DEV => array(
00033 SnapVersion::STATUS_LIVE => 'approveForLive',
00034 SnapVersion::STATUS_DEFUNCT => 'defunct'
00035 ),
00036 SnapVersion::STATUS_LIVE => array(
00037 SnapVersion::STATUS_DEFUNCT => 'defunct'
00038 ),
00039 SnapVersion::STATUS_DEFUNCT => array(
00040 -1 => 'destroy'
00041 )
00042 );
00043
00059 public static function listVersionsRecursive($base = '//', $depth = self::DEPTH_ALL,
00060 $constraint = array(), $limit = array(), $order = array())
00061 {
00062 $baseObj = SnapFile::lookup($base);
00063 if(!$baseObj) {
00064 self::setStaticError('Invalid base path: \'' . $base . '\'');
00065 return null;
00066 }
00067
00068 if($depth == self::DEPTH_IMMEDIATE) {
00069 $baseId = $baseObj->getId();
00070 if($baseObj->getType() == 'Directory') {
00071 $query = <<<END_QUERY
00072 SELECT versionId, path FROM PathCache
00073 LEFT JOIN Version ON PathCache.versionId = Version.id
00074 WHERE PathCache.path LIKE BINARY '$base%'
00075 AND PathCache.versionId IS NOT NULL
00076 AND Version.resourceId IN
00077 (SELECT id FROM Resource
00078 LEFT JOIN ResourceLink ON Resource.id = ResourceLink.childId
00079 WHERE ResourceLink.parentId = $baseId)
00080 END_QUERY;
00081 }
00082 else {
00083 $query = <<<END_QUERY
00084 SELECT versionId, path FROM PathCache
00085 LEFT JOIN Version ON PathCache.versionId = Version.id
00086 WHERE PathCache.path LIKE BINARY '$base%'
00087 AND PathCache.versionId IS NOT NULL
00088 AND Version.resourceId = $baseId
00089 END_QUERY;
00090 }
00091 }
00092 else {
00093 $query = 'SELECT versionId, path FROM PathCache WHERE versionId IS NOT NULL AND path LIKE BINARY \''
00094 . $base . '%\'';
00095 }
00096
00097 if(!SnapDBI::query($query)) {
00098 self::setStaticError('Error doing query');
00099 return null;
00100 }
00101
00102 $ret = array();
00103 while(($row = SnapDBI::getRow()) !== false) {
00104 $ret[$row['versionId']] = $row['path'];
00105 }
00106
00107 SnapDBI::freeResult();
00108
00109 return $ret;
00110 }
00111
00127 public static function listResourcesRecursive($base = '//', $depth = self::DEPTH_ALL,
00128 $constraint = array(), $limit = array(), $order = array())
00129 {
00130 }
00131
00148 public static function listDirectoriesRecursive($base = '//', $depth = self::DEPTH_ALL,
00149 $constraint = array(), $limit = array(), $order = array())
00150 {
00151 }
00152
00198 public static function bulkApprove($base, $from, $to, $options = array()) {
00199 $recursive = isset($options['recursive']) ? $options['recursive'] : true;
00200 $single = isset($options['single']) ? $options['single'] : true;
00201 $fullStats = isset($options['fullStats']) ? $options['fullStats'] : false;
00202 $dryRun = isset($options['dryRun']) ? $options['dryRun'] : false;
00203
00204 $success = 0;
00205 $fail = 0;
00206
00207 $versions = self::listVersionsRecursive($base, $recursive ? self::DEPTH_ALL : self::DEPTH_IMMEDIATE);
00208
00209 if($single) {
00210 $resources = array();
00211 $curResource = -1;
00212 $skipped = 0;
00213 }
00214
00215 if($fullStats)
00216 $stats = array();
00217
00218 foreach($versions as $id => $path) {
00219 $v = SnapVersion::retrieve($id);
00220 if($single && $v->getResourceId() != $curResource) {
00221 $resources[$v->getResourceId()] = array(
00222 SnapVersion::STATUS_PRIVATE => false,
00223 SnapVersion::STATUS_PENDING => false,
00224 SnapVersion::STATUS_DEV => false,
00225 SnapVersion::STATUS_LIVE => false,
00226 SnapVersion::STATUS_DEFUNCT => false
00227 );
00228 $curResource = $v->getResourceId();
00229 }
00230
00231 if($v->getStatus() == $from) {
00232 if($single) {
00233 if($resources[$curResource][$from] == true) {
00234 if($fullStats)
00235 $stats[$path] = 'skipped';
00236 $skipped++;
00237 continue;
00238 }
00239 else
00240 $resources[$curResource][$from] = true;
00241 }
00242
00243 $func = self::$VERSION_ACTIONS[$from][$to];
00244 if($dryRun)
00245 $func = 'can' . ucfirst($func);
00246
00247 if($v->$func()) {
00248 if($fullStats)
00249 $stats[$path] = 'success';
00250 $success++;
00251 }
00252 else {
00253 if($fullStats) {
00254 if($dryRun)
00255 $stats[$path] = 'failure: ' . $v->getReason();
00256 else
00257 $stats[$path] = 'failure: ' . $v->getLastError();
00258 }
00259 $fail++;
00260 }
00261 }
00262 unset($v);
00263 }
00264
00265 if($fullStats && $single)
00266 return array($success, $fail, $skipped, $stats);
00267 else if($fullStats)
00268 return array($success, $fail, $stats);
00269 else if($single)
00270 return array($success, $fail, $skipped);
00271 else
00272 return array($success, $fail);
00273 }
00274 }
00275
00276 ?>