Page 1 of 1

Show Random Entries in Sidebar

Posted: Mon Jun 04, 2007 10:46 pm
by chrisbra
Hi,
I created the plugin "Random Entries". This Plugin displays a configurable number of random old entries in the sidebar.
You can get it here.

regards,
Christian

Re: Show Random Entries in Sidebar

Posted: Wed Jun 06, 2007 1:18 pm
by garvinhicking
Hi!

Thanks for sharing your plugin!

Here are a few thoughts:

One problem of the plugin is that it queries the 'entries' table directly instead using the s9y function 'Serendipity_fetchEntries'. This should be done to also use privileges on which articles a user is allowed to read/access.

Another thing is that you seed the random generator everytime inside the while() loop. Seeding is only required once.

I would also recommend not doing a SQL query within a loop, because this doesn't scale so well. Instead, fetch the title already with the ID together, if possible. Maybe even using caching.

Actually, the problems you have only occur because SQLite and pgsql do not support the MySQL "ORDER BY RAND()" functionality. If one could use that, it would be much better. Using your approach to fetch everything into PHP scope and sort it there is really bad in terms of performance.

Last but not least, I would recommend patching the functionality into the existing plugin "Recent Entries" that comes with serendipity, because that plugin would only need a "randomize" function and could then offer more flexibility.

Maybe on MySQL systems you can rewrite the code to use 'ORDER BY RAND' and only use your method as a fallback for pgsql/sqlite?

Regards,
Garvin

Re: Show Random Entries in Sidebar

Posted: Thu Jun 07, 2007 2:07 pm
by chrisbra
garvinhicking wrote: Another thing is that you seed the random generator everytime inside the while() loop. Seeding is only required once.
ok was not sure about that. Fixed.
garvinhicking wrote: I would also recommend not doing a SQL query within a loop, because this doesn't scale so well. Instead, fetch the title already with the ID together, if possible. Maybe even using caching.
Done. I am not sure how to implement caching.
garvinhicking wrote: Maybe on MySQL systems you can rewrite the code to use 'ORDER BY RAND' and only use your method as a fallback for pgsql/sqlite?
Done. My Patch for serendipity_plugin_recententries can be found here

Regards,
Christian

Re: Show Random Entries in Sidebar

Posted: Fri Jun 08, 2007 10:53 am
by garvinhicking
Hi!

I just looked up the documentation, and it seems both SQLite and postgresql support the "ORDER BY RANDOM()" facility. :-)

So I reworked your patch a bit and committed it:

http://svn.berlios.de/viewcvs/serendipi ... 17&r2=1716

Maybe you could try if it still works? :)

Regards,
Garvin

Posted: Sun Jun 10, 2007 6:47 pm
by chrisbra
I just looked up the documentation, and it seems both SQLite and postgresql support the "ORDER BY RANDOM()" facility.
Cool, I did not know that. Next time I need to read the SQL-specs more carefully...
So I reworked your patch a bit and committed it:

http://svn.berlios.de/viewcvs/serendipi ... 17&r2=1716

Maybe you could try if it still works?
Ok, there was one simple syntax error:

Code: Select all

--- serendipity_plugin_recententries.php.1.7.web	2007-06-10 18:39:18.762815600 +0200
+++ serendipity_plugin_recententries.php	2007-06-10 18:39:51.395245100 +0200
@@ -160,13 +160,13 @@
             $dateformat = '%A, %B %e %Y';
         }
         if ($randomize)
-            if ($db ==  'mysql' || $db == 'mysqli')) {
+            if (($db ==  'mysql') || ($db == 'mysqli')) {
                 $sql_order = "ORDER BY RAND()";
             } else {
                 // SQLite and PostgreSQL support this, hooray.
                 $sql_order = "ORDER BY RANDOM()";
             }
-        } else {
+        else {
             $sql_order = "ORDER BY timestamp DESC ";
         }
 
Fixing that works for me. But I am neither using postgresql nor sqllite. So I cannot confirm if it works there too (or with which version).

Thanks for you help and your work on serendipity.

regards,
Christian

Posted: Mon Jun 11, 2007 12:15 pm
by garvinhicking
Hi!

Great, thanks for that catch, also just committed. :) And thanks for the kind words, I do my best.

Thanks again for contributing to Serendipity!

Regards,
Garvin

Posted: Sat Jun 16, 2007 3:16 am
by scottwalsh
Hi Garvin,

Thought would have a look at the recent entry plugin with the random feature, and can't seem to find it on Spartacus. Either via the sidebar webpage or the package_sidebar_en.xml file...


Thanks

Posted: Sun Jun 17, 2007 1:43 pm
by garvinhicking
Hi!

The recent-entries plugin is bundled with serendipity, so it can't be found in Spartacus but only in the release tarballs...

HTH,
Garvin