YOURLS Plugin – Popular Links

[NOTE: I've just come back from a 5 month downtime (couldn't afford a server after my last host went out of business), I've since moved my URL shortener off of Yourls, and moved it to Drupal, using the Shurly mod. When YOURLS advances a bit and has better spam filtering, I'll probably move back! Plugin re-posted here for your convenience, but may be out of date. I last used YOURLS 1.51-beta]

 

Short post, but I have thrown together a short plugin for YOURLS (using a lot of the code from http://gkurl.us/popular) that shows the most popular links created in 1,30,365, and 1000 days on a YOURLS site. It’s based on code from Mrtech, but is now in the form of a plugin.

Download here, save as plugin.php and upload to YOURLS_DIRECTORY/user/plugins/popular-links

 

http://pastebin.com/raw.php?i=NmM9D2Pi

Advanced:

At the bottom of the plugin, you can edit the values

> show_top(1000,5);

1000 means the number of days you are selecting from the database

5 means the number of links you are selecting.

Yourls Pages and Mods

I have been using Yourls for over a year now with Gkurl.us, so I thought I would share the code for some of the modifications I have made.

Popular Page

This page shows the top links for day/week/month, and year, etc.

Note that you will need to change the website URL, and possibly the table name (mine is url). Save as popular.php and upload to the pages directory.

<?php

require( ‘includes/load-yourls.php’ );

// TOP TODAY

$urlt2  = ”;

$sqlcode = “SELECT title, timestamp, url, keyword, clicks FROM `url` WHERE timestamp >= SUBDATE(CURDATE(), 1) “;

$sqlcode .= “order by clicks desc limit 1″;

$query = $ydb->get_results($sqlcode);

if ($query) {

foreach( $query as $query_result ) {

$thisURLArray  = parse_url(stripslashes($query_result->url));

$urlt2  .= ‘<a href=”http://gkurl.us/’ . $query_result->keyword .’” target=”blank”>’;

$urlt2  .= str_replace(‘www.’, ”, $query_result->title) . ‘</a> <a href=”http://gkurl.us/’ . $query_result->keyword .’+” target=”blank”>(‘ . $query_result->clicks . ‘) </a> – ‘ .$thisURLArray[host] .’ Submitted ‘ . $query_result->timestamp . ‘<br/>’;

}

}

echo ‘<h3><b>Trending Today:</b></h3><br/> ‘ . $urlt2 . “<br><br>\n\r”;

?>

<?php

// THIS WEEKS TOP LINKS

$urlt2  = ”;

$sqlcode = “SELECT title, timestamp, url, keyword, clicks FROM `url` WHERE timestamp >= SUBDATE(CURDATE(), 7) “;

$sqlcode .= “order by clicks desc limit 10″;

$query = $ydb->get_results($sqlcode);

if ($query) {

foreach( $query as $query_result ) {

$thisURLArray  = parse_url(stripslashes($query_result->url));

$urlt2  .= ‘<a href=”http://gkurl.us/’ . $query_result->keyword .’” target=”blank”>’;

$urlt2  .= str_replace(‘www.’, ”, $query_result->title) . ‘</a> <a href=”http://gkurl.us/’ . $query_result->keyword .’+” target=”blank”>(‘ . $query_result->clicks . ‘) </a> – ‘ .$thisURLArray[host] .’ Submitted ‘ . $query_result->timestamp . ‘<br/>’;

}

}

echo ‘<h3><b>This Week\’s Top links:</b></h3><br/> ‘ . $urlt2 . “<br><br>\n\r”;

?>

<?php

// THIS MONTHS TOP LINKS

$urlt4  = ”;

$sqlmonth = “SELECT title, timestamp, url, keyword, clicks FROM `url` WHERE timestamp >= SUBDATE(CURDATE(), 31) “;

$sqlmonth .= “order by clicks desc limit 10″;

$query = $ydb->get_results($sqlmonth);

if ($query) {

foreach( $query as $query_result ) {

$thisURLArray  = parse_url(stripslashes($query_result->url));

$urlt4  .= ‘<a href=”http://gkurl.us/’ . $query_result->keyword .’” target=”blank”>’;

$urlt4  .= str_replace(‘www.’, ”, $query_result->title) . ‘</a> <a href=”http://gkurl.us/’ . $query_result->keyword .’+” target=”blank”>(‘ . $query_result->clicks . ‘) </a> – ‘ .$thisURLArray[host] .’ Submitted ‘ . $query_result->timestamp . ‘<br/>’;

}

}

echo ‘<h3><b> This Months\’s Top links:</b></h3><br/>’ . $urlt4 . “<br><br>\n\r”;

?>

<?php

//THIS YEAR

$urlt5  = ”;

// This year

$sqlyear = “SELECT title, timestamp, url, keyword, clicks FROM `url` WHERE timestamp >= SUBDATE(CURDATE(), 366) “;

$sqlyear .= “order by clicks desc limit 10″;

$query = $ydb->get_results($sqlyear);

if ($query) {

foreach( $query as $query_result ) {

$thisURLArray  = parse_url(stripslashes($query_result->url));

$urlt5 .= ‘<a href=”http://gkurl.us/’ . $query_result->keyword .’” target=”blank”>’;

$urlt5  .= str_replace(‘www.’, ”, $query_result->title) . ‘</a> <a href=”http://gkurl.us/’ . $query_result->keyword .’+” target=”blank”>(‘ . $query_result->clicks . ‘) </a> – ‘ .$thisURLArray[host] .’ Submitted ‘ . $query_result->timestamp . ‘<br/>’;

}

}

echo ‘<h3><b> All Time Top links:</b></h3><br/> ‘ . $urlt5 . “<br><br>\n\r”;

?>

 

Top Pics Page

This code shows the top images (urls with gif, jpg, jpeg, or png).

Again, you will have to change the URL and table name.

<?php
require( 'includes/load-yourls.php' );
?>
<?php
$urlt5 = ”;
// This month
$sqlyear = "SELECT title, timestamp, url, keyword, clicks FROM `url` WHERE timestamp >= SUBDATE(CURDATE(), 1)  AND url like '%.jpg%' OR  timestamp >= SUBDATE(CURDATE(), 1)  AND url like '%.gif%' OR  timestamp >= SUBDATE(CURDATE(), 1)  AND url like '%.png%'  ";
$sqlyear .= "order by clicks desc limit 5";
$query = $ydb->get_results($sqlyear);
if ($query) {
$i = 0;
foreach( $query as $query_result ) {
$i++;
$thisURLArray = parse_url(stripslashes($query_result->url));
$url = $query_result->url;
$urlt5 .= '<a href="http://gkurl.us/' .$query_result->keyword. '"  title="' . $query_result->title . ' ( ' . $query_result->clicks . ') "><img align="center" src="' .$url. '" width="20%" height="20%" alt="" /></a>';
}
}
echo '<h3><b> Top Pictures Today:</b></h3><br/> ' . $urlt5 . "<br><br>\n\r";
?>
<?php
//THIS week
$urlt5 = '';
// This weel
$sqlyear = "SELECT title, timestamp, url, keyword, clicks FROM `url` WHERE timestamp >= SUBDATE(CURDATE(), 7)  AND url like '%jpg%' OR  timestamp >= SUBDATE(CURDATE(), 7) AND url like '%.gif%' OR  timestamp >= SUBDATE(CURDATE(), 7) AND url like '%.png%'  ";
$sqlyear .= "order by clicks desc limit 5";
$query = $ydb->get_results($sqlyear);
if ($query) {
$i = 0;
foreach( $query as $query_result ) {
$i++;
$thisURLArray = parse_url(stripslashes($query_result->url));
$url = $query_result->url;
$urlt5 .= '<a href="http://gkurl.us/' .$query_result->keyword. '"  title="' . $query_result->title . ' ( ' . $query_result->clicks . ') "><img src="' .$url. '" width="20%" height="20%" alt="" /></a>';
}
}
echo '<h3><b> This Weeks Top Pics:</b></h3><br/> ' . $urlt5 . "<br><br>\n\r";
?>
<?php
//THIS month
$urlt5 = '';
// This month
$sqlyear = "SELECT title, timestamp, url, keyword, clicks FROM `url` WHERE timestamp >= SUBDATE(CURDATE(), 31) AND url like '%.jpg%' OR timestamp >= SUBDATE(CURDATE(), 31) AND url like '%.gif%' OR timestamp >= SUBDATE(CURDATE(), 31) AND  url like '%.png%'   ";
$sqlyear .= "order by clicks desc limit 5";
$query = $ydb->get_results($sqlyear);
if ($query) {
$i = 0;
foreach( $query as $query_result ) {
$i++;
$thisURLArray = parse_url(stripslashes($query_result->url));
$url = $query_result->url;
$urlt5 .= '<a href="http://gkurl.us/' .$query_result->keyword. '"  title="' . $query_result->title . ' ( ' . $query_result->clicks . ') "><img src="' .$url. '" width="20%" height="20%" alt="" /></a>';
}
}
echo '<h3><b> This Months Top Pics:</b></h3><br/> ' . $urlt5 . "<br><br>\n\r";
?>
<?php
//THIS YEAR
$urlt5 = '';
// This year
$sqlyear = "SELECT title, timestamp, url, keyword, clicks FROM `url` WHERE url like '%jpg%' OR url like '%.gif%' OR url like '%.png%' ";
$sqlyear .= "order by clicks desc limit 5";
$query = $ydb->get_results($sqlyear);
if ($query) {
$i = 0;
foreach( $query as $query_result ) {
$i++;
$thisURLArray = parse_url(stripslashes($query_result->url));
$url = $query_result->url;
$urlt5 .= '<a href="http://gkurl.us/' .$query_result->keyword. '"  title="' . $query_result->title . ' ( ' . $query_result->clicks . ') "><img src="' .$url. '" width="20%" height="20%" alt="" /></a>';
}
}
echo '<h3><b> All Time Top Pics:</b></h3><br/> ' . $urlt5 . "<br><br>\n\r";
?>

More Coming Soon

As I make more mods and pages, I’ll add them here.