Do not try to retrieve thumbnails for internal link

Also adds a helper function to determine if a link is a note and apply it across multiple files.
This commit is contained in:
ArthurHoaro 2019-02-09 14:13:08 +01:00
parent 905f8675a7
commit a8e7da0114
6 changed files with 42 additions and 7 deletions

View file

@ -59,7 +59,7 @@ public static function formatLink($link, $indexUrl)
{ {
$out['id'] = $link['id']; $out['id'] = $link['id'];
// Not an internal link // Not an internal link
if ($link['url'][0] != '?') { if (! is_note($link['url'])) {
$out['url'] = $link['url']; $out['url'] = $link['url'];
} else { } else {
$out['url'] = $indexUrl . $link['url']; $out['url'] = $indexUrl . $link['url'];

View file

@ -220,3 +220,16 @@ function link_small_hash($date, $id)
{ {
return smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id); return smallHash($date->format(LinkDB::LINK_DATE_FORMAT) . $id);
} }
/**
* Returns whether or not the link is an internal note.
* Its URL starts by `?` because it's actually a permalink.
*
* @param string $linkUrl
*
* @return bool true if internal note, false otherwise.
*/
function is_note($linkUrl)
{
return isset($linkUrl[0]) && $linkUrl[0] === '?';
}

View file

@ -147,8 +147,8 @@ public function buildData()
protected function buildItem($link, $pageaddr) protected function buildItem($link, $pageaddr)
{ {
$link['guid'] = $pageaddr . '?' . $link['shorturl']; $link['guid'] = $pageaddr . '?' . $link['shorturl'];
// Check for both signs of a note: starting with ? and 7 chars long. // Prepend the root URL for notes
if ($link['url'][0] === '?' && strlen($link['url']) === 7) { if (is_note($link['url'])) {
$link['url'] = $pageaddr . $link['url']; $link['url'] = $pageaddr . $link['url'];
} }
if ($this->usePermalinks === true) { if ($this->usePermalinks === true) {

View file

@ -54,7 +54,7 @@ public static function filterAndFormat($linkDb, $selection, $prependNoteUrl, $in
$link['timestamp'] = $date->getTimestamp(); $link['timestamp'] = $date->getTimestamp();
$link['taglist'] = str_replace(' ', ',', $link['tags']); $link['taglist'] = str_replace(' ', ',', $link['tags']);
if (startsWith($link['url'], '?') && $prependNoteUrl) { if (is_note($link['url']) && $prependNoteUrl) {
$link['url'] = $indexUrl . $link['url']; $link['url'] = $indexUrl . $link['url'];
} }

View file

@ -362,7 +362,7 @@ function showDailyRSS($conf, $loginManager)
$conf->get('redirector.encode_url') $conf->get('redirector.encode_url')
); );
$link['timestamp'] = $link['created']->getTimestamp(); $link['timestamp'] = $link['created']->getTimestamp();
if (startsWith($link['url'], '?')) { if (is_note($link['url'])) {
$link['url'] = index_url($_SERVER) . $link['url']; // make permalink URL absolute $link['url'] = index_url($_SERVER) . $link['url']; // make permalink URL absolute
} }
} }
@ -1176,7 +1176,9 @@ function renderPage($conf, $pluginManager, $LINKSDB, $history, $sessionManager,
$link['title'] = $link['url']; $link['title'] = $link['url'];
} }
if ($conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE) { if ($conf->get('thumbnails.mode', Thumbnailer::MODE_NONE) !== Thumbnailer::MODE_NONE
&& ! is_note($link['url'])
) {
$thumbnailer = new Thumbnailer($conf); $thumbnailer = new Thumbnailer($conf);
$link['thumbnail'] = $thumbnailer->get($url); $link['thumbnail'] = $thumbnailer->get($url);
} }
@ -1558,7 +1560,7 @@ function ($a, $b) {
$ids = []; $ids = [];
foreach ($LINKSDB as $link) { foreach ($LINKSDB as $link) {
// A note or not HTTP(S) // A note or not HTTP(S)
if ($link['url'][0] === '?' || ! startsWith(strtolower($link['url']), 'http')) { if (is_note($link['url']) || ! startsWith(strtolower($link['url']), 'http')) {
continue; continue;
} }
$ids[] = $link['id']; $ids[] = $link['id'];

View file

@ -317,6 +317,26 @@ public function testHashtagAutolinkNoIndex()
$this->assertNotContains('>#nothashtag', $autolinkedDescription); $this->assertNotContains('>#nothashtag', $autolinkedDescription);
} }
/**
* Test is_note with note URLs.
*/
public function testIsNote()
{
$this->assertTrue(is_note('?'));
$this->assertTrue(is_note('?abcDEf'));
$this->assertTrue(is_note('?_abcDEf#123'));
}
/**
* Test is_note with non note URLs.
*/
public function testIsNotNote()
{
$this->assertFalse(is_note(''));
$this->assertFalse(is_note('nope'));
$this->assertFalse(is_note('https://github.com/shaarli/Shaarli/?hi'));
}
/** /**
* Util function to build an hashtag link. * Util function to build an hashtag link.
* *