Skip to content

Commit

Permalink
Give job yaml generation a go
Browse files Browse the repository at this point in the history
  • Loading branch information
addshore committed Jul 28, 2024
1 parent c9b4d7c commit 645cbe6
Showing 1 changed file with 66 additions and 6 deletions.
72 changes: 66 additions & 6 deletions app/Jobs/GenerateDepictsQuestions.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Wikibase\MediaInfo\DataModel\MediaInfoId;
use Addwiki\Wikibase\Query\PrefixSets;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Symfony\Component\Yaml\Yaml;

class GenerateDepictsQuestions implements ShouldQueue, ShouldBeUnique
{
Expand Down Expand Up @@ -59,12 +60,12 @@ class GenerateDepictsQuestions implements ShouldQueue, ShouldBeUnique
* @return void
*/
public function __construct(
string $category,
string $ignoreCategories,
string $ignoreCategoriesRegex,
string $depictItemId,
string $depictName,
int $limit
string $category = "",
string $ignoreCategories = "",
string $ignoreCategoriesRegex = "",
string $depictItemId = "",
string $depictName = "",
int $limit = 0
)
{
$this->category = $category;
Expand All @@ -91,6 +92,49 @@ public function uniqueId()
* @return void
*/
public function handle() {
// If we were constructed with values, handle this one instacne
// Otherwise, query the YAML and run each job
if ($this->depictItemId !== "") {
$this->handleOne();
return;
}
// GenerateDepictsQuestions
$depictsYamlDir =__DIR__ . '/../../spec/';
$depictsYamlFiles = $this->getRecursiveYamlFilesInDirectory( $depictsYamlDir );
$depictsJobs = [];
foreach( $depictsYamlFiles as $file ) {
$file = Yaml::parse(file_get_contents($file), Yaml::PARSE_OBJECT_FOR_MAP);
if( is_array( $file ) ) {
$depictsJobs = array_merge( $depictsJobs, $file );
} else {
$depictsJobs[] = $file;
}
}
foreach( $depictsJobs as $job ) {
// Make sure that job is an object
if( !is_object( $job ) ) {
echo "Job is not an object\n";
continue;
}
// Make sure it has the required fields
if( !isset( $job->category ) || !isset( $job->depictsId ) || !isset( $job->name ) || !isset( $job->limit ) ) {
echo "Job is missing required fields\n";
continue;
}

$inner = new self(
$job->category,
implode($job->exclude ?: [], '|||'),
$job->excludeRegex ?: "",
$job->depictsId,
$job->name,
$job->limit
);
$inner->handle();
}
}

public function handleOne() {
$this->createQuestionGroups();

// Figure out how many questions for the category we already have
Expand Down Expand Up @@ -380,4 +424,20 @@ private function getThumbUrl( PageIdentifier $filePageIdentifier ) : ?string {
}
return $thumbUrl;
}

/**
* TODO move this function elsewhere?
*/
private function getRecursiveYamlFilesInDirectory(string $directory){
$files = [];
$dir = new \RecursiveDirectoryIterator($directory);
$iterator = new \RecursiveIteratorIterator($dir);
foreach ($iterator as $file) {
if ( $file->isFile() ) {
$files[] = realpath($file->getPathname());
}
}
return $files;
}

}

0 comments on commit 645cbe6

Please sign in to comment.