Show Random Entries in Sidebar

Creating and modifying plugins.
Post Reply
chrisbra
Regular
Posts: 63
Joined: Wed Jun 08, 2005 4:12 pm

Show Random Entries in Sidebar

Post 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
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Show Random Entries in Sidebar

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
chrisbra
Regular
Posts: 63
Joined: Wed Jun 08, 2005 4:12 pm

Re: Show Random Entries in Sidebar

Post 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
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Show Random Entries in Sidebar

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
chrisbra
Regular
Posts: 63
Joined: Wed Jun 08, 2005 4:12 pm

Post 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
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
scottwalsh
Regular
Posts: 17
Joined: Sun May 20, 2007 9:05 am
Contact:

Post 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
------

Scott Walsh

http://zone3.net.nz/
profile, journal of travels and photos
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Post 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
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
Post Reply