-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from thekid/refactor/import
Speed up importing from local directory
- Loading branch information
Showing
18 changed files
with
508 additions
and
221 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
<?php namespace de\thekid\dialog\api; | ||
|
||
use lang\Value; | ||
use util\{Date, Objects}; | ||
|
||
/** Represents an entry passed to the /entries API */ | ||
class Entry implements Value { | ||
private $attributes= []; | ||
|
||
public string $slug { | ||
get => $this->attributes['slug']; | ||
set { $this->attributes['slug']= $value; } | ||
} | ||
|
||
public Date $date { | ||
get => $this->attributes['date']; | ||
set { $this->attributes['date']= $value; } | ||
} | ||
|
||
public string $title { | ||
get => $this->attributes['title']; | ||
set { $this->attributes['title']= $value; } | ||
} | ||
|
||
public string $content { | ||
get => $this->attributes['content']; | ||
set { $this->attributes['content']= $value; } | ||
} | ||
|
||
public array<string, mixed> $is { | ||
get => $this->attributes['is']; | ||
set { $this->attributes['is']= $value; } | ||
} | ||
|
||
public ?string $parent { | ||
get => $this->attributes['parent'] ?? null; | ||
set { $this->attributes['parent']= $value; } | ||
} | ||
|
||
public array<string> $keywords { | ||
get => $this->attributes['keywords'] ?? []; | ||
set { $this->attributes['keywords']= $value; } | ||
} | ||
|
||
public array<array<mixed>> $locations { | ||
get => $this->attributes['locations'] ?? []; | ||
set { $this->attributes['locations']= $value; } | ||
} | ||
|
||
public array<string, mixed> $weather { | ||
get => $this->attributes['weather']; | ||
set { $this->attributes['weather']= $value; } | ||
} | ||
|
||
public ?Date $published { | ||
get => $this->attributes['published'] ?? null; | ||
set { $this->attributes['published']= $value; } | ||
} | ||
|
||
/** Returns all attributes as a map */ | ||
public function attributes(): array<string, mixed> { return $this->attributes; } | ||
|
||
/** @return string */ | ||
public function hashCode() { return 'E'.Objects::hashOf($this->attributes); } | ||
|
||
/** @return string */ | ||
public function toString() { return nameof($this).'@'.Objects::stringOf($this->attributes); } | ||
|
||
/** | ||
* Comparison | ||
* | ||
* @param var $value | ||
* @return int | ||
*/ | ||
public function compareTo($value) { | ||
return $value instanceof self ? Objects::compare($this->attributes, $value->attributes) : 1; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php namespace de\thekid\dialog\import; | ||
|
||
use de\thekid\dialog\processing\Files; | ||
|
||
/** Imports contents */ | ||
class Content extends Source { | ||
|
||
public function entryFrom(Description $description): array<string, mixed> { | ||
return [ | ||
'slug' => $this->name(), | ||
'parent' => $this->parent(), | ||
'date' => $description->meta['date'], | ||
'title' => $description->meta['title'], | ||
'keywords' => $description->meta['keywords'] ?? [], | ||
'content' => $description->content, | ||
'locations' => [...$description->locations($description->meta['date']->getTimeZone())], | ||
'is' => ['content' => true], | ||
]; | ||
} | ||
|
||
public function contentsIn(Files $files): iterable { | ||
yield from $this->mediaIn($files); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php namespace de\thekid\dialog\import; | ||
|
||
use de\thekid\dialog\processing\Files; | ||
|
||
/** Imports the cover image */ | ||
class Cover extends Source { | ||
|
||
public function entryFrom(Description $description): array<string, mixed> { | ||
return [ | ||
'slug' => '@cover', | ||
'parent' => '~', | ||
'date' => $description->meta['date'], | ||
'title' => $description->meta['title'], | ||
'keywords' => $description->meta['keywords'] ?? [], | ||
'content' => $description->content, | ||
'locations' => [...$description->locations($description->meta['date']->getTimeZone())], | ||
'is' => ['cover' => true], | ||
]; | ||
} | ||
|
||
public function contentsIn(Files $files): iterable { | ||
yield from $this->mediaIn($files); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php namespace de\thekid\dialog\import; | ||
|
||
use webservices\rest\Endpoint; | ||
|
||
class DeleteEntry extends Task { | ||
|
||
public function __construct(private string $slug) { } | ||
|
||
public function execute(Endpoint $api) { | ||
return $api->resource('entries/{0}', [$this->slug])->delete(); | ||
} | ||
|
||
public function description(): string { return "Removing entry {$this->slug}"; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?php namespace de\thekid\dialog\import; | ||
|
||
use webservices\rest\Endpoint; | ||
|
||
class DeleteMedia extends Task { | ||
|
||
public function __construct(private string $slug, private string $name) { } | ||
|
||
public function execute(Endpoint $api) { | ||
return $api->resource('entries/{0}/images/{1}', [$this->slug, $this->name])->delete(); | ||
} | ||
|
||
/** @return string */ | ||
public function description(): string { return "Deleting media {$this->slug}/{$this->name}"; } | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
<?php namespace de\thekid\dialog\import; | ||
|
||
use webservices\rest\Endpoint; | ||
|
||
class FetchEntry extends Task { | ||
|
||
public function __construct(private string $slug) { } | ||
|
||
public function execute(Endpoint $api) { | ||
return $api->resource('entries/{0}?expand=$children', [$this->slug]) | ||
->put([]) | ||
->value() | ||
; | ||
} | ||
|
||
public function description(): string { return "Fetching entry {$this->slug}"; } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php namespace de\thekid\dialog\import; | ||
|
||
use de\thekid\dialog\processing\Files; | ||
use io\File; | ||
|
||
/** Imports journeys */ | ||
class Journey extends Source { | ||
|
||
/** Subfolders of a journey form its child contents */ | ||
private function childrenIn(Files $files): iterable { | ||
$children= []; | ||
foreach ($this->entry['$children'] as $child) { | ||
$children[$child['slug']]= $child; | ||
} | ||
|
||
foreach ($this->origin->entries() as $path) { | ||
if ($path->isFolder()) { | ||
$folder= $path->asFolder(); | ||
$slug= $this->entry['slug'].'/'.$folder->dirname; | ||
|
||
yield from new Content($folder, new File($folder, 'content.md'), $children[$slug] ?? null) | ||
->nestedIn($this->entry['slug']) | ||
->synchronize($files) | ||
; | ||
unset($children[$slug]); | ||
} | ||
} | ||
|
||
foreach ($children as $rest) { | ||
yield new DeleteEntry($rest['slug']); | ||
} | ||
} | ||
|
||
public function entryFrom(Description $description): array<string, mixed> { | ||
return [ | ||
'slug' => $this->name(), | ||
'date' => $description->meta['from'], | ||
'title' => $description->meta['title'], | ||
'keywords' => $description->meta['keywords'] ?? [], | ||
'content' => $description->content, | ||
'locations' => [...$description->locations($description->meta['from']->getTimeZone())], | ||
'is' => [ | ||
'journey' => true, | ||
'from' => $description->meta['from'], | ||
'until' => $description->meta['until'], | ||
], | ||
]; | ||
} | ||
|
||
public function contentsIn(Files $files): iterable { | ||
yield from $this->mediaIn($files); | ||
yield from $this->childrenIn($files); | ||
} | ||
} |
Oops, something went wrong.