Add a persistent 'shorturl' key to all links

All existing link will keep their permalinks.
New links will have smallhash generated with date+id.

The purpose of this is to avoid collision between links due to their creation date.
This commit is contained in:
ArthurHoaro 2016-11-28 18:24:15 +01:00
parent c3dfd89959
commit d592daea83
15 changed files with 115 additions and 91 deletions

View file

@ -143,7 +143,7 @@ public function buildData()
*/ */
protected function buildItem($link, $pageaddr) protected function buildItem($link, $pageaddr)
{ {
$link['guid'] = $pageaddr .'?'. smallHash($link['created']->format('Ymd_His')); $link['guid'] = $pageaddr .'?'. $link['shorturl'];
// Check for both signs of a note: starting with ? and 7 chars long. // Check for both signs of a note: starting with ? and 7 chars long.
if ($link['url'][0] === '?' && strlen($link['url']) === 7) { if ($link['url'][0] === '?' && strlen($link['url']) === 7) {
$link['url'] = $pageaddr . $link['url']; $link['url'] = $pageaddr . $link['url'];

View file

@ -22,6 +22,7 @@
* Can be absolute or relative. * Can be absolute or relative.
* Relative URLs are permalinks (e.g.'?m-ukcw') * Relative URLs are permalinks (e.g.'?m-ukcw')
* - real_url Absolute processed URL. * - real_url Absolute processed URL.
* - shorturl Permalink smallhash
* *
* Implements 3 interfaces: * Implements 3 interfaces:
* - ArrayAccess: behaves like an associative array; * - ArrayAccess: behaves like an associative array;
@ -264,6 +265,7 @@ private function check()
'created'=> new DateTime(), 'created'=> new DateTime(),
'tags'=>'opensource software' 'tags'=>'opensource software'
); );
$link['shorturl'] = link_small_hash($link['created'], $link['id']);
$this->links[1] = $link; $this->links[1] = $link;
$link = array( $link = array(
@ -273,8 +275,9 @@ private function check()
'description'=>'Shhhh! I\'m a private link only YOU can see. You can delete me too.', 'description'=>'Shhhh! I\'m a private link only YOU can see. You can delete me too.',
'private'=>1, 'private'=>1,
'created'=> new DateTime('1 minute ago'), 'created'=> new DateTime('1 minute ago'),
'tags'=>'secretstuff' 'tags'=>'secretstuff',
); );
$link['shorturl'] = link_small_hash($link['created'], $link['id']);
$this->links[0] = $link; $this->links[0] = $link;
// Write database to disk // Write database to disk
@ -335,10 +338,11 @@ private function read()
// To be able to load links before running the update, and prepare the update // To be able to load links before running the update, and prepare the update
if (! isset($link['created'])) { if (! isset($link['created'])) {
$link['id'] = $link['linkdate']; $link['id'] = $link['linkdate'];
$link['created'] = DateTime::createFromFormat('Ymd_His', $link['linkdate']); $link['created'] = DateTime::createFromFormat(self::LINK_DATE_FORMAT, $link['linkdate']);
if (! empty($link['updated'])) { if (! empty($link['updated'])) {
$link['updated'] = DateTime::createFromFormat('Ymd_His', $link['updated']); $link['updated'] = DateTime::createFromFormat(self::LINK_DATE_FORMAT, $link['updated']);
} }
$link['shorturl'] = smallHash($link['linkdate']);
} }
} }
@ -558,7 +562,7 @@ public function getNextId()
* *
* @param int $id Persistent ID of a link. * @param int $id Persistent ID of a link.
* *
* @return int Real offset in local array, or null if doesn't exists. * @return int Real offset in local array, or null if doesn't exist.
*/ */
protected function getLinkOffset($id) protected function getLinkOffset($id)
{ {

View file

@ -120,7 +120,7 @@ private function filterSmallHash($smallHash)
{ {
$filtered = array(); $filtered = array();
foreach ($this->links as $key => $l) { foreach ($this->links as $key => $l) {
if ($smallHash == smallHash($l['created']->format('Ymd_His'))) { if ($smallHash == $l['shorturl']) {
// Yes, this is ugly and slow // Yes, this is ugly and slow
$filtered[$key] = $l; $filtered[$key] = $l;
return $filtered; return $filtered;

View file

@ -169,3 +169,16 @@ function space2nbsp($text)
function format_description($description, $redirector = '', $indexUrl = '') { function format_description($description, $redirector = '', $indexUrl = '') {
return nl2br(space2nbsp(hashtag_autolink(text2clickable($description, $redirector), $indexUrl))); return nl2br(space2nbsp(hashtag_autolink(text2clickable($description, $redirector), $indexUrl)));
} }
/**
* Generate a small hash for a link.
*
* @param DateTime $date Link creation date.
* @param int $id Link ID.
*
* @return string the small hash generated from link data.
*/
function link_small_hash($date, $id)
{
return smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id);
}

View file

@ -174,6 +174,7 @@ public static function import($post, $files, $linkDb, $pagecache)
$newLinkDate->setTimezone(new DateTimeZone(date_default_timezone_get())); $newLinkDate->setTimezone(new DateTimeZone(date_default_timezone_get()));
$newLink['created'] = $newLinkDate; $newLink['created'] = $newLinkDate;
$newLink['id'] = $linkDb->getNextId(); $newLink['id'] = $linkDb->getNextId();
$newLink['shorturl'] = link_small_hash($newLink['created'], $newLink['id']);
$linkDb[$newLink['id']] = $newLink; $linkDb[$newLink['id']] = $newLink;
$importCount++; $importCount++;
} }

View file

@ -223,6 +223,9 @@ public function updateMethodEscapeUnescapedConfig()
* Since this update is very sensitve (changing the whole database), the datastore will be * Since this update is very sensitve (changing the whole database), the datastore will be
* automatically backed up into the file datastore.<datetime>.php. * automatically backed up into the file datastore.<datetime>.php.
* *
* LinkDB also adds the field 'shorturl' with the precedent format (linkdate smallhash),
* which will be saved by this method.
*
* @return bool true if the update is successful, false otherwise. * @return bool true if the update is successful, false otherwise.
*/ */
public function updateMethodDatastoreIds() public function updateMethodDatastoreIds()

View file

@ -31,7 +31,11 @@ function logm($logFile, $clientIp, $message)
* - are NOT cryptographically secure (they CAN be forged) * - are NOT cryptographically secure (they CAN be forged)
* *
* In Shaarli, they are used as a tinyurl-like link to individual entries, * In Shaarli, they are used as a tinyurl-like link to individual entries,
* e.g. smallHash('20111006_131924') --> yZH23w * built once with the combination of the date and item ID.
* e.g. smallHash('20111006_131924' . 142) --> eaWxtQ
*
* @warning before v0.8.1, smallhashes were built only with the date,
* and their value has been preserved.
* *
* @param string $text Create a hash from this text. * @param string $text Create a hash from this text.
* *

View file

@ -566,21 +566,17 @@ function showDailyRSS($conf) {
/* Some Shaarlies may have very few links, so we need to look /* Some Shaarlies may have very few links, so we need to look
back in time until we have enough days ($nb_of_days). back in time until we have enough days ($nb_of_days).
*/ */
$ids = array();
foreach ($LINKSDB as $id => $value) {
$ids[] = $id;
}
$nb_of_days = 7; // We take 7 days. $nb_of_days = 7; // We take 7 days.
$today = date('Ymd'); $today = date('Ymd');
$days = array(); $days = array();
foreach ($ids as $id) { foreach ($LINKSDB as $link) {
$day = $LINKSDB[$id]['created']->format('Ymd'); // Extract day (without time) $day = $link['created']->format('Ymd'); // Extract day (without time)
if (strcmp($day, $today) < 0) { if (strcmp($day, $today) < 0) {
if (empty($days[$day])) { if (empty($days[$day])) {
$days[$day] = array(); $days[$day] = array();
} }
$days[$day][] = $id; $days[$day][] = $link;
} }
if (count($days) > $nb_of_days) { if (count($days) > $nb_of_days) {
@ -600,23 +596,18 @@ function showDailyRSS($conf) {
echo '<copyright>'. $pageaddr .'</copyright>'. PHP_EOL; echo '<copyright>'. $pageaddr .'</copyright>'. PHP_EOL;
// For each day. // For each day.
foreach ($days as $day => $ids) { foreach ($days as $day => $links) {
$dayDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $day.'_000000'); $dayDate = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $day.'_000000');
$absurl = escape(index_url($_SERVER).'?do=daily&day='.$day); // Absolute URL of the corresponding "Daily" page. $absurl = escape(index_url($_SERVER).'?do=daily&day='.$day); // Absolute URL of the corresponding "Daily" page.
// Build the HTML body of this RSS entry.
$links = array();
// We pre-format some fields for proper output. // We pre-format some fields for proper output.
foreach ($ids as $id) { foreach ($links as &$link) {
$l = $LINKSDB[$id]; $link['formatedDescription'] = format_description($link['description'], $conf->get('redirector.url'));
$l['formatedDescription'] = format_description($l['description'], $conf->get('redirector.url')); $link['thumbnail'] = thumbnail($conf, $link['url']);
$l['thumbnail'] = thumbnail($conf, $l['url']); $link['timestamp'] = $link['created']->getTimestamp();
$l['timestamp'] = $l['created']->getTimestamp(); if (startsWith($link['url'], '?')) {
if (startsWith($l['url'], '?')) { $link['url'] = index_url($_SERVER) . $link['url']; // make permalink URL absolute
$l['url'] = index_url($_SERVER) . $l['url']; // make permalink URL absolute
} }
$links[$id] = $l;
} }
// Then build the HTML for this day: // Then build the HTML for this day:
@ -675,7 +666,6 @@ function showDaily($pageBuilder, $LINKSDB, $conf, $pluginManager)
$taglist = explode(' ',$link['tags']); $taglist = explode(' ',$link['tags']);
uasort($taglist, 'strcasecmp'); uasort($taglist, 'strcasecmp');
$linksToDisplay[$key]['shorturl'] = smallHash($link['created']->format('Ymd_His'));
$linksToDisplay[$key]['taglist']=$taglist; $linksToDisplay[$key]['taglist']=$taglist;
$linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $conf->get('redirector.url')); $linksToDisplay[$key]['formatedDescription'] = format_description($link['description'], $conf->get('redirector.url'));
$linksToDisplay[$key]['thumbnail'] = thumbnail($conf, $link['url']); $linksToDisplay[$key]['thumbnail'] = thumbnail($conf, $link['url']);
@ -829,7 +819,7 @@ function renderPage($conf, $pluginManager)
// Get only links which have a thumbnail. // Get only links which have a thumbnail.
foreach($links as $link) foreach($links as $link)
{ {
$permalink='?'.escape(smallHash($link['created']->format('Ymd_His'))); $permalink='?'.$link['shorturl'];
$thumb=lazyThumbnail($conf, $link['url'],$permalink); $thumb=lazyThumbnail($conf, $link['url'],$permalink);
if ($thumb!='') // Only output links which have a thumbnail. if ($thumb!='') // Only output links which have a thumbnail.
{ {
@ -1249,7 +1239,7 @@ function renderPage($conf, $pluginManager)
} }
// lf_id should only be present if the link exists. // lf_id should only be present if the link exists.
$id = !empty($_POST['lf_id']) ? (int) escape($_POST['lf_id']) : $LINKSDB->getNextId(); $id = !empty($_POST['lf_id']) ? intval(escape($_POST['lf_id'])) : $LINKSDB->getNextId();
// Linkdate is kept here to: // Linkdate is kept here to:
// - use the same permalink for notes as they're displayed when creating them // - use the same permalink for notes as they're displayed when creating them
// - let users hack creation date of their posts // - let users hack creation date of their posts
@ -1257,11 +1247,11 @@ function renderPage($conf, $pluginManager)
$linkdate = escape($_POST['lf_linkdate']); $linkdate = escape($_POST['lf_linkdate']);
if (isset($LINKSDB[$id])) { if (isset($LINKSDB[$id])) {
// Edit // Edit
$created = DateTime::createFromFormat('Ymd_His', $linkdate); $created = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $linkdate);
$updated = new DateTime(); $updated = new DateTime();
} else { } else {
// New link // New link
$created = DateTime::createFromFormat('Ymd_His', $linkdate); $created = DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $linkdate);
$updated = null; $updated = null;
} }
@ -1288,7 +1278,8 @@ function renderPage($conf, $pluginManager)
'private' => (isset($_POST['lf_private']) ? 1 : 0), 'private' => (isset($_POST['lf_private']) ? 1 : 0),
'created' => $created, 'created' => $created,
'updated' => $updated, 'updated' => $updated,
'tags' => str_replace(',', ' ', $tags) 'tags' => str_replace(',', ' ', $tags),
'shorturl' => link_small_hash($created, $id),
); );
// If title is empty, use the URL as title. // If title is empty, use the URL as title.
@ -1311,7 +1302,7 @@ function renderPage($conf, $pluginManager)
$returnurl = !empty($_POST['returnurl']) ? $_POST['returnurl'] : '?'; $returnurl = !empty($_POST['returnurl']) ? $_POST['returnurl'] : '?';
$location = generateLocation($returnurl, $_SERVER['HTTP_HOST'], array('addlink', 'post', 'edit_link')); $location = generateLocation($returnurl, $_SERVER['HTTP_HOST'], array('addlink', 'post', 'edit_link'));
// Scroll to the link which has been edited. // Scroll to the link which has been edited.
$location .= '#' . smallHash($created->format('Ymd_His')); $location .= '#' . $link['shorturl'];
// After saving the link, redirect to the page the user was on. // After saving the link, redirect to the page the user was on.
header('Location: '. $location); header('Location: '. $location);
exit; exit;
@ -1325,7 +1316,7 @@ function renderPage($conf, $pluginManager)
$link = $LINKSDB[(int) escape($_POST['lf_id'])]; $link = $LINKSDB[(int) escape($_POST['lf_id'])];
$returnurl = ( isset($_POST['returnurl']) ? $_POST['returnurl'] : '?' ); $returnurl = ( isset($_POST['returnurl']) ? $_POST['returnurl'] : '?' );
// Scroll to the link which has been edited. // Scroll to the link which has been edited.
$returnurl .= '#'.smallHash($link['created']->format('Ymd_His')); $returnurl .= '#'. $link['shorturl'];
$returnurl = generateLocation($returnurl, $_SERVER['HTTP_HOST'], array('addlink', 'post', 'edit_link')); $returnurl = generateLocation($returnurl, $_SERVER['HTTP_HOST'], array('addlink', 'post', 'edit_link'));
header('Location: '.$returnurl); // After canceling, redirect to the page the user was on. header('Location: '.$returnurl); // After canceling, redirect to the page the user was on.
exit; exit;
@ -1341,7 +1332,7 @@ function renderPage($conf, $pluginManager)
// - we are protected from XSRF by the token. // - we are protected from XSRF by the token.
// FIXME! We keep `lf_linkdate` for consistency before a proper API. To be removed. // FIXME! We keep `lf_linkdate` for consistency before a proper API. To be removed.
$id = isset($_POST['lf_id']) ? (int) escape($_POST['lf_id']) : (int) escape($_POST['lf_linkdate']); $id = isset($_POST['lf_id']) ? intval(escape($_POST['lf_id'])) : intval(escape($_POST['lf_linkdate']));
$pluginManager->executeHooks('delete_link', $LINKSDB[$id]); $pluginManager->executeHooks('delete_link', $LINKSDB[$id]);
@ -1387,7 +1378,7 @@ function renderPage($conf, $pluginManager)
$id = (int) escape($_GET['edit_link']); $id = (int) escape($_GET['edit_link']);
$link = $LINKSDB[$id]; // Read database $link = $LINKSDB[$id]; // Read database
if (!$link) { header('Location: ?'); exit; } // Link not found in database. if (!$link) { header('Location: ?'); exit; } // Link not found in database.
$link['linkdate'] = $link['created']->format('Ymd_His'); $link['linkdate'] = $link['created']->format(LinkDB::LINK_DATE_FORMAT);
$data = array( $data = array(
'link' => $link, 'link' => $link,
'link_is_new' => false, 'link_is_new' => false,
@ -1414,7 +1405,7 @@ function renderPage($conf, $pluginManager)
if (! $link) if (! $link)
{ {
$link_is_new = true; $link_is_new = true;
$linkdate = strval(date('Ymd_His')); $linkdate = strval(date(LinkDB::LINK_DATE_FORMAT));
// Get title if it was provided in URL (by the bookmarklet). // Get title if it was provided in URL (by the bookmarklet).
$title = empty($_GET['title']) ? '' : escape($_GET['title']); $title = empty($_GET['title']) ? '' : escape($_GET['title']);
// Get description if it was provided in URL (by the bookmarklet). [Bronco added that] // Get description if it was provided in URL (by the bookmarklet). [Bronco added that]
@ -1438,7 +1429,7 @@ function renderPage($conf, $pluginManager)
} }
if ($url == '') { if ($url == '') {
$url = '?' . smallHash($linkdate); $url = '?' . smallHash($linkdate . $LINKSDB->getNextId());
$title = 'Note: '; $title = 'Note: ';
} }
$url = escape($url); $url = escape($url);
@ -1453,7 +1444,7 @@ function renderPage($conf, $pluginManager)
'private' => $private 'private' => $private
); );
} else { } else {
$link['linkdate'] = $link['created']->format('Ymd_His'); $link['linkdate'] = $link['created']->format(LinkDB::LINK_DATE_FORMAT);
} }
$data = array( $data = array(
@ -1668,7 +1659,6 @@ function buildLinkList($PAGE,$LINKSDB, $conf, $pluginManager)
$taglist = explode(' ', $link['tags']); $taglist = explode(' ', $link['tags']);
uasort($taglist, 'strcasecmp'); uasort($taglist, 'strcasecmp');
$link['taglist'] = $taglist; $link['taglist'] = $taglist;
$link['shorturl'] = smallHash($link['created']->format('Ymd_His'));
// Check for both signs of a note: starting with ? and 7 chars long. // Check for both signs of a note: starting with ? and 7 chars long.
if ($link['url'][0] === '?' && if ($link['url'][0] === '?' &&
strlen($link['url']) === 7) { strlen($link['url']) === 7) {

View file

@ -43,9 +43,7 @@ function hook_isso_render_linklist($data, $conf)
$link = reset($data['links']); $link = reset($data['links']);
$issoHtml = file_get_contents(PluginManager::$PLUGINS_PATH . '/isso/isso.html'); $issoHtml = file_get_contents(PluginManager::$PLUGINS_PATH . '/isso/isso.html');
// FIXME! KO thread unique si même date $isso = sprintf($issoHtml, $issoUrl, $issoUrl, $link['id'], $link['id']);
$linkDate = $link['created']->format('Ymd_His');
$isso = sprintf($issoHtml, $issoUrl, $issoUrl, $linkDate, $linkDate);
$data['plugin_end_zone'][] = $isso; $data['plugin_end_zone'][] = $isso;
// Hackish way to include this CSS file only when necessary. // Hackish way to include this CSS file only when necessary.

View file

@ -86,7 +86,7 @@ public function testRSSBuildData()
// Test first link (note link) // Test first link (note link)
$link = reset($data['links']); $link = reset($data['links']);
$this->assertEquals(41, $link['id']); $this->assertEquals(41, $link['id']);
$this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']); $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
$this->assertEquals('http://host.tld/?WDWyig', $link['guid']); $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
$this->assertEquals('http://host.tld/?WDWyig', $link['url']); $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
$this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']); $this->assertRegExp('/Tue, 10 Mar 2015 11:46:51 \+\d{4}/', $link['pub_iso_date']);
@ -140,7 +140,7 @@ public function testBuildDataFiltered()
$this->assertEquals(1, count($data['links'])); $this->assertEquals(1, count($data['links']));
$link = array_shift($data['links']); $link = array_shift($data['links']);
$this->assertEquals(41, $link['id']); $this->assertEquals(41, $link['id']);
$this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']); $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
} }
/** /**
@ -157,7 +157,7 @@ public function testBuildDataCount()
$this->assertEquals(1, count($data['links'])); $this->assertEquals(1, count($data['links']));
$link = array_shift($data['links']); $link = array_shift($data['links']);
$this->assertEquals(41, $link['id']); $this->assertEquals(41, $link['id']);
$this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']); $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
} }
/** /**
@ -174,7 +174,7 @@ public function testBuildDataPermalinks()
// First link is a permalink // First link is a permalink
$link = array_shift($data['links']); $link = array_shift($data['links']);
$this->assertEquals(41, $link['id']); $this->assertEquals(41, $link['id']);
$this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114651'), $link['created']); $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'), $link['created']);
$this->assertEquals('http://host.tld/?WDWyig', $link['guid']); $this->assertEquals('http://host.tld/?WDWyig', $link['guid']);
$this->assertEquals('http://host.tld/?WDWyig', $link['url']); $this->assertEquals('http://host.tld/?WDWyig', $link['url']);
$this->assertContains('Direct link', $link['description']); $this->assertContains('Direct link', $link['description']);
@ -182,7 +182,7 @@ public function testBuildDataPermalinks()
// Second link is a direct link // Second link is a direct link
$link = array_shift($data['links']); $link = array_shift($data['links']);
$this->assertEquals(8, $link['id']); $this->assertEquals(8, $link['id']);
$this->assertEquals(DateTime::createFromFormat('Ymd_His', '20150310_114633'), $link['created']); $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'), $link['created']);
$this->assertEquals('http://host.tld/?RttfEw', $link['guid']); $this->assertEquals('http://host.tld/?RttfEw', $link['guid']);
$this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']); $this->assertEquals('https://static.fsf.org/nosvn/faif-2.0.pdf', $link['url']);
$this->assertContains('Direct link', $link['description']); $this->assertContains('Direct link', $link['description']);

View file

@ -191,7 +191,7 @@ public function testSave()
'url'=>'http://dum.my', 'url'=>'http://dum.my',
'description'=>'One more', 'description'=>'One more',
'private'=>0, 'private'=>0,
'created'=> DateTime::createFromFormat('Ymd_His', '20150518_190000'), 'created'=> DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150518_190000'),
'tags'=>'unit test' 'tags'=>'unit test'
); );
$testDB[$link['id']] = $link; $testDB[$link['id']] = $link;
@ -447,17 +447,17 @@ public function testFilterHashInValid()
*/ */
public function testReorderLinksDesc() public function testReorderLinksDesc()
{ {
self::$publicLinkDB->reorder('ASC'); self::$privateLinkDB->reorder('ASC');
$linkIdToTest = 42; $linkIds = array(42, 4, 1, 0, 7, 6, 8, 41);
foreach (self::$publicLinkDB as $key => $value) { $cpt = 0;
$this->assertEquals($linkIdToTest, $key); foreach (self::$privateLinkDB as $key => $value) {
break; $this->assertEquals($linkIds[$cpt++], $key);
} }
self::$publicLinkDB->reorder('DESC'); self::$privateLinkDB->reorder('DESC');
$linkIdToTest = 41; $linkIds = array_reverse($linkIds);
foreach (self::$publicLinkDB as $key => $value) { $cpt = 0;
$this->assertEquals($linkIdToTest, $key); foreach (self::$privateLinkDB as $key => $value) {
break; $this->assertEquals($linkIds[$cpt++], $key);
} }
} }
} }

View file

@ -116,7 +116,7 @@ public function testImportInternetExplorerEncoding()
$this->assertEquals( $this->assertEquals(
array( array(
'id' => 0, 'id' => 0,
'created' => DateTime::createFromFormat('Ymd_His', '20160618_203944'), 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160618_203944'),
'title' => 'Hg Init a Mercurial tutorial by Joel Spolsky', 'title' => 'Hg Init a Mercurial tutorial by Joel Spolsky',
'url' => 'http://hginit.com/', 'url' => 'http://hginit.com/',
'description' => '', 'description' => '',
@ -145,7 +145,7 @@ public function testImportNested()
$this->assertEquals( $this->assertEquals(
array( array(
'id' => 0, 'id' => 0,
'created' => DateTime::createFromFormat('Ymd_His', '20160225_235541'), 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235541'),
'title' => 'Nested 1', 'title' => 'Nested 1',
'url' => 'http://nest.ed/1', 'url' => 'http://nest.ed/1',
'description' => '', 'description' => '',
@ -158,7 +158,7 @@ public function testImportNested()
$this->assertEquals( $this->assertEquals(
array( array(
'id' => 1, 'id' => 1,
'created' => DateTime::createFromFormat('Ymd_His', '20160225_235542'), 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235542'),
'title' => 'Nested 1-1', 'title' => 'Nested 1-1',
'url' => 'http://nest.ed/1-1', 'url' => 'http://nest.ed/1-1',
'description' => '', 'description' => '',
@ -171,7 +171,7 @@ public function testImportNested()
$this->assertEquals( $this->assertEquals(
array( array(
'id' => 2, 'id' => 2,
'created' => DateTime::createFromFormat('Ymd_His', '20160225_235547'), 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235547'),
'title' => 'Nested 1-2', 'title' => 'Nested 1-2',
'url' => 'http://nest.ed/1-2', 'url' => 'http://nest.ed/1-2',
'description' => '', 'description' => '',
@ -184,7 +184,7 @@ public function testImportNested()
$this->assertEquals( $this->assertEquals(
array( array(
'id' => 3, 'id' => 3,
'created' => DateTime::createFromFormat('Ymd_His', '20160202_202222'), 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'),
'title' => 'Nested 2-1', 'title' => 'Nested 2-1',
'url' => 'http://nest.ed/2-1', 'url' => 'http://nest.ed/2-1',
'description' => 'First link of the second section', 'description' => 'First link of the second section',
@ -197,7 +197,7 @@ public function testImportNested()
$this->assertEquals( $this->assertEquals(
array( array(
'id' => 4, 'id' => 4,
'created' => DateTime::createFromFormat('Ymd_His', '20160119_230227'), 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'),
'title' => 'Nested 2-2', 'title' => 'Nested 2-2',
'url' => 'http://nest.ed/2-2', 'url' => 'http://nest.ed/2-2',
'description' => 'Second link of the second section', 'description' => 'Second link of the second section',
@ -210,7 +210,7 @@ public function testImportNested()
$this->assertEquals( $this->assertEquals(
array( array(
'id' => 5, 'id' => 5,
'created' => DateTime::createFromFormat('Ymd_His', '20160202_202222'), 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160202_202222'),
'title' => 'Nested 3-1', 'title' => 'Nested 3-1',
'url' => 'http://nest.ed/3-1', 'url' => 'http://nest.ed/3-1',
'description' => '', 'description' => '',
@ -223,7 +223,7 @@ public function testImportNested()
$this->assertEquals( $this->assertEquals(
array( array(
'id' => 6, 'id' => 6,
'created' => DateTime::createFromFormat('Ymd_His', '20160119_230227'), 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160119_230227'),
'title' => 'Nested 3-2', 'title' => 'Nested 3-2',
'url' => 'http://nest.ed/3-2', 'url' => 'http://nest.ed/3-2',
'description' => '', 'description' => '',
@ -236,7 +236,7 @@ public function testImportNested()
$this->assertEquals( $this->assertEquals(
array( array(
'id' => 7, 'id' => 7,
'created' => DateTime::createFromFormat('Ymd_His', '20160229_111541'), 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160229_111541'),
'title' => 'Nested 2', 'title' => 'Nested 2',
'url' => 'http://nest.ed/2', 'url' => 'http://nest.ed/2',
'description' => '', 'description' => '',
@ -269,7 +269,7 @@ public function testImportDefaultPrivacyNoPost()
array( array(
'id' => 0, 'id' => 0,
// Old link - UTC+4 (note that TZ in the import file is ignored). // Old link - UTC+4 (note that TZ in the import file is ignored).
'created' => DateTime::createFromFormat('Ymd_His', '20001010_135536'), 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'),
'title' => 'Secret stuff', 'title' => 'Secret stuff',
'url' => 'https://private.tld', 'url' => 'https://private.tld',
'description' => "Super-secret stuff you're not supposed to know about", 'description' => "Super-secret stuff you're not supposed to know about",
@ -282,7 +282,7 @@ public function testImportDefaultPrivacyNoPost()
$this->assertEquals( $this->assertEquals(
array( array(
'id' => 1, 'id' => 1,
'created' => DateTime::createFromFormat('Ymd_His', '20160225_235548'), 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'),
'title' => 'Public stuff', 'title' => 'Public stuff',
'url' => 'http://public.tld', 'url' => 'http://public.tld',
'description' => '', 'description' => '',
@ -313,7 +313,7 @@ public function testImportKeepPrivacy()
array( array(
'id' => 0, 'id' => 0,
// Note that TZ in the import file is ignored. // Note that TZ in the import file is ignored.
'created' => DateTime::createFromFormat('Ymd_His', '20001010_135536'), 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20001010_135536'),
'title' => 'Secret stuff', 'title' => 'Secret stuff',
'url' => 'https://private.tld', 'url' => 'https://private.tld',
'description' => "Super-secret stuff you're not supposed to know about", 'description' => "Super-secret stuff you're not supposed to know about",
@ -326,7 +326,7 @@ public function testImportKeepPrivacy()
$this->assertEquals( $this->assertEquals(
array( array(
'id' => 1, 'id' => 1,
'created' => DateTime::createFromFormat('Ymd_His', '20160225_235548'), 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160225_235548'),
'title' => 'Public stuff', 'title' => 'Public stuff',
'url' => 'http://public.tld', 'url' => 'http://public.tld',
'description' => '', 'description' => '',

View file

@ -352,20 +352,20 @@ public function testDatastoreIds()
$this->assertEquals('Naming conventions... #private', $linkDB[0]['description']); $this->assertEquals('Naming conventions... #private', $linkDB[0]['description']);
$this->assertEquals('samba cartoon web', $linkDB[0]['tags']); $this->assertEquals('samba cartoon web', $linkDB[0]['tags']);
$this->assertTrue($linkDB[0]['private']); $this->assertTrue($linkDB[0]['private']);
$this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_142300'), $linkDB[0]['created']); $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'), $linkDB[0]['created']);
$this->assertTrue(isset($linkDB[1])); $this->assertTrue(isset($linkDB[1]));
$this->assertFalse(isset($linkDB[1]['linkdate'])); $this->assertFalse(isset($linkDB[1]['linkdate']));
$this->assertEquals(1, $linkDB[1]['id']); $this->assertEquals(1, $linkDB[1]['id']);
$this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']); $this->assertEquals('UserFriendly - Samba', $linkDB[1]['title']);
$this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_172539'), $linkDB[1]['created']); $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'), $linkDB[1]['created']);
$this->assertTrue(isset($linkDB[2])); $this->assertTrue(isset($linkDB[2]));
$this->assertFalse(isset($linkDB[2]['linkdate'])); $this->assertFalse(isset($linkDB[2]['linkdate']));
$this->assertEquals(2, $linkDB[2]['id']); $this->assertEquals(2, $linkDB[2]['id']);
$this->assertEquals('Geek and Poke', $linkDB[2]['title']); $this->assertEquals('Geek and Poke', $linkDB[2]['title']);
$this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_182539'), $linkDB[2]['created']); $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'), $linkDB[2]['created']);
$this->assertEquals(DateTime::createFromFormat('Ymd_His', '20121206_190301'), $linkDB[2]['updated']); $this->assertEquals(DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_190301'), $linkDB[2]['updated']);
} }
/** /**

View file

@ -52,8 +52,9 @@ function testIssoDisplayed()
'title' => $str, 'title' => $str,
'links' => array( 'links' => array(
array( array(
'id' => 12,
'url' => $str, 'url' => $str,
'created' => DateTime::createFromFormat('Ymd_His', $date), 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date),
) )
) )
); );
@ -66,7 +67,14 @@ function testIssoDisplayed()
// plugin data // plugin data
$this->assertEquals(1, count($data['plugin_end_zone'])); $this->assertEquals(1, count($data['plugin_end_zone']));
$this->assertNotFalse(strpos($data['plugin_end_zone'][0], $date)); $this->assertNotFalse(strpos(
$data['plugin_end_zone'][0],
'data-isso-id="'. $data['links'][0]['id'] .'"'
));
$this->assertNotFalse(strpos(
$data['plugin_end_zone'][0],
'data-title="'. $data['links'][0]['id'] .'"'
));
$this->assertNotFalse(strpos($data['plugin_end_zone'][0], 'embed.min.js')); $this->assertNotFalse(strpos($data['plugin_end_zone'][0], 'embed.min.js'));
} }
@ -85,12 +93,14 @@ function testIssoMultipleLinks()
'title' => $str, 'title' => $str,
'links' => array( 'links' => array(
array( array(
'id' => 12,
'url' => $str, 'url' => $str,
'created' => DateTime::createFromFormat('Ymd_His', $date1), 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date1),
), ),
array( array(
'id' => 13,
'url' => $str . '2', 'url' => $str . '2',
'created' => DateTime::createFromFormat('Ymd_His', $date2), 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date2),
), ),
) )
); );
@ -114,8 +124,9 @@ function testIssoNotDisplayedWhenSearch()
'title' => $str, 'title' => $str,
'links' => array( 'links' => array(
array( array(
'id' => 12,
'url' => $str, 'url' => $str,
'created' => DateTime::createFromFormat('Ymd_His', $date), 'created' => DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, $date),
) )
), ),
'search_term' => $str 'search_term' => $str

View file

@ -21,7 +21,7 @@ public function __construct()
'?WDWyig', '?WDWyig',
'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag', 'Stallman has a beard and is part of the Free Software Foundation (or not). Seriously, read this. #hashtag',
0, 0,
DateTime::createFromFormat('Ymd_His', '20150310_114651'), DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114651'),
'sTuff', 'sTuff',
null, null,
'WDWyig' 'WDWyig'
@ -33,7 +33,7 @@ public function __construct()
'?WDWyig', '?WDWyig',
'Used to test links reordering.', 'Used to test links reordering.',
0, 0,
DateTime::createFromFormat('Ymd_His', '20100310_101010'), DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20100310_101010'),
'ut' 'ut'
); );
@ -43,9 +43,9 @@ public function __construct()
'https://static.fsf.org/nosvn/faif-2.0.pdf', 'https://static.fsf.org/nosvn/faif-2.0.pdf',
'Richard Stallman and the Free Software Revolution. Read this. #hashtag', 'Richard Stallman and the Free Software Revolution. Read this. #hashtag',
0, 0,
DateTime::createFromFormat('Ymd_His', '20150310_114633'), DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20150310_114633'),
'free gnu software stallman -exclude stuff hashtag', 'free gnu software stallman -exclude stuff hashtag',
DateTime::createFromFormat('Ymd_His', '20160803_093033') DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20160803_093033')
); );
$this->addLink( $this->addLink(
@ -54,7 +54,7 @@ public function __construct()
'http://mediagoblin.org/', 'http://mediagoblin.org/',
'A free software media publishing platform #hashtagOther', 'A free software media publishing platform #hashtagOther',
0, 0,
DateTime::createFromFormat('Ymd_His', '20130614_184135'), DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20130614_184135'),
'gnu media web .hidden hashtag', 'gnu media web .hidden hashtag',
null, null,
'IuWvgA' 'IuWvgA'
@ -66,7 +66,7 @@ public function __construct()
'https://dvcs.w3.org/hg/markup-validator/summary', 'https://dvcs.w3.org/hg/markup-validator/summary',
'Mercurial repository for the W3C Validator #private', 'Mercurial repository for the W3C Validator #private',
1, 1,
DateTime::createFromFormat('Ymd_His', '20141125_084734'), DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20141125_084734'),
'css html w3c web Mercurial' 'css html w3c web Mercurial'
); );
@ -76,7 +76,7 @@ public function __construct()
'http://ars.userfriendly.org/cartoons/?id=20121206', 'http://ars.userfriendly.org/cartoons/?id=20121206',
'Naming conventions... #private', 'Naming conventions... #private',
0, 0,
DateTime::createFromFormat('Ymd_His', '20121206_142300'), DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_142300'),
'dev cartoon web' 'dev cartoon web'
); );
@ -86,7 +86,7 @@ public function __construct()
'http://ars.userfriendly.org/cartoons/?id=20010306', 'http://ars.userfriendly.org/cartoons/?id=20010306',
'Tropical printing', 'Tropical printing',
0, 0,
DateTime::createFromFormat('Ymd_His', '20121206_172539'), DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_172539'),
'samba cartoon web' 'samba cartoon web'
); );
@ -96,7 +96,7 @@ public function __construct()
'http://geek-and-poke.com/', 'http://geek-and-poke.com/',
'', '',
1, 1,
DateTime::createFromFormat('Ymd_His', '20121206_182539'), DateTime::createFromFormat(LinkDB::LINK_DATE_FORMAT, '20121206_182539'),
'dev cartoon tag1 tag2 tag3 tag4 ' 'dev cartoon tag1 tag2 tag3 tag4 '
); );
} }
@ -115,7 +115,7 @@ protected function addLink($id, $title, $url, $description, $private, $date, $ta
'tags' => $tags, 'tags' => $tags,
'created' => $date, 'created' => $date,
'updated' => $updated, 'updated' => $updated,
'shorturl' => $shorturl ? $shorturl : smallHash($date->format('Ymd_His') . $id), 'shorturl' => $shorturl ? $shorturl : smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id),
); );
$this->_links[$id] = $link; $this->_links[$id] = $link;