From: <tr...@us...> - 2012-12-01 22:36:48
|
Revision: 10297 http://sourceforge.net/p/xoops/svn/10297 Author: trabis Date: 2012-12-01 22:36:45 +0000 (Sat, 01 Dec 2012) Log Message: ----------- publisher - updating permissions and category for performance Modified Paths: -------------- XoopsModules/publisher/trunk/publisher/admin/category.php XoopsModules/publisher/trunk/publisher/archive.php XoopsModules/publisher/trunk/publisher/category.php XoopsModules/publisher/trunk/publisher/class/category.php XoopsModules/publisher/trunk/publisher/class/item.php XoopsModules/publisher/trunk/publisher/class/permission.php XoopsModules/publisher/trunk/publisher/include/common.php Modified: XoopsModules/publisher/trunk/publisher/admin/category.php =================================================================== --- XoopsModules/publisher/trunk/publisher/admin/category.php 2012-12-01 15:55:26 UTC (rev 10296) +++ XoopsModules/publisher/trunk/publisher/admin/category.php 2012-12-01 22:36:45 UTC (rev 10297) @@ -109,25 +109,8 @@ $categoryObj->setVar('weight', isset($_POST['weight']) ? intval($_POST['weight']) : 1); // Groups and permissions - if (isset($_POST['groups_read'])) { - $categoryObj->setGroups_read($_POST['groups_read']); - } else { - $categoryObj->setGroups_read(); - } $grpread = isset($_POST['groups_read']) ? $_POST['groups_read'] : array(); - - if (isset($_POST['groups_submit'])) { - $categoryObj->setGroups_submit($_POST['groups_submit']); - } else { - $categoryObj->setGroups_submit(); - } $grpsubmit = isset($_POST['groups_submit']) ? $_POST['groups_submit'] : array(); - - if (isset($_POST['groups_moderation'])) { - $categoryObj->setGroups_moderation($_POST['groups_moderation']); - } else { - $categoryObj->setGroups_moderation(); - } $grpmoderation = isset($_POST['groups_moderation']) ? $_POST['groups_moderation'] : array(); @@ -167,9 +150,9 @@ exit; } // TODO : put this function in the category class - publisher_saveCategoryPermissions($categoryObj->getGroups_read(), $categoryObj->categoryid(), 'category_read'); - publisher_saveCategoryPermissions($categoryObj->getGroups_submit(), $categoryObj->categoryid(), 'item_submit'); - publisher_saveCategoryPermissions($categoryObj->getGroups_moderation(), $categoryObj->categoryid(), 'category_moderation'); + publisher_saveCategoryPermissions($grpread, $categoryObj->categoryid(), 'category_read'); + publisher_saveCategoryPermissions($grpsubmit, $categoryObj->categoryid(), 'item_submit'); + publisher_saveCategoryPermissions($grpmoderation, $categoryObj->categoryid(), 'category_moderation'); //Added by fx2024 @@ -180,18 +163,15 @@ $categoryObj = $publisher->getHandler('category')->create(); $categoryObj->setVar('name', $_POST['scname'][$i]); $categoryObj->setVar('parentid', $parentCat); - $categoryObj->setGroups_read($grpread); - $categoryObj->setGroups_submit($grpsubmit); - $categoryObj->setGroups_moderation($grpmoderation); if (!$categoryObj->store()) { redirect_header("javascript:history.go(-1)", 3, _AM_PUBLISHER_SUBCATEGORY_SAVE_ERROR . publisher_formatErrors($categoryObj->getErrors())); exit; } // TODO : put this function in the category class - publisher_saveCategoryPermissions($categoryObj->getGroups_read(), $categoryObj->categoryid(), 'category_read'); - publisher_saveCategoryPermissions($categoryObj->getGroups_submit(), $categoryObj->categoryid(), 'item_submit'); - publisher_saveCategoryPermissions($categoryObj->getGroups_moderation(), $categoryObj->categoryid(), 'category_moderation'); + publisher_saveCategoryPermissions($grpread, $categoryObj->categoryid(), 'category_read'); + publisher_saveCategoryPermissions($grpsubmit, $categoryObj->categoryid(), 'item_submit'); + publisher_saveCategoryPermissions($grpmoderation, $categoryObj->categoryid(), 'category_moderation'); } } //end of fx2024 code @@ -209,7 +189,6 @@ $categoryObj->setVar('name', $_POST['name']); $categoryObj->setVar('description', $_POST['description']); $categoryObj->setVar('weight', $_POST['weight']); - $categoryObj->setGroups_read(isset($_POST['groups_read']) ? $_POST['groups_read'] : array()); if (isset($parentCat)) { $categoryObj->setVar('parentid', $parentCat); } Modified: XoopsModules/publisher/trunk/publisher/archive.php =================================================================== --- XoopsModules/publisher/trunk/publisher/archive.php 2012-12-01 15:55:26 UTC (rev 10296) +++ XoopsModules/publisher/trunk/publisher/archive.php 2012-12-01 22:36:45 UTC (rev 10297) @@ -70,6 +70,7 @@ //Get all articles dates as an array to save memory $items = $publisher->getHandler('item')->getAll($criteria, array('datesub'), false); $itemsCount = count($items); + if (!($itemsCount > 0)) { redirect_header(XOOPS_URL, 2, _MD_PUBLISHER_NO_TOP_PERMISSIONS); exit; @@ -144,6 +145,7 @@ $criteria->add($critdatesub); $criteria->setSort('o.datesub'); $criteria->setOrder('DESC'); + $criteria->setLimit(3000); $storyarray = $itemhandler->getByLink($criteria); //Query Efficiency? $count = count($storyarray); Modified: XoopsModules/publisher/trunk/publisher/category.php =================================================================== --- XoopsModules/publisher/trunk/publisher/category.php 2012-12-01 15:55:26 UTC (rev 10296) +++ XoopsModules/publisher/trunk/publisher/category.php 2012-12-01 22:36:45 UTC (rev 10297) @@ -162,7 +162,7 @@ $category['total'] = $thiscategory_itemcount; if (count($itemsObj) > 0) { - $userids = array(); + /*$userids = array(); if ($itemsObj) { foreach ($itemsObj as $key => $thisitem) { $itemids[] = $thisitem->getVar('itemid'); @@ -170,7 +170,8 @@ } } $member_handler = xoops_gethandler('member'); - $users = $member_handler->getUsers(new Criteria('uid', "(" . implode(',', array_keys($userids)) . ")", "IN"), true); + //$users = $member_handler->getUsers(new Criteria('uid', "(" . implode(',', array_keys($userids)) . ")", "IN"), true); + */ // Adding the items of the selected category for ($i = 0; $i < $totalItemOnPage; $i++) { Modified: XoopsModules/publisher/trunk/publisher/class/category.php =================================================================== --- XoopsModules/publisher/trunk/publisher/class/category.php 2012-12-01 15:55:26 UTC (rev 10296) +++ XoopsModules/publisher/trunk/publisher/class/category.php 2012-12-01 22:36:45 UTC (rev 10297) @@ -32,21 +32,6 @@ /** * @var array */ - public $_groups_read = null; - - /** - * @var array - */ - public $_groups_submit = null; - - /** - * @var array - */ - public $_groups_moderation = null; - - /** - * @var array - */ public $_categoryPath = false; /** @@ -97,16 +82,6 @@ } /** - * Assigns permissions - */ - public function assignOtherProperties() - { - $this->_groups_read = $this->publisher->getHandler('permission')->getGrantedGroupsById('category_read', $this->categoryid()); - $this->_groups_submit = $this->publisher->getHandler('permission')->getGrantedGroupsById('item_submit', $this->categoryid()); - $this->_groups_moderation = $this->publisher->getHandler('permission')->getGrantedGroupsById('category_moderation', $this->categoryid()); - } - - /** * @return bool */ public function checkPermission() @@ -119,7 +94,7 @@ if (is_object($xoopsUser) && $xoopsUser->getVar('uid') == $this->moderator()) { return true; } - $categoriesGranted = $this->publisher->getHandler('permission')->getGrantedItems('category_read'); + $categoriesGranted = $this->publisher->getHandler('permission')->isGranted('category_read'); if (in_array($this->categoryid(), $categoriesGranted)) { $ret = true; } @@ -199,59 +174,26 @@ */ public function getGroups_read() { - if (count($this->_groups_read) < 1) { - $this->assignOtherProperties(); - } - return $this->_groups_read; + return $this->publisher->getHandler('permission')->getGrantedGroupsById('category_read', $this->categoryid()); } /** - * @param array $groups_read - */ - public function setGroups_read($groups_read = array('0')) - { - $this->_groups_read = $groups_read; - } - - /** * @return array|null */ public function getGroups_submit() { - if (count($this->_groups_submit) < 1) { - $this->assignOtherProperties(); - } - return $this->_groups_submit; + return $this->publisher->getHandler('permission')->getGrantedGroupsById('item_submit', $this->categoryid()); } /** - * @param array $groups_submit - */ - public function setGroups_submit($groups_submit = array('0')) - { - $this->_groups_submit = $groups_submit; - } - - /** * @return array|null */ public function getGroups_moderation() { - if (count($this->_groups_moderation) < 1) { - $this->assignOtherProperties(); - } - return $this->_groups_moderation; + return $this->publisher->getHandler('permission')->getGrantedGroupsById('category_moderation', $this->categoryid()); } /** - * @param array $groups_moderation - */ - public function setGroups_moderation($groups_moderation = array('0')) - { - $this->_groups_moderation = $groups_moderation; - } - - /** * @return string */ public function getCategoryUrl() @@ -412,10 +354,12 @@ */ public function &get($id) { - $obj = parent::get($id); - if (is_object($obj)) { - $obj->assignOtherProperties(); + static $cats; + if (isset($cats[$id])) { + return $cats[$id]; } + $obj = parent::get($id); + $cats[$id] = $obj; return $obj; } @@ -489,10 +433,7 @@ { $ret = array(); $theObjects = parent::getObjects($criteria, true); - $publisher_category_group = $this->publisher->getHandler('permission')->getGrantedGroups(); foreach ($theObjects as $theObject) { - $theObject->_groups_read = isset($publisher_category_group['category_read'][$theObject->categoryid()]) ? $publisher_category_group['category_read'][$theObject->categoryid()] : array(); - $theObject->_groups_submit = isset($publisher_category_group['item_submit'][$theObject->categoryid()]) ? $publisher_category_group['item_submit'][$theObject->categoryid()] : array(); if (!$id_as_key) { $ret[] = $theObject; } else { @@ -504,19 +445,6 @@ } /** - * @return array - */ - public function getAllCategoriesObj() - { - static $publisher_allCategoriesObj; - if (!isset($publisher_allCategoriesObj)) { - $publisher_allCategoriesObj = $this->getObjects(null, true); - $publisher_allCategoriesObj[0] = array(); - } - return $publisher_allCategoriesObj; - } - - /** * @param int $limit * @param int $start * @param int $parentid Modified: XoopsModules/publisher/trunk/publisher/class/item.php =================================================================== --- XoopsModules/publisher/trunk/publisher/class/item.php 2012-12-01 15:55:26 UTC (rev 10296) +++ XoopsModules/publisher/trunk/publisher/class/item.php 2012-12-01 22:36:45 UTC (rev 10297) @@ -77,7 +77,6 @@ foreach ($item->vars as $k => $v) { $this->assignVar($k, $v['value']); } - $this->assignOtherProperties(); } } @@ -94,15 +93,6 @@ } /** - * Just like the name says - */ - public function assignOtherProperties() - { - $publisher_allCategoriesObj = $this->publisher->getHandler('category')->getAllCategoriesObj(); - $this->_category = $publisher_allCategoriesObj[$this->getVar('categoryid')]; - } - - /** * @return null|PublisherCategory */ public function category() @@ -426,11 +416,7 @@ { global $xoopsConfig, $xoopsUser; $adminLinks = ''; - $groups = ($xoopsUser) ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS; - $gperm_handler = xoops_gethandler('groupperm'); - $module_id = $this->publisher->getModule()->getVar('mid'); - // Do we have access to the parent category - if (is_object($xoopsUser) && (publisher_userIsAdmin() || publisher_userIsAuthor($this) || $gperm_handler->checkRight('item_submit', $this->categoryid(), $groups, $module_id))) { + if (is_object($xoopsUser) && (publisher_userIsAdmin() || publisher_userIsAuthor($this) || $this->publisher->getHandler('permission')->isGranted('item_submit', $this->categoryid()))) { if (publisher_userIsAdmin() || publisher_userIsAuthor($this) || publisher_userIsModerator($this)) { if ($this->publisher->getConfig('perm_edit') || publisher_userIsModerator($this) || publisher_userIsAdmin()) { // Edit button @@ -890,18 +876,14 @@ */ public function accessGranted() { - global $xoopsUser; if (publisher_userIsAdmin()) { return true; } if ($this->status() != _PUBLISHER_STATUS_PUBLISHED) { return false; } - $gperm_handler = xoops_gethandler('groupperm'); - $groups = $xoopsUser ? $xoopsUser->getGroups() : XOOPS_GROUP_ANONYMOUS; - $module_id = $this->publisher->getModule()->getVar('mid'); // Do we have access to the parent category - if ($gperm_handler->checkRight('category_read', $this->categoryid(), $groups, $module_id)) { + if ($this->publisher->getHandler('permission')->isGranted('category_read', $this->categoryid())) { return true; } return false; @@ -1181,11 +1163,7 @@ $theObjects[$myrow['itemid']] = $item; unset($item); } - // since we need the categoryObj and the items permissions for all these items, let's - // fetch them only once ;-) - $publisher_allCategoriesObj = $this->publisher->getHandler('category')->getAllCategoriesObj(); foreach ($theObjects as $theObject) { - $theObject->_category = isset($publisher_allCategoriesObj[$theObject->getVar('categoryid')]) ? $publisher_allCategoriesObj[$theObject->getVar('categoryid')] : null; if ($id_key == 'none') { $ret[] = $theObject; } elseif ($id_key == 'itemid') { @@ -1621,7 +1599,8 @@ public function getLastPublishedByCat($status = array(_PUBLISHER_STATUS_PUBLISHED)) { $ret = array(); - $cat = array(); + /*$cat = array(); + $sql = "SELECT categoryid, MAX(datesub) as date FROM " . $this->db->prefix('publisher_items') . " WHERE status IN (" . implode(',', $status) . ") GROUP BY categoryid"; $result = $this->db->query($sql); while ($row = $this->db->fetchArray($result)) { @@ -1644,6 +1623,19 @@ $ret[$row['categoryid']] = $item; unset($item); } + */ + $sql = "SELECT mi.categoryid, mi.itemid, mi.title, mi.short_url, mi.uid, mi.datesub"; + $sql .= " FROM (SELECT categoryid, MAX(datesub) AS date FROM " . $this->db->prefix('publisher_items'); + $sql .= " WHERE status IN (" . implode(',', $status) . ")"; + $sql .= " GROUP BY categoryid)mo"; + $sql .= " JOIN " . $this->db->prefix('publisher_items') . " mi ON mi.datesub = mo.date"; + $result = $this->db->query($sql); + while ($row = $this->db->fetchArray($result)) { + $item = new PublisherItem(); + $item->assignVars($row); + $ret[$row['categoryid']] = $item; + unset($item); + } return $ret; } Modified: XoopsModules/publisher/trunk/publisher/class/permission.php =================================================================== --- XoopsModules/publisher/trunk/publisher/class/permission.php 2012-12-01 15:55:26 UTC (rev 10296) +++ XoopsModules/publisher/trunk/publisher/class/permission.php 2012-12-01 22:36:45 UTC (rev 10297) @@ -21,9 +21,7 @@ * @version $Id$ */ defined("XOOPS_ROOT_PATH") or die("XOOPS root path not defined"); - include_once dirname(dirname(__FILE__)) . '/include/common.php'; - class PublisherPermissionHandler extends XoopsObjectHandler { /** @@ -47,8 +45,30 @@ */ public function getGrantedGroupsById($gperm_name, $id) { - $groups = $this->getGrantedGroups($gperm_name); - return isset($groups[$id]) ? $groups[$id] : array(); + static $items; + if (isset($items[$gperm_name][$id])) { + return $items[$gperm_name][$id]; + } + $groups = array(); + $criteria = new CriteriaCompo(); + $criteria->add(new Criteria('gperm_modid', $this->publisher->getModule()->getVar('mid'))); + $criteria->add(new Criteria('gperm_name', $gperm_name)); + $criteria->add(new Criteria('gperm_itemid', $id)); + //Instead of calling groupperm handler and get objects, we will save some memory and do it our way + $db = XoopsDatabaseFactory::getDatabaseConnection(); + $limit = $start = 0; + $sql = 'SELECT gperm_groupid FROM ' . $db->prefix('group_permission'); + if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { + $sql .= ' ' . $criteria->renderWhere(); + $limit = $criteria->getLimit(); + $start = $criteria->getStart(); + } + $result = $db->query($sql, $limit, $start); + while ($myrow = $db->fetchArray($result)) { + $groups[$myrow['gperm_groupid']] = $myrow['gperm_groupid']; + } + $items[$gperm_name][$id] = $groups; + return $groups; } /** @@ -56,27 +76,20 @@ * * @return array */ - public function getGrantedGroups($gperm_name = '') + public function getGrantedGroups($gperm_name) { - static $groups; - static $publisher_all_permissions_fetched; - if ($gperm_name) { - if (isset($groups[$gperm_name])) { - return $groups[$gperm_name]; - } elseif (true === $publisher_all_permissions_fetched) { - //trying to get non existing permission - return array(); - } - } else if (true === $publisher_all_permissions_fetched) { - return $groups; + static $items; + if (isset($items[$gperm_name])) { + return $items[$gperm_name]; } - $publisher_all_permissions_fetched = true; + $groups = array(); $criteria = new CriteriaCompo(); $criteria->add(new Criteria('gperm_modid', $this->publisher->getModule()->getVar('mid'))); + $criteria->add(new Criteria('gperm_name', $gperm_name)); //Instead of calling groupperm handler and get objects, we will save some memory and do it our way $db = XoopsDatabaseFactory::getDatabaseConnection(); $limit = $start = 0; - $sql = 'SELECT * FROM ' . $db->prefix('group_permission'); + $sql = 'SELECT gperm_groupid FROM ' . $db->prefix('group_permission'); if (isset($criteria) && is_subclass_of($criteria, 'criteriaelement')) { $sql .= ' ' . $criteria->renderWhere(); $limit = $criteria->getLimit(); @@ -84,10 +97,11 @@ } $result = $db->query($sql, $limit, $start); while ($myrow = $db->fetchArray($result)) { - $groups[$myrow['gperm_name']][$myrow['gperm_itemid']][] = $myrow['gperm_groupid']; + $groups[$myrow['gperm_groupid']] = $myrow['gperm_groupid']; } + $items[$gperm_name] = $groups; //Return the permission array - return $this->getGrantedGroups($gperm_name); + return $groups; } /** @@ -98,33 +112,36 @@ * * @return array */ - public function getGrantedItems($gperm_name, $id = null) + public function getGrantedItems($gperm_name, $id = 0) { + static $items; + if (isset($items[$gperm_name][$id])) { + return $items[$gperm_name][$id]; + } global $xoopsUser; - static $permissions; - if (!isset($permissions[$gperm_name]) || ($id != null && !isset($permissions[$gperm_name][$id]))) { - //Instead of calling groupperm handler and get objects, we will save some memory and do it our way - $criteria = new CriteriaCompo(new Criteria('gperm_name', $gperm_name)); - $criteria->add(new Criteria('gperm_modid', $this->publisher->getModule()->getVar('mid'))); - //Get user's groups - $groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : array(XOOPS_GROUP_ANONYMOUS); - $criteria2 = new CriteriaCompo(); - foreach ($groups as $gid) { - $criteria2->add(new Criteria('gperm_groupid', $gid), 'OR'); - } - $criteria->add($criteria2); - $db = XoopsDatabaseFactory::getDatabaseConnection(); - $sql = 'SELECT * FROM ' . $db->prefix('group_permission'); - $sql .= ' ' . $criteria->renderWhere(); - $result = $db->query($sql, 0, 0); - $permissions[$gperm_name] = array(); - while ($myrow = $db->fetchArray($result)) { - $permissions[$gperm_name][] = $myrow['gperm_itemid']; - } - $permissions[$gperm_name] = array_unique($permissions[$gperm_name]); + $ret = array(); + //Instead of calling groupperm handler and get objects, we will save some memory and do it our way + $criteria = new CriteriaCompo(new Criteria('gperm_name', $gperm_name)); + $criteria->add(new Criteria('gperm_modid', $this->publisher->getModule()->getVar('mid'))); + if ($id != 0) { + $criteria->add(new Criteria('gperm_itemid', $id)); } - //Return the permission array - return isset($permissions[$gperm_name]) ? $permissions[$gperm_name] : array(); + //Get user's groups + $groups = is_object($xoopsUser) ? $xoopsUser->getGroups() : array(XOOPS_GROUP_ANONYMOUS); + $criteria2 = new CriteriaCompo(); + foreach ($groups as $gid) { + $criteria2->add(new Criteria('gperm_groupid', $gid), 'OR'); + } + $criteria->add($criteria2); + $db = XoopsDatabaseFactory::getDatabaseConnection(); + $sql = 'SELECT gperm_itemid FROM ' . $db->prefix('group_permission'); + $sql .= ' ' . $criteria->renderWhere(); + $result = $db->query($sql, 0, 0); + while ($myrow = $db->fetchArray($result)) { + $ret[$myrow['gperm_itemid']] = $myrow['gperm_itemid']; + } + $items[$gperm_name][$id] = $ret; + return $ret; } /** @@ -135,13 +152,20 @@ */ public function isGranted($gperm_name, $id = null) { - static $permissions; - if ($id == null) return false; - if (!isset($permissions[$gperm_name]) || !isset($permissions[$gperm_name][$id])) { - $userpermissions = in_array($id, $this->getGrantedItems($gperm_name)) ? true : false; - $permissions[$gperm_name][$id] = $userpermissions; + $permissions = $this->getGrantedItems($gperm_name, $id); + if ($id == null) { + if (!empty($permissions)) { + return true; + } else { + return false; + } + } else { + if (!empty($permissions) && isset ($permissions[$id])) { + return true; + } else { + return false; + } } - return $permissions[$gperm_name][$id]; } /** Modified: XoopsModules/publisher/trunk/publisher/include/common.php =================================================================== --- XoopsModules/publisher/trunk/publisher/include/common.php 2012-12-01 15:55:26 UTC (rev 10296) +++ XoopsModules/publisher/trunk/publisher/include/common.php 2012-12-01 22:36:45 UTC (rev 10297) @@ -39,7 +39,7 @@ include_once PUBLISHER_ROOT_PATH . '/class/publisher.php'; include_once PUBLISHER_ROOT_PATH . '/class/request.php'; -$debug = true; +$debug = false; $publisher = PublisherPublisher::getInstance($debug); //This is needed or it will not work in blocks. |