static function AddNewFiles($sync_data, $progress_bar = null, $max_batch_size = 0) { self::PrintDebugTrace(); $keys = array_keys($sync_data->new_files); $upload_dir = self::cleanPath(WPFB_Core::UploadDir()); $upload_dir_len = strlen($upload_dir); $batch_size = 0; $start_time = $cur_time = time(); WPFB_Category::DisableBubbling(); foreach ($keys as $i) { if (!empty($progress_bar)) { $progress_bar->step(); } $fn = $sync_data->new_files[$i]; $rel_path = substr($fn, $upload_dir_len); unset($sync_data->new_files[$i]); if (empty($fn) || isset($sync_data->known_filenames[$rel_path])) { continue; } // skip files that where already added, for some reason if (is_null($ex_file = WPFB_Item::GetByPath($rel_path))) { self::PrintDebugTrace("add_existing_file:{$fn}"); $res = WPFB_Admin::AddExistingFile($fn, empty($sync_data->thumbnails[$fn]) ? null : $sync_data->thumbnails[$fn]); self::PrintDebugTrace("added_existing_file"); if (empty($res['error'])) { $sync_data->log['added'][] = empty($res['file']) ? substr($fn, $upload_dir_len) : $res['file']; $sync_data->known_filenames[$rel_path] = 1; if (!empty($res['file']) && $res['file']->GetThumbPath()) { $sync_data->known_filenames[substr(self::cleanPath($res['file']->GetThumbPath()), $upload_dir_len)] = 1; } } else { $sync_data->log['error'][] = $res['error'] . " (file {$fn})"; } } else { //$res = array('file' => $ex_file); $sync_data->log['added'][] = $ex_file; $sync_data->known_filenames[$rel_path] = 1; } $sync_data->num_files_processed++; if (!empty($res['file'])) { $batch_size += $res['file']->file_size; if ($max_batch_size > 0 && $batch_size > $max_batch_size) { return false; } } } if (!empty($progress_bar)) { $progress_bar->complete(); } return true; }
/** * @depends testCreateCatAndFile */ function testCreateTree() { $this->testSetUser(); wpfb_loadclass('Admin'); WPFB_Category::DisableBubbling(false); /** @var WPFB_Category $parent */ $parent = null; /** @var WPFB_Category[] $cats */ $cats = array(); for ($d = 0; $d < 4; $d++) { $res = WPFB_Admin::InsertCategory(array('cat_name' => "layer {$d}", 'cat_parent' => $parent ? $parent->GetId() : 0)); $this->assertEmpty($res['error'], $res['error']); /** @var WPFB_Category $cat */ $cat = $res['cat']; $this->assertTrue($parent ? $cat->GetParent()->Equals($parent) : is_null($cat->GetParent())); $this->assertTrue(is_dir($cat->GetLocalPath())); $cats[] = $cat; $parent = $cat; } $this->assertEquals($cats[0]->cat_id, $cats[1]->GetParent()->cat_id); //$this->assertEquals($cats[2]->GetParent(), $cats[1], '', 0.0, 2, true); // print_r(array_map( function($c) { return strval($c);}, $cats)); $files = new TestFileSet(); $res = WPFB_Admin::InsertFile(array('file_remote_uri' => 'file://' . $files->getImageBanner(), 'file_category' => $parent)); $this->assertEmpty($res['error'], $res['error']); /** @var WPFB_File $file01 */ $file01 = $res['file']; $res = WPFB_Admin::InsertFile(array('file_remote_uri' => 'file://' . $files->getSmallTxt(), 'file_category' => $parent->GetParent())); $this->assertEmpty($res['error'], $res['error']); /** @var WPFB_File $file02 */ $file02 = $res['file']; $this->assertEquals($file01->GetParent()->cat_id, $parent->cat_id); $this->assertEquals($file02->GetParent()->cat_id, $parent->GetParent()->cat_id); $this->assertEquals($file02->GetParent(), $parent->GetParent()); $this->assertEquals(2, $parent->GetParent()->cat_num_files_total); $this->assertEquals(2, $file02->GetParent()->cat_num_files_total); $this->assertEquals(1, $file02->GetParent()->cat_num_files); $this->assertEquals(2, $cats[0]->cat_num_files_total); $this->assertEquals(1, count($parent->GetParent()->GetChildCats(true))); $this->assertEquals(1, count($file02->GetParent()->GetChildCats(true))); $this->assertEquals(2, count($cats[0]->GetChildFiles(true)), $cats[0]); $this->assertEquals(3, count($cats[0]->GetChildCats(true)), $cats[0]); $this->assertEquals(2, count($cats[2]->GetChildFiles(true)), $cats[2]); $this->assertEquals(1, count($cats[2]->GetChildCats(true)), $cats[2]); $this->assertEquals(2, count($cats[1]->GetChildCats(true)), $cats[1]); $this->assertEquals(2, count($cats[1]->GetChildFiles(true)), $cats[1]); $res = $parent->Delete(); $this->assertEmpty($res['error'], $res['error']); unset($cats[3]); $file01->DBReload(); // TODO fix: need to reload from DB! $this->assertFileExists($file01->GetLocalPath()); $this->assertFileExists($file01->GetThumbPath()); // print_r(array_map( function($c) { return strval($c);}, $cats)); $this->assertEquals(strval($file01->GetParent()), strval($file02->GetParent())); $this->assertEquals(0, count($cats[2]->DBReload()->GetChildCats(true)), $cats[2]); $this->assertEquals(2, count($cats[2]->GetChildFiles(false)), $cats[2]); $this->assertEquals(1, count($cats[1]->DBReload()->GetChildCats(true)), $cats[1]); $this->assertEquals(2, count($cats[1]->GetChildFiles(true)), $cats[1]); $this->assertEquals(2, count($cats[0]->DBReload()->GetChildCats(true)), $cats[0]); $this->assertEquals(2, count($cats[0]->GetChildFiles(true)), $cats[0]); foreach ($cats as $cat) { $res = $cat->DBReload()->Delete(); $this->assertEmpty($res['error'], $res['error']); } $thumb = $file01->GetThumbPath(); $this->assertTrue($file01->DBReload()->Delete()); $this->assertTrue($file02->DBReload()->Delete()); $this->assertFileNotExists($thumb); }