|
#!/usr/bin/env php |
|
<?php |
|
$_SERVER['REMOTE_ADDR'] = '99.293.39.39'; |
|
$_SERVER['REQUEST_METHOD'] = 'GET'; |
|
$_SERVER['SERVER_SOFTWARE'] = 'nginx/0'; |
|
|
|
const HREF_SEARCH = 'click.linksynergy'; |
|
$regex = '/href="(https?\:\/\/click\.linksynergy[^"]+)"/'; |
|
$add = 'at=10l4D3'; |
|
|
|
error_reporting(E_ERROR | E_WARNING | E_PARSE); |
|
require './includes/bootstrap.inc'; |
|
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL); |
|
|
|
$replacement_map = array(); |
|
|
|
// Build up replacement map with blog posts and songs |
|
print('Building replacement map'."\n"); |
|
|
|
$sql = 'SELECT nid FROM {node} WHERE type = "blog" OR type = "song" ORDER BY nid'; |
|
$query = db_query($sql); |
|
while ($row = db_fetch_array($query)) { |
|
$node = node_load($row['nid']); |
|
$content = $node->body; |
|
|
|
if ($node->type != 'blog') { |
|
$content = $node->field_affil_itunes[0]['value']; |
|
} |
|
|
|
$matches = array(); |
|
|
|
if (strpos($content, HREF_SEARCH) === FALSE) { |
|
continue; |
|
} |
|
|
|
preg_match_all($regex, $content, $matches); |
|
|
|
foreach ($matches as $match_arr) { |
|
foreach ($match_arr as $search) { |
|
$search = preg_replace('/^href="?/', '', $search); |
|
$search = str_replace('"', '', $search); |
|
|
|
$ls_url = htmlspecialchars_decode($search, ENT_QUOTES); |
|
$query_str = parse_url($ls_url, PHP_URL_QUERY); |
|
$pieces = array(); |
|
|
|
if ($query_str === FALSE) { |
|
throw new \Exception(sprintf('Failed to parse LinkShare URL: %s', $ls_url)); |
|
} |
|
|
|
parse_str($query_str, $pieces); |
|
if (!isset($pieces['RD_PARM1'])) { |
|
throw new \Exception(sprintf('Missing RD_PARM1: %s', $ls_url)); |
|
} |
|
|
|
$itunes_url = urldecode(urldecode($pieces['RD_PARM1'])); |
|
$parsed = parse_url($itunes_url); |
|
|
|
if ($parsed === FALSE) { |
|
throw new \Exception(sprintf('Failed to parse iTunes URL: %s', $itunes_url)); |
|
} |
|
|
|
$itunes_url = sprintf('https://%s%s?%s', $parsed['host'], $parsed['path'], $add); |
|
|
|
if (substr($search, 0, 4) !== 'http') { |
|
throw new \Exception(sprintf('Malformed key: %s', $search)); |
|
} |
|
|
|
$replacement_map[$search] = htmlspecialchars($itunes_url, ENT_QUOTES); |
|
} |
|
} |
|
} |
|
|
|
// Fix all the content using the replacement map |
|
$query = db_query($sql); |
|
while ($row = db_fetch_array($query)) { |
|
$node = node_load($row['nid']); |
|
$content = &$node->body; |
|
|
|
if ($node->type != 'blog') { |
|
$content = &$node->field_affil_itunes[0]['value']; |
|
} |
|
|
|
if (strpos($content, HREF_SEARCH) === FALSE) { |
|
continue; |
|
} |
|
|
|
printf('Updating node: %s (type: %s, nid: %d)'."\n", $node->title, $node->type, $row['nid']); |
|
|
|
foreach ($replacement_map as $search => $repl) { |
|
if (strpos($content, $search) === FALSE) { |
|
continue; |
|
} |
|
|
|
printf(' %s -> %s'."\n", $search, $repl); |
|
$content = str_replace($search, $repl, $content); |
|
} |
|
|
|
$content = str_replace('http://itunes.', 'https://itunes.', $content); |
|
|
|
node_save($node); |
|
|
|
// Re-generate teaser |
|
if ($node->type == 'blog') { |
|
printf(' Regenerating teaser for "%s" (nid: %d)'."\n", $node->title, $row['nid']); |
|
|
|
$teaser = node_teaser($content); |
|
$update_query = db_rewrite_sql('UPDATE {node_revisions} n SET n.teaser = "%s" WHERE n.vid = %d AND n.nid = %d'); |
|
db_query($update_query, array( |
|
$teaser, |
|
$node->vid, |
|
$row['nid'], |
|
)); |
|
} |
|
} |
|
|
|
// kate: indent-width 2; tab-width 2; |