? schedule.patch
? archive/_________station_archive.install
? archive/____station_archive.module
? catalog/The Database.mdb
? catalog/fixit.php
? catalog/import.php
Index: catalog/station_catalog.install
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/station/catalog/station_catalog.install,v
retrieving revision 1.8
diff -u -p -u -p -r1.8 station_catalog.install
--- catalog/station_catalog.install	7 Sep 2007 01:15:06 -0000	1.8
+++ catalog/station_catalog.install	11 Sep 2007 19:34:15 -0000
@@ -17,12 +17,15 @@ function station_catalog_install() {
           `album` varchar(255) NOT NULL default '',
           `year` int(10) unsigned NOT NULL,
           `label` varchar(255) NOT NULL default '',
+          `mb_release_id` varchar(36) NOT NULL default  '',
+          `asin` varchar(16) NOT NULL default '',
           PRIMARY KEY  (`nid`),
           UNIQUE KEY `station_catalog_number` (`number`),
           KEY `station_catalog_artist` (`artist`),
           KEY `station_catalog_album` (`album`),
           KEY `station_catalog_year` (`year`)
           KEY `station_catalog_label` (`label`),
+          KEY `mb_release` (`mb_release_id`)
         ) /*!40100 DEFAULT CHARACTER SET utf8 */;
       ");
       break;
@@ -84,3 +87,24 @@ function station_catalog_update_5200() {
   return $ret;
 }
 
