Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Database abstraction #8

Open
wants to merge 104 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
104 commits
Select commit Hold shift + click to select a range
1d13c33
First new version of item classes.
rmccue Dec 10, 2011
4accaf9
Add DB classes.
rmccue Dec 10, 2011
ae454f9
Mess around with setting and stuff.
rmccue Dec 10, 2011
99c09d3
Let Lilina_Object::_from_obj() accept arrays too.
rmccue Dec 10, 2011
873ce95
Ignore the `feed` property and throw Exceptions from __set
rmccue Dec 10, 2011
163191b
Enable a prefix for the table names.
rmccue Dec 11, 2011
93b0f10
Fix syntax error.
rmccue Dec 11, 2011
d432602
Rename conditions to where and use PHP equality operators.
rmccue Dec 11, 2011
c246ff4
Add update method.
rmccue Dec 11, 2011
d33deff
Rename $options to $temp, which is what it should be.
rmccue Dec 11, 2011
c68b17f
Table is a required parameter and allow multiple where in update.
rmccue Dec 11, 2011
8888555
Pretend save.
rmccue Dec 11, 2011
53109dc
Again, table is required.
rmccue Dec 11, 2011
85eb1c5
Factorise the WHERE builder.
rmccue Dec 11, 2011
b331d3d
Table, required, etc.
rmccue Dec 11, 2011
6f9150f
Data must be an array or an object.
rmccue Dec 11, 2011
2678cdb
Document new data parameter option
rmccue Dec 11, 2011
8f0849a
Let objects specify a _db_export method
rmccue Dec 11, 2011
f90df04
Enable a custom export of Lilina_Item for databases.
rmccue Dec 11, 2011
2a327fa
Add insert method.
rmccue Dec 11, 2011
a9b4256
Missed a PHPDoc param before.
rmccue Dec 11, 2011
db34a11
Add docs to Lilina_DB_Adapter_Base.
rmccue Dec 11, 2011
6aa3987
Pass DB options to _db_export too
rmccue Dec 11, 2011
313ffc9
Add docs to some methods.
rmccue Dec 11, 2011
42d1404
DB_Adapter_Base, not Adapter_Base.
rmccue Dec 11, 2011
8bf6d41
Use is_callable correctly.
rmccue Dec 11, 2011
4828614
Don't allow duplicate entries on insert.
rmccue Dec 11, 2011
dd240a4
Use correct error code for missing table parameter.
rmccue Dec 11, 2011
a9ca095
Add more error codes.
rmccue Dec 11, 2011
69a9934
If data doesn't end up being an array, throw exception.
rmccue Dec 11, 2011
576e906
Reindex in MySQL if needed.
rmccue Dec 11, 2011
e1bea30
Reindex before typecasting.
rmccue Dec 11, 2011
400f18e
Ensure we get enclosure correctly when exporting for the DB.
rmccue Dec 11, 2011
505bf5c
In _from_obj(), if $vars isn't an array by the end, return an empty c…
rmccue Dec 11, 2011
80c8587
Add Adapter::count()
rmccue Dec 11, 2011
f1c405b
Rewrite Lilina_Items.
rmccue Dec 11, 2011
b98bf31
Add Items_Iterator::each()
rmccue Dec 11, 2011
6ec4f17
Query for 10 items by default.
rmccue Dec 11, 2011
93c4d19
Don't query in Lilina_Items::__construct()
rmccue Dec 11, 2011
46f05d6
Specify array if we're using one. Oops.
rmccue Dec 11, 2011
8e5156f
Switch to Lilina_Items instead of Items.
rmccue Dec 11, 2011
91a1444
Strip Items down as a shell for Lilina_Items.
rmccue Dec 11, 2011
f717eff
Fix syntax error.
rmccue Dec 11, 2011
3e8da08
If we slice, ensure we preserve keys.
rmccue Dec 11, 2011
d4d652d
Preserve keys when resorting.
rmccue Dec 11, 2011
405266d
Ensure we reindex if using non-file DBs.
rmccue Dec 11, 2011
defe17f
Slice after sorting, not before.
rmccue Dec 11, 2011
8efb84c
Make DB_Adapter_File::save() actually save.
rmccue Dec 11, 2011
ac2d133
Lilina_Item export for JSON when using the API.
rmccue Dec 11, 2011
aa309be
Actually insert/update rather than exiting early.
rmccue Dec 11, 2011
ab9e5ba
Ensure Services::get_for_item() still works for associative arrays.
rmccue Dec 11, 2011
23c0429
Ensure we always prepend the prefix to table names.
rmccue Dec 11, 2011
3537492
Quote manually instead of using PDOStatement::quote() for orderby
rmccue Dec 11, 2011
8837cf9
Add options for DB stuff.
rmccue Dec 11, 2011
ddde7a1
Add Lilina_Items::next() to complement the rest.
rmccue Dec 11, 2011
3709839
Add docs for Lilina_Items::each()
rmccue Dec 11, 2011
45e30c6
Ensure we iterate in has_items()
rmccue Dec 11, 2011
7ebabfb
Rework some more stuff in skin.
rmccue Dec 11, 2011
6434b2e
Return by reference from get_instance()
rmccue Dec 11, 2011
7cff17a
Add delete method and allow the use of orderby in update.
rmccue Dec 11, 2011
e344855
Document the previous.
rmccue Dec 11, 2011
776fb4c
Remove stuff we didn't need in the delete method.
rmccue Dec 11, 2011
2de9529
Add a filter for DB adapter.
rmccue Dec 11, 2011
97b38ca
Factor iterator stuff into a new class
rmccue Jan 2, 2012
ab32ef9
Replace Feeds with Lilina_Feeds, backed by Lilina_DB
rmccue Jan 2, 2012
91c74d2
Use correct keys for Lilina_Feed uses
rmccue Jan 7, 2012
00d6cb5
Ensure we don't have duplicate keys for WHERE queries
rmccue Jan 7, 2012
a7359cd
Ensure we pass options into object_to_array
rmccue Jan 7, 2012
a30c2df
Remove corresponding items when removing feeds.
rmccue Jan 7, 2012
2660e35
If the file doesn't exist when we load, return an array instead of er…
rmccue Jan 17, 2012
df29a41
For future timestamps, just give the full date instead.
rmccue Jan 17, 2012
d5b7db7
Merge branch 'master' into new-item-classes
rmccue Mar 1, 2012
9087d98
Merge branch 'new-item-classes' of github.com:Lilina/Lilina into new-…
rmccue Mar 2, 2012
db4c4ec
Fix sprintf, again
rmccue Mar 2, 2012
6bec85e
Forget about all the complicated adapter stuff and just upgrade.
rmccue Mar 2, 2012
beef0de
Add helper method to AdminOptions
rmccue Mar 2, 2012
dd50143
Allow checkbox inline labels in the admin
rmccue Mar 2, 2012
a079550
Fix time delta test.
rmccue Mar 2, 2012
ca8418b
Change Feeds class to use Lilina_Feeds
rmccue Mar 2, 2012
2078cef
Fix Feeds a little more, and note that it's deprecated.
rmccue Mar 2, 2012
bfffd3d
Mark the old Items class as deprecated too.
rmccue Mar 2, 2012
3e5418f
Move baseurl from being hardcoded into the options
rmccue Mar 2, 2012
3462047
Ensure we always get an array from ::load()
rmccue Mar 3, 2012
007b44a
Fix some errors in the DB classes
rmccue Mar 3, 2012
2dd73f4
Switch to using the DB classes for Options too
rmccue Mar 3, 2012
d88b5b8
Add lazy updating again
rmccue Mar 3, 2012
bb9d4f9
Update some of the updating code.
rmccue Mar 3, 2012
fb7ba90
Remove debugging call
rmccue Mar 3, 2012
6634b48
Move all bootstrapping into Lilina::bootstrap()
rmccue Mar 3, 2012
3774d54
Introducing a new upgrader.
rmccue Mar 3, 2012
8ad8d60
Don't load options in conf.php
rmccue Mar 3, 2012
fc92ba4
Ensure we actually load the options when bootstrapping.
rmccue Mar 3, 2012
2cef477
Actually add the adapter too.
rmccue Mar 3, 2012
c3b8cfe
DB::$adapter is now always set, so don't bother checking
rmccue Mar 3, 2012
89714a0
Use DB::get_adapter() directly rather than storing it
rmccue Mar 3, 2012
4564f44
We need access to $settings before using it. D'oh.
rmccue Mar 3, 2012
4af0e2e
Escape WHERE keys
rmccue Mar 3, 2012
3811abb
Default to the razor template in the admin too
rmccue Mar 3, 2012
2a1cb2f
Default to UTC in the admin too
rmccue Mar 3, 2012
89f1127
Switch update checkers to use options instead of DataHandler
rmccue Mar 3, 2012
a03a53d
Check plugin_update_status a bit more thoroughly
rmccue Mar 3, 2012
eecf181
Cache options once they're fetched
rmccue Mar 3, 2012
d52600d
Correct spelling error
rmccue Mar 3, 2012
a0d0f57
Actually returning the value from Options::get is a good idea
rmccue Mar 3, 2012
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions admin/admin-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,28 +53,51 @@ public static function init() {
* Callback for feeds.add
*/
public static function feeds_add($url, $name = '') {
$result = Feeds::get_instance()->add( $url, $name );
return array('success' => 1, 'msg' => $result['msg'], 'data' => Feeds::get_instance()->get($result['id']));
$feed = Lilina_Feed::create($url, $name);
$result = Lilina_Feeds::get_instance()->insert($feed);
return array(
'success' => 1,
'msg' => sprintf(_r('Added feed "%1$s"'), $feed->name),
'data' => $feed
);
}
/**
* Callback for feeds.change
*/
public static function feeds_change($feed_id, $url = '', $name = '') {
$data = array();
if(!empty($url))
$data['feed'] = $url;
if(!empty($name))
$data['name'] = $name;
$result = Feeds::get_instance()->update($feed_id, $data);
$feed = Lilina_Feeds::get_instance()->get($feed_id);

return array('success' => 1, 'msg' => $result, 'data' => Feeds::get_instance()->get($feed_id));
if (!empty($url)) {
$feed->feed = $url;
}

if (!empty($name)) {
$feed->name = $name;
}

Lilina_Feeds::get_instance()->update($feed);

return array(
'success' => 1,
'msg' => sprintf(_r('Changed "%s"'), $feed->name),
'data' => $feed
);
}
/**
* Callback for feeds.remove
*/
public static function feeds_remove($feed_id) {
$success = Feeds::get_instance()->delete($feed_id);
return array('success' => 1, 'msg' => $success);
$feed = Lilina_Feeds::get_instance()->get($feed_id);
Lilina_Feeds::get_instance()->delete($feed);

return array(
'success' => 1,
'msg' => sprintf(
_r('Removed "%1$s" &mdash; <a href="%2$s">Undo</a>?'),
$feed->name,
'feeds.php?action=add&amp;add_name=' . urlencode($feed->name) . '&amp;add_url=' . urlencode($feed->feed)
)
);
}
/**
* Callback for feeds.list
Expand All @@ -85,8 +108,20 @@ public static function feeds_list() {
/**
* Callback for feeds.get
*/
public static function feeds_get() {
return Feeds::get_instance()->getAll();
public static function feeds_get($start = 0, $limit = 0) {
$args = array();
$start = (int) $start;
$limit = (int) $limit;
if ($start !== 0) {
$args['offset'] = $start;
}
if ($limit !== 0) {
$args['limit'] = $limit;
}

Lilina_Feeds::get_instance()->query($args);

return Lilina_Feeds::get_instance()->get_items();
}
}

Expand Down
11 changes: 3 additions & 8 deletions admin/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,13 @@
define('LILINA_ADMIN', 1) ;
define('LILINA_PAGE', 'admin');

global $settings;

require_once(LILINA_INCPATH . '/core/Lilina.php');
Lilina::check_installed();
Lilina::bootstrap();


Lilina_Plugins::init();
Localise::load_default_textdomain();
require_once(LILINA_INCPATH . '/core/update-functions.php');
require_once(LILINA_INCPATH . '/core/file-functions.php');
require_once(LILINA_INCPATH . '/core/version.php');
require_once(LILINA_INCPATH . '/core/feed-functions.php');
require_once(LILINA_INCPATH . '/contrib/parseopml.php');
require_once(LILINA_INCPATH . '/core/auth-functions.php');
Expand All @@ -43,9 +41,6 @@
lilina_login_form('', '');
}

