Skip to content

Commit

Permalink
alternative random function to fix wallabag#1082
Browse files Browse the repository at this point in the history
  • Loading branch information
tcitworld committed Feb 15, 2015
1 parent d523c99 commit 6b60741
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
8 changes: 8 additions & 0 deletions inc/poche/Database.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,14 @@ public function getEntriesByViewCount($view, $user_id, $tag_id = 0)

return $count;
}
public function getRandomId($row, $user_id) {
$sql = "SELECT id FROM entries WHERE user_id=? LIMIT 1 OFFSET ? ";
$params = array($user_id, $row);
$query = $this->executeQuery($sql, $params);

return $query->fetchAll();
}


public function updateContent($id, $content, $user_id)
{
Expand Down
11 changes: 5 additions & 6 deletions inc/poche/Poche.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -393,13 +393,12 @@ public function action($action, Url $url, $id = 0, $import = FALSE, $autoclose =

/* For some unknown reason I can't get displayView() to work here (it redirects to home view afterwards). So here's a dirty fix which redirects directly to URL */
case 'random':
$id = 0;
while ($this->store->retrieveOneById($id,$this->user->getId()) == null) {
$count = $this->store->getEntriesByViewCount($view, $this->user->getId());
$id = rand(1,$count);
}
$count = $this->store->getEntriesByViewCount($view, $this->user->getId());
$id_query = $this->store->getRandomId(rand(1,$count)-1, $this->user->getId());
$id = $id_query[0];
Tools::logm('get a random article');
Tools::redirect('?view=view&id=' . $id);
Tools::redirect('?view=view&id=' . $id[0]);

//$this->displayView('view', $id);
break;
default:
Expand Down

0 comments on commit 6b60741

Please sign in to comment.