+/**
+ * Add MusicBrainz release id field and Amazon ASIN field.
+ */
+function station_catalog_update_5201() {
+  $ret = array();
+  switch ($GLOBALS['db_type']) {
+    case 'mysql':
+    case 'mysqli':
+      $ret[] = update_sql("ALTER TABLE {station_catalog} 
+        ADD COLUMN `mb_release_id` VARCHAR(36) NOT NULL DEFAULT '' AFTER `label`,
+        ADD INDEX mb_release(`mb_release_id`);
+      ");
+      $ret[] = update_sql("ALTER TABLE {station_catalog} 
+        ADD COLUMN `asin` varchar(16) NOT NULL default '' AFTER `mb_release_id`;
+      ");
+
+    break;
+  }
+  return $ret;
+}
+
Index: catalog/station_catalog.module
===================================================================
RCS file: /cvs/drupal-contrib/contributions/modules/station/catalog/station_catalog.module,v
retrieving revision 1.23
diff -u -p -u -p -r1.23 station_catalog.module
--- catalog/station_catalog.module	7 Sep 2007 01:56:38 -0000	1.23
+++ catalog/station_catalog.module	11 Sep 2007 23:19:56 -0000
@@ -124,7 +124,28 @@ function station_catalog_album_next_numb
 }
 
 /**
- * Build a form for program nodes.
+ * Implementation of hook_prepare().
+ */
+function station_catalog_album_prepare(&$node) {
+  if (isset($node->album['artist']) && isset($node->album['album'])) {
+    if ($mb_info = _station_catalog_musicbrainz_album($node->album['artist'], $node->album['album'])) {
+      $fields = array('year' => t('Year'), 'mb_release_id' => t('Music Brainz Release ID'), 'asin' => t('Amazon Standard Identification Number'));
+      $adjusted = array();
+      foreach ($fields as $field => $name) {
+        if (empty($node->album[$field])) {
+          $adjusted[] = $name;
+          $node->album[$field] = $mb_info[$field];
+        }
+      }
+      if ($adjusted) {
+        drupal_set_message(t('Values for @fields fields were loaded from the MusicBrainz data.', array('@fields' => station_anded_list($adjusted))));
+      }
+    }
+  }
+}
+
+/**
+ * Implementation of hook_form().
  */
 function station_catalog_album_form($node) {
   // Make it a little easier when doing data entry and redirect back to the
@@ -171,10 +192,26 @@ function station_catalog_album_form($nod
     '#type' => 'textfield',
     '#title' => t('Label'),
     '#default_value' => $node->album['label'],
-    '#required' => TRUE,
+    '#required' => FALSE,
     '#maxlength' => 255,
     '#description' => t("The year the album was released."),
   );
+  $form['album']['mb_release_id'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Music Brainz Release ID'),
+    '#default_value' => $node->album['mb_release_id'],
+    '#required' => FALSE,
+    '#maxlength' => 255,
+    '#description' => t("MusicBrainz ID for this release."),
+  );
+  $form['album']['asin'] = array(
+    '#type' => 'textfield',
+    '#title' => t('Amazon Standard Identification Number'),
+    '#default_value' => $node->album['asin'],
+    '#required' => FALSE,
+    '#maxlength' => 255,
+    '#description' => t("Amazon product ID for this album."),
+  );
 
   if ($type->has_body) {
     $form['body_filter']['body'] = array(
@@ -225,7 +262,7 @@ function station_catalog_album_validate(
  * Implementation of hook_load().
  */
 function station_catalog_album_load($node) {
-  $result = db_query('SELECT number, artist, album, year, label FROM {station_catalog} WHERE nid = %d', $node->nid);
+  $result = db_query('SELECT number, artist, album, year, label, mb_release_id, asin FROM {station_catalog} WHERE nid = %d', $node->nid);
   return array('album' => db_fetch_array($result)); 
 }
 
@@ -233,7 +270,7 @@ function station_catalog_album_load($nod
  * Implementation of hook_insert().
  */
 function station_catalog_album_insert($node) {
-  db_query("INSERT INTO {station_catalog} (nid, number, artist, album, year, label) VALUES (%d, %d, '%s', '%s', %d, '%s')", $node->nid, $node->album['number'], $node->album['artist'], $node->album['album'], $node->album['year'], $node->album['label']);
+  db_query("INSERT INTO {station_catalog} (nid, number, artist, album, year, label, mb_release_id, asin) VALUES (%d, %d, '%s', '%s', %d, '%s', '%s', '%s')", $node->nid, $node->album['number'], $node->album['artist'], $node->album['album'], $node->album['year'], $node->album['label'], $node->album['mb_release_id'], $node->album['asin']);
 }
 
 /**
@@ -248,7 +285,7 @@ function station_catalog_album_delete($n
  */
 function station_catalog_album_update($node) {
   db_query("DELETE FROM {station_catalog} WHERE nid = %d", $node->nid);
-  db_query("INSERT INTO {station_catalog} (nid, number, artist, album, year, label) VALUES (%d, %d, '%s', '%s', %d, '%s')", $node->nid, $node->album['number'], $node->album['artist'], $node->album['album'], $node->album['year'], $node->album['label']);
+  db_query("INSERT INTO {station_catalog} (nid, number, artist, album, year, label, mb_release_id, asin) VALUES (%d, %d, '%s', '%s', %d, '%s', '%s', '%s')", $node->nid, $node->album['number'], $node->album['artist'], $node->album['album'], $node->album['year'], $node->album['label'], $node->album['mb_release_id'], $node->album['asin']);
 }
 
 /**
@@ -297,6 +334,23 @@ function station_catalog_album_view(&$no
     ),
   );
 
+  if (!empty($node->album['mb_release_id'])) {
+    $node->content['album']['mb_release_id'] = array(
+      '#type' => 'item',
+      '#title' => t('MusicBrainz'),
+      '#value' => l($node->album['mb_release_id'], "http://musicbrainz.org/release/{$node->album['mb_release_id']}.html"),
+      '#weight' => 0,
+    );
+  }
+  if (!empty($node->album['asin'])) {
+    $node->content['album']['asin'] = array(
+      '#type' => 'item',
+      '#title' => t('Amazon'),
+      '#value' => l($node->album['asin'], 'http://www.amazon.com/gp/product/'. $node->album['asin']),
+      '#weight' => 0,
+    );
+  }
+
   return $node;
 }
 
@@ -440,3 +494,64 @@ function station_catalog_search_form_sub
       }
   }
 }
+
+function _station_catalog_musicbrainz_album($artist, $title) {
+  $args = array(
+    'type' => 'xml',
+    'artist' => $artist,
+    'title' => $title
+  );
+  $url = url('http://musicbrainz.org/ws/1/release/', drupal_query_string_encode($args));
+
+  $ret = array();
+
+  $response = drupal_http_request($url);
+  if ($response->code == 200) {
+    $doc = DOMDocument::loadXML($response->data);
+
+    $xpath = new DOMXPath($doc);
+    // Can't use the default namespace, must explicitly set one...
+    $xpath->registerNameSpace('mb', 'http://musicbrainz.org/ns/mmd-1.0#');
+#dvm($doc->saveXML());
+    // Only return exact matches.
+    $result = $xpath->query('//mb:release-list/mb:release[@ext:score="100"]');
+    if ($result && $result->length) {
+      foreach ($result as $item) {
+        $release = simplexml_import_dom($result->item(0));
+#        dvm($release);
+      }
+      $release = simplexml_import_dom($result->item(0));
+      $ret['mb_release_id'] = (string) $release['id'];
+      $ret['artist'] = (string) $release->artist->name;
+      $ret['album'] = (string) $release->title;
+      $ret['asin'] = (string) $release->asin;
+      // Release year is a bit of a pain, the tag name isn't a valid PHP 
+      // identifier and we need to use a regular expression to match the 
+      // year portion of the ate.
+      $event = 'release-event-list';
+      preg_match('/.*US (\d{4}).*/', $release->$event->event['date'], $matches);
+      $ret['year'] = (string) $matches[1];
+
+      // Now that we've got the release id, we need to make another call to get the label.
+#      $label = _station_catalog_musicbrainz_label($ret['mb_release_id']);
+      $ret['label'] = '';
+    }
+  }
+  return $ret;
+}
+
+function _station_catalog_musicbrainz_label($release_id) {
+  $args = array(
+    'type' => 'xml',
+    'inc' => 'labels',
+  );
+  $url = url('http://musicbrainz.org/ws/1/release/'. $release_id, drupal_query_string_encode($args));
+dvm($url);
+  $ret = array();
+  $response = drupal_http_request($url);
+  if ($response->code == 200) {
+    $doc = DOMDocument::loadXML($response->data);
+dvm($doc->savexml());
+  }
+  return $ret;
+}