/** This sanitises all input variables, so we don't have to worry about them later */
Lilina::level_playing_field();

require_once(LILINA_PATH . '/admin/includes/common.php');

if ( !empty($_REQUEST['page']) ) {
Expand Down
9 changes: 5 additions & 4 deletions admin/includes/feeds.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,17 @@
*/
function feed_list_table() {
//Defined in admin panel
$feeds = Feeds::get_instance()->getAll();
Lilina_Feeds::get_instance()->query();
$feeds = Lilina_Feeds::get_instance()->get_items();
$j = 0;
$table = '';
if(is_array($feeds) && !empty($feeds)) {
foreach($feeds as $this_feed) {
$table .= '
<tr id="feed-' . $j . '" class="' . (($j % 2) ? 'alt' : '') . '">
<td class="name-col"><span>' . stripslashes($this_feed['name']) . '</span></td>
<td class="url-col"><span>' . $this_feed['feed'] . '</span></td>
<!--<td class="cat-col"><span>' . $this_feed['cat'] . '</span></td>-->
<td class="name-col"><span>' . stripslashes($this_feed->name) . '</span></td>
<td class="url-col"><span>' . $this_feed->feed . '</span></td>
<!--<td class="cat-col"><span>' . $this_feed->cat . '</span></td>-->
' . apply_filters('admin-feeds-infocol', '', $this_feed, $j) . '
<!--<td class="change-col"><a href="feeds.php?change=' . $j . '&amp;action=change" class="change_link">' . _r('Change') . '</a></td>-->
<td class="remove-col"><a href="feeds.php?remove=' . $j . '&amp;action=remove">' . _r('Remove') . '</a></td>
Expand Down
44 changes: 21 additions & 23 deletions admin/includes/import/import-gregarius.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,10 @@ public function dispatch() {
protected function import_feeds($feeds) {
foreach ($feeds as $feed) {
try {
$result = Feeds::get_instance()->add($feed->url, $feed->title);
echo '<li>' . $result['msg'] . '</li>';
$this->old[$feed->id] = $result['id'];
$real = Lilina_Feed::create($feed->url, $feed->title);
Lilina_Feeds::get_instance()->insert($real);
echo '<li>' . sprintf(_r('Added feed "%1$s"'), $real->name) . '</li>';
$this->old[$feed->id] = $real->id;
}
catch (Exception $e) {
$id = sha1($feed->url);
Expand All @@ -54,27 +55,25 @@ protected function import_feeds($feeds) {
}
protected function import_item($old) {
$new = $this->convert_item($old);
return Items::get_instance()->check_item($new);
return Lilina_Items::get_instance()->check($new);
}
protected function convert_item($old) {
$new = (object) array(
'hash' => sha1($old->guid),
'timestamp' => strtotime($old->pubdate),
'title' => $old->title,
'content' => $old->description,
'summary' => $old->description,
'permalink' => $old->url,
'metadata' => (object) array(
'enclosure' => $old->enclosure,
'enclosure_data' => false
),
'author' => array(
'name' => $old->author,
'url' => false
),
'feed' => false,
'feed_id' => $this->lookup_cid($old->cid)
);
$new = new Lilina_Item();
$new->hash = sha1($old->guid);
$new->timestamp = strtotime($old->pubdate);
$new->title = $old->title;
$new->content = $old->description;
$new->summary = $old->description;
$new->permalink = $old->url;
$new->feed_id = $this->lookup_cid($old->cid);

$enclosure = new Lilina_Enclosure();
$enclosure->url = $old->enclosure;
$new->enclosure = $enclosure;

$author = new Lilina_Author();
$author->name = $old->author;
$new->author = $author;
return $new;
}
protected function lookup_cid($cid) {
Expand Down Expand Up @@ -150,7 +149,6 @@ protected function import_items() {
$count++;
}
}
Items::get_instance()->save_cache();
printf('<p>' . _r('Done! Imported %d items.') . '</p>', $count);
}
catch (Exception $e) {
Expand Down
8 changes: 7 additions & 1 deletion admin/includes/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ class AdminOptions {
public $sections = array();

public function __construct() {
$this->whitelisted = array('sitename', 'template', 'locale', 'timezone', 'updateon');
$this->whitelisted = array('sitename', 'baseurl', 'template', 'locale', 'timezone', 'updateon');
}

public static function &instance() {
Expand Down Expand Up @@ -270,4 +270,10 @@ public function do_fields($section) {
echo '</div>';
}
}

public static function checked($a, $b) {
if ($a === $b) {
echo ' checked="checked"';
}
}
}
6 changes: 3 additions & 3 deletions admin/index.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,18 +87,18 @@ function relative_time($posted_date) {
?>
<h1><?php _e('Welcome!') ?></h1>
<?php
if (count(Feeds::get_instance()->getAll()) === 0) {
if (Lilina_Feeds::get_instance()->total_count() === 0) {
?>
<p><?php _e("Firstly, thanks for using Lilina! To help you settle in, we've included a few nifty tools in Lilina, just to help you get started.") ?></p>
<?php
}
else {
$updated = get_option('last_updated');
if (!$updated) {
$message = sprintf(_r('You currently have %d items in %d feeds. Never updated.'), count(Items::get_instance()->get_items()), count(Feeds::get_instance()->getAll()));
$message = sprintf(_r('You currently have %d items in %d feeds. Never updated.'), Lilina_Items::get_instance()->total_count(), Lilina_Feeds::get_instance()->total_count());
}
else {
$message = sprintf(_r('You currently have %d items in %d feeds. Last updated %s.'), count(Items::get_instance()->get_items()), count(Feeds::get_instance()->getAll()), relative_time($updated));
$message = sprintf(_r('You currently have %d items in %d feeds. Last updated %s.'), Lilina_Items::get_instance()->total_count(), Lilina_Feeds::get_instance()->total_count(), relative_time($updated));
}
?>
<p><?php echo $message ?></p>
Expand Down
12 changes: 12 additions & 0 deletions admin/resources/core.css
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,18 @@ legend {
padding: 2px 2em 2px 0;
font-size: 1.2em;
}
.row label.checkbox {
float: none;
display: inline;
text-align: left;
width: auto;
margin: 0 10px 0 0;
padding: 0;
}
.row label.checkbox input {
width: auto;
padding: 0 2px;
}
.row input, .row select, .row .input {
width: 56%;
padding: 3px;
Expand Down
11 changes: 5 additions & 6 deletions admin/settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,7 @@
</div>
<div class="row">
<label for="baseurl"><?php _e('Site address (URL)'); ?>:</label>
<input type="text" name="baseurl" id="baseurl" value="<?php echo get_option('baseurl'); ?>" disabled="disabled" />
<p class="sidenote"><?php _e('This option must be changed in content/system/config/settings.php manually.'); ?></p>
<input type="text" name="baseurl" id="baseurl" value="<?php echo get_option('baseurl'); ?>" />
</div>
</fieldset>
<fieldset id="views">
Expand Down Expand Up @@ -112,7 +111,7 @@
echo '<optgroup label="' . $continent . '">';
foreach($cities as $city => $tz) {
echo '<option';
if($tz === get_option('timezone')) {
if($tz === get_option('timezone', 'UTC')) {
echo ' selected="selected"';
}
echo ' value="' . $tz . '">' . $city . '</option>';
Expand Down Expand Up @@ -141,7 +140,7 @@
<?php
foreach ($available as $key => $template) {
$class = 'template';
if ($template->slug === get_option('template')) {
if ($template->slug === get_option('template', 'razor')) {
$class .= ' current';
}
?>
Expand All @@ -161,14 +160,14 @@
}

$title = sprintf(_r('%1$s %2$s by %3$s'), $template->name, $template->version, $author);
if ($template->slug === get_option('template')) {
if ($template->slug === get_option('template', 'razor')) {
$title .= ' (Current)';
}
?>
<h2><?php echo $title ?></h2>
<p><?php echo $template->description ?></p>
<?php
if ($template->slug !== get_option('template')) {
if ($template->slug !== get_option('template', 'razor')) {
?>
<button type="submit" name="activate_template" value="<?php echo $template->slug ?>">Activate</button>
<?php
Expand Down
8 changes: 5 additions & 3 deletions admin/subscribe.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,17 @@ public static function run() {

if (!empty($discovered))
$feed = $discovered[0];
$result = Feeds::get_instance()->add( $_POST['url'], $_POST['name'] );

$real = Lilina_Feed::create($feed, $_POST['name']);
Lilina_Feeds::get_instance()->insert($real);
}
catch( Exception $e ) {
return self::sub_page($e->getMessage());
}
return self::success_page($result);
return self::success_page();
}

public static function success_page($result) {
public static function success_page() {
self::head();
?>
<div id="message">
Expand Down
2 changes: 1 addition & 1 deletion content/plugins/instapaper.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public static function page() {

protected static function submit() {
$id = $_GET['item'];
$item = Items::get_instance()->get_item($id);
$item = Lilina_Items::get_instance()->get($id);

if ( false === $item ) {
throw new Exception(_r('Invalid item ID specified', 'instapaper'));
Expand Down
17 changes: 17 additions & 0 deletions inc/core/Errors.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,23 @@ protected static function init() {
// 110-119 = api.items.
self::$codes['api.items.no_method'] = 110;

// 200-299 = db.
// 200-219 = db.general
self::$codes['db.general.missingtable'] = 200;
self::$codes['db.general.datatypewrong'] = 201;
self::$codes['db.general.invalidwhere'] = 202;
self::$codes['db.general.adapternotfound'] = 203;

// 220-239 = db.insert
self::$codes['db.insert.duplicate'] = 220;
self::$codes['db.insert.missingprimary'] = 221;

// 240-259 = db.update
self::$codes['db.update.missingwhere'] = 240;

// 260-279 = db.delete
self::$codes['db.delete.missingwhere'] = 260;

// 800-899 = auth.
self::$codes['auth.none'] = 800;
// 900-999 = update.
Expand Down
Loading