aboutsummaryrefslogtreecommitdiffstats
path: root/core/include/feed
diff options
context:
space:
mode:
authorThomas Lange <code@nerdmind.de>2021-06-22 01:18:02 +0200
committerThomas Lange <code@nerdmind.de>2021-06-22 01:46:42 +0200
commit8cd1105b111b89106f24c5b50795afb5ff28a935 (patch)
treec789faac9de4d83cb409c45779e76be8938a4d66 /core/include/feed
parent7937df540b7d70b2bb87797442d0a0a0df197133 (diff)
downloadblog-8cd1105b111b89106f24c5b50795afb5ff28a935.tar.gz
blog-8cd1105b111b89106f24c5b50795afb5ff28a935.tar.xz
blog-8cd1105b111b89106f24c5b50795afb5ff28a935.zip
Implement new Repository and Entity classes
This commit adds new Repository and Entity classes which are better abstracted from the rest of the application. They dont know anymore about configuration options or how to parse to HTML because this is not the job for the ORM but for other parts of the application. The previous commits were a preparation for this big change. An entity now represents just a single record from a specific table of the database – nothing more. The repositories job is it to fetch or update records of the database and instantiate the entities. Another problem that was solved is the high amount of database queries that was needed before. For example, on the blogs home page first were all 10 latest post IDs fetched from the database and then another query was executed with "WHERE id = :id" for *each* single post?! ... This problem is solved with the new repository classes; they now use a single query to fetch and build the entities of the 10 latest posts. This change also solves the problem with database queries spread across the application and limits the exzessive use of try/catch blocks which were used before. The new classes make the whole code much cleaner. :)
Diffstat (limited to 'core/include/feed')
-rw-r--r--core/include/feed/main.php59
1 files changed, 24 insertions, 35 deletions
diff --git a/core/include/feed/main.php b/core/include/feed/main.php
index 4dc7c81..637c54b 100644
--- a/core/include/feed/main.php
+++ b/core/include/feed/main.php
@@ -1,9 +1,8 @@
<?php
#===============================================================================
-# Get instances
+# Get repositories
#===============================================================================
-$Database = Application::getDatabase();
-$Language = Application::getLanguage();
+$UserRepository = Application::getRepository('User');
#===============================================================================
# HEADER: Content-Type for XML document
@@ -14,26 +13,21 @@ HTTP::responseHeader(HTTP::HEADER_CONTENT_TYPE, HTTP::CONTENT_TYPE_XML);
# Post feed
#===============================================================================
if(!isset($param) OR $param !== 'page') {
- $POST['FEED_SORT'] = Application::get('POST.FEED_SORT');
- $POST['FEED_SIZE'] = Application::get('POST.FEED_SIZE');
+ $PostRepository = Application::getRepository('Post');
- $execSQL = "SELECT id FROM %s ORDER BY {$POST['FEED_SORT']} LIMIT {$POST['FEED_SIZE']}";
- $postIDs = $Database->query(sprintf($execSQL, Post\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN);
+ $posts = $PostRepository->getPaginated(
+ Application::get('POST.FEED_SORT'),
+ Application::get('POST.FEED_SIZE')
+ );
- foreach($postIDs as $postID) {
- try {
- $Post = Post\Factory::build($postID);
- $User = User\Factory::build($Post->get('user'));
+ foreach($posts as $Post) {
+ $User = $UserRepository->find($Post->get('user'));
- $ItemTemplate = Template\Factory::build('feed/item_post');
- $ItemTemplate->set('POST', generateItemTemplateData($Post));
- $ItemTemplate->set('USER', generateItemTemplateData($User));
+ $ItemTemplate = Template\Factory::build('feed/item_post');
+ $ItemTemplate->set('POST', generateItemTemplateData($Post));
+ $ItemTemplate->set('USER', generateItemTemplateData($User));
- $post_templates[] = $ItemTemplate;
- }
-
- catch(Post\Exception $Exception){}
- catch(User\Exception $Exception){}
+ $post_templates[] = $ItemTemplate;
}
}
@@ -41,26 +35,21 @@ if(!isset($param) OR $param !== 'page') {
# Page feed
#===============================================================================
if(!isset($param) OR $param !== 'post') {
- $PAGE['FEED_SORT'] = Application::get('PAGE.FEED_SORT');
- $PAGE['FEED_SIZE'] = Application::get('PAGE.FEED_SIZE');
-
- $execSQL = "SELECT id FROM %s ORDER BY {$PAGE['FEED_SORT']} LIMIT {$PAGE['FEED_SIZE']}";
- $pageIDs = $Database->query(sprintf($execSQL, Page\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN);
+ $PageRepository = Application::getRepository('Page');
- foreach($pageIDs as $pageID) {
- try {
- $Page = Page\Factory::build($pageID);
- $User = User\Factory::build($Page->get('user'));
+ $pages = $PageRepository->getPaginated(
+ Application::get('PAGE.FEED_SORT'),
+ Application::get('PAGE.FEED_SIZE')
+ );
- $ItemTemplate = Template\Factory::build('feed/item_page');
- $ItemTemplate->set('PAGE', generateItemTemplateData($Page));
- $ItemTemplate->set('USER', generateItemTemplateData($User));
+ foreach($pages as $Page) {
+ $User = $UserRepository->find($Page->get('user'));
- $page_templates[] = $ItemTemplate;
- }
+ $ItemTemplate = Template\Factory::build('feed/item_page');
+ $ItemTemplate->set('PAGE', generateItemTemplateData($Page));
+ $ItemTemplate->set('USER', generateItemTemplateData($User));
- catch(Page\Exception $Exception){}
- catch(User\Exception $Exception){}
+ $page_templates[] = $ItemTemplate;
}
}