boyska 7 years ago
commit
f2183d21de
2 changed files with 53 additions and 0 deletions
  1. 8 0
      audiofix_ror.info.yml
  2. 45 0
      audiofix_ror.module

+ 8 - 0
audiofix_ror.info.yml

@@ -0,0 +1,8 @@
+name: Audio fix ROR
+type: module
+description: 'FIX Archive.org links; add duration'
+core: 8.x
+version: 0.1
+package: Ondarossa
+dependencies:
+        - paragraphs

+ 45 - 0
audiofix_ror.module

@@ -0,0 +1,45 @@
+<?php
+function isArchive(string $url): bool {
+	$pos = strpos($url, "archive.org/");
+	if($pos === FALSE) {
+		return FALSE;
+	}
+	return TRUE;
+}
+
+function normalizeArchive(string $url) {
+	//assume il link sia di archive
+	if(strpos($url, "https://archive.org/download") !== FALSE) {
+		return $url;
+	}
+	if(strpos($url, "https://archive.org/details") !== FALSE) {
+		// aagh qui in teoria non c'e' nulla da fare a meno che non guardiamo sul sito
+		return $url;
+	}
+	// https://ia123456.us.archive.org/30/items/Bucket/filename.ogg
+	$parts = explode("/", $url);
+	$bucket = $parts[count($parts)-2];
+	$fname = $parts[count($parts)-1];
+	return "https://archive.org/download/" . $bucket . "/" . $fname;
+}
+
+/*
+ * Implements hook_ENTITY_TYPE_presave
+ */
+function audiofix_ror_paragraph_presave(Drupal\Core\Entity\EntityInterface $entity) {
+    // per i paragraph, getType() === bundle(). 
+    if($entity->bundle() !== 'audio') { // audio e' il nome del nostro paragraph type
+        return;
+    }
+    // campi utili: field_audio_link e field_durata
+    $link = $entity->get('field_audio_link')->getValue();
+    if(count($link) == 0) { return; }
+    $link = $link[0]['uri'];
+    if(is_string($link) && isArchive($link)) {
+        $norm = normalizeArchive($link);
+        if($norm !== $link) {
+            \Drupal::logger('audiofix_ror')->notice('Aggiornato da :old a :new', array(":old" => $link, ":new" => $norm));
+            $entity->set('field_audio_link', $norm);
+        }
+    }
+}