Last active
October 30, 2024 16:43
-
-
Save artlung/92053fb8840d631a98997a593cd89891 to your computer and use it in GitHub Desktop.
legacy-links-php-export.php
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
<?php | |
//$hostname = ""; // the address of the MySQL server | |
//$username = ""; // your username | |
//$password = ""; //your password | |
//$databaseName = ""; // name of the database | |
$cn = mysqli_connect($hostname, $username, $password, $databaseName) or die("Cannot connect to database."); | |
//Array | |
//( | |
// [0] => 6978 | |
// [id] => 6978 | |
// [1] => 2013-05-01 18:41:56 | |
// [dateadded] => 2013-05-01 18:41:56 | |
// [2] => http://oembed.com/ | |
// [url] => http://oembed.com/ | |
// [3] => oEmbed | |
//[urltext] => oEmbed | |
//[4] => oEmbed is a format for allowing an embedded representation of a URL on third party sites. The simple API allows a website to display embedded content (such as photos or videos) when a user posts a link to that resource, without having to parse the resource directly. | |
//[accompanyingtext] => oEmbed is a format for allowing an embedded representation of a URL on third party sites. The simple API allows a website to display embedded content (such as photos or videos) when a user posts a link to that resource, without having to parse the resource directly. | |
//[5] => | |
// [via] => | |
// [6] => | |
// [category] => | |
// [7] => standards,oembed,multimedia,programming,webdev,json,link,api,web services,video,embed | |
//[tags] => standards,oembed,multimedia,programming,webdev,json,link,api,web services,video,embed | |
//[8] => 1 | |
// [liveonsite] => 1 | |
// [9] => | |
// [hash] => | |
// [10] => | |
// [meta] => | |
// [11] => | |
// [toread] => | |
//) | |
/** | |
* @param $row | |
* @return void | |
*/ | |
function rowToStructuredData($row) { | |
$id = 'sidebarlinks.' . $row['id']; | |
$tags = explode(",", $row['tags']); | |
// trim any empty tags | |
$tags = array_filter($tags, 'trim'); | |
$date_added = $row['dateadded']; | |
// format as iso 8601 | |
$date_added = date('c', strtotime($date_added)); | |
$url_text = $row['urltext']; | |
$accompanying_text = $row['accompanyingtext']; | |
$via = $row['via']; | |
$category = $row['category']; | |
$live_on_site = $row['liveonsite']; | |
$url = $row['url']; | |
$hash = $row['hash']; | |
$meta = $row['meta']; | |
$to_read = $row['toread']; | |
$nonsense = ''; | |
if (trim($hash) != '') { | |
$nonsense .= 'hash:' . $hash . "|"; | |
} | |
if (trim($meta) != '') { | |
$nonsense .= 'meta:' . $meta . "|"; | |
} | |
$private = !$live_on_site; | |
if ($to_read) { | |
$nonsense .= 'to_read:' . json_encode($to_read) . "|"; | |
} | |
// trim final pipe from nonsense | |
if (trim($via) != '') { | |
// if it begins with http, add it to nonsense | |
if (substr($via, 0, 4) == 'http') { | |
$nonsense .= 'via:' . $via . "|"; | |
$domain_name = parse_url($via, PHP_URL_HOST); | |
// if it has a www. at beginning, strip it | |
if (substr($domain_name, 0, 4) == 'www.') { | |
$domain_name = substr($domain_name, 4); | |
} | |
$viaTag = 'via:' . $domain_name; | |
$tags[] = $viaTag; | |
} else { | |
$viaTag = 'via:' . $via; | |
$tags[] = $viaTag; | |
} | |
} | |
// add the domain as a tag | |
$domain_name = parse_url($url, PHP_URL_HOST); | |
// if it has a www. at beginning, strip it | |
if (substr($domain_name, 0, 4) == 'www.') { | |
$domain_name = substr($domain_name, 4); | |
} | |
$domainTag = 'domain:' . $domain_name; | |
$tags[] = $domainTag; | |
$nonsense = rtrim($nonsense, '|'); | |
return [ | |
'id' => $id, | |
'url' => $url, | |
'tags' => $tags, | |
'date_added' => $date_added, | |
'title' => $url_text, | |
'description' => $accompanying_text, | |
'private' => $private, | |
'via' => $via, | |
// 'category' => $category, NOT USED | |
// 'live_on_site' => $live_on_site, | |
// 'hash' => $hash, | |
// 'meta' => $meta, | |
// 'to_read' => $to_read, | |
'extra_derived' => $nonsense, | |
]; | |
} | |
//$requested_id = (int)$_GET['id']; | |
$query = "SELECT | |
* | |
FROM `sidebarlinks` | |
order by id asc limit 30000"; | |
// min 3 | |
// max 28109 | |
// get by column name | |
$result_mode = MYSQLI_ASSOC; | |
$result = mysqli_query($cn, $query, $result_mode); | |
// verbose error | |
if (!$result) { | |
die('Invalid query: ' . mysqli_error()); | |
} | |
// json header | |
//header('Content-Type: application/json'); | |
while ($row = mysqli_fetch_array($result)) { | |
$id = $row['id']; | |
// format filename like 00003.json | |
$filename = str_pad($id, 5, '0', STR_PAD_LEFT) . '.json'; | |
$data = rowToStructuredData($row); | |
$json = json_encode($data, JSON_PRETTY_PRINT); | |
file_put_contents($filename, $json); | |
print $filename . "\n"; | |
// print json_encode(rowToStructuredData($row)); | |
} | |
// 3 to 28109 | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment