/** * Install. */ public function install() { SyncResource::install(); $syncers = $this->getEnabledSyncers(); foreach ($syncers as $syncer) { $syncer->install(); } }
function test_ls() { RemoteSyncPlugin::instance()->install(); $api = new RemoteSyncApi(); $res = $api->ls(array("type" => "post")); $this->assertCount(0, $res); $id = wp_insert_post(array("post_title" => "hello", "post_type" => "post", "post_content" => "something", "post_name" => "hello-slug")); $res = $api->ls(array("type" => "post")); $this->assertCount(1, $res); $syncResources = SyncResource::findAllForType("post", SyncResource::POPULATE_LOCAL); $this->assertCount(1, $syncResources); $this->assertEquals(NULL, $syncResources[0]->id); $res0 = $syncResources[0]; $res0->save(); $this->assertNotEquals(NULL, $syncResources[0]->id); wp_trash_post($id); $res = $api->ls(array("type" => "post")); $this->assertCount(0, $res); }
/** * Uninstall. */ function rs_uninstall() { SyncResource::uninstall(); }
/** * Push. */ public function push() { $syncers = RemoteSyncPlugin::instance()->getEnabledSyncers(); foreach ($syncers as $syncer) { $this->log("Push: " . $syncer->getType()); $syncResources = SyncResource::findAllForType($syncer->getType(), SyncResource::POPULATE_LOCAL | SyncResource::POPULATE_REMOTE); foreach ($syncResources as $syncResource) { switch ($syncResource->getState()) { case SyncResource::NEW_REMOTE: case SyncResource::UPDATED_REMOTE: case SyncResource::DELETED_REMOTE: $this->log($syncResource->getSlug() . ": Remotely changed, please pull."); break; case SyncResource::NEW_LOCAL: $syncResource->createRemoteResource(); $syncResource->save(); $this->log(" " . $syncResource->getSlug() . ": Created remote."); break; case SyncResource::UPDATED_LOCAL: $syncResource->updateRemoteResource(); $syncResource->save(); $this->log(" " . $syncResource->getSlug() . ": Updated remote."); break; case SyncResource::DELETED_LOCAL: $syncResource->deleteRemoteResource(); $syncResource->delete(); $this->log(" " . $syncResource->getSlug() . ": Deleted remote."); break; case SyncResource::CONFLICT: $this->log(" " . $syncResource->getSlug() . ": Conflict, skipping."); break; case SyncResource::UP_TO_DATE: $syncResource->save(); break; case SyncResource::GARBAGE: //echo "garbage!!!\n"; $syncResource->delete(); break; } } } }
function test_state() { update_option("rs_remote_site_url", "helloworld"); RemoteSyncPlugin::instance()->syncers = array(new SRTestSyncer("testType")); RemoteSyncPlugin::instance()->install(); $syncer = RemoteSyncPlugin::instance()->getSyncerByType("testType"); $data = $syncer->getResource("slug1"); $rev = md5(json_encode($data)); Curl::mockResult(array(array("slug" => "onlyremote", "revision" => "05a1ad082ad35cad7aac7b18e232feb3"), array("slug" => "slug1", "revision" => $rev))); $syncResources = SyncResource::findAllForType("testType", SyncResource::POPULATE_REMOTE | SyncResource::POPULATE_LOCAL); $a = array(); foreach ($syncResources as $syncResource) { $a[$syncResource->getSlug()] = $syncResource; } //echo "l: ".sizeof($syncResources); $syncResource = $syncResources[0]; $data = $syncResource->getData(); $rev = $syncResource->getLocalRevision(); $this->assertEquals($a["onlyremote"]->getState(), SyncResource::NEW_REMOTE); $this->assertEquals($a["onlylocal"]->getState(), SyncResource::NEW_LOCAL); $this->assertEquals($a["slug1"]->getState(), SyncResource::UP_TO_DATE); }
/** * Delete. */ public function del($args) { if (!isset($args["type"]) || !$args["type"]) { throw new Exception("Expected parameter type"); } if (!isset($args["slug"]) || !$args["slug"]) { throw new Exception("Expected parameter slug"); } $resource = SyncResource::findOneForType($args["type"], $args["slug"]); if (!$resource) { throw new Exception("Doesn't exist locally"); } $syncer = $resource->getSyncer(); $syncer->deleteResource($resource->getSlug()); if (!$resource->getBaseRevision() && $resource->id) { $resource->delete(); } return array("ok" => 1); }
function setUp() { parent::setUp(); SyncResource::install(); }
/** * Find all for type. * Optionally populate with local and remote resource data. */ public static function findAllForType($type, $findFlags = 0) { $syncer = RemoteSyncPlugin::instance()->getSyncerByType($type); $syncResources = SyncResource::findAllBy("type", $type); $syncResourcesBySlug = array(); foreach ($syncResources as $syncResource) { $syncResourcesBySlug[$syncResource->getSlug()] = $syncResource; } if ($findFlags & SyncResource::POPULATE_LOCAL) { $slugs = $syncer->listResourceSlugs(); foreach ($slugs as $slug) { if (!isset($syncResourcesBySlug[$slug])) { $syncResource = new SyncResource($type, $slug); $syncResources[] = $syncResource; $syncResourcesBySlug[$slug] = $syncResource; } } } usort($syncResources, "SyncResource::cmpResourceWeight"); if ($findFlags & SyncResource::POPULATE_REMOTE) { $remoteResources = RemoteResource::fetchAllForType($type); foreach ($remoteResources as $remoteResource) { $slug = $remoteResource->getSlug(); if (!isset($syncResourcesBySlug[$slug])) { $syncResource = new SyncResource($type, $slug); $syncResources[] = $syncResource; $syncResourcesBySlug[$slug] = $syncResource; } $syncResource = $syncResourcesBySlug[$slug]; $syncResource->setRemoteResource($remoteResource); } foreach ($syncResources as $syncResource) { $syncResource->remoteResourceSet = TRUE; } } if ($findFlags & SyncResource::ONLY_LOCAL_EXISTING) { if ($findFlags & SyncResource::POPULATE_REMOTE) { throw new Exception("Can't use POPULATE_REMOTE and ONLY_LOCAL_EXISTING at the same time"); } $tmp = $syncResources; $syncResources = array(); foreach ($tmp as $syncResource) { if ($syncResource->getLocalRevision()) { $syncResources[] = $syncResource; } } } return $syncResources; }