Change item id everywhere (shorter and use less db space)

This commit is contained in:
Frederic Guillot 2013-07-26 21:00:39 -04:00
parent f21f2c1951
commit 0b5895733b
13 changed files with 104 additions and 82 deletions

View File

@ -97,7 +97,7 @@ Router\get_action('show-help', function() {
// Show item without bottom nav // Show item without bottom nav
Router\get_action('show', function() { Router\get_action('show', function() {
$id = Model\decode_item_id(Request\param('id')); $id = Request\param('id');
$item = Model\get_item($id); $item = Model\get_item($id);
$feed = Model\get_feed($item['feed_id']); $feed = Model\get_feed($item['feed_id']);
@ -114,7 +114,7 @@ Router\get_action('show', function() {
// Show item with bottom nav // Show item with bottom nav
Router\get_action('read', function() { Router\get_action('read', function() {
$id = Model\decode_item_id(Request\param('id')); $id = Request\param('id');
$item = Model\get_item($id); $item = Model\get_item($id);
$feed = Model\get_feed($item['feed_id']); $feed = Model\get_feed($item['feed_id']);
$nav = Model\get_nav_item($item); // must be placed before set_item_read() $nav = Model\get_nav_item($item); // must be placed before set_item_read()
@ -133,7 +133,7 @@ Router\get_action('read', function() {
// Mark item as read and redirect to the listing page // Mark item as read and redirect to the listing page
Router\get_action('mark-item-read', function() { Router\get_action('mark-item-read', function() {
$id = Model\decode_item_id(Request\param('id')); $id = Request\param('id');
$redirect = Request\param('redirect', 'unread'); $redirect = Request\param('redirect', 'unread');
$offset = Request\int_param('offset', 0); $offset = Request\int_param('offset', 0);
@ -146,7 +146,7 @@ Router\get_action('mark-item-read', function() {
// Mark item as unread and redirect to the listing page // Mark item as unread and redirect to the listing page
Router\get_action('mark-item-unread', function() { Router\get_action('mark-item-unread', function() {
$id = Model\decode_item_id(Request\param('id')); $id = Request\param('id');
$redirect = Request\param('redirect', 'history'); $redirect = Request\param('redirect', 'history');
$offset = Request\int_param('offset', 0); $offset = Request\int_param('offset', 0);
@ -159,7 +159,7 @@ Router\get_action('mark-item-unread', function() {
// Mark item as removed and redirect to the listing page // Mark item as removed and redirect to the listing page
Router\get_action('mark-item-removed', function() { Router\get_action('mark-item-removed', function() {
$id = Model\decode_item_id(Request\param('id')); $id = Request\param('id');
$redirect = Request\param('redirect', 'history'); $redirect = Request\param('redirect', 'history');
$offset = Request\int_param('offset', 0); $offset = Request\int_param('offset', 0);
@ -172,7 +172,7 @@ Router\get_action('mark-item-removed', function() {
// Ajax call to mark item read // Ajax call to mark item read
Router\post_action('mark-item-read', function() { Router\post_action('mark-item-read', function() {
$id = Model\decode_item_id(Request\param('id')); $id = Request\param('id');
Model\set_item_read($id); Model\set_item_read($id);
Response\json(array('Ok')); Response\json(array('Ok'));
}); });
@ -181,7 +181,7 @@ Router\post_action('mark-item-read', function() {
// Ajax call to mark item unread // Ajax call to mark item unread
Router\post_action('mark-item-unread', function() { Router\post_action('mark-item-unread', function() {
$id = Model\decode_item_id(Request\param('id')); $id = Request\param('id');
Model\set_item_unread($id); Model\set_item_unread($id);
Response\json(array('Ok')); Response\json(array('Ok'));
}); });
@ -190,7 +190,7 @@ Router\post_action('mark-item-unread', function() {
// Ajax call to bookmark an item // Ajax call to bookmark an item
Router\post_action('bookmark-item', function() { Router\post_action('bookmark-item', function() {
$id = Model\decode_item_id(Request\param('id')); $id = Request\param('id');
Model\bookmark_item($id); Model\bookmark_item($id);
Response\json(array('Ok')); Response\json(array('Ok'));
}); });
@ -199,10 +199,10 @@ Router\post_action('bookmark-item', function() {
// Ajax call change item status // Ajax call change item status
Router\post_action('change-item-status', function() { Router\post_action('change-item-status', function() {
$id = Model\decode_item_id(Request\param('id')); $id = Request\param('id');
Response\json(array( Response\json(array(
'item_id' => Model\encode_item_id($id), 'item_id' => $id,
'status' => Model\switch_item_status($id) 'status' => Model\switch_item_status($id)
)); ));
}); });
@ -211,8 +211,7 @@ Router\post_action('change-item-status', function() {
// Add new bookmark // Add new bookmark
Router\get_action('bookmark', function() { Router\get_action('bookmark', function() {
$param_id = Request\param('id'); $id = Request\param('id');
$id = Model\decode_item_id($param_id);
$redirect = Request\param('redirect', 'unread'); $redirect = Request\param('redirect', 'unread');
$offset = Request\int_param('offset', 0); $offset = Request\int_param('offset', 0);
@ -220,11 +219,11 @@ Router\get_action('bookmark', function() {
if ($redirect === 'show') { if ($redirect === 'show') {
Response\Redirect('?action=show&id='.$param_id); Response\Redirect('?action=show&id='.$id);
} }
else if ($redirect === 'read') { else if ($redirect === 'read') {
Response\Redirect('?action=read&id='.$param_id); Response\Redirect('?action=read&id='.$id);
} }
Response\Redirect('?action='.$redirect.'&offset='.$offset); Response\Redirect('?action='.$redirect.'&offset='.$offset);

View File

@ -22,7 +22,7 @@ use PicoFeed\Reader;
use PicoFeed\Export; use PicoFeed\Export;
const DB_VERSION = 10; const DB_VERSION = 11;
const HTTP_USERAGENT = 'Miniflux - http://miniflux.net'; const HTTP_USERAGENT = 'Miniflux - http://miniflux.net';
const LIMIT_ALL = -1; const LIMIT_ALL = -1;
@ -99,18 +99,6 @@ function write_debug()
} }
function encode_item_id($input)
{
return strtr(base64_encode($input), '+/=', '-_,');
}
function decode_item_id($input)
{
return base64_decode(strtr($input, '-_,', '+/='));
}
function export_feeds() function export_feeds()
{ {
$opml = new Export(get_feeds()); $opml = new Export(get_feeds());
@ -530,8 +518,8 @@ function mark_items_as_read(array $items_id)
{ {
\PicoTools\singleton('db')->startTransaction(); \PicoTools\singleton('db')->startTransaction();
foreach($items_id as $encoded_id) { foreach ($items_id as $id) {
set_item_read(decode_item_id($encoded_id)); set_item_read($id);
} }
\PicoTools\singleton('db')->closeTransaction(); \PicoTools\singleton('db')->closeTransaction();

View File

@ -3,6 +3,36 @@
namespace Schema; namespace Schema;
function version_11($pdo)
{
$rq = $pdo->prepare('
SELECT
items.id, items.url AS item_url, feeds.site_url
FROM items
LEFT JOIN feeds ON feeds.id=items.feed_id
');
$rq->execute();
$items = $rq->fetchAll(\PDO::FETCH_ASSOC);
foreach ($items as $item) {
if ($item['id'] !== $item['item_url']) {
$id = hash('crc32b', $item['id'].$item['site_url']);
}
else {
$id = hash('crc32b', $item['item_url'].$item['site_url']);
}
$rq = $pdo->prepare('UPDATE items SET id=? WHERE id=?');
$rq->execute(array($id, $item['id']));
}
}
function version_10($pdo) function version_10($pdo)
{ {
$pdo->exec('ALTER TABLE config ADD COLUMN theme TEXT DEFAULT "original"'); $pdo->exec('ALTER TABLE config ADD COLUMN theme TEXT DEFAULT "original"');

View File

@ -10,13 +10,12 @@
<section class="items" id="listing"> <section class="items" id="listing">
<?php foreach ($items as $item): ?> <?php foreach ($items as $item): ?>
<?php $item_id = Model\encode_item_id($item['id']) ?> <article id="item-<?= $item['id'] ?>" data-item-id="<?= $item['id'] ?>">
<article id="item-<?= $item_id ?>" data-item-id="<?= $item_id ?>">
<h2> <h2>
<a <a
href="?action=show&amp;id=<?= $item_id ?>" href="?action=show&amp;id=<?= $item['id'] ?>"
data-item-id="<?= $item_id ?>" data-item-id="<?= $item['id'] ?>"
id="open-<?= $item_id ?>" id="open-<?= $item['id'] ?>"
> >
<?= Helper\escape($item['title']) ?> <?= Helper\escape($item['title']) ?>
</a> </a>
@ -26,17 +25,17 @@
<?= dt('%e %B %Y %k:%M', $item['updated']) ?> | <?= dt('%e %B %Y %k:%M', $item['updated']) ?> |
<span class="hide-mobile"> <span class="hide-mobile">
<a href="?action=bookmark&amp;value=0&amp;id=<?= $item_id ?>&amp;redirect=bookmarks&amp;offset=<?= $offset ?>"> <a href="?action=bookmark&amp;value=0&amp;id=<?= $item['id'] ?>&amp;redirect=bookmarks&amp;offset=<?= $offset ?>">
<?= t('remove bookmark') ?> <?= t('remove bookmark') ?>
</a> | </a> |
</span> </span>
<a <a
href="<?= $item['url'] ?>" href="<?= $item['url'] ?>"
id="original-<?= $item_id ?>" id="original-<?= $item['id'] ?>"
rel="noreferrer" rel="noreferrer"
target="_blank" target="_blank"
data-item-id="<?= $item_id ?>" data-item-id="<?= $item['id'] ?>"
> >
<?= t('original link') ?> <?= t('original link') ?>
</a> </a>

View File

@ -10,14 +10,13 @@
<section class="items" id="listing"> <section class="items" id="listing">
<?php foreach ($items as $item): ?> <?php foreach ($items as $item): ?>
<?php $item_id = Model\encode_item_id($item['id']) ?> <article id="item-<?= $item['id'] ?>" data-item-id="<?= $item['id'] ?>">
<article id="item-<?= $item_id ?>" data-item-id="<?= $item_id ?>">
<h2> <h2>
<?= $item['bookmark'] ? '★ ' : '' ?> <?= $item['bookmark'] ? '★ ' : '' ?>
<a <a
href="?action=show&amp;id=<?= $item_id ?>" href="?action=show&amp;id=<?= $item['id'] ?>"
data-item-id="<?= $item_id ?>" data-item-id="<?= $item['id'] ?>"
id="open-<?= $item_id ?>" id="open-<?= $item['id'] ?>"
> >
<?= Helper\escape($item['title']) ?> <?= Helper\escape($item['title']) ?>
</a> </a>
@ -28,10 +27,10 @@
<a <a
href="<?= $item['url'] ?>" href="<?= $item['url'] ?>"
id="original-<?= $item_id ?>" id="original-<?= $item['id'] ?>"
rel="noreferrer" rel="noreferrer"
target="_blank" target="_blank"
data-item-id="<?= $item_id ?>" data-item-id="<?= $item['id'] ?>"
> >
<?= t('original link') ?> <?= t('original link') ?>
</a> </a>

View File

@ -11,14 +11,13 @@
<section class="items" id="listing"> <section class="items" id="listing">
<?php foreach ($items as $item): ?> <?php foreach ($items as $item): ?>
<?php $item_id = Model\encode_item_id($item['id']) ?> <article id="item-<?= $item['id'] ?>" data-item-id="<?= $item['id'] ?>" data-item-page="<?= $menu ?>" data-hide="true">
<article id="item-<?= $item_id ?>" data-item-id="<?= $item_id ?>" data-item-page="<?= $menu ?>" data-hide="true">
<h2> <h2>
<?= $item['bookmark'] ? '★ ' : '' ?> <?= $item['bookmark'] ? '★ ' : '' ?>
<a <a
href="?action=show&amp;id=<?= $item_id ?>" href="?action=show&amp;id=<?= $item['id'] ?>"
data-item-id="<?= $item_id ?>" data-item-id="<?= $item['id'] ?>"
id="open-<?= $item_id ?>" id="open-<?= $item['id'] ?>"
> >
<?= Helper\escape($item['title']) ?> <?= Helper\escape($item['title']) ?>
</a> </a>
@ -30,28 +29,28 @@
<?php if (! $item['bookmark']): ?> <?php if (! $item['bookmark']): ?>
<span class="hide-mobile"> <span class="hide-mobile">
<a id="bookmark-<?= $item_id ?>" href="?action=bookmark&amp;value=1&amp;id=<?= $item_id ?>&amp;redirect=history&amp;offset=<?= $offset ?>"><?= t('bookmark') ?></a> | <a id="bookmark-<?= $item['id'] ?>" href="?action=bookmark&amp;value=1&amp;id=<?= $item['id'] ?>&amp;redirect=history&amp;offset=<?= $offset ?>"><?= t('bookmark') ?></a> |
</span> </span>
<?php endif ?> <?php endif ?>
<a <a
href="?action=mark-item-unread&amp;id=<?= $item_id ?>&amp;offset=<?= $offset ?>" href="?action=mark-item-unread&amp;id=<?= $item['id'] ?>&amp;offset=<?= $offset ?>"
data-action="mark-unread" data-action="mark-unread"
data-item-id="<?= $item_id ?>" data-item-id="<?= $item['id'] ?>"
> >
<?= t('mark as unread') ?> <?= t('mark as unread') ?>
</a> | </a> |
<span class="hide-mobile"> <span class="hide-mobile">
<a href="?action=mark-item-removed&amp;id=<?= $item_id ?>&amp;offset=<?= $offset ?>"><?= t('remove') ?></a> | <a href="?action=mark-item-removed&amp;id=<?= $item['id'] ?>&amp;offset=<?= $offset ?>"><?= t('remove') ?></a> |
</span> </span>
<a <a
href="<?= $item['url'] ?>" href="<?= $item['url'] ?>"
id="original-<?= $item_id ?>" id="original-<?= $item['id'] ?>"
rel="noreferrer" rel="noreferrer"
target="_blank" target="_blank"
data-item-id="<?= $item_id ?>" data-item-id="<?= $item['id'] ?>"
> >
<?= t('original link') ?> <?= t('original link') ?>
</a> </a>

View File

@ -3,8 +3,7 @@
<p class="alert alert-info"><?= t('Item not found') ?></p> <p class="alert alert-info"><?= t('Item not found') ?></p>
<?php else: ?> <?php else: ?>
<?php $item_id = Model\encode_item_id($item['id']) ?> <article class="item" id="current-item" data-item-id="<?= $item['id'] ?>" data-item-page="<?= $menu ?>">
<article class="item" id="current-item" data-item-id="<?= Model\encode_item_id($item['id']) ?>" data-item-page="<?= $menu ?>">
<h1> <h1>
<a href="<?= $item['url'] ?>" rel="noreferrer" target="_blank" id="original-item"> <a href="<?= $item['url'] ?>" rel="noreferrer" target="_blank" id="original-item">
<?= Helper\escape($item['title']) ?> <?= Helper\escape($item['title']) ?>
@ -15,9 +14,9 @@
<?= Helper\escape($feed['title']) ?> | <?= Helper\escape($feed['title']) ?> |
<span class="hide-mobile"><?= dt('%A %e %B %Y %k:%M', $item['updated']) ?> |</span> <span class="hide-mobile"><?= dt('%A %e %B %Y %k:%M', $item['updated']) ?> |</span>
<?php if ($item['bookmark']): ?> <?php if ($item['bookmark']): ?>
<a href="?action=bookmark&amp;value=0&amp;id=<?= $item_id ?>&amp;redirect=<?= $menu ?>"><?= t('remove bookmark') ?></a> <a href="?action=bookmark&amp;value=0&amp;id=<?= $item['id'] ?>&amp;redirect=<?= $menu ?>"><?= t('remove bookmark') ?></a>
<?php else: ?> <?php else: ?>
<a href="?action=bookmark&amp;value=1&amp;id=<?= $item_id ?>&amp;redirect=<?= $menu ?>"><?= t('bookmark') ?></a> <a href="?action=bookmark&amp;value=1&amp;id=<?= $item['id'] ?>&amp;redirect=<?= $menu ?>"><?= t('bookmark') ?></a>
<?php endif ?> <?php endif ?>
</p> </p>
@ -27,7 +26,7 @@
<nav> <nav>
<span class="nav-left"> <span class="nav-left">
<?php if ($item_nav['previous']): ?> <?php if ($item_nav['previous']): ?>
<a href="?action=read&amp;id=<?= Model\encode_item_id($item_nav['previous']['id']) ?>" id="previous-item">« <?= t('Previous') ?></a> <a href="?action=read&amp;id=<?= $item_nav['previous']['id'] ?>" id="previous-item">« <?= t('Previous') ?></a>
<?php else: ?> <?php else: ?>
« <?= t('Previous') ?> « <?= t('Previous') ?>
<?php endif ?> <?php endif ?>
@ -35,11 +34,11 @@
<span class="nav-middle"> <span class="nav-middle">
<?php if ($item_nav['previous'] && $item_nav['next']): ?> <?php if ($item_nav['previous'] && $item_nav['next']): ?>
<a href="?action=default#item-<?= Model\encode_item_id($item_nav['next']['id']) ?>"><?= t('Unread items') ?></a> <a href="?action=default#item-<?= $item_nav['next']['id'] ?>"><?= t('Unread items') ?></a>
<?php elseif ($item_nav['previous'] && ! $item_nav['next']): ?> <?php elseif ($item_nav['previous'] && ! $item_nav['next']): ?>
<a href="?action=default#item-<?= Model\encode_item_id($item_nav['previous']['id']) ?>"><?= t('Unread items') ?></a> <a href="?action=default#item-<?= $item_nav['previous']['id'] ?>"><?= t('Unread items') ?></a>
<?php elseif (! $item_nav['previous'] && $item_nav['next']): ?> <?php elseif (! $item_nav['previous'] && $item_nav['next']): ?>
<a href="?action=default#item-<?= Model\encode_item_id($item_nav['next']['id']) ?>"><?= t('Unread items') ?></a> <a href="?action=default#item-<?= $item_nav['next']['id'] ?>"><?= t('Unread items') ?></a>
<?php elseif (! $item_nav['previous'] && ! $item_nav['next']): ?> <?php elseif (! $item_nav['previous'] && ! $item_nav['next']): ?>
<a href="?action=default"><?= t('Unread items') ?></a> <a href="?action=default"><?= t('Unread items') ?></a>
<?php endif ?> <?php endif ?>
@ -47,7 +46,7 @@
<span class="nav-right"> <span class="nav-right">
<?php if ($item_nav['next']): ?> <?php if ($item_nav['next']): ?>
<a href="?action=read&amp;id=<?= Model\encode_item_id($item_nav['next']['id']) ?>" id="next-item"><?= t('Next') ?> »</a> <a href="?action=read&amp;id=<?= $item_nav['next']['id'] ?>" id="next-item"><?= t('Next') ?> »</a>
<?php else: ?> <?php else: ?>
<?= t('Next') ?> » <?= t('Next') ?> »
<?php endif ?> <?php endif ?>

View File

@ -17,14 +17,13 @@
<section class="items" id="listing"> <section class="items" id="listing">
<?php foreach ($items as $item): ?> <?php foreach ($items as $item): ?>
<?php $item_id = Model\encode_item_id($item['id']) ?> <article id="item-<?= $item['id'] ?>" data-item-id="<?= $item['id'] ?>" data-item-page="<?= $menu ?>" data-hide="true">
<article id="item-<?= $item_id ?>" data-item-id="<?= $item_id ?>" data-item-page="<?= $menu ?>" data-hide="true">
<h2> <h2>
<?= $item['bookmark'] ? '★ ' : '' ?> <?= $item['bookmark'] ? '★ ' : '' ?>
<a <a
href="?action=read&amp;id=<?= $item_id ?>" href="?action=read&amp;id=<?= $item['id'] ?>"
data-item-id="<?= $item_id ?>" data-item-id="<?= $item['id'] ?>"
id="open-<?= $item_id ?>" id="open-<?= $item['id'] ?>"
> >
<?= Helper\escape($item['title']) ?> <?= Helper\escape($item['title']) ?>
</a> </a>
@ -38,25 +37,25 @@
<span class="hide-mobile"> <span class="hide-mobile">
<?php if ($item['bookmark']): ?> <?php if ($item['bookmark']): ?>
<a id="bookmark-<?= $item_id ?>" href="?action=bookmark&amp;value=0&amp;id=<?= $item_id ?>&amp;redirect=unread&amp;offset=<?= $offset ?>"><?= t('remove bookmark') ?></a> | <a id="bookmark-<?= $item['id'] ?>" href="?action=bookmark&amp;value=0&amp;id=<?= $item['id'] ?>&amp;redirect=unread&amp;offset=<?= $offset ?>"><?= t('remove bookmark') ?></a> |
<?php else: ?> <?php else: ?>
<a id="bookmark-<?= $item_id ?>" href="?action=bookmark&amp;value=1&amp;id=<?= $item_id ?>&amp;redirect=unread&amp;offset=<?= $offset ?>"><?= t('bookmark') ?></a> | <a id="bookmark-<?= $item['id'] ?>" href="?action=bookmark&amp;value=1&amp;id=<?= $item['id'] ?>&amp;redirect=unread&amp;offset=<?= $offset ?>"><?= t('bookmark') ?></a> |
<?php endif ?> <?php endif ?>
</span> </span>
<a <a
href="?action=mark-item-read&amp;id=<?= $item_id ?>&amp;offset=<?= $offset ?>" href="?action=mark-item-read&amp;id=<?= $item['id'] ?>&amp;offset=<?= $offset ?>"
data-action="mark-read" data-action="mark-read"
data-item-id="<?= $item_id ?>" data-item-id="<?= $item['id'] ?>"
> >
<?= t('mark as read') ?> <?= t('mark as read') ?>
</a> | </a> |
<a <a
href="<?= $item['url'] ?>" href="<?= $item['url'] ?>"
id="original-<?= $item_id ?>" id="original-<?= $item['id'] ?>"
rel="noreferrer" rel="noreferrer"
target="_blank" target="_blank"
data-item-id="<?= $item_id ?>" data-item-id="<?= $item['id'] ?>"
data-action="original-link" data-action="original-link"
data-hide="true" data-hide="true"
> >

View File

@ -91,4 +91,11 @@ abstract class Parser
$value = str_replace("\n", "", $value); $value = str_replace("\n", "", $value);
return trim($value); return trim($value);
} }
public function generateId()
{
// crc32b seems to be faster and shorter than other hash algorithms
return hash('crc32b', implode(func_get_args()));
}
} }

View File

@ -25,13 +25,15 @@ class Atom extends \PicoFeed\Parser
if (isset($entry->author->name)) { if (isset($entry->author->name)) {
$author = $entry->author->name; $author = (string) $entry->author->name;
} }
$id = (string) $entry->id;
$item = new \StdClass; $item = new \StdClass;
$item->id = (string) $entry->id;
$item->title = $this->stripWhiteSpace((string) $entry->title);
$item->url = $this->getUrl($entry); $item->url = $this->getUrl($entry);
$item->id = $this->generateId($id !== $item->url ? $id : $item->$url, $this->url);
$item->title = $this->stripWhiteSpace((string) $entry->title);
$item->updated = strtotime((string) $entry->updated); $item->updated = strtotime((string) $entry->updated);
$item->author = $author; $item->author = $author;
$item->content = $this->filterHtml($this->getContent($entry), $item->url); $item->content = $this->filterHtml($this->getContent($entry), $item->url);

View File

@ -73,7 +73,7 @@ class Rss10 extends \PicoFeed\Parser
if (empty($item->title)) $item->title = $item->url; if (empty($item->title)) $item->title = $item->url;
$item->id = $item->url; $item->id = $this->generateId($item->url, $this->url);
$item->content = $this->filterHtml($item->content, $item->url); $item->content = $this->filterHtml($item->content, $item->url);
$this->items[] = $item; $this->items[] = $item;
} }

View File

@ -94,11 +94,12 @@ class Rss20 extends \PicoFeed\Parser
if (isset($entry->guid) && isset($entry->guid['isPermaLink']) && (string) $entry->guid['isPermaLink'] != 'false') { if (isset($entry->guid) && isset($entry->guid['isPermaLink']) && (string) $entry->guid['isPermaLink'] != 'false') {
$item->id = (string) $entry->guid; $id = (string) $entry->guid;
$item->id = $this->generateId($id !== '' && $id !== $item->url ? $id : $item->url, $this->url);
} }
else { else {
$item->id = $item->url; $item->id = $this->generateId($item->url, $this->url);
} }
if (empty($item->title)) $item->title = $item->url; if (empty($item->title)) $item->title = $item->url;