diff options
-rw-r--r-- | admin/page/index.php | 10 | ||||
-rw-r--r-- | admin/page/search.php | 16 | ||||
-rw-r--r-- | admin/post/index.php | 10 | ||||
-rw-r--r-- | admin/post/search.php | 16 | ||||
-rw-r--r-- | admin/user/index.php | 10 | ||||
-rw-r--r-- | core/include/feed/main.php | 16 | ||||
-rw-r--r-- | core/include/home.php | 8 | ||||
-rw-r--r-- | core/include/page/list.php | 8 | ||||
-rw-r--r-- | core/include/post/list.php | 8 | ||||
-rw-r--r-- | core/include/search/main.php | 8 | ||||
-rw-r--r-- | core/include/user/list.php | 13 | ||||
-rw-r--r-- | core/namespace/Item.php | 30 | ||||
-rw-r--r-- | core/namespace/ItemFactory.php | 14 | ||||
-rw-r--r-- | core/namespace/ItemInterface.php | 2 | ||||
-rw-r--r-- | core/namespace/Page/Item.php | 16 | ||||
-rw-r--r-- | core/namespace/Post/Item.php | 16 |
16 files changed, 130 insertions, 71 deletions
diff --git a/admin/page/index.php b/admin/page/index.php index 7c8b1a6..82ac05f 100644 --- a/admin/page/index.php +++ b/admin/page/index.php @@ -26,14 +26,14 @@ if($currentSite < 1 OR ($currentSite > $lastSite AND $lastSite > 0)) { } #=============================================================================== -# Fetch page IDs from database +# Fetch items from database #=============================================================================== -$execSQL = "SELECT id FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; -$pageIDs = $Database->query(sprintf($execSQL, Page\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN); +$execSQL = "SELECT * FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; +$Statement = $Database->query(sprintf($execSQL, Page\Attribute::TABLE)); -foreach($pageIDs as $pageID) { +while($Attribute = $Statement->fetchObject('Page\Attribute')) { try { - $Page = Page\Factory::build($pageID); + $Page = Page\Factory::buildByAttribute($Attribute); $User = User\Factory::build($Page->attr('user')); $ItemTemplate = generatePageItemTemplate($Page, $User); diff --git a/admin/page/search.php b/admin/page/search.php index eb57a7c..9f68b69 100644 --- a/admin/page/search.php +++ b/admin/page/search.php @@ -14,17 +14,15 @@ require '../../core/application.php'; # IF: Handle search request #=============================================================================== if($search = HTTP::GET('q')) { - if($pageIDs = Page\Item::getSearchResultIDs($search, $Database)) { - foreach($pageIDs as $pageID) { - try { - $Page = Page\Factory::build($pageID); - $User = User\Factory::build($Page->attr('user')); + foreach(Page\Item::getSearchResults($search, $Database) as $Attribute) { + try { + $Page = Page\Factory::buildByAttribute($Attribute); + $User = User\Factory::build($Page->attr('user')); - $pages[] = generatePageItemTemplate($Page, $User); - } - catch(Page\Exception $Exception){} - catch(User\Exception $Exception){} + $pages[] = generatePageItemTemplate($Page, $User); } + catch(Page\Exception $Exception){} + catch(User\Exception $Exception){} } } diff --git a/admin/post/index.php b/admin/post/index.php index e18a634..1269236 100644 --- a/admin/post/index.php +++ b/admin/post/index.php @@ -26,14 +26,14 @@ if($currentSite < 1 OR ($currentSite > $lastSite AND $lastSite > 0)) { } #=============================================================================== -# Fetch post IDs from database +# Fetch items from database #=============================================================================== -$execSQL = "SELECT id FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; -$postIDs = $Database->query(sprintf($execSQL, Post\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN); +$execSQL = "SELECT * FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; +$Statement = $Database->query(sprintf($execSQL, Post\Attribute::TABLE)); -foreach($postIDs as $postID) { +while($Attribute = $Statement->fetchObject('Post\Attribute')) { try { - $Post = Post\Factory::build($postID); + $Post = Post\Factory::buildByAttribute($Attribute); $User = User\Factory::build($Post->attr('user')); $ItemTemplate = generatePostItemTemplate($Post, $User); diff --git a/admin/post/search.php b/admin/post/search.php index 8fe6a51..261cc06 100644 --- a/admin/post/search.php +++ b/admin/post/search.php @@ -14,17 +14,15 @@ require '../../core/application.php'; # IF: Handle search request #=============================================================================== if($search = HTTP::GET('q')) { - if($postIDs = Post\Item::getSearchResultIDs($search, [NULL, NULL, NULL], $Database)) { - foreach($postIDs as $postID) { - try { - $Post = Post\Factory::build($postID); - $User = User\Factory::build($Post->attr('user')); + foreach(Post\Item::getSearchResults($search, [NULL, NULL, NULL], $Database) as $Attribute) { + try { + $Post = Post\Factory::buildByAttribute($Attribute); + $User = User\Factory::build($Post->attr('user')); - $posts[] = generatePostItemTemplate($Post, $User); - } - catch(Post\Exception $Exception){} - catch(User\Exception $Exception){} + $posts[] = generatePostItemTemplate($Post, $User); } + catch(Post\Exception $Exception){} + catch(User\Exception $Exception){} } } diff --git a/admin/user/index.php b/admin/user/index.php index 7d0cfa8..8276d91 100644 --- a/admin/user/index.php +++ b/admin/user/index.php @@ -26,14 +26,14 @@ if($currentSite < 1 OR ($currentSite > $lastSite AND $lastSite > 0)) { } #=============================================================================== -# Fetch user IDs from database +# Fetch items from database #=============================================================================== -$execSQL = "SELECT id FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; -$userIDs = $Database->query(sprintf($execSQL, User\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN); +$execSQL = "SELECT * FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; +$Statement = $Database->query(sprintf($execSQL, User\Attribute::TABLE)); -foreach($userIDs as $userID) { +while($Attribute = $Statement->fetchObject('User\Attribute')) { try { - $User = User\Factory::build($userID); + $User = User\Factory::buildByAttribute($Attribute); $ItemTemplate = generateUserItemTemplate($User); $users[] = $ItemTemplate; diff --git a/core/include/feed/main.php b/core/include/feed/main.php index 765f296..59be3d1 100644 --- a/core/include/feed/main.php +++ b/core/include/feed/main.php @@ -17,12 +17,12 @@ if(!isset($param) OR $param !== 'page') { $POST['FEED_SORT'] = Application::get('POST.FEED_SORT'); $POST['FEED_SIZE'] = Application::get('POST.FEED_SIZE'); - $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); + $execSQL = "SELECT * FROM %s ORDER BY {$POST['FEED_SORT']} LIMIT {$POST['FEED_SIZE']}"; + $Statement = $Database->query(sprintf($execSQL, Post\Attribute::TABLE)); - foreach($postIDs as $postID) { + while($Attribute = $Statement->fetchObject('Post\Attribute')) { try { - $Post = Post\Factory::build($postID); + $Post = Post\Factory::buildByAttribute($Attribute); $User = User\Factory::build($Post->attr('user')); $ItemTemplate = Template\Factory::build('feed/item_post'); @@ -44,12 +44,12 @@ 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); + $execSQL = "SELECT * FROM %s ORDER BY {$PAGE['FEED_SORT']} LIMIT {$PAGE['FEED_SIZE']}"; + $Statement = $Database->query(sprintf($execSQL, Page\Attribute::TABLE)); - foreach($pageIDs as $pageID) { + while($Attribute = $Statement->fetchObject('Page\Attribute')) { try { - $Page = Page\Factory::build($pageID); + $Page = Page\Factory::buildByAttribute($Attribute); $User = User\Factory::build($Page->attr('user')); $ItemTemplate = Template\Factory::build('feed/item_page'); diff --git a/core/include/home.php b/core/include/home.php index dfa2a36..fe732de 100644 --- a/core/include/home.php +++ b/core/include/home.php @@ -5,14 +5,12 @@ $Database = Application::getDatabase(); $Language = Application::getLanguage(); -$execSQL = 'SELECT id FROM %s ORDER BY '.Application::get('POST.LIST_SORT').' LIMIT '.Application::get('POST.LIST_SIZE'); +$execSQL = 'SELECT * FROM %s ORDER BY '.Application::get('POST.LIST_SORT').' LIMIT '.Application::get('POST.LIST_SIZE'); $Statement = $Database->query(sprintf($execSQL, Post\Attribute::TABLE)); -$postIDs = $Statement->fetchAll($Database::FETCH_COLUMN); - -foreach($postIDs as $postID) { +while($Attribute = $Statement->fetchObject('Post\Attribute')) { try { - $Post = Post\Factory::build($postID); + $Post = Post\Factory::buildByAttribute($Attribute); $User = User\Factory::build($Post->attr('user')); $ItemTemplate = generatePostItemTemplate($Post, $User); diff --git a/core/include/page/list.php b/core/include/page/list.php index 0165116..2bb2639 100644 --- a/core/include/page/list.php +++ b/core/include/page/list.php @@ -30,12 +30,12 @@ if(Application::get('PAGE.SINGLE_REDIRECT') === TRUE AND $count === '1') { HTTP::redirect($Page->getURL()); } -$execSQL = "SELECT id FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; -$pageIDs = $Database->query(sprintf($execSQL, Page\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN); +$execSQL = "SELECT * FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; +$Statement = $Database->query(sprintf($execSQL, Page\Attribute::TABLE)); -foreach($pageIDs as $pageID) { +while($Attribute = $Statement->fetchObject('Page\Attribute')) { try { - $Page = Page\Factory::build($pageID); + $Page = Page\Factory::buildByAttribute($Attribute); $User = User\Factory::build($Page->attr('user')); $ItemTemplate = generatePageItemTemplate($Page, $User); diff --git a/core/include/post/list.php b/core/include/post/list.php index 0f5ef1f..68183c7 100644 --- a/core/include/post/list.php +++ b/core/include/post/list.php @@ -30,12 +30,12 @@ if(Application::get('POST.SINGLE_REDIRECT') === TRUE AND $count === '1') { HTTP::redirect($Post->getURL()); } -$execSQL = "SELECT id FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; -$postIDs = $Database->query(sprintf($execSQL, Post\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN); +$execSQL = "SELECT * FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; +$Statement = $Database->query(sprintf($execSQL, Post\Attribute::TABLE)); -foreach($postIDs as $postID) { +while($Attribute = $Statement->fetchObject('Post\Attribute')) { try { - $Post = Post\Factory::build($postID); + $Post = Post\Factory::buildByAttribute($Attribute); $User = User\Factory::build($Post->attr('user')); $ItemTemplate = generatePostItemTemplate($Post, $User); diff --git a/core/include/search/main.php b/core/include/search/main.php index f1b0693..ac7e23a 100644 --- a/core/include/search/main.php +++ b/core/include/search/main.php @@ -11,7 +11,7 @@ $M_LIST = $Database->query(sprintf('SELECT DISTINCT MONTH(time_insert) AS temp F $Y_LIST = $Database->query(sprintf('SELECT DISTINCT YEAR(time_insert) AS temp FROM %s ORDER BY temp', Post\Attribute::TABLE)); if($search = HTTP::GET('q')) { - if(!$postIDs = Post\Item::getSearchResultIDs($search, [HTTP::GET('d'), HTTP::GET('m'), HTTP::GET('y')], $Database)) { + if(!$attributes = Post\Item::getSearchResults($search, [HTTP::GET('d'), HTTP::GET('m'), HTTP::GET('y')], $Database)) { $message = $Language->text('search_no_results', escapeHTML($search)); } } @@ -37,10 +37,10 @@ $search_data = [ #=============================================================================== # Build document #=============================================================================== -if(isset($postIDs) AND !empty($postIDs)) { - foreach($postIDs as $postID) { +if(isset($attributes) AND !empty($attributes)) { + foreach($attributes as $Attribute) { try { - $Post = Post\Factory::build($postID); + $Post = Post\Factory::buildByAttribute($Attribute); $User = User\Factory::build($Post->attr('user')); $posts[] = generatePostItemTemplate($Post, $User); diff --git a/core/include/user/list.php b/core/include/user/list.php index afc8179..72375a4 100644 --- a/core/include/user/list.php +++ b/core/include/user/list.php @@ -30,8 +30,17 @@ if(Application::get('USER.SINGLE_REDIRECT') === TRUE AND $count === '1') { HTTP::redirect($User->getURL()); } -$execSQL = "SELECT id FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; -$userIDs = $Database->query(sprintf($execSQL, User\Attribute::TABLE))->fetchAll($Database::FETCH_COLUMN); +$execSQL = "SELECT * FROM %s ORDER BY {$site_sort} LIMIT ".(($currentSite-1) * $site_size).", {$site_size}"; +$Statement = $Database->query(sprintf($execSQL, User\Attribute::TABLE)); + +while($Attribute = $Statement->fetchObject('User\Attribute')) { + try { + $User = User\Factory::buildByAttribute($Attribute); + $ItemTemplate = generateUserItemTemplate($User); + + $users[] = $ItemTemplate; + } catch(User\Exception $Exception){} +} foreach($userIDs as $userID) { try { diff --git a/core/namespace/Item.php b/core/namespace/Item.php index 02de382..0a7d364 100644 --- a/core/namespace/Item.php +++ b/core/namespace/Item.php @@ -10,7 +10,7 @@ abstract class Item implements ItemInterface { #=============================================================================== # Abstract item constructor #=============================================================================== - public final function __construct($itemID, \Database $Database) { + public final function __construct($param, \Database $Database) { $this->Database = $Database; $this->Reflection = new ReflectionObject($this); @@ -18,6 +18,17 @@ abstract class Item implements ItemInterface { $attribute = "{$this->Reflection->getNamespaceName()}\\Attribute"; $exception = "{$this->Reflection->getNamespaceName()}\\Exception"; + # If $param is an instance of Attribute, + # skip fetching attribute from database! + if($param instanceof Attribute) { + $this->Attribute = $param; + return; + } + + # If this gets executed, then $param + # is not an instance of Attribute! + $itemID = $param; + #=============================================================================== # Checking if item in database exists #=============================================================================== @@ -168,4 +179,21 @@ abstract class Item implements ItemInterface { return 0; } + + #=============================================================================== + # Return Attribute instance based on field comparison with value + #=============================================================================== + public static function getByField($field, $value, \Database $Database): Attribute { + $exception = (new ReflectionClass(get_called_class()))->getNamespaceName().'\\Exception'; + $attribute = (new ReflectionClass(get_called_class()))->getNamespaceName().'\\Attribute'; + $Statement = $Database->prepare('SELECT * FROM '.$attribute::TABLE." WHERE {$field} = ?"); + + if($Statement->execute([$value])) { + if(!$Attribute = $Statement->fetchObject($attribute)) { + throw new $exception(sprintf("Item with value %s on field %s does not exist.", $value, $field)); + } + + return $Attribute; + } + } } diff --git a/core/namespace/ItemFactory.php b/core/namespace/ItemFactory.php index d81ff9f..ba32cd5 100644 --- a/core/namespace/ItemFactory.php +++ b/core/namespace/ItemFactory.php @@ -18,6 +18,18 @@ abstract class ItemFactory extends Factory { #=========================================================================== public static function buildBySlug($slug): Item { $Item = (new ReflectionClass(get_called_class()))->getNamespaceName().'\\Item'; - return self::build($Item::getIDByField('slug', $slug, \Application::getDatabase())); + return self::buildByAttribute($Item::getByField('slug', $slug, \Application::getDatabase())); + } + + #=========================================================================== + # Build instance by Attribute + #=========================================================================== + public static function buildByAttribute(Attribute $Attribute) { + if(!$Instance = parent::fetchInstance($Attribute->get('id'))) { + $Item = (new ReflectionClass(get_called_class()))->getNamespaceName().'\\Item'; + $Instance = parent::storeInstance($Attribute->get('id'), new $Item($Attribute, \Application::getDatabase())); + } + + return $Instance; } } diff --git a/core/namespace/ItemInterface.php b/core/namespace/ItemInterface.php index efee734..917db6a 100644 --- a/core/namespace/ItemInterface.php +++ b/core/namespace/ItemInterface.php @@ -1,4 +1,4 @@ <?php interface ItemInterface { - public function __construct($itemID, \Database $Database); + public function __construct($param, \Database $Database); } diff --git a/core/namespace/Page/Item.php b/core/namespace/Page/Item.php index d559b81..d875e8a 100644 --- a/core/namespace/Page/Item.php +++ b/core/namespace/Page/Item.php @@ -27,16 +27,24 @@ class Item extends \Item { } #=============================================================================== - # Return unique page IDs for search results + # Return search results as Page\Attribute #=============================================================================== - public static function getSearchResultIDs($search, \Database $Database): array { - $Statement = $Database->prepare(sprintf("SELECT id FROM %s WHERE + public static function getSearchResults($search, \Database $Database): array { + $Statement = $Database->prepare(sprintf("SELECT * FROM %s WHERE MATCH(name, body) AGAINST(? IN BOOLEAN MODE) LIMIT 20", Attribute::TABLE)); if($Statement->execute([$search])) { - return $Statement->fetchAll($Database::FETCH_COLUMN); + return $Statement->fetchAll($Database::FETCH_CLASS, 'Page\Attribute'); } return []; } + + #=============================================================================== + # Return associated User\Attribute + #=============================================================================== + public function getUserAttribute() { + $Statement = $this->Database->query(sprintf('SELECT * FROM user WHERE id = %d', $this->Attribute->get('user'))); + return $Statement->fetchObject('User\Attribute'); + } } diff --git a/core/namespace/Post/Item.php b/core/namespace/Post/Item.php index 0f2c6a5..6109e20 100644 --- a/core/namespace/Post/Item.php +++ b/core/namespace/Post/Item.php @@ -27,23 +27,31 @@ class Item extends \Item { } #=============================================================================== - # Return unique post IDs for search results + # Return search results as Post\Attribute #=============================================================================== - public static function getSearchResultIDs($search, array $date, \Database $Database): array { + public static function getSearchResults($search, array $date, \Database $Database): array { $D = ($D = intval($date[0])) !== 0 ? $D : 'NULL'; $M = ($M = intval($date[1])) !== 0 ? $M : 'NULL'; $Y = ($Y = intval($date[2])) !== 0 ? $Y : 'NULL'; - $Statement = $Database->prepare(sprintf("SELECT id FROM %s WHERE + $Statement = $Database->prepare(sprintf("SELECT * FROM %s WHERE ({$Y} IS NULL OR YEAR(time_insert) = {$Y}) AND ({$M} IS NULL OR MONTH(time_insert) = {$M}) AND ({$D} IS NULL OR DAY(time_insert) = {$D}) AND MATCH(name, body) AGAINST(? IN BOOLEAN MODE) LIMIT 20", Attribute::TABLE)); if($Statement->execute([$search])) { - return $Statement->fetchAll($Database::FETCH_COLUMN); + return $Statement->fetchAll($Database::FETCH_CLASS, 'Post\Attribute'); } return []; } + + #=============================================================================== + # Return associated User\Attribute + #=============================================================================== + public function getUserAttribute() { + $Statement = $this->Database->query(sprintf('SELECT * FROM user WHERE id = %d', $this->Attribute->get('user'))); + return $Statement->fetchObject('User\Attribute'); + } } |