00001 <?php
00002
00014 class SnapPathCache extends SObject {
00025 public static function addPathSet($paths, $objId, $objType) {
00026 if(!SnapDBI::startTransaction())
00027 return false;
00028
00029 $dirId = 'NULL';
00030 $resId = 'NULL';
00031 $versId = 'NULL';
00032 if($objType == 'Directory') {
00033 $dirId = $objId;
00034 $type = 'DIR';
00035 }
00036 else if($objType == 'Resource') {
00037 $resId = $objId;
00038 $type = 'RES';
00039 }
00040 else {
00041 $versId = $objId;
00042 $type = 'VERS';
00043 }
00044
00045 $query = 'INSERT INTO PathCache (path, directoryId, resourceId, versionId, type) VALUES ';
00046 foreach($paths as $path)
00047 $query .= "('$path', $dirId, $resId, $versId, '$type'), ";
00048 $query = substr($query, 0, -2);
00049
00050 if(!SnapDBI::query($query)) {
00051 SnapDBI::cancelTransaction();
00052 return false;
00053 }
00054
00055 SnapDBI::commitTransaction();
00056
00057 return true;
00058 }
00059
00068 public static function removePathSet($paths) {
00069 if(!SnapDBI::startTransaction())
00070 return false;
00071
00072 foreach($paths as $path) {
00073 $query = 'DELETE FROM PathCache WHERE path LIKE BINARY \'' . $path . '%\'';
00074 if(!SnapDBI::query($query)) {
00075 SnapDBI::cancelTransaction();
00076 return false;
00077 }
00078 }
00079
00080 SnapDBI::commitTransaction();
00081
00082 return true;
00083 }
00084
00094 public static function retrievePath($type, $id) {
00095 $query = 'SELECT path FROM PathCache WHERE ' . strtolower($type) . 'Id = ' . $id;
00096 $results = SnapDBI::query($query, true);
00097 if(!$results)
00098 return false;
00099 if(count($results) == 1)
00100 return $results[0]['path'];
00101 else
00102 return false;
00103 }
00104
00105
00115 public static function updatePathSet($newPaths, $oldPaths) {
00116 if(!SnapDBI::startTransaction())
00117 return false;
00118
00119 foreach($newPaths as $i => $newPath) {
00120 $oldPath = $oldPaths[$i];
00121 $query = 'UPDATE PathCache SET path = CONCAT(\'' . $newPath . '\', SUBSTR(path, ' . (strlen($oldPath) + 1)
00122 . ')) WHERE path LIKE BINARY \'' . $oldPath . '%\'';
00123 if(!SnapDBI::query($query)) {
00124 SnapDBI::cancelTransaction();
00125 return false;
00126 }
00127 }
00128
00129 SnapDBI::commitTransaction();
00130
00131 return true;
00132 }
00133
00141 public static function regenPathCache() {
00142
00143 set_time_limit(450);
00144
00145 SnapDBI::startTransaction();
00146
00147 if(!SnapDBI::query('TRUNCATE PathCache')) {
00148 SnapDBI::cancelTransaction();
00149 return false;
00150 }
00151
00152 $query = 'INSERT INTO PathCache (path, directoryId, type) VALUES (\'//\', 1, \'DIR\'), (\'/DELETED/\', 2, \'DIR\')';
00153 if(!SnapDBI::query($query)) {
00154 SnapDBI::cancelTransaction();
00155 return false;
00156 }
00157
00158 if(!self::recRegenDirectory('//', 1)) {
00159 SnapDBI::cancelTransaction();
00160 return false;
00161 }
00162
00163 if(!self::recRegenDirectory('/DELETED/', 2)) {
00164 SnapDBI::cancelTransaction();
00165 return false;
00166 }
00167
00168 SnapDBI::commitTransaction();
00169
00170 return true;
00171 }
00172
00182 private static function recRegenDirectory($pathBase, $dirId) {
00183 $query = 'SELECT childId, shortName FROM DirectoryLink WHERE parentId = ' . $dirId . ' AND childId <> parentId';
00184 $results = SnapDBI::query($query, true);
00185 if($results === false)
00186 return false;
00187
00188 if(count($results) > 0) {
00189 $query = 'INSERT INTO PathCache (path, directoryId, type) VALUES ';
00190 foreach($results as $row) {
00191 $query .= "('$pathBase$row[shortName]/',$row[childId],'DIR'),";
00192 if(!self::recRegenDirectory($pathBase . $row['shortName'] . '/', $row['childId']))
00193 return false;
00194 }
00195 $query = substr($query, 0, -1);
00196 if(!SnapDBI::query($query))
00197 return false;
00198 }
00199
00200 $query = 'SELECT childId, shortName FROM ResourceLink WHERE parentId = ' . $dirId;
00201 $results = SnapDBI::query($query, true);
00202 if($results === false)
00203 return false;
00204
00205 if(count($results) > 0) {
00206 $query = 'INSERT INTO PathCache (path, resourceId, type) VALUES ';
00207 foreach($results as $row) {
00208 $query .= "('$pathBase$row[shortName]',$row[childId],'RES'),";
00209 if(!self::recRegenResource($pathBase . $row['shortName'], $row['childId']))
00210 return false;
00211 }
00212 $query = substr($query, 0, -1);
00213 if(!SnapDBI::query($query))
00214 return false;
00215 }
00216
00217 return true;
00218 }
00219
00229 private static function recRegenResource($pathBase, $resId) {
00230 $query = 'SELECT id, ordinal FROM Version WHERE resourceId = ' . $resId;
00231 $results = SnapDBI::query($query, true);
00232 if($results === false)
00233 return false;
00234
00235 if(count($results) > 0) {
00236 $query = 'INSERT INTO PathCache (path, versionId, type) VALUES ';
00237 foreach($results as $row)
00238 $query .= "('$pathBase@$row[ordinal]',$row[id],'VERS'),";
00239 $query = substr($query, 0, -1);
00240 if(!SnapDBI::query($query))
00241 return false;
00242 }
00243
00244 return true;
00245 }
00246 }
00247
00248 ?>