Skip to content

Commit

Permalink
now stores source code of articles; script for getting source of prev…
Browse files Browse the repository at this point in the history
…iously added articles available at https://gist.github.com/doersino/23743cb4763a8b8dc02f
  • Loading branch information
doersino committed Sep 16, 2014
1 parent 63464a9 commit 80f08d3
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 29 deletions.
28 changes: 15 additions & 13 deletions Article.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,55 @@
require_once "Helper.class.php";

class Article {
public static function add($url, $state = "unread", $title = false) {
public static function add($url, $state = "unread", $source = false, $title = false) {
if (!Config::$allowDuplicateArticles) {
$query = DB::query("SELECT * FROM `read` WHERE `url` = %s", $url);
$query = DB::queryFirstField("SELECT 1 FROM `read` WHERE `url` = %s", $url);
if (!empty($query))
return false;
}

if (!$source)
$source = Helper::getSource($url);
if (!$title)
$title = Helper::getTitle($url);
$title = Helper::getTitle($source);
if ($state === "unread")
$query = DB::query("INSERT INTO `read` ( `url`, `title`, `time` ) VALUES (%s, %s, %s)", $url, $title, time());
$query = DB::query("INSERT INTO `read` ( `url`, `source`, `title`, `time` ) VALUES (%s, %s, %s, %s)", $url, $source, $title, time());
else if ($state === "archived")
$query = DB::query("INSERT INTO `read` ( `url`, `title`, `time`, `archived` ) VALUES (%s, %s, %s, %s)", $url, $title, time(), 1);
$query = DB::query("INSERT INTO `read` ( `url`, `source`, `title`, `time`, `archived` ) VALUES (%s, %s, %s, %s, %s)", $url, $source, $title, time(), 1);
else if ($state === "starred")
$query = DB::query("INSERT INTO `read` ( `url`, `title`, `time`, `archived`, `starred` ) VALUES (%s, %s, %s, %s, %s)", $url, $title, time(), 1, 1);
$query = DB::query("INSERT INTO `read` ( `url`, `source`, `title`, `time`, `archived`, `starred` ) VALUES (%s, %s, %s, %s, %s, %s)", $url, $source, $title, time(), 1, 1);
return true;
}

public static function archive($id) {
$query = DB::queryFirstRow("SELECT * FROM `read` WHERE `id` = %i", $id);
if (empty($query) || $query["archived"] == 1)
$query = DB::queryFirstField("SELECT 1 FROM `read` WHERE `id` = %i", $id);
if (empty($query))
return false;

DB::query("UPDATE `read` SET `archived` = %i, `time` = %i WHERE `id` = %i", 1, time(), $id);
return true;
}

public static function star($id) {
$query = DB::queryFirstRow("SELECT * FROM `read` WHERE `id` = %i", $id);
if (empty($query) || $query["starred"] == 1)
$query = DB::queryFirstField("SELECT 1 FROM `read` WHERE `id` = %i", $id);
if (empty($query))
return false;

DB::query("UPDATE `read` SET `starred` = %i WHERE `id` = %i", 1, $id);
return true;
}

public static function unstar($id) {
$query = DB::queryFirstRow("SELECT * FROM `read` WHERE `id` = %i", $id);
if (empty($query) || $query["starred"] == 0)
$query = DB::queryFirstField("SELECT 1 FROM `read` WHERE `id` = %i", $id);
if (empty($query))
return false;

DB::query("UPDATE `read` SET `starred` = %i WHERE `id` = %i", 0, $id);
return true;
}

public static function remove($id) {
$query = DB::queryFirstRow("SELECT * FROM `read` WHERE `id` = %i", $id);
$query = DB::queryFirstRow("SELECT 1 FROM `read` WHERE `id` = %i", $id);
if (empty($query))
return false;

Expand Down
3 changes: 1 addition & 2 deletions Helper.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,7 @@ public static function getSource($url) {
return $source;
}

public static function getTitle($url) {
$source = self::getSource($url);
public static function getTitle($source) {
if (preg_match("/<title>(.+?)<\/title>/isx", $source, $title))
return $title[1];
return "";
Expand Down
18 changes: 9 additions & 9 deletions Read.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public static function getTotalArticleCount($state = false) {

public static function getArticles($state, $offset, $limit) {
if ($state === "unread")
$query = DB::query("SELECT * FROM `read` WHERE `archived` = %i ORDER BY `time` DESC LIMIT %i OFFSET %i", 0, $limit, $offset);
$query = DB::query("SELECT `id`, `url`, `title`, `time`, `starred` FROM `read` WHERE `archived` = %i ORDER BY `time` DESC LIMIT %i OFFSET %i", 0, $limit, $offset);
else if ($state === "archived")
$query = DB::query("SELECT * FROM `read` WHERE `archived` = %i ORDER BY `time` DESC LIMIT %i OFFSET %i", 1, $limit, $offset);
$query = DB::query("SELECT `id`, `url`, `title`, `time`, `starred` FROM `read` WHERE `archived` = %i ORDER BY `time` DESC LIMIT %i OFFSET %i", 1, $limit, $offset);
else if ($state === "starred")
$query = DB::query("SELECT * FROM `read` WHERE `starred` = %i ORDER BY `time` DESC LIMIT %i OFFSET %i", 1, $limit, $offset);
$query = DB::query("SELECT `id`, `url`, `title`, `time`, `starred` FROM `read` WHERE `starred` = %i ORDER BY `time` DESC LIMIT %i OFFSET %i", 1, $limit, $offset);
else
return false;

Expand All @@ -48,11 +48,11 @@ public static function getArticles($state, $offset, $limit) {

public static function getSearchResults($state, $search) {
if ($state === "unread")
$query = DB::query("SELECT * FROM `read` WHERE `archived` = %i ORDER BY `time` DESC", 0);
$query = DB::query("SELECT `id`, `url`, `title`, `time`, `starred` FROM `read` WHERE `archived` = %i ORDER BY `time` DESC", 0);
else if ($state === "archived")
$query = DB::query("SELECT * FROM `read` WHERE `archived` = %i ORDER BY `time` DESC", 1);
$query = DB::query("SELECT `id`, `url`, `title`, `time`, `starred` FROM `read` WHERE `archived` = %i ORDER BY `time` DESC", 1);
else if ($state === "starred")
$query = DB::query("SELECT * FROM `read` WHERE `starred` = %i ORDER BY `time` DESC", 1);
$query = DB::query("SELECT `id`, `url`, `title`, `time`, `starred` FROM `read` WHERE `starred` = %i ORDER BY `time` DESC", 1);
else
return false;

Expand All @@ -72,17 +72,17 @@ public static function getSearchResults($state, $search) {
public static function getArticlesPerDay($state, $search = false) {
if ($state === "unread") {
if ($search)
$query = DB::query("SELECT `url`, `title`, `time`, `starred` FROM `read` WHERE `archived` = %i ORDER BY `time` ASC", 0);
$query = DB::query("SELECT `url`, `title`, `time` FROM `read` WHERE `archived` = %i ORDER BY `time` ASC", 0);
else
$query = DB::query("SELECT `time` FROM `read` WHERE `archived` = %i ORDER BY `time` ASC", 0);
} else if ($state === "archived") {
if ($search)
$query = DB::query("SELECT `url`, `title`, `time`, `starred` FROM `read` WHERE `archived` = %i ORDER BY `time` ASC", 1);
$query = DB::query("SELECT `url`, `title`, `time` FROM `read` WHERE `archived` = %i ORDER BY `time` ASC", 1);
else
$query = DB::query("SELECT `time` FROM `read` WHERE `archived` = %i ORDER BY `time` ASC", 1);
} else if ($state === "starred") {
if ($search)
$query = DB::query("SELECT `url`, `title`, `time`, `starred` FROM `read` WHERE `starred` = %i ORDER BY `time` ASC", 1);
$query = DB::query("SELECT `url`, `title`, `time` FROM `read` WHERE `starred` = %i ORDER BY `time` ASC", 1);
else
$query = DB::query("SELECT `time` FROM `read` WHERE `starred` = %i ORDER BY `time` ASC", 1);
} else
Expand Down
9 changes: 5 additions & 4 deletions import.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ SET time_zone = "+00:00";
CREATE TABLE IF NOT EXISTS `read` (
`id` mediumint(9) NOT NULL AUTO_INCREMENT,
`url` varchar(2048) COLLATE utf8_unicode_ci NOT NULL,
`source` mediumtext COLLATE utf8_unicode_ci NOT NULL,
`title` varchar(2048) COLLATE utf8_unicode_ci NOT NULL,
`time` int(11) NOT NULL,
`archived` tinyint(1) NOT NULL,
Expand All @@ -18,10 +19,10 @@ CREATE TABLE IF NOT EXISTS `read` (
) ENGINE=MyISAM DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;

INSERT INTO `read` (`id`, `url`, `title`, `time`, `archived`, `starred`) VALUES
(1, 'http://www.43folders.com/2011/04/22/cranking', 'Cranking | 43 Folders', 1407667692, 1, 1),
(2, 'http://news.stanford.edu/news/2005/june15/jobs-061505.html', 'Text of Steve Jobs'' Commencement address (2005)', 1407668713, 0, 0),
(3, 'http://www.cgpgrey.com/blog/i-have-died-many-times', 'I Have Died Many Times — CGP Grey', 1407668745, 1, 0),
(4, 'http://lifehacker.com/im-ira-glass-host-of-this-american-life-and-this-is-h-1609562031/all', 'I''m Ira Glass, Host of This American Life, and This Is How I Work', 1407668760, 1, 0);
(1, 'http://www.43folders.com/2011/04/22/cranking', '<!-- source code of sample articles not included -->', 'Cranking | 43 Folders', 1407667692, 1, 1),
(2, 'http://news.stanford.edu/news/2005/june15/jobs-061505.html', '<!-- source code of sample articles not included -->', 'Text of Steve Jobs'' Commencement address (2005)', 1407668713, 0, 0),
(3, 'http://www.cgpgrey.com/blog/i-have-died-many-times', '<!-- source code of sample articles not included -->', 'I Have Died Many Times — CGP Grey', 1407668745, 1, 0),
(4, 'http://lifehacker.com/im-ira-glass-host-of-this-american-life-and-this-is-h-1609562031/all', '<!-- source code of sample articles not included -->', 'I''m Ira Glass, Host of This American Life, and This Is How I Work', 1407668760, 1, 0);

/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
Expand Down
2 changes: 1 addition & 1 deletion style.css
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ main div.actions input {
}

/* MOBILE */
@media (max-width: 640px) {
@media (max-width: 720px) {
html {
font-size: 12px;
}
Expand Down

0 comments on commit 80f08d3

Please sign in to comment.