Add a library to mint tag URIs

We've been making pretty crummy tag: URIs for a while. We should
continue to favor HTTP URIs, since it's nice to be able to discover
things about an object you've shared the ID of. Where that's not
possible, this makes nicer tag URIs.
This commit is contained in:
Evan Prodromou 2010-02-20 13:23:08 -05:00
parent 36d21fa716
commit 61a072b3c4
3 changed files with 133 additions and 1 deletions

View File

@ -175,7 +175,7 @@ $default =
array('enabled' => false), array('enabled' => false),
'integration' => 'integration' =>
array('source' => 'StatusNet', # source attribute for Twitter array('source' => 'StatusNet', # source attribute for Twitter
'taguri' => $_server.',2009'), # base for tag URIs 'taguri' => null), # base for tag URIs
'twitter' => 'twitter' =>
array('enabled' => true, array('enabled' => true,
'consumer_key' => null, 'consumer_key' => null,

96
lib/taguri.php Normal file
View File

@ -0,0 +1,96 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Utility for creating new tag: URIs
*
* 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 URI
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @copyright 2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
exit(1);
}
/**
* Mint tag: URIs
*
* tag: URIs are unique identifiers according to http://tools.ietf.org/html/rfc4151.
*
* We use them for creating URIs for things that can't be HTTP retrieved.
*
* @category URI
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPLv3
* @link http://status.net/
*/
class TagURI
{
/**
* Return the base part of a tag URI
*
* Note: use mint() instead.
*
* @return string Tag URI base to use
*/
static function base()
{
$base = common_config('integration', 'taguri');
if (empty($base)) {
$base = common_config('site', 'server').','.date('Y-m-d');
$pathPart = trim(common_config('site', 'path'), '/');
if (!empty($pathPart)) {
$base .= ':'.str_replace('/', ':', $pathPart);
}
}
return $base;
}
/**
* Make a new tag URI
*
* Builds the proper base and creates all the parts
*
* @return string minted URI
*/
static function mint()
{
$base = self::base();
$args = func_get_args();
$format = array_shift($args);
$extra = vsprintf($format, $args);
return 'tag:'.$base.':'.$extra;
}
}

36
tests/TagURITest.php Normal file
View File

@ -0,0 +1,36 @@
<?php
if (isset($_SERVER) && array_key_exists('REQUEST_METHOD', $_SERVER)) {
print "This script must be run from the command line\n";
exit();
}
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
define('STATUSNET', true);
require_once INSTALLDIR . '/lib/common.php';
$config['site']['server'] = 'example.net';
$config['site']['path'] = '/apps/statusnet';
class TagURITest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider provider
*/
public function testProduction($format, $args, $uri)
{
$minted = call_user_func_array(array('TagURI', 'mint'),
array_merge(array($format), $args));
$this->assertEquals($uri, $minted);
}
static public function provider()
{
return array(array('favorite:%d:%d',
array(1, 3),
'tag:example.net,'.date('Y-m-d').':apps:statusnet:favorite:1:3'));
}
}