-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
EMMR-107 Add legacy transcription item update script
- Loading branch information
1 parent
b9f3d26
commit 8e2015f
Showing
1 changed file
with
74 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
/** | ||
* @file | ||
* Contains transupdate.php. | ||
* This script updates all legacy recipes to the new CKEditor 5 transcription format. | ||
*/ | ||
|
||
use Drupal\node\Entity\Node; | ||
|
||
echo "\n"; | ||
// Get an array of all 'recipe' node ids. | ||
$recipe_nids = \Drupal::entityQuery('node') | ||
->condition('type', 'emmr_recipe') | ||
->accessCheck(FALSE)->execute(); | ||
|
||
// Load all the recipes. | ||
$recipes = Node::loadMultiple($recipe_nids); | ||
// Iterate. | ||
foreach ($recipes as $recipe) { | ||
// Get transcription value. | ||
$trans = $recipe->field_recipe_transcription->getValue()[0]['value']; | ||
// Find transcription item matches. | ||
preg_match_all('/<trxn>(.*?)<\/trxn>/', $trans, $matches); | ||
// Iterate. | ||
foreach ($matches[1] as $match) { | ||
// If transcription insert... | ||
if (str_contains($match, 'trxn-caret')) { | ||
// Get text. | ||
preg_match('/<span class="trxn-text">(.*?)<\/span>/', $match, $text); | ||
$text = $text[1]; | ||
// Build new transcription item. | ||
$new = | ||
"<trxnin><span class='trxn-caret'>^</span><span class='trxn-text'>$text</span></trxnin>"; | ||
// Replace old item with new in transcription. | ||
$trans = str_replace("<trxn>$match</trxn>", $new, $trans, $count); | ||
} | ||
// If transcription replace... | ||
if (str_contains($match, 'trxn-retext')) { | ||
// Get text. | ||
preg_match('/<s>(.*?)<\/s>/', $match, $text); | ||
$text = $text[1]; | ||
// Get replacement text. | ||
preg_match('/<span class="trxn-retext">(.*?)<\/span>/', $match, $retext); | ||
$retext = $retext[1]; | ||
// Build new transcription item. | ||
$new = | ||
"<trxnrep><span class='trxn-retext'>$retext</span><s class='trxn-replaced'>$text</s></trxnrep>"; | ||
// Replace old item with new in transcription. | ||
$trans = str_replace("<trxn>$match</trxn>", $new, $trans, $count); | ||
} | ||
// If transcription marginalia... | ||
if (str_contains($match, 'trxn-number')) { | ||
// Get number. | ||
preg_match('/<span class="trxn-number">(.*?)<\/span>/', $match, $num); | ||
$num = $num[1]; | ||
// Get text. | ||
preg_match('/<span class="trxn-text">(.*?)<\/span>/', $match, $text); | ||
$text = $text[1]; | ||
// Build new transcription item. | ||
$new = | ||
"<trxnmar><span class='trxn-number'>$num</span><span class='trxn-text'>$text</span></trxnmar>"; | ||
// Replace old item with new in transcription. | ||
$trans = str_replace("<trxn>$match</trxn>", $new, $trans, $count); | ||
} | ||
} | ||
if ($trans != $recipe->field_recipe_transcription->getValue()[0]['value']) { | ||
$recipe->set('field_recipe_transcription', $trans); | ||
$recipe->save(); | ||
$title = $recipe->title->getValue()[0]['value']; | ||
echo "\nUpdated emmr_recipe [$title]"; | ||
} | ||
} | ||
echo "\n"; |