joepie91 / pdfy

The platform behind PDFy, a free instant PDF host.
http://pdf.yt/
Other
196 stars 18 forks source link

Add "popular documents" list #8

Open joepie91 opened 10 years ago

joepie91 commented 10 years ago

The frontpage should contain a list of "popular documents"; that is, the documents from the past 72 hours that received the most views.

This would be a naive implementation; eventually, it should be possible to detect surges in traffic for documents uploaded longer than 72 hours ago, that only recently became popular.

ad-m commented 10 years ago
diff --git public_html/modules/index.php public_html/modules/index.php
index d98d283..ac1f47d 100644
--- public_html/modules/index.php
+++ public_html/modules/index.php
@@ -13,7 +13,6 @@

 if(!isset($_APP)) { die("Unauthorized."); }

-$sDocuments = array();

 try
 {
@@ -23,7 +22,8 @@ catch (NotFoundException $e)
 {
    $latest = array();
 }
-   
+
+$sDocuments = array();
 foreach($latest as $document)
 {
    $sDocuments[] = array(
@@ -33,5 +33,27 @@ foreach($latest as $document)
    );
 }

+
+
+$time = round(strtotime($cphp_config->fresh_time)/86400)*86400; # Approximation to day for database optimalization 
+try
+{
+   $pop = Document::CreateFromQuery("SELECT * FROM documents WHERE `Public` = 1 AND Uploaded >= :Time ORDER BY `Views` DESC, `Id` DESC LIMIT 6",array('Time'=>$time));
+}
+catch (NotFoundException $e)
+{
+   $pop = array();
+}
+
+$pDocuments = array();
+foreach($pop as $document)
+{
+   $pDocuments[] = array(
+       "slug" => $document->sSlugId,
+       "name" => $document->sOriginalFilename,
+       "views" => $document->sViews
+   );
+}
+
 $sPageTitle = "Upload";
-$sPageContents = NewTemplater::Render("index", $locale->strings, array("documents" => $sDocuments));
+$sPageContents = NewTemplater::Render("index", $locale->strings, array("documents" => $sDocuments,"popular"=>$pDocuments));
diff --git public_html/templates/index.html public_html/templates/index.html
index ac215ae..d5d48cd 100644
--- public_html/templates/index.html
+++ public_html/templates/index.html
@@ -79,6 +79,17 @@
    <div class="clear"></div>
 </div>

+<div class="latest">
+   <h2>Fresh popular documents</h2>
+   {%foreach document in popular}
+       <a href="/d/{%?document[slug]}" class="document">
+           <img src="/static/thumbs/{%?document[slug]}.png" title="{%?document[name]}">
+       </a>
+   {%/foreach}
+   <div class="clear"></div>
+</div>
+
+
 <div class="footer">
    The source code for PDFy is available at <a href="https://github.com/joepie91/pdfy">https://github.com/joepie91/pdfy</a>. You may also file bug tickets there, if you encounter problems.
 </div>

You need also add new option in config.json:

"fresh_time": "-7 days",

It is required to set how long documents are "fresh".

Edit: In above code I added a little hacks to made query cachable. It should give bring closer a "full" day, so the time variable switching rarely, so cache can work. Without that every requests the timestampt switches and never cache (built-in MySQL also) work.