Skip to content

Commit

Permalink
More refatoring:
Browse files Browse the repository at this point in the history
Move tags out of Jonah and into Content.
Fix permissions checks.
CS/General cleanup.
  • Loading branch information
mrubinsk committed Dec 21, 2016
1 parent d3ec460 commit 5910dff
Show file tree
Hide file tree
Showing 16 changed files with 339 additions and 473 deletions.
3 changes: 2 additions & 1 deletion jonah/delivery/rss.php
Expand Up @@ -24,7 +24,8 @@
'limit' => 10,
);
if ($tag_id = Horde_Util::getFormData('tag_id')) {
$criteria['tags'] = $driver->getTagNames(explode(':', $tag_id));
$criteria['tags'] = $injector->getInstance('Jonah_Tagger')
->getTagNames(explode(':', $tag_id));
}
if ($tag = Horde_Util::getFormData('tag')) {
$criteria['tags'] = explode(':', $tag);
Expand Down
58 changes: 39 additions & 19 deletions jonah/lib/Api.php
Expand Up @@ -107,7 +107,7 @@ public function publish($channel_id, $story)
$driver = $GLOBALS['injector']->getInstance('Jonah_Driver');
$channel = $driver->getChannel($channel_id);
/* Check permissions. */
if (!Jonah::checkPermissions('channels', Horde_Perms::EDIT, $channel_id)) {
if (!Jonah::checkPermissions('channels', Horde_Perms::EDIT, array($channel_id))) {
throw new Horde_Exception_PermissionDenied(_("You are not authorised for this action."));
}
$story['author'] = $GLOBALS['registry']->getAuth();
Expand Down Expand Up @@ -151,15 +151,18 @@ public function hasComments()
* of resources that are linked to that tag.
*
* @param array $tags An optional array of tag_ids. If omitted, all tags
* will be included.
* will be included.@deprecated and currently ignored.
*
* @param array $channel_id An optional array of channel_ids.
* @param array $channel_id An optional array of channel_ids. @todo - only
* the first requested channel is honored.
*
* @return array An array containing tag_name, and total
*/
public function listTagInfo($tags = array(), $channel_id = null)
{
return $GLOBALS['injector']->getInstance('Jonah_Driver')->listTagInfo($tags, $channel_id);
return $GLOBALS['injector']
->getInstance('Jonah_Driver')
->listTagInfo(current($channel_id));
}

/**
Expand Down Expand Up @@ -188,7 +191,7 @@ public function getTagIds($names)
* <pre>
* max The maximum number of stories to return.
* from The number of the story to start with.
* channel_id An array of channel_ids to limit the search to.
* channel_id (integer) A channel_id to restrict to.
* order How to order the results (a Jonah::ORDER_* constant)
* </pre>
* @param boolean $raw Return the raw story data?
Expand All @@ -205,42 +208,59 @@ public function getTagIds($names)
*/
public function searchTags($names, $filter = array(), $raw = false)
{
global $registry;
global $registry, $injector;

// @TODO: Refactor when moving tag to content_tagger
$filter = new Horde_Support_Array($filter);
$results = $GLOBALS['injector']
$criteria = array(
'tags' => $names,
'startnumber' => $filter->from,
'limit' => $filter->max,
'channel_id' => $filter->channel_id
);
$results = $injector
->getInstance('Jonah_Driver')
->searchTags($names, $filter->max, $filter->from, $filter->channel_id, $filter->order);

->getStories($criteria, $filter->order);
$return = array();

if ($raw) {
// Requesting the raw story information as returned from searchTags,
// but add some additional information that external apps might
// find useful.
$comments = $GLOBALS['conf']['comments']['allow'] && $registry->hasMethod('forums/numMessages');
$comments = $GLOBALS['conf']['comments']['allow']
&& $registry->hasMethod('forums/numMessages');

foreach ($results as $story) {
if (empty($story['body_type']) || $story['body_type'] == 'text') {
$story['body_html'] = $GLOBALS['injector']->getInstance('Horde_Core_Factory_TextFilter')->filter($story['body'], 'text2html', array('parselevel' => Horde_Text_Filter_Text2html::MICRO));
$story['body_html'] = $injector
->getInstance('Horde_Core_Factory_TextFilter')
->filter(
$story['body'],
'text2html',
array('parselevel' => Horde_Text_Filter_Text2html::MICRO)
);
} else {
$story['body_html'] = $story['body'];
}

if ($comments) {
$story['num_comments'] = $registry->call('forums/numMessages',
array($story['id'],
$registry->getApp()));
$story['num_comments'] = $registry->call(
'forums/numMessages',
array($story['id'],
$registry->getApp())
);
}

$return[$story['id']] = $story;
}
} else {
foreach($results as $story) {
if (!empty($story)) {
$return[] = array('title' => $story['title'],
'desc' => $story['desc'],
'view_url' => $story['link'],
'app' => 'jonah');
$return[] = array(
'title' => $story['title'],
'desc' => $story['desc'],
'view_url' => $story['link'],
'app' => 'jonah'
);
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions jonah/lib/Application.php
Expand Up @@ -103,7 +103,7 @@ public function menu($menu)
if ($channel_id = Horde_Util::getFormData('channel_id')) {
$news = $GLOBALS['injector']->getInstance('Jonah_Driver');
$channel = $news->getChannel($channel_id);
if (Jonah::checkPermissions('channels', Horde_Perms::EDIT, $channel_id)) {
if (Jonah::checkPermissions('channels', Horde_Perms::EDIT, array($channel_id))) {
$menu->addArray(array(
'icon' => 'new.png',
'text' => _("_New Story"),
Expand All @@ -120,8 +120,7 @@ public function menu($menu)
public function topbarCreate(Horde_Tree_Renderer_Base $tree, $parent = null,
array $params = array())
{
if (!Jonah::checkPermissions('jonah:news', Horde_Perms::EDIT) ||
!in_array('internal', $GLOBALS['conf']['news']['enable'])) {
if (!Jonah::checkPermissions('jonah:news', Horde_Perms::EDIT)) {
return;
}

Expand Down
9 changes: 6 additions & 3 deletions jonah/lib/Block/Cloud.php
Expand Up @@ -28,19 +28,22 @@ protected function _params()
'results_url' => array(
'name' => _("Results URL"),
'type' => 'text',
'default' => Horde::url('stories/results.php?tag_id=@id@'),
'default' => Horde::url('stories/results.php?tag=@tag@'),
),
);
}

protected function _content()
{
/* Get the tags */
$tags = $GLOBALS['injector']->getInstance('Jonah_Driver')->listTagInfo();
$tags = $GLOBALS['injector']
->getInstance('Jonah_Driver')
->listTagInfo();

if (count($tags)) {
$cloud = new Horde_Core_Ui_TagCloud();
foreach ($tags as $id => $tag) {
$cloud->addElement($tag['tag_name'], str_replace(array('@id@', '@tag@'), array($id, $tag['tag_name']), $this->_params['results_url']), $tag['total']);
$cloud->addElement($tag['tag_name'], str_replace(array('%40id%40', '%40tag%40', '@id@', '@tag@'), array($id, $tag['tag_name']), $this->_params['results_url']), $tag['total']);
}
$html = $cloud->buildHTML();
} else {
Expand Down
151 changes: 70 additions & 81 deletions jonah/lib/Driver.php
Expand Up @@ -45,6 +45,17 @@ public function deleteChannel($info)
return $this->_deleteChannel($info['channel_id']);
}

/**
* Get a list of stored channels.
*
* @return array An array of channel hashes.
* @throws Jonah_Exception
*/
public function getChannels()
{
return $this->_getChannels();
}

/**
* Fetches the requested channel, while actually passing on the request to
* the backend _getChannel() function to do the real work.
Expand Down Expand Up @@ -83,48 +94,29 @@ public function getChannel($channel_id)
*
* @param integer $criteria An associative array of attributes on which
* the resulting stories should be filtered.
* Examples:
* 'channel' => string Channel slug
* 'channel_id' => int Channel ID
* 'author' => string Story author
* 'updated-min' => Horde_Date Only return
* stories updated
* on or after this
* date
* 'updated-max' => Horde_Date Only return
* stories updated
* on or before this
* date
* 'published-min' => Horde_Date Only return
* stories
* published on or
* after this date
* 'published-max' => Horde_Date Only return
* stories
* published on or
* before date
* 'tags' => array Array of tag names ANY of
* which may match the story to
* be included
* 'alltags' => array Array of tag names ALL of
* which must be associated
* with the story to be
* included
* 'keywords' => array Array of strings ALL of
* which matching must
* include
* 'published' => boolean Whether to return only
* published stories;
* null will return both
* published and
* unpublished
* 'startnumber' => int Story number to begin
* 'endnumber' => int Story number to end
* 'limit' => int Max number of stories
*
* @param integer $order How to order the results for internal
* channels. Possible values are the
* Jonah::ORDER_* constants.
* Examples:
* 'channel' => (string) Channel slug
* 'channel_id' => (integer) Channel ID (Either an id or slug is required)
* 'author' => (string) Story author
* 'updated-min' => (Horde_Date) Only return stories updated on or
* after this date
* 'updated-max' => (Horde_Date) Only return stories updatedon or
* before this date
* 'published-min' => (Horde_Date) Only return stories published on or
* after this date
* 'published-max' => (Horde_Date) Only return stories published on or
* before date
* 'tags' => (array) Tag names that must match to be included
* 'keywords' => (array) Strings which must match to be included
* 'published' => (boolean) Whether to return only published stories:
* Possible values:
* null return both
* 'published' returns publised
* 'unpublished' returns unpublished
* 'startnumber' => (integer) Story number to start at
* 'limit' => (integer) Max number of stories
* @param integer $order How to order the results. A Jonah::ORDER_*
* constant.
*
* @return array The specified number (or less, if there are fewer) of
* stories from the given channel.
Expand All @@ -137,35 +129,37 @@ public function getStories($criteria, $order = Jonah::ORDER_PUBLISHED)
$criteria['channel_id'] = $this->getIdBySlug($criteria['channel']);
}

if (empty($criteria['channel_id'])) {
throw InvalidArgumentException('Missing expected channel_id parameter.');
}

// Validate that we have proper Horde_Date objects
if (isset($criteria['updated-min'])) {
if (!is_a($criteria['updated-min'], 'Horde_Date')) {
throw new InvalidArgumentException("Invalid date object provided for update start date.");
throw new InvalidArgumentException('Invalid date object provided for update start date.');
}
}
if (isset($criteria['updated-max'])) {
if (!is_a($criteria['updated-max'], 'Horde_Date')) {
throw new InvalidArgumentException("Invalid date object provided for update end date.");
throw new InvalidArgumentException('Invalid date object provided for update end date.');
}
}
if (isset($criteria['published-min'])) {
if (!is_a($criteria['published-min'], 'Horde_Date')) {
throw new InvalidArgumentException("Invalid date object provided for published start date.");
throw new InvalidArgumentException('Invalid date object provided for published start date.');
}
}
if (isset($criteria['published-max'])) {
if (!is_a($criteria['published-max'], 'Horde_Date')) {
throw new InvalidArgumentException("Invalid date object provided for published end date.");
throw new InvalidArgumentException('Invalid date object provided for published end date.');
}
}

// Collect the applicable tag IDs
$criteria['tagIDs'] = array();
if (isset($criteria['tags'])) {
$criteria['tagIDs'] = array_merge($criteria['tagIDs'], $this->getTagIds($criteria['tags']));
}
if (isset($criteria['alltags'])) {
$criteria['tagIDs'] = array_merge($criteria['tagIDs'], $this->getTagIds($criteria['alltags']));
if (!empty($criteria['tags'])) {
$criteria['ids'] = $GLOBALS['injector']
->getInstance('Jonah_Tagger')
->search($criteria['tags'], array('channel_ids' => $criteria['channel_id']));
unset($criteria['tags']);
}

return $this->_getStories($criteria, $order);
Expand Down Expand Up @@ -226,10 +220,7 @@ public function getStoryLink($channel, $story)
} else {
$url = Horde::url('stories/view.php', true, -1)->add(array('channel_id' => '%c', 'id' => '%s'))->setRaw(false);
}
Horde::debug($channel);
Horde::debug(str_replace(array('%25c', '%25s', '%c', '%s'),
array('%c', '%s', $channel['channel_id'], $story['id']),
$url));

return new Horde_Url(str_replace(array('%25c', '%25s', '%c', '%s'),
array('%c', '%s', $channel['channel_id'], $story['id']),
$url));
Expand Down Expand Up @@ -440,39 +431,37 @@ protected function getStoryAsMessage($story)
}

/**
* Stubs for the tag functions. If supported by the backend, these need
* to be implemented in the concrete Jonah_Driver_* class.
* Return a list of story_ids contained in the specified
* channel.
*
* @param integer $channel_id The channel_id
*
* @TODO: These will be moved to a new Tagger class and will interface
* with the Content_Tagger api.
* @return array An array of story_ids.
*/
function writeTags($resource_id, $channel_id, $tags)
{
return PEAR::raiseError(_("Tag support not enabled in backend."));
}

function readTags($resource_id)
public function getStoryIdsByChannel($channel_id)
{
return PEAR::raiseError(_("Tag support not enabled in backend."));
return $this->_getStoryIdsByChannel($channel_id);
}

function listTagInfo($tags = array(), $channel_id = null)
public function listTagInfo($channel_id = null)
{
return PEAR::raiseError(_("Tag support not enabled in backend."));
}

function searchTagsById($ids, $max = 10, $from = 0, $channel_id = array(),
$order = Jonah::ORDER_PUBLISHED)
{
return PEAR::raiseError(_("Tag support not enabled in backend."));
}
global $injector;
var_dump($channel_id);
// All channels
if (!isset($channel_id)) {
return $injector
->getInstance('Jonah_Tagger')
->getCloud(null, null);
}

function getTagNames($ids)
{
return PEAR::raiseError(_("Tag support not enabled in backend."));
// Limit by channel_id
$story_ids = $this->_getStoryIdsByChannel($channel_id);
return $injector
->getInstance('Jonah_Tagger')
->getTagCountsByObjects($story_ids, Jonah_Tagger::TYPE_STORY);
}

function getIdBySlug($channel)
public function getIdBySlug($channel)
{
return $this->_getIdBySlug($channel);
}
Expand Down

0 comments on commit 5910dff

Please sign in to comment.