Skip to content

Instantly share code, notes, and snippets.

@joshuaadickerson
Created December 26, 2014 16:12
Show Gist options
  • Save joshuaadickerson/99fee0551787aea1673d to your computer and use it in GitHub Desktop.
Save joshuaadickerson/99fee0551787aea1673d to your computer and use it in GitHub Desktop.
Elkarte Hashtags
<?php
namespace Hashtags\Controller;
class Hashtags
{
protected $hashtag_manager;
public function pre_dispatch()
{
loadTemplate('Hashtags');
loadLanguage('Hashtags');
}
public function action_search()
{
// Get the tag
$tag = $this->getTag();
// We have a tag! Time to search for it
if (!empty($tag))
{
$this->results($tag);
}
// Should we get the trends here?
}
public function results($tag)
{
require_once(SUBS_DIR . '/Boards.subs.php');
$accessible_boards = accessibleBoards();
// if empty($accessible_boards), exit with "no messages" error
$msg_ids = $this->hashtag_manager->getMessageIdsByTag($tag, $accessible_boards, $start, $limit);
$messages = [];
foreach ($msg_ids as $id)
{
$messages[] = basicMessageInfo($id, true);
}
}
public function action_cleanOldTags()
{
// if request == POST ? $hashtag_manager->removeOldTags();
$GLOBALS['context']['sub_template'] = 'cleanOld';
}
public function getTag()
{
if (!empty($_REQUEST['tag']) && $this->hashtag_manager->isTag($_REQUEST['tag']))
{
// Sanitize?
$tag = $_REQUEST['tag'];
}
else
{
$tag = '';
}
return $tag;
}
}
<?php
namespace Hashtags\Model;
class HashtagsManager
{
protected $db;
public function __construct($db)
{
$this->db = $db;
}
public function getTagsFromString($string)
{
// Must be preceeded by whitespace or start of the string
// Must start with #
// Ends with any non-alphanumeric character except dash or underscore
return $tags;
}
public function getMessageIdsByTag($tag, array $accessible_boards, \DateTime $start, $limit = 20, $order_dir = null)
{
// Need to check to see if the user can see the message
/*
* SELECT id_msg
* FROM tags AS t
* INNER JOIN tag_messages AS tp USING (id_tag)
* INNER JOIN messages AS m USING(id_msg)
* INNER JOIN topics AS t USING(id_topic)
* WHERE t.tag = $tag
* AND tp.tag_dtg >= $start
* AND m.approved = 1
* AND t.id_board IN($accessible_boards)
* $order_dir === null ? '' : "ORDER BY id_msg $order_dir"
* LIMIT $limit
*/
return $ids;
}
public function addTag($tag, $msg_id)
{
/*
* INSERT IGNORE INTO tags
* 'tag' = $tag
*
* $tag_id = LAST_INSERT_ID()
*/
return $tag_id;
}
public function getTagIds(array $tags)
{
/*
* SELECT tag_id
* FROM tags
* WHERE tag IN ($tags)
*/
}
public function addTagsToMessage(array $tags, $msg_id, $is_edit = false)
{
$insert = $is_edit ? 'REPLACE' : 'INSERT';
$rows = [];
foreach ($tags as $tag)
{
$rows[] = [
'tag' => $tag,
'msg_id' => $msg_id,
'tag_dtg' => 'NOW()'
];
}
/*
* $insert INTO tag_messages
* tag = $tag,
* msg_id = $msg_id
* tag_dtg = NOW()
*/
}
/**
* Check if a string matches the tag pattern
* @param string $tag
* @return bool
*/
public function isTag($tag)
{
}
public function parseHashtagsInPost($msg_id, $string, $old_string = null)
{
$tags = $this->getTagsFromString($string);
// Old string means it's an edit.
if ($old_string !== null)
{
$old_tags = $this->getTagsFromString($old_string);
// Nothing to do if nothing changed
if ($old_tags === $tags)
{
return;
}
$tags_diff = array_diff($old_tags, $tags);
if (!empty($tags_diff))
{
$this->removeTags($tags_diff);
}
}
$this->addTagsToMessage($tags, $msg_id);
}
public function removeTags($msg_id, array $tags)
{
// Delete them from the post
/*
* DELETE FROM tag_posts
* WHERE id_tag IN($tags)
* AND msg_id = $msg_id
*/
// If there are no posts with this tag, just delete the tag
// Maybe we shouldn't do this? Just run it as a cleanup process
// The tags will most likely be used at other times
// Maybe a scheduled task once a month?
$this->removeOldTags();
}
public function removeOldTags()
{
/*
* DELETE FROM tags
* WHERE id_tag IN(
* SELECT id_tag
* FROM tags
* LEFT JOIN tag_posts USING(id_tag)
* WHERE msg_id IS NULL
* )
*/
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment