Give users more control over URL shortening

Users and administrators can set how long an URL can be before it's
shortened, and how long a notice can be before all its URLs are
shortened. They can also turn off shortening altogether.

Squashed commit of the following:

commit d136b39011
Author: Evan Prodromou <evan@status.net>
Date:   Mon Apr 26 02:39:00 2010 -0400

    use site and user settings to determine when to shorten URLs

commit 1e1c851ff3
Author: Evan Prodromou <evan@status.net>
Date:   Mon Apr 26 02:38:40 2010 -0400

    add a method to force shortening URLs

commit 4d29ca0b91
Author: Evan Prodromou <evan@status.net>
Date:   Mon Apr 26 02:37:41 2010 -0400

    static method for getting best URL shortening service

commit a9c6a3bace
Author: Evan Prodromou <evan@status.net>
Date:   Mon Apr 26 02:37:11 2010 -0400

    allow 0 in numeric entries in othersettings

commit 767ff2f7ec
Author: Evan Prodromou <evan@status.net>
Date:   Mon Apr 26 02:36:46 2010 -0400

    allow 0 or blank string in inputs

commit 1e21af42a6
Author: Evan Prodromou <evan@status.net>
Date:   Mon Apr 26 02:01:11 2010 -0400

    add more URL-shortening options to othersettings

commit 869a6be0f5
Author: Evan Prodromou <evan@status.net>
Date:   Sat Apr 24 14:22:51 2010 -0400

    move url shortener superclass to lib from plugin

commit 9c0c9863d5
Author: Evan Prodromou <evan@status.net>
Date:   Sat Apr 24 14:20:28 2010 -0400

    documentation and whitespace on UrlShortenerPlugin

commit 7a1dd5798f
Author: Evan Prodromou <evan@status.net>
Date:   Sat Apr 24 14:05:46 2010 -0400

    add defaults for URL shortening

commit d259c37ad2
Author: Evan Prodromou <evan@status.net>
Date:   Sat Apr 24 13:40:10 2010 -0400

    Add User_urlshortener_prefs

    Add a table for URL shortener prefs, a corresponding class, and the
    correct mumbo-jumbo in statusnet.ini to make everything work.
This commit is contained in:
Evan Prodromou 2010-04-26 02:40:36 -04:00
parent 727ea5a516
commit 14adb7cc41
16 changed files with 435 additions and 129 deletions

20
README
View File

@ -843,9 +843,7 @@ sslserver: use an alternate server name for SSL URLs, like
parameters correctly so that both the SSL server and the parameters correctly so that both the SSL server and the
"normal" server can access the session cookie and "normal" server can access the session cookie and
preferably other cookies as well. preferably other cookies as well.
shorturllength: Length of URL at which URLs in a message exceeding 140 shorturllength: ignored. See 'url' section below.
characters will be sent to the user's chosen
shortening service.
dupelimit: minimum time allowed for one person to say the same thing dupelimit: minimum time allowed for one person to say the same thing
twice. Default 60s. Anything lower is considered a user twice. Default 60s. Anything lower is considered a user
or UI error. or UI error.
@ -1468,6 +1466,22 @@ disallow: Array of (virtual) directories to disallow. Default is 'main',
'search', 'message', 'settings', 'admin'. Ignored when site 'search', 'message', 'settings', 'admin'. Ignored when site
is private, in which case the entire site ('/') is disallowed. is private, in which case the entire site ('/') is disallowed.
url
---
Everybody loves URL shorteners. These are some options for fine-tuning
how and when the server shortens URLs.
shortener: URL shortening service to use by default. Users can override
individually. 'ur1.ca' by default.
maxlength: If an URL is strictly longer than this limit, it will be
shortened. Note that the URL shortener service may return an
URL longer than this limit. Defaults to 25. Users can
override. If set to 0, all URLs will be shortened.
maxnoticelength: If a notice is strictly longer than this limit, all
URLs in the notice will be shortened. Users can override.
-1 means the text limit for notices.
Plugins Plugins
======= =======

View File

@ -98,8 +98,10 @@ class OthersettingsAction extends AccountSettingsAction
$this->hidden('token', common_session_token()); $this->hidden('token', common_session_token());
$this->elementStart('ul', 'form_data'); $this->elementStart('ul', 'form_data');
$shorteners = array(); $shorteners = array(_('[none]') => array('freeService' => false));
Event::handle('GetUrlShorteners', array(&$shorteners)); Event::handle('GetUrlShorteners', array(&$shorteners));
$services = array(); $services = array();
foreach($shorteners as $name=>$value) foreach($shorteners as $name=>$value)
{ {
@ -119,8 +121,22 @@ class OthersettingsAction extends AccountSettingsAction
$this->elementEnd('li'); $this->elementEnd('li');
} }
$this->elementStart('li'); $this->elementStart('li');
$this->input('maxurllength',
_('URL longer than'),
(!is_null($this->arg('maxurllength'))) ?
$this->arg('maxurllength') : User_urlshortener_prefs::maxUrlLength($user),
_('URLs longer than this will be shortened.'));
$this->elementEnd('li');
$this->elementStart('li');
$this->input('maxnoticelength',
_('Text longer than'),
(!is_null($this->arg('maxnoticelength'))) ?
$this->arg('maxnoticelength') : User_urlshortener_prefs::maxNoticeLength($user),
_('URLs in notices longer than this will be shortened.'));
$this->elementEnd('li');
$this->elementStart('li');
$this->checkbox('viewdesigns', _('View profile designs'), $this->checkbox('viewdesigns', _('View profile designs'),
$user->viewdesigns, _('Show or hide profile designs.')); - $user->viewdesigns, _('Show or hide profile designs.'));
$this->elementEnd('li'); $this->elementEnd('li');
$this->elementEnd('ul'); $this->elementEnd('ul');
$this->submit('save', _('Save')); $this->submit('save', _('Save'));
@ -156,6 +172,18 @@ class OthersettingsAction extends AccountSettingsAction
$viewdesigns = $this->boolean('viewdesigns'); $viewdesigns = $this->boolean('viewdesigns');
$maxurllength = $this->trimmed('maxurllength');
if (!Validate::number($maxurllength, array('min' => 0))) {
throw new ClientException(_('Invalid number for max url length.'));
}
$maxnoticelength = $this->trimmed('maxnoticelength');
if (!Validate::number($maxnoticelength, array('min' => 0))) {
throw new ClientException(_('Invalid number for max notice length.'));
}
$user = common_current_user(); $user = common_current_user();
assert(!is_null($user)); // should already be checked assert(!is_null($user)); // should already be checked
@ -175,6 +203,32 @@ class OthersettingsAction extends AccountSettingsAction
return; return;
} }
$prefs = User_urlshortener_prefs::getPrefs($user);
$orig = null;
if (empty($prefs)) {
$prefs = new User_urlshortener_prefs();
$prefs->user_id = $user->id;
$prefs->created = common_sql_now();
} else {
$orig = clone($prefs);
}
$prefs->urlshorteningservice = $urlshorteningservice;
$prefs->maxurllength = $maxurllength;
$prefs->maxnoticelength = $maxnoticelength;
if (!empty($orig)) {
$result = $prefs->update($orig);
} else {
$result = $prefs->insert();
}
if (!$result) {
throw new ServerException(_('Error saving user URL shortening preferences.'));
}
$user->query('COMMIT'); $user->query('COMMIT');
$this->showForm(_('Preferences saved.'), true); $this->showForm(_('Preferences saved.'), true);

View File

@ -176,22 +176,52 @@ class File_redirection extends Memcached_DataObject
* @param string $long_url * @param string $long_url
* @return string * @return string
*/ */
function makeShort($long_url) {
function makeShort($long_url)
{
$canon = File_redirection::_canonUrl($long_url); $canon = File_redirection::_canonUrl($long_url);
$short_url = File_redirection::_userMakeShort($canon); $short_url = File_redirection::_userMakeShort($canon);
// Did we get one? Is it shorter? // Did we get one? Is it shorter?
if (!empty($short_url) && mb_strlen($short_url) < mb_strlen($long_url)) {
if (!empty($short_url)) {
return $short_url; return $short_url;
} else { } else {
return $long_url; return $long_url;
} }
} }
function _userMakeShort($long_url) { /**
$short_url = common_shorten_url($long_url); * Shorten a URL with the current user's configured shortening
* options, if applicable.
*
* If it cannot be shortened or the "short" URL is longer than the
* original, the original is returned.
*
* If the referenced item has not been seen before, embedding data
* may be saved.
*
* @param string $long_url
* @return string
*/
function forceShort($long_url)
{
$canon = File_redirection::_canonUrl($long_url);
$short_url = File_redirection::_userMakeShort($canon, true);
// Did we get one? Is it shorter?
if (!empty($short_url)) {
return $short_url;
} else {
return $long_url;
}
}
function _userMakeShort($long_url, $force = false) {
$short_url = common_shorten_url($long_url, $force);
if (!empty($short_url) && $short_url != $long_url) { if (!empty($short_url) && $short_url != $long_url) {
$short_url = (string)$short_url; $short_url = (string)$short_url;
// store it // store it

View File

@ -0,0 +1,105 @@
<?php
/*
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2010, StatusNet, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1);
}
class User_urlshortener_prefs extends Memcached_DataObject
{
###START_AUTOCODE
/* the code below is auto generated do not remove the above tag */
public $__table = 'user_urlshortener_prefs'; // table name
public $user_id; // int(4) primary_key not_null
public $urlshorteningservice; // varchar(50) default_ur1.ca
public $maxurllength; // int(4) not_null
public $maxnoticelength; // int(4) not_null
public $created; // datetime not_null default_0000-00-00%2000%3A00%3A00
public $modified; // timestamp not_null default_CURRENT_TIMESTAMP
/* Static get */
function staticGet($k,$v=NULL) { return Memcached_DataObject::staticGet('User_urlshortener_prefs',$k,$v); }
/* the code above is auto generated do not remove the tag below */
###END_AUTOCODE
function sequenceKey()
{
return array(false, false, false);
}
static function maxUrlLength($user)
{
$def = common_config('url', 'maxlength');
$prefs = self::getPrefs($user);
if (empty($prefs)) {
return $def;
} else {
return $prefs->maxurllength;
}
}
static function maxNoticeLength($user)
{
$def = common_config('url', 'maxnoticelength');
if ($def == -1) {
$def = Notice::maxContent();
}
$prefs = self::getPrefs($user);
if (empty($prefs)) {
return $def;
} else {
return $prefs->maxnoticelength;
}
}
static function urlShorteningService($user)
{
$def = common_config('url', 'shortener');
$prefs = self::getPrefs($user);
if (empty($prefs)) {
if (!empty($user)) {
return $user->urlshorteningservice;
} else {
return $def;
}
} else {
return $prefs->urlshorteningservice;
}
}
static function getPrefs($user)
{
if (empty($user)) {
return null;
}
$prefs = User_urlshortener_prefs::staticGet('user_id', $user->id);
return $prefs;
}
}

View File

@ -649,3 +649,14 @@ user_id = K
transport = K transport = K
transport = U transport = U
screenname = U screenname = U
[user_urlshortener_prefs]
user_id = 129
urlshorteningservice = 2
maxurllength = 129
maxnoticelength = 129
created = 142
modified = 384
[user_urlshortener_prefs__keys]
user_id = K

View File

@ -665,3 +665,16 @@ create table local_group (
modified timestamp comment 'date this record was modified' modified timestamp comment 'date this record was modified'
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin; ) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;
create table user_urlshortener_prefs (
user_id integer not null comment 'user' references user (id),
urlshorteningservice varchar(50) default 'ur1.ca' comment 'service to use for auto-shortening URLs',
maxurllength integer not null comment 'urls greater than this length will be shortened, 0 = always, null = never',
maxnoticelength integer not null comment 'notices with content greater than this value will have all urls shortened, 0 = always, null = never',
created datetime not null comment 'date this record was created',
modified timestamp comment 'date this record was modified',
constraint primary key (user_id)
) ENGINE=InnoDB CHARACTER SET utf8 COLLATE utf8_bin;

View File

@ -304,4 +304,8 @@ $default =
array('subscribers' => true, array('subscribers' => true,
'members' => true, 'members' => true,
'peopletag' => true), 'peopletag' => true),
'url' =>
array('shortener' => 'ur1.ca',
'maxlength' => 25,
'maxnoticelength' => -1)
); );

View File

@ -176,7 +176,7 @@ class HTMLOutputter extends XMLOutputter
$attrs = array('name' => $id, $attrs = array('name' => $id,
'type' => 'text', 'type' => 'text',
'id' => $id); 'id' => $id);
if ($value) { if (!is_null($value)) { // value can be 0 or ''
$attrs['value'] = $value; $attrs['value'] = $value;
} }
$this->element('input', $attrs); $this->element('input', $attrs);

155
lib/urlshortenerplugin.php Normal file
View File

@ -0,0 +1,155 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Superclass for plugins that do URL shortening
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Plugin
* @package StatusNet
* @author Craig Andrews <candrews@integralblue.com>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1);
}
/**
* Superclass for plugins that do URL shortening
*
* @category Plugin
* @package StatusNet
* @author Craig Andrews <candrews@integralblue.com>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
abstract class UrlShortenerPlugin extends Plugin
{
public $shortenerName;
public $freeService = false;
// Url Shortener plugins should implement some (or all)
// of these methods
/**
* Make an URL shorter.
*
* @param string $url URL to shorten
*
* @return string shortened version of the url, or null on failure
*/
protected abstract function shorten($url);
/**
* Utility to get the data at an URL
*
* @param string $url URL to fetch
*
* @return string response body
*
* @todo rename to code-standard camelCase httpGet()
*/
protected function http_get($url)
{
$request = HTTPClient::start();
$response = $request->get($url);
return $response->getBody();
}
/**
* Utility to post a request and get a response URL
*
* @param string $url URL to fetch
* @param array $data post parameters
*
* @return string response body
*
* @todo rename to code-standard httpPost()
*/
protected function http_post($url, $data)
{
$request = HTTPClient::start();
$response = $request->post($url, null, $data);
return $response->getBody();
}
// Hook handlers
/**
* Called when all plugins have been initialized
*
* @return boolean hook value
*/
function onInitializePlugin()
{
if (!isset($this->shortenerName)) {
throw new Exception("must specify a shortenerName");
}
return true;
}
/**
* Called when a showing the URL shortener drop-down box
*
* Properties of the shortening service currently only
* include whether it's a free service.
*
* @param array &$shorteners array mapping shortener name to properties
*
* @return boolean hook value
*/
function onGetUrlShorteners(&$shorteners)
{
$shorteners[$this->shortenerName] =
array('freeService' => $this->freeService);
return true;
}
/**
* Called to shorten an URL
*
* @param string $url URL to shorten
* @param string $shortenerName Shortening service. Don't handle if it's
* not you!
* @param string &$shortenedUrl URL after shortening; out param.
*
* @return boolean hook value
*/
function onStartShortenUrl($url, $shortenerName, &$shortenedUrl)
{
if ($shortenerName == $this->shortenerName) {
$result = $this->shorten($url);
if (isset($result) && $result != null && $result !== false) {
$shortenedUrl = $result;
common_log(LOG_INFO,
__CLASS__ . ": $this->shortenerName ".
"shortened $url to $shortenedUrl");
return false;
}
}
return true;
}
}

View File

@ -828,9 +828,21 @@ function common_linkify($url) {
function common_shorten_links($text) function common_shorten_links($text)
{ {
$maxLength = Notice::maxContent(); common_debug("common_shorten_links() called");
if ($maxLength == 0 || mb_strlen($text) <= $maxLength) return $text;
return common_replace_urls_callback($text, array('File_redirection', 'makeShort')); $user = common_current_user();
$maxLength = User_urlshortener_prefs::maxNoticeLength($user);
common_debug("maxLength = $maxLength");
if (mb_strlen($text) > $maxLength) {
common_debug("Forcing shortening");
return common_replace_urls_callback($text, array('File_redirection', 'forceShort'));
} else {
common_debug("Not forcing shortening");
return common_replace_urls_callback($text, array('File_redirection', 'makeShort'));
}
} }
function common_xml_safe_str($str) function common_xml_safe_str($str)
@ -1734,30 +1746,42 @@ function common_database_tablename($tablename)
/** /**
* Shorten a URL with the current user's configured shortening service, * Shorten a URL with the current user's configured shortening service,
* or ur1.ca if configured, or not at all if no shortening is set up. * or ur1.ca if configured, or not at all if no shortening is set up.
* Length is not considered.
* *
* @param string $long_url * @param string $long_url original URL
* @param boolean $force Force shortening (used when notice is too long)
*
* @return string may return the original URL if shortening failed * @return string may return the original URL if shortening failed
* *
* @fixme provide a way to specify a particular shortener * @fixme provide a way to specify a particular shortener
* @fixme provide a way to specify to use a given user's shortening preferences * @fixme provide a way to specify to use a given user's shortening preferences
*/ */
function common_shorten_url($long_url)
function common_shorten_url($long_url, $force = false)
{ {
common_debug("Shortening URL '$long_url' (force = $force)");
$long_url = trim($long_url); $long_url = trim($long_url);
$user = common_current_user(); $user = common_current_user();
if (empty($user)) {
// common current user does not find a user when called from the XMPP daemon $maxUrlLength = User_urlshortener_prefs::maxUrlLength($user);
// therefore we'll set one here fix, so that XMPP given URLs may be shortened common_debug("maxUrlLength = $maxUrlLength");
$shortenerName = 'ur1.ca';
} else { // $force forces shortening even if it's not strictly needed
$shortenerName = $user->urlshorteningservice;
if (mb_strlen($long_url) < $maxUrlLength && !$force) {
common_debug("Skipped shortening URL.");
return $long_url;
} }
if(Event::handle('StartShortenUrl', array($long_url,$shortenerName,&$shortenedUrl))){ $shortenerName = User_urlshortener_prefs::urlShorteningService($user);
common_debug("Shortener name = '$shortenerName'");
if (Event::handle('StartShortenUrl', array($long_url, $shortenerName, &$shortenedUrl))) {
//URL wasn't shortened, so return the long url //URL wasn't shortened, so return the long url
return $long_url; return $long_url;
}else{ } else {
//URL was shortened, so return the result //URL was shortened, so return the result
return trim($shortenedUrl); return trim($shortenedUrl);
} }

View File

@ -31,8 +31,6 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR.'/plugins/UrlShortener/UrlShortenerPlugin.php';
class BitlyUrlPlugin extends UrlShortenerPlugin class BitlyUrlPlugin extends UrlShortenerPlugin
{ {
public $serviceUrl; public $serviceUrl;

View File

@ -31,8 +31,6 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR.'/plugins/UrlShortener/UrlShortenerPlugin.php';
class LilUrlPlugin extends UrlShortenerPlugin class LilUrlPlugin extends UrlShortenerPlugin
{ {
public $serviceUrl; public $serviceUrl;

View File

@ -30,7 +30,6 @@
if (!defined('STATUSNET')) { if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR.'/plugins/UrlShortener/UrlShortenerPlugin.php';
class PtitUrlPlugin extends UrlShortenerPlugin class PtitUrlPlugin extends UrlShortenerPlugin
{ {

View File

@ -31,8 +31,6 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR.'/plugins/UrlShortener/UrlShortenerPlugin.php';
class SimpleUrlPlugin extends UrlShortenerPlugin class SimpleUrlPlugin extends UrlShortenerPlugin
{ {
public $serviceUrl; public $serviceUrl;

View File

@ -31,8 +31,6 @@ if (!defined('STATUSNET')) {
exit(1); exit(1);
} }
require_once INSTALLDIR.'/plugins/UrlShortener/UrlShortenerPlugin.php';
class TightUrlPlugin extends UrlShortenerPlugin class TightUrlPlugin extends UrlShortenerPlugin
{ {
public $serviceUrl; public $serviceUrl;

View File

@ -1,95 +0,0 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Superclass for plugins that do URL shortening
*
* PHP version 5
*
* LICENCE: This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* @category Plugin
* @package StatusNet
* @author Craig Andrews <candrews@integralblue.com>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET') && !defined('LACONICA')) {
exit(1);
}
/**
* Superclass for plugins that do URL shortening
*
* @category Plugin
* @package StatusNet
* @author Craig Andrews <candrews@integralblue.com>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
abstract class UrlShortenerPlugin extends Plugin
{
public $shortenerName;
public $freeService=false;
//------------Url Shortener plugin should implement some (or all) of these methods------------\\
/**
* Short a URL
* @param url
* @return string shortened version of the url, or null if URL shortening failed
*/
protected abstract function shorten($url);
//------------These methods may help you implement your plugin------------\\
protected function http_get($url)
{
$request = HTTPClient::start();
$response = $request->get($url);
return $response->getBody();
}
protected function http_post($url,$data)
{
$request = HTTPClient::start();
$response = $request->post($url, null, $data);
return $response->getBody();
}
//------------Below are the methods that connect StatusNet to the implementing Url Shortener plugin------------\\
function onInitializePlugin(){
if(!isset($this->shortenerName)){
throw new Exception("must specify a shortenerName");
}
}
function onGetUrlShorteners(&$shorteners)
{
$shorteners[$this->shortenerName]=array('freeService'=>$this->freeService);
}
function onStartShortenUrl($url,$shortenerName,&$shortenedUrl)
{
if($shortenerName == $this->shortenerName && strlen($url) >= common_config('site', 'shorturllength')){
$result = $this->shorten($url);
if(isset($result) && $result != null && $result !== false){
$shortenedUrl=$result;
common_log(LOG_INFO, __CLASS__ . ": $this->shortenerName shortened $url to $shortenedUrl");
return false;
}
}
}
}