Skip to content

Commit

Permalink
Refactor migrate:osci-publication command to access sqlite DB using…
Browse files Browse the repository at this point in the history
… built-it Laravel mechanisms
  • Loading branch information
nikhiltri committed Jun 21, 2024
1 parent 6a0a6f3 commit 15f95f1
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 62 deletions.
78 changes: 16 additions & 62 deletions app/Console/Commands/MigrateOSCIPublicationOne.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,27 +3,13 @@
namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Arr;
use App\Repositories\Api\PublicationRepository;
use App\Models\DigitalPublication;
use App\Models\DigitalPublicationArticle;
use App\Models\Vendor\Block;

/**
* MigrationDB
*
* Wrapper around the migration db -- basically just an open()-er?
*
* TODO: construct() should take a migration filename string
*/
class MigrationDB extends \SQLite3 {

function __construct()
{
$this->open('migration.sqlite3');
}

}

class MigrateOSCIPublicationOne extends Command
{
/**
Expand All @@ -40,21 +26,6 @@ class MigrateOSCIPublicationOne extends Command
*/
protected $description = 'Migrate an OSCI publication from a migration file to a website DigitalPublication';

protected $db;

/**
* Create a new command instance.
*
* @return void
*
* TODO: This should take.. a migration filename?
*/
public function __construct()
{
$this->db = new MigrationDB();
parent::__construct();
}

/**
* Execute the console command.
*
Expand All @@ -66,58 +37,45 @@ public function handle()

// NB!: For each of these types in the admin UI it's impossible to validate the save without setting a date!

$pubQuery = $this->db->prepare("SELECT json_extract(publications.data,'$._title') as title FROM publications LEFT JOIN tocs ON tocs.package=publications.pub_id WHERE pub_id=:id");
$pubQuery->bindValue(':id',$pubId);

$result = $pubQuery->execute();
$pub = $result->fetchArray();
$pubs = DB::connection('osci_migration')->select("SELECT json_extract(publications.data,'$._title') as title FROM publications LEFT JOIN tocs ON tocs.package=publications.pub_id WHERE pub_id=:id", ['id' => $pubId]);
$pub = Arr::first($pubs);

// TODO: Handle italics / etc in the title
// TODO: Set any other publication level metadata (date?)

$webPub = new DigitalPublication();
$webPub->title = 'Migrated ' . date('M j, Y') . ' | ' . $pub["title"];
$webPub->title = 'Migrated ' . date('M j, Y') . ' | ' . $pub->title;
$webPub->published = false;
$webPub->is_dsc_stub = false;
$webPub->save();

$webPubId = $webPub->id;

$textsQuery = $this->db->prepare("SELECT coalesce(json_extract(data,'$._title'),'FIXME') as title,text_id FROM texts WHERE package=:pubId and text_id NOT LIKE '%ncxtoc%'");
$textsQuery->bindValue(':pubId',$pubId);
$texts = DB::connection('osci_migration')->select("SELECT coalesce(json_extract(data,'$._title'),'FIXME') as title,text_id FROM texts WHERE package=:pubId and text_id NOT LIKE '%ncxtoc%'", ['pubId' => $pubId]);

$textResult = $textsQuery->execute();
$text = $textResult->fetchArray();
foreach ($texts as $text) {

while ($text) {

$webArticle = new DigitalPublicationArticle();
$webArticle->type = "text";
$webArticle->title = $text['title'];
$webArticle->title = $text->title;
$webArticle->published = false;
$webArticle->digital_publication_id = $webPubId;
$webArticle->date = date('M j, Y');
$webArticle->updated_at = date('M j, Y');
$webArticle->created_at = date('M j, Y');

// TODO: Join toc position via json_tree against toc data
$webArticle->position = 0;
$webArticle->position = 0;

$webArticle->save();

// TODO: Use a multiline string!! Also this can be moved outside the while

$blocksQuery = $this->db->prepare("SELECT json_extract(blk.value,'$.html') as html, blk.id as position, json_extract(blk.value,'$.blockType') as type, json_extract(blk.value,'$.fallback_url') as figure_url, coalesce(json_extract(blk.value,'$.caption_html'),'') as figure_capt from texts, json_each(texts.data,'$.sections') as sects,json_each(sects.value,'$.blocks') as blk where texts.text_id=:textId");

$blocksQuery->bindValue(':textId',$text['text_id']);

$blocksResult = $blocksQuery->execute();

$blk = $blocksResult->fetchArray();
$blocks = DB::connection('osci_migration')->select("SELECT json_extract(blk.value,'$.html') as html, blk.id as position, json_extract(blk.value,'$.blockType') as type, json_extract(blk.value,'$.fallback_url') as figure_url, coalesce(json_extract(blk.value,'$.caption_html'),'') as figure_capt from texts, json_each(texts.data,'$.sections') as sects,json_each(sects.value,'$.blocks') as blk where texts.text_id=:textId", ['textId' => $text->text_id]);

$order = 0;

while ($blk) {
foreach ($blocks as $blk) {

$block = new Block();
$block->blockable_id = $webArticle->id;
Expand All @@ -126,32 +84,28 @@ public function handle()
$block->position = $order;

// TODO: Adapt Trevin's spec image code here
if ($blk['type'] == 'figure') {
if ($blk->type == 'figure') {

// $figText = '<span>'.$blk['figure_url'].'</span>';
$figText = '<figure><img src="'. $blk['figure_url'] . '" alt /><figcaption>'.$blk['figure_capt'].'</figcaption></figure>';
$figText = '<figure><img src="'. $blk->figure_url . '" alt /><figcaption>'.$blk->figure_capt.'</figcaption></figure>';

$block->content = [ 'paragraph' => $figText ];
} else {
$block->content = [ 'paragraph' => $blk['html'] ];
$block->content = [ 'paragraph' => $blk->html ];
}

$block->type = 'paragraph';
$block->save();

$webArticle->blocks()->save($block);

$blk = $blocksResult->fetchArray();
$order += 1;
}

$webArticle->save();
$webPub->articles()->save($webArticle);

$text = $textResult->fetchArray();

}

$webPub->save();
}
}
9 changes: 9 additions & 0 deletions config/database.php
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,15 @@
'sslmode' => 'prefer',
],

'osci_migration' => [
'driver' => 'sqlite',
'database' => storage_path('app/migration.sqlite3'),
'prefix' => '',
'foreign_key_constraints' => true,
],



],

/*
Expand Down

0 comments on commit 15f95f1

Please sign in to comment.