Merge branch 'testing' of gitorious.org:statusnet/mainline into 0.9.x

This commit is contained in:
Brion Vibber 2010-06-11 12:05:53 -07:00
commit 7f3b3620af
24 changed files with 1859 additions and 94 deletions

View File

@ -232,7 +232,8 @@ class ApiDirectMessageAction extends ApiAuthAction
function showXmlDirectMessages() function showXmlDirectMessages()
{ {
$this->initDocument('xml'); $this->initDocument('xml');
$this->elementStart('direct-messages', array('type' => 'array')); $this->elementStart('direct-messages', array('type' => 'array',
'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
foreach ($this->messages as $m) { foreach ($this->messages as $m) {
$dm_array = $this->directMessageArray($m); $dm_array = $this->directMessageArray($m);

View File

@ -100,32 +100,43 @@ class ApiStatusesDestroyAction extends ApiAuthAction
parent::handle($args); parent::handle($args);
if (!in_array($this->format, array('xml', 'json'))) { if (!in_array($this->format, array('xml', 'json'))) {
$this->clientError(_('API method not found.'), $code = 404); $this->clientError(
return; _('API method not found.'),
404
);
return;
} }
if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) { if (!in_array($_SERVER['REQUEST_METHOD'], array('POST', 'DELETE'))) {
$this->clientError(_('This method requires a POST or DELETE.'), $this->clientError(
400, $this->format); _('This method requires a POST or DELETE.'),
return; 400,
} $this->format
);
return;
}
if (empty($this->notice)) { if (empty($this->notice)) {
$this->clientError(_('No status found with that ID.'), $this->clientError(
404, $this->format); _('No status found with that ID.'),
return; 404, $this->format
} );
return;
}
if ($this->user->id == $this->notice->profile_id) { if ($this->user->id == $this->notice->profile_id) {
$replies = new Reply; $replies = new Reply;
$replies->get('notice_id', $this->notice_id); $replies->get('notice_id', $this->notice_id);
$replies->delete(); $replies->delete();
$this->notice->delete(); $this->notice->delete();
$this->showNotice(); $this->showNotice();
} else { } else {
$this->clientError(_('You may not delete another user\'s status.'), $this->clientError(
403, $this->format); _('You may not delete another user\'s status.'),
} 403,
$this->format
);
}
} }
/** /**

View File

@ -113,7 +113,7 @@ class ApiUserShowAction extends ApiPrivateAuthAction
if ($this->format == 'xml') { if ($this->format == 'xml') {
$this->initDocument('xml'); $this->initDocument('xml');
$this->showTwitterXmlUser($twitter_user); $this->showTwitterXmlUser($twitter_user, 'user', true);
$this->endDocument('xml'); $this->endDocument('xml');
} elseif ($this->format == 'json') { } elseif ($this->format == 'json') {
$this->initDocument('json'); $this->initDocument('json');

View File

@ -126,9 +126,19 @@ class DesignadminpanelAction extends AdminPanelAction
return; return;
} }
// check for an image upload // check for file uploads
$bgimage = $this->saveBackgroundImage(); $bgimage = $this->saveBackgroundImage();
$customTheme = $this->saveCustomTheme();
$oldtheme = common_config('site', 'theme');
if ($customTheme) {
// This feels pretty hacky :D
$this->args['theme'] = $customTheme;
$themeChanged = true;
} else {
$themeChanged = ($this->trimmed('theme') != $oldtheme);
}
static $settings = array('theme', 'logo'); static $settings = array('theme', 'logo');
@ -140,15 +150,13 @@ class DesignadminpanelAction extends AdminPanelAction
$this->validate($values); $this->validate($values);
$oldtheme = common_config('site', 'theme');
$config = new Config(); $config = new Config();
$config->query('BEGIN'); $config->query('BEGIN');
// Only update colors if the theme has not changed. // Only update colors if the theme has not changed.
if ($oldtheme == $values['theme']) { if (!$themeChanged) {
$bgcolor = new WebColor($this->trimmed('design_background')); $bgcolor = new WebColor($this->trimmed('design_background'));
$ccolor = new WebColor($this->trimmed('design_content')); $ccolor = new WebColor($this->trimmed('design_content'));
@ -190,6 +198,13 @@ class DesignadminpanelAction extends AdminPanelAction
Config::save('design', 'backgroundimage', $bgimage); Config::save('design', 'backgroundimage', $bgimage);
} }
if (common_config('custom_css', 'enabled')) {
$css = $this->arg('css');
if ($css != common_config('custom_css', 'css')) {
Config::save('custom_css', 'css', $css);
}
}
$config->query('COMMIT'); $config->query('COMMIT');
} }
@ -263,6 +278,33 @@ class DesignadminpanelAction extends AdminPanelAction
} }
} }
/**
* Save the custom theme if the user uploaded one.
*
* @return mixed custom theme name, if succesful, or null if no theme upload.
* @throws ClientException for invalid theme archives
* @throws ServerException if trouble saving the theme files
*/
function saveCustomTheme()
{
if (common_config('theme_upload', 'enabled') &&
$_FILES['design_upload_theme']['error'] == UPLOAD_ERR_OK) {
$upload = ThemeUploader::fromUpload('design_upload_theme');
$basedir = common_config('local', 'dir');
if (empty($basedir)) {
$basedir = INSTALLDIR . '/local';
}
$name = 'custom'; // @todo allow multiples, custom naming?
$outdir = $basedir . '/theme/' . $name;
$upload->extract($outdir);
return $name;
} else {
return null;
}
}
/** /**
* Attempt to validate setting values * Attempt to validate setting values
* *
@ -371,7 +413,15 @@ class DesignAdminPanelForm extends AdminForm
function formData() function formData()
{ {
$this->showLogo();
$this->showTheme();
$this->showBackground();
$this->showColors();
$this->showAdvanced();
}
function showLogo()
{
$this->out->elementStart('fieldset', array('id' => 'settings_design_logo')); $this->out->elementStart('fieldset', array('id' => 'settings_design_logo'));
$this->out->element('legend', null, _('Change logo')); $this->out->element('legend', null, _('Change logo'));
@ -384,6 +434,11 @@ class DesignAdminPanelForm extends AdminForm
$this->out->elementEnd('ul'); $this->out->elementEnd('ul');
$this->out->elementEnd('fieldset'); $this->out->elementEnd('fieldset');
}
function showTheme()
{
$this->out->elementStart('fieldset', array('id' => 'settings_design_theme')); $this->out->elementStart('fieldset', array('id' => 'settings_design_theme'));
$this->out->element('legend', null, _('Change theme')); $this->out->element('legend', null, _('Change theme'));
@ -407,10 +462,23 @@ class DesignAdminPanelForm extends AdminForm
false, $this->value('theme')); false, $this->value('theme'));
$this->unli(); $this->unli();
if (common_config('theme_upload', 'enabled')) {
$this->li();
$this->out->element('label', array('for' => 'design_upload_theme'), _('Custom theme'));
$this->out->element('input', array('id' => 'design_upload_theme',
'name' => 'design_upload_theme',
'type' => 'file'));
$this->out->element('p', 'form_guide', _('You can upload a custom StatusNet theme as a .ZIP archive.'));
$this->unli();
}
$this->out->elementEnd('ul'); $this->out->elementEnd('ul');
$this->out->elementEnd('fieldset'); $this->out->elementEnd('fieldset');
}
function showBackground()
{
$design = $this->out->design; $design = $this->out->design;
$this->out->elementStart('fieldset', array('id' => $this->out->elementStart('fieldset', array('id' =>
@ -486,6 +554,11 @@ class DesignAdminPanelForm extends AdminForm
$this->out->elementEnd('ul'); $this->out->elementEnd('ul');
$this->out->elementEnd('fieldset'); $this->out->elementEnd('fieldset');
}
function showColors()
{
$design = $this->out->design;
$this->out->elementStart('fieldset', array('id' => 'settings_design_color')); $this->out->elementStart('fieldset', array('id' => 'settings_design_color'));
$this->out->element('legend', null, _('Change colours')); $this->out->element('legend', null, _('Change colours'));
@ -493,6 +566,7 @@ class DesignAdminPanelForm extends AdminForm
$this->out->elementStart('ul', 'form_data'); $this->out->elementStart('ul', 'form_data');
try { try {
// @fixme avoid loop unrolling in non-performance-critical contexts like this
$bgcolor = new WebColor($design->backgroundcolor); $bgcolor = new WebColor($design->backgroundcolor);
@ -560,6 +634,7 @@ class DesignAdminPanelForm extends AdminForm
$this->unli(); $this->unli();
} catch (WebColorException $e) { } catch (WebColorException $e) {
// @fixme normalize them individually!
common_log(LOG_ERR, 'Bad color values in site design: ' . common_log(LOG_ERR, 'Bad color values in site design: ' .
$e->getMessage()); $e->getMessage());
} }
@ -569,6 +644,27 @@ class DesignAdminPanelForm extends AdminForm
$this->out->elementEnd('ul'); $this->out->elementEnd('ul');
} }
function showAdvanced()
{
if (common_config('custom_css', 'enabled')) {
$this->out->elementStart('fieldset', array('id' => 'settings_design_advanced'));
$this->out->element('legend', null, _('Advanced'));
$this->out->elementStart('ul', 'form_data');
$this->li();
$this->out->element('label', array('for' => 'css'), _('Custom CSS'));
$this->out->element('textarea', array('name' => 'css',
'id' => 'css',
'cols' => '50',
'rows' => '10'),
strval(common_config('custom_css', 'css')));
$this->unli();
$this->out->elementEnd('fieldset');
$this->out->elementEnd('ul');
}
}
/** /**
* Action elements * Action elements
* *

View File

@ -154,7 +154,9 @@ class FoafAction extends Action
} }
$person = $this->showMicrobloggingAccount($this->profile, $person = $this->showMicrobloggingAccount($this->profile,
common_root_url(), $this->user->uri, false); common_root_url(), $this->user->uri,
/*$fetchSubscriptions*/true,
/*$isSubscriber*/false);
// Get people who subscribe to user // Get people who subscribe to user
@ -209,7 +211,8 @@ class FoafAction extends Action
$this->showMicrobloggingAccount($profile, $this->showMicrobloggingAccount($profile,
($local == 'local') ? common_root_url() : null, ($local == 'local') ? common_root_url() : null,
$uri, $uri,
true); /*$fetchSubscriptions*/false,
/*$isSubscriber*/($type == LISTENER || $type == BOTH));
if ($foaf_url) { if ($foaf_url) {
$this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url)); $this->element('rdfs:seeAlso', array('rdf:resource' => $foaf_url));
} }
@ -234,7 +237,21 @@ class FoafAction extends Action
$this->elementEnd('PersonalProfileDocument'); $this->elementEnd('PersonalProfileDocument');
} }
function showMicrobloggingAccount($profile, $service=null, $useruri=null, $isSubscriber=false) /**
* Output FOAF <account> bit for the given profile.
*
* @param Profile $profile
* @param mixed $service Root URL of this StatusNet instance for a local
* user, otherwise null.
* @param mixed $useruri URI string for the referenced profile..
* @param boolean $fetchSubscriptions Should we load and list all their subscriptions?
* @param boolean $isSubscriber if not fetching subs, we can still mark the user as following the current page.
*
* @return array if $fetchSubscribers is set, return a list of info on those
* subscriptions.
*/
function showMicrobloggingAccount($profile, $service=null, $useruri=null, $fetchSubscriptions=false, $isSubscriber=false)
{ {
$attr = array(); $attr = array();
if ($useruri) { if ($useruri) {
@ -256,9 +273,7 @@ class FoafAction extends Action
$person = array(); $person = array();
if ($isSubscriber) { if ($fetchSubscriptions) {
$this->element('sioc:follows', array('rdf:resource'=>$this->user->uri . '#acct'));
} else {
// Get people user is subscribed to // Get people user is subscribed to
$sub = new Subscription(); $sub = new Subscription();
$sub->subscriber = $profile->id; $sub->subscriber = $profile->id;
@ -283,6 +298,9 @@ class FoafAction extends Action
} }
unset($sub); unset($sub);
} else if ($isSubscriber) {
// Just declare that they follow the user whose FOAF we're showing.
$this->element('sioc:follows', array('rdf:resource' => $this->user->uri . '#acct'));
} }
$this->elementEnd('OnlineAccount'); $this->elementEnd('OnlineAccount');

View File

@ -128,12 +128,13 @@ class Memcached_DataObject extends Safe_DataObject
} }
static function cacheKey($cls, $k, $v) { static function cacheKey($cls, $k, $v) {
if (is_object($cls) || is_object($k) || is_object($v)) { if (is_object($cls) || is_object($k) || (is_object($v) && !($v instanceof DB_DataObject_Cast))) {
$e = new Exception(); $e = new Exception();
common_log(LOG_ERR, __METHOD__ . ' object in param: ' . common_log(LOG_ERR, __METHOD__ . ' object in param: ' .
str_replace("\n", " ", $e->getTraceAsString())); str_replace("\n", " ", $e->getTraceAsString()));
} }
return common_cache_key(strtolower($cls).':'.$k.':'.$v); $vstr = self::valueString($v);
return common_cache_key(strtolower($cls).':'.$k.':'.$vstr);
} }
static function getcached($cls, $k, $v) { static function getcached($cls, $k, $v) {
@ -229,10 +230,10 @@ class Memcached_DataObject extends Safe_DataObject
if (empty($this->$key)) { if (empty($this->$key)) {
continue; continue;
} }
$ckeys[] = $this->cacheKey($this->tableName(), $key, $this->$key); $ckeys[] = $this->cacheKey($this->tableName(), $key, self::valueString($this->$key));
} else if ($type == 'K' || $type == 'N') { } else if ($type == 'K' || $type == 'N') {
$pkey[] = $key; $pkey[] = $key;
$pval[] = $this->$key; $pval[] = self::valueString($this->$key);
} else { } else {
throw new Exception("Unknown key type $key => $type for " . $this->tableName()); throw new Exception("Unknown key type $key => $type for " . $this->tableName());
} }
@ -604,5 +605,30 @@ class Memcached_DataObject extends Safe_DataObject
return $c->set($cacheKey, $value); return $c->set($cacheKey, $value);
} }
static function valueString($v)
{
$vstr = null;
if (is_object($v) && $v instanceof DB_DataObject_Cast) {
switch ($v->type) {
case 'date':
$vstr = $v->year . '-' . $v->month . '-' . $v->day;
break;
case 'blob':
case 'string':
case 'sql':
case 'datetime':
case 'time':
throw new ServerException("Unhandled DB_DataObject_Cast type passed as cacheKey value: '$v->type'");
break;
default:
throw new ServerException("Unknown DB_DataObject_Cast type passed as cacheKey value: '$v->type'");
break;
}
} else {
$vstr = strval($v);
}
return $vstr;
}
} }

View File

@ -1254,6 +1254,8 @@ class Notice extends Memcached_DataObject
if (!empty($cur)) { if (!empty($cur)) {
$noticeInfoAttr['favorite'] = ($cur->hasFave($this)) ? "true" : "false"; $noticeInfoAttr['favorite'] = ($cur->hasFave($this)) ? "true" : "false";
$profile = $cur->getProfile();
$noticeInfoAttr['repeated'] = ($profile->hasRepeated($this->id)) ? "true" : "false";
} }
if (!empty($this->repeat_of)) { if (!empty($this->repeat_of)) {

View File

@ -18,6 +18,7 @@ VALUES
('Facebook','Facebook','http://apps.facebook.com/identica/', now()), ('Facebook','Facebook','http://apps.facebook.com/identica/', now()),
('feed2omb','feed2omb','http://projects.ciarang.com/p/feed2omb/', now()), ('feed2omb','feed2omb','http://projects.ciarang.com/p/feed2omb/', now()),
('get2gnow', 'get2gnow', 'http://uberchicgeekchick.com/?projects=get2gnow', now()), ('get2gnow', 'get2gnow', 'http://uberchicgeekchick.com/?projects=get2gnow', now()),
('gNewBook', 'gNewBook', 'http://www.gnewbook.org/', now()),
('gravity', 'Gravity', 'http://mobileways.de/gravity', now()), ('gravity', 'Gravity', 'http://mobileways.de/gravity', now()),
('Gwibber','Gwibber','http://launchpad.net/gwibber', now()), ('Gwibber','Gwibber','http://launchpad.net/gwibber', now()),
('HelloTxt','HelloTxt','http://hellotxt.com/', now()), ('HelloTxt','HelloTxt','http://hellotxt.com/', now()),

View File

@ -235,6 +235,16 @@ class Action extends HTMLOutputter // lawsuit
Event::handle('EndShowDesign', array($this)); Event::handle('EndShowDesign', array($this));
} }
Event::handle('EndShowStyles', array($this)); Event::handle('EndShowStyles', array($this));
if (common_config('custom_css', 'enabled')) {
$css = common_config('custom_css', 'css');
if (Event::handle('StartShowCustomCss', array($this, &$css))) {
if (trim($css) != '') {
$this->style($css);
}
Event::handle('EndShowCustomCss', array($this));
}
}
} }
} }

View File

@ -284,9 +284,10 @@ class AdminPanelAction extends Action
$this->clientError(_("Unable to delete design setting.")); $this->clientError(_("Unable to delete design setting."));
return null; return null;
} }
return $result;
} }
return $result; return null;
} }
function canAdmin($name) function canAdmin($name)

View File

@ -295,6 +295,10 @@ class ApiAction extends Action
} }
} }
// StatusNet-specific
$twitter_user['statusnet:profile_url'] = $profile->profileurl;
return $twitter_user; return $twitter_user;
} }
@ -396,6 +400,10 @@ class ApiAction extends Action
$twitter_status['user'] = $twitter_user; $twitter_status['user'] = $twitter_user;
} }
// StatusNet-specific
$twitter_status['statusnet:html'] = $notice->rendered;
return $twitter_status; return $twitter_status;
} }
@ -563,9 +571,13 @@ class ApiAction extends Action
} }
} }
function showTwitterXmlStatus($twitter_status, $tag='status') function showTwitterXmlStatus($twitter_status, $tag='status', $namespaces=false)
{ {
$this->elementStart($tag); $attrs = array();
if ($namespaces) {
$attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
}
$this->elementStart($tag, $attrs);
foreach($twitter_status as $element => $value) { foreach($twitter_status as $element => $value) {
switch ($element) { switch ($element) {
case 'user': case 'user':
@ -599,9 +611,13 @@ class ApiAction extends Action
$this->elementEnd('group'); $this->elementEnd('group');
} }
function showTwitterXmlUser($twitter_user, $role='user') function showTwitterXmlUser($twitter_user, $role='user', $namespaces=false)
{ {
$this->elementStart($role); $attrs = array();
if ($namespaces) {
$attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
}
$this->elementStart($role, $attrs);
foreach($twitter_user as $element => $value) { foreach($twitter_user as $element => $value) {
if ($element == 'status') { if ($element == 'status') {
$this->showTwitterXmlStatus($twitter_user['status']); $this->showTwitterXmlStatus($twitter_user['status']);
@ -683,7 +699,7 @@ class ApiAction extends Action
{ {
$this->initDocument('xml'); $this->initDocument('xml');
$twitter_status = $this->twitterStatusArray($notice); $twitter_status = $this->twitterStatusArray($notice);
$this->showTwitterXmlStatus($twitter_status); $this->showTwitterXmlStatus($twitter_status, 'status', true);
$this->endDocument('xml'); $this->endDocument('xml');
} }
@ -699,7 +715,8 @@ class ApiAction extends Action
{ {
$this->initDocument('xml'); $this->initDocument('xml');
$this->elementStart('statuses', array('type' => 'array')); $this->elementStart('statuses', array('type' => 'array',
'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
if (is_array($notice)) { if (is_array($notice)) {
foreach ($notice as $n) { foreach ($notice as $n) {
@ -866,9 +883,13 @@ class ApiAction extends Action
$this->elementEnd('entry'); $this->elementEnd('entry');
} }
function showXmlDirectMessage($dm) function showXmlDirectMessage($dm, $namespaces=false)
{ {
$this->elementStart('direct_message'); $attrs = array();
if ($namespaces) {
$attrs['xmlns:statusnet'] = 'http://status.net/schema/api/1/';
}
$this->elementStart('direct_message', $attrs);
foreach($dm as $element => $value) { foreach($dm as $element => $value) {
switch ($element) { switch ($element) {
case 'sender': case 'sender':
@ -945,7 +966,7 @@ class ApiAction extends Action
{ {
$this->initDocument('xml'); $this->initDocument('xml');
$dmsg = $this->directMessageArray($message); $dmsg = $this->directMessageArray($message);
$this->showXmlDirectMessage($dmsg); $this->showXmlDirectMessage($dmsg, true);
$this->endDocument('xml'); $this->endDocument('xml');
} }
@ -1062,7 +1083,8 @@ class ApiAction extends Action
{ {
$this->initDocument('xml'); $this->initDocument('xml');
$this->elementStart('users', array('type' => 'array')); $this->elementStart('users', array('type' => 'array',
'xmlns:statusnet' => 'http://status.net/schema/api/1/'));
if (is_array($user)) { if (is_array($user)) {
foreach ($user as $u) { foreach ($user as $u) {

View File

@ -141,10 +141,17 @@ $default =
'dir' => null, 'dir' => null,
'path'=> null, 'path'=> null,
'ssl' => null), 'ssl' => null),
'theme_upload' =>
array('enabled' => extension_loaded('zip')),
'javascript' => 'javascript' =>
array('server' => null, array('server' => null,
'path'=> null, 'path'=> null,
'ssl' => null), 'ssl' => null),
'local' => // To override path/server for themes in 'local' dir (not currently applied to local plugins)
array('server' => null,
'dir' => null,
'path' => null,
'ssl' => null),
'throttle' => 'throttle' =>
array('enabled' => false, // whether to throttle edits; false by default array('enabled' => false, // whether to throttle edits; false by default
'count' => 20, // number of allowed messages in timespan 'count' => 20, // number of allowed messages in timespan
@ -260,6 +267,9 @@ $default =
'linkcolor' => null, 'linkcolor' => null,
'backgroundimage' => null, 'backgroundimage' => null,
'disposition' => null), 'disposition' => null),
'custom_css' =>
array('enabled' => true,
'css' => ''),
'notice' => 'notice' =>
array('contentlimit' => null), array('contentlimit' => null),
'message' => 'message' =>

View File

@ -82,9 +82,12 @@ abstract class Installer
{ {
$pass = true; $pass = true;
if (file_exists(INSTALLDIR.'/config.php')) { $config = INSTALLDIR.'/config.php';
$this->warning('Config file "config.php" already exists.'); if (file_exists($config)) {
$pass = false; if (!is_writable($config) || filesize($config) > 0) {
$this->warning('Config file "config.php" already exists.');
$pass = false;
}
} }
if (version_compare(PHP_VERSION, '5.2.3', '<')) { if (version_compare(PHP_VERSION, '5.2.3', '<')) {

View File

@ -540,7 +540,7 @@ class Router
$m->connect('api/favorites/:id.:format', $m->connect('api/favorites/:id.:format',
array('action' => 'ApiTimelineFavorites', array('action' => 'ApiTimelineFavorites',
'id' => '[a-zA-Z0-9]+', 'id' => '[a-zA-Z0-9]+',
'format' => '(xmljson|rss|atom)')); 'format' => '(xml|json|rss|atom)'));
$m->connect('api/favorites/create/:id.:format', $m->connect('api/favorites/create/:id.:format',
array('action' => 'ApiFavoriteCreate', array('action' => 'ApiFavoriteCreate',
@ -597,7 +597,7 @@ class Router
$m->connect('api/statusnet/groups/timeline/:id.:format', $m->connect('api/statusnet/groups/timeline/:id.:format',
array('action' => 'ApiTimelineGroup', array('action' => 'ApiTimelineGroup',
'id' => '[a-zA-Z0-9]+', 'id' => '[a-zA-Z0-9]+',
'format' => '(xmljson|rss|atom)')); 'format' => '(xml|json|rss|atom)'));
$m->connect('api/statusnet/groups/show.:format', $m->connect('api/statusnet/groups/show.:format',
array('action' => 'ApiGroupShow', array('action' => 'ApiGroupShow',
@ -658,7 +658,7 @@ class Router
// Tags // Tags
$m->connect('api/statusnet/tags/timeline/:tag.:format', $m->connect('api/statusnet/tags/timeline/:tag.:format',
array('action' => 'ApiTimelineTag', array('action' => 'ApiTimelineTag',
'format' => '(xmljson|rss|atom)')); 'format' => '(xml|json|rss|atom)'));
// media related // media related
$m->connect( $m->connect(

View File

@ -39,6 +39,9 @@ if (!defined('STATUSNET') && !defined('LACONICA')) {
* in them. They're found in either local/theme (for locally-installed themes) * in them. They're found in either local/theme (for locally-installed themes)
* or theme/ subdir of installation dir. * or theme/ subdir of installation dir.
* *
* Note that the 'local' directory can be overridden as $config['local']['path']
* and $config['local']['dir'] etc.
*
* This used to be a couple of functions, but for various reasons it's nice * This used to be a couple of functions, but for various reasons it's nice
* to have a class instead. * to have a class instead.
* *
@ -76,7 +79,7 @@ class Theme
if (file_exists($fulldir) && is_dir($fulldir)) { if (file_exists($fulldir) && is_dir($fulldir)) {
$this->dir = $fulldir; $this->dir = $fulldir;
$this->path = common_path('local/theme/'.$name.'/'); $this->path = $this->relativeThemePath('local', 'local', 'theme/' . $name);
return; return;
} }
@ -89,44 +92,65 @@ class Theme
if (file_exists($fulldir) && is_dir($fulldir)) { if (file_exists($fulldir) && is_dir($fulldir)) {
$this->dir = $fulldir; $this->dir = $fulldir;
$this->path = $this->relativeThemePath('theme', 'theme', $name);
$path = common_config('theme', 'path');
if (empty($path)) {
$path = common_config('site', 'path') . '/theme/';
}
if ($path[strlen($path)-1] != '/') {
$path .= '/';
}
if ($path[0] != '/') {
$path = '/'.$path;
}
$server = common_config('theme', 'server');
if (empty($server)) {
$server = common_config('site', 'server');
}
$ssl = common_config('theme', 'ssl');
if (is_null($ssl)) { // null -> guess
if (common_config('site', 'ssl') == 'always' &&
!common_config('theme', 'server')) {
$ssl = true;
} else {
$ssl = false;
}
}
$protocol = ($ssl) ? 'https' : 'http';
$this->path = $protocol . '://'.$server.$path.$name;
} }
} }
/**
* Build a full URL to the given theme's base directory, possibly
* using an offsite theme server path.
*
* @param string $group configuration section name to pull paths from
* @param string $fallbackSubdir default subdirectory under INSTALLDIR
* @param string $name theme name
*
* @return string URL
*
* @todo consolidate code with that for other customizable paths
*/
protected function relativeThemePath($group, $fallbackSubdir, $name)
{
$path = common_config($group, 'path');
if (empty($path)) {
$path = common_config('site', 'path') . '/';
if ($fallbackSubdir) {
$path .= $fallbackSubdir . '/';
}
}
if ($path[strlen($path)-1] != '/') {
$path .= '/';
}
if ($path[0] != '/') {
$path = '/'.$path;
}
$server = common_config($group, 'server');
if (empty($server)) {
$server = common_config('site', 'server');
}
$ssl = common_config($group, 'ssl');
if (is_null($ssl)) { // null -> guess
if (common_config('site', 'ssl') == 'always' &&
!common_config($group, 'server')) {
$ssl = true;
} else {
$ssl = false;
}
}
$protocol = ($ssl) ? 'https' : 'http';
$path = $protocol . '://'.$server.$path.$name;
return $path;
}
/** /**
* Gets the full local filename of a file in this theme. * Gets the full local filename of a file in this theme.
* *
@ -236,7 +260,13 @@ class Theme
protected static function localRoot() protected static function localRoot()
{ {
return INSTALLDIR.'/local/theme'; $basedir = common_config('local', 'dir');
if (empty($basedir)) {
$basedir = INSTALLDIR . '/local';
}
return $basedir . '/theme';
} }
/** /**

311
lib/themeuploader.php Normal file
View File

@ -0,0 +1,311 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Utilities for theme files and paths
*
* 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 Paths
* @package StatusNet
* @author Brion Vibber <brion@status.net>
* @copyright 2010 StatusNet, Inc.
* @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);
}
/**
* Encapsulation of the validation-and-save process when dealing with
* a user-uploaded StatusNet theme archive...
*
* @todo extract theme metadata from css/display.css
* @todo allow saving multiple themes
*/
class ThemeUploader
{
protected $sourceFile;
protected $isUpload;
private $prevErrorReporting;
public function __construct($filename)
{
if (!class_exists('ZipArchive')) {
throw new Exception(_("This server cannot handle theme uploads without ZIP support."));
}
$this->sourceFile = $filename;
}
public static function fromUpload($name)
{
if (!isset($_FILES[$name]['error'])) {
throw new ServerException(_("Theme upload missing or failed."));
}
if ($_FILES[$name]['error'] != UPLOAD_ERR_OK) {
throw new ServerException(_("Theme upload missing or failed."));
}
return new ThemeUploader($_FILES[$name]['tmp_name']);
}
/**
* @param string $destDir
* @throws Exception on bogus files
*/
public function extract($destDir)
{
$zip = $this->openArchive();
// First pass: validate but don't save anything to disk.
// Any errors will trip an exception.
$this->traverseArchive($zip);
// Second pass: now that we know we're good, actually extract!
$tmpDir = $destDir . '.tmp' . getmypid();
$this->traverseArchive($zip, $tmpDir);
$zip->close();
if (file_exists($destDir)) {
$killDir = $tmpDir . '.old';
$this->quiet();
$ok = rename($destDir, $killDir);
$this->loud();
if (!$ok) {
common_log(LOG_ERR, "Could not move old custom theme from $destDir to $killDir");
throw new ServerException(_("Failed saving theme."));
}
} else {
$killDir = false;
}
$this->quiet();
$ok = rename($tmpDir, $destDir);
$this->loud();
if (!$ok) {
common_log(LOG_ERR, "Could not move saved theme from $tmpDir to $destDir");
throw new ServerException(_("Failed saving theme."));
}
if ($killDir) {
$this->recursiveRmdir($killDir);
}
}
/**
*
*/
protected function traverseArchive($zip, $outdir=false)
{
$sizeLimit = 2 * 1024 * 1024; // 2 megabyte space limit?
$blockSize = 4096; // estimated; any entry probably takes this much space
$totalSize = 0;
$hasMain = false;
$commonBaseDir = false;
for ($i = 0; $i < $zip->numFiles; $i++) {
$data = $zip->statIndex($i);
$name = str_replace('\\', '/', $data['name']);
if (substr($name, -1) == '/') {
// A raw directory... skip!
continue;
}
// Check the directory structure...
$path = pathinfo($name);
$dirs = explode('/', $path['dirname']);
$baseDir = array_shift($dirs);
if ($commonBaseDir === false) {
$commonBaseDir = $baseDir;
} else {
if ($commonBaseDir != $baseDir) {
throw new ClientException(_("Invalid theme: bad directory structure."));
}
}
foreach ($dirs as $dir) {
$this->validateFileOrFolder($dir);
}
// Is this a safe or skippable file?
if ($this->skippable($path['filename'], $path['extension'])) {
// Documentation and such... booooring
continue;
} else {
$this->validateFile($path['filename'], $path['extension']);
}
$fullPath = $dirs;
$fullPath[] = $path['basename'];
$localFile = implode('/', $fullPath);
if ($localFile == 'css/display.css') {
$hasMain = true;
}
$size = $data['size'];
$estSize = $blockSize * max(1, intval(ceil($size / $blockSize)));
$totalSize += $estSize;
if ($totalSize > $sizeLimit) {
$msg = sprintf(_("Uploaded theme is too large; " .
"must be less than %d bytes uncompressed."),
$sizeLimit);
throw new ClientException($msg);
}
if ($outdir) {
$this->extractFile($zip, $data['name'], "$outdir/$localFile");
}
}
if (!$hasMain) {
throw new ClientException(_("Invalid theme archive: " .
"missing file css/display.css"));
}
}
protected function skippable($filename, $ext)
{
$skip = array('txt', 'rtf', 'doc', 'docx', 'odt');
if (strtolower($filename) == 'readme') {
return true;
}
if (in_array(strtolower($ext), $skip)) {
return true;
}
return false;
}
protected function validateFile($filename, $ext)
{
$this->validateFileOrFolder($filename);
$this->validateExtension($ext);
// @fixme validate content
}
protected function validateFileOrFolder($name)
{
if (!preg_match('/^[a-z0-9_-]+$/i', $name)) {
$msg = _("Theme contains invalid file or folder name. " .
"Stick with ASCII letters, digits, underscore, and minus sign.");
throw new ClientException($msg);
}
return true;
}
protected function validateExtension($ext)
{
$allowed = array('css', 'png', 'gif', 'jpg', 'jpeg');
if (!in_array(strtolower($ext), $allowed)) {
$msg = sprintf(_("Theme contains file of type '.%s', " .
"which is not allowed."),
$ext);
throw new ClientException($msg);
}
return true;
}
/**
* @return ZipArchive
*/
protected function openArchive()
{
$zip = new ZipArchive;
$ok = $zip->open($this->sourceFile);
if ($ok !== true) {
common_log(LOG_ERR, "Error opening theme zip archive: " .
"{$this->sourceFile} code: {$ok}");
throw new Exception(_("Error opening theme archive."));
}
return $zip;
}
/**
* @param ZipArchive $zip
* @param string $from original path inside ZIP archive
* @param string $to final destination path in filesystem
*/
protected function extractFile($zip, $from, $to)
{
$dir = dirname($to);
if (!file_exists($dir)) {
$this->quiet();
$ok = mkdir($dir, 0755, true);
$this->loud();
if (!$ok) {
common_log(LOG_ERR, "Failed to mkdir $dir while uploading theme");
throw new ServerException(_("Failed saving theme."));
}
} else if (!is_dir($dir)) {
common_log(LOG_ERR, "Output directory $dir not a directory while uploading theme");
throw new ServerException(_("Failed saving theme."));
}
// ZipArchive::extractTo would be easier, but won't let us alter
// the directory structure.
$in = $zip->getStream($from);
if (!$in) {
common_log(LOG_ERR, "Couldn't open archived file $from while uploading theme");
throw new ServerException(_("Failed saving theme."));
}
$this->quiet();
$out = fopen($to, "wb");
$this->loud();
if (!$out) {
common_log(LOG_ERR, "Couldn't open output file $to while uploading theme");
throw new ServerException(_("Failed saving theme."));
}
while (!feof($in)) {
$buffer = fread($in, 65536);
fwrite($out, $buffer);
}
fclose($in);
fclose($out);
}
private function quiet()
{
$this->prevErrorReporting = error_reporting();
error_reporting($this->prevErrorReporting & ~E_WARNING);
}
private function loud()
{
error_reporting($this->prevErrorReporting);
}
private function recursiveRmdir($dir)
{
$list = dir($dir);
while (($file = $list->read()) !== false) {
if ($file == '.' || $file == '..') {
continue;
}
$full = "$dir/$file";
if (is_dir($full)) {
$this->recursiveRmdir($full);
} else {
unlink($full);
}
}
$list->close();
rmdir($dir);
}
}

View File

@ -376,7 +376,7 @@ class GeonamesPlugin extends Plugin
return true; return true;
} }
$url = 'http://sw.geonames.org/' . $location->location_id . '/'; $url = 'http://sws.geonames.org/' . $location->location_id . '/';
// it's been filled, so don't process further. // it's been filled, so don't process further.
return false; return false;

View File

@ -0,0 +1,163 @@
<?php
/**
* StatusNet - the distributed open-source microblogging tool
* Copyright (C) 2010, StatusNet, Inc.
*
* Creates a dynamic sitemap for a StatusNet site
*
* PHP version 5
*
* 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 Sample
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @copyright 2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
if (!defined('STATUSNET')) {
// This check helps protect against security problems;
// your code file can't be executed directly from the web.
exit(1);
}
/**
* Sitemap plugin
*
* @category Sample
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @copyright 2010 StatusNet, Inc.
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html AGPL 3.0
* @link http://status.net/
*/
class SitemapPlugin extends Plugin
{
const USERS_PER_MAP = 50000;
const NOTICES_PER_MAP = 50000;
/**
* Load related modules when needed
*
* @param string $cls Name of the class to be loaded
*
* @return boolean hook value; true means continue processing, false means stop.
*/
function onAutoload($cls)
{
$dir = dirname(__FILE__);
switch ($cls)
{
case 'Sitemap_user_count':
case 'Sitemap_notice_count':
require_once $dir . '/' . $cls . '.php';
return false;
case 'SitemapindexAction':
case 'NoticesitemapAction':
case 'UsersitemapAction':
require_once $dir . '/' . strtolower(mb_substr($cls, 0, -6)) . '.php';
return false;
case 'SitemapAction':
require_once $dir . '/' . strtolower($cls) . '.php';
return false;
default:
return true;
}
}
/**
* Add sitemap-related information at the end of robots.txt
*
* @param Action $action Action being run
*
* @return boolean hook value.
*/
function onEndRobotsTxt($action)
{
$url = common_local_url('sitemapindex');
print "\nSitemap: $url\n";
return true;
}
/**
* Map URLs to actions
*
* @param Net_URL_Mapper $m path-to-action mapper
*
* @return boolean hook value; true means continue processing, false means stop.
*/
function onRouterInitialized($m)
{
$m->connect('sitemapindex.xml',
array('action' => 'sitemapindex'));
$m->connect('/notice-sitemap-:year-:month-:day-:index.xml',
array('action' => 'noticesitemap'),
array('year' => '[0-9]{4}',
'month' => '[01][0-9]',
'day' => '[0123][0-9]',
'index' => '[1-9][0-9]*'));
$m->connect('/user-sitemap-:year-:month-:day-:index.xml',
array('action' => 'usersitemap'),
array('year' => '[0-9]{4}',
'month' => '[01][0-9]',
'day' => '[0123][0-9]',
'index' => '[1-9][0-9]*'));
return true;
}
/**
* Database schema setup
*
* We cache some data persistently to avoid overlong queries.
*
* @see Sitemap_user_count
* @see Sitemap_notice_count
*
* @return boolean hook value; true means continue processing, false means stop.
*/
function onCheckSchema()
{
$schema = Schema::get();
$schema->ensureTable('sitemap_user_count',
array(new ColumnDef('registration_date', 'date', null,
true, 'PRI'),
new ColumnDef('user_count', 'integer'),
new ColumnDef('created', 'datetime',
null, false),
new ColumnDef('modified', 'timestamp')));
$schema->ensureTable('sitemap_notice_count',
array(new ColumnDef('notice_date', 'date', null,
true, 'PRI'),
new ColumnDef('notice_count', 'integer'),
new ColumnDef('created', 'datetime',
null, false),
new ColumnDef('modified', 'timestamp')));
return true;
}
}

View File

@ -0,0 +1,288 @@
<?php
/**
* Data class for counting notice postings by date
*
* PHP version 5
*
* @category Data
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://status.net/
*
* 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')) {
exit(1);
}
require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
/**
* Data class for counting notices by date
*
* We make a separate sitemap for each notice posted by date.
* To save ourselves some (not inconsiderable) processing effort,
* we cache this data in the sitemap_notice_count table. Each
* row represents a day since the site has been started, with a count
* of notices posted on that day. Since, after the end of the day,
* this number doesn't change, it's a good candidate for persistent caching.
*
* @category Data
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://status.net/
*
* @see DB_DataObject
*/
class Sitemap_notice_count extends Memcached_DataObject
{
public $__table = 'sitemap_notice_count'; // table name
public $notice_date; // date primary_key not_null
public $notice_count; // int(4)
public $created;
public $modified;
/**
* Get an instance by key
*
* This is a utility method to get a single instance with a given key value.
*
* @param string $k Key to use to lookup (usually 'notice_id' for this class)
* @param mixed $v Value to lookup
*
* @return Sitemap_notice_count object found, or null for no hits
*
*/
function staticGet($k, $v=null)
{
return Memcached_DataObject::staticGet('Sitemap_notice_count', $k, $v);
}
/**
* return table definition for DB_DataObject
*
* DB_DataObject needs to know something about the table to manipulate
* instances. This method provides all the DB_DataObject needs to know.
*
* @return array array of column definitions
*/
function table()
{
return array('notice_date' => DB_DATAOBJECT_DATE + DB_DATAOBJECT_NOTNULL,
'notice_count' => DB_DATAOBJECT_INT,
'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
'modified' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
}
/**
* return key definitions for DB_DataObject
*
* DB_DataObject needs to know about keys that the table has; this function
* defines them.
*
* @return array key definitions
*/
function keys()
{
return array('notice_date' => 'K');
}
/**
* return key definitions for Memcached_DataObject
*
* Our caching system uses the same key definitions, but uses a different
* method to get them.
*
* @return array key definitions
*/
function keyTypes()
{
return $this->keys();
}
static function getAll()
{
$noticeCounts = self::cacheGet('sitemap:notice:counts');
if ($noticeCounts === false) {
$snc = new Sitemap_notice_count();
$snc->orderBy('notice_date DESC');
// Fetch the first one to check up-to-date-itude
$n = $snc->find(true);
$today = self::today();
$noticeCounts = array();
if (!$n) { // No counts saved yet
$noticeCounts = self::initializeCounts();
} else if ($snc->notice_date < $today) { // There are counts but not up to today
$noticeCounts = self::fillInCounts($snc->notice_date);
} else if ($snc->notice_date == $today) { // Refresh today's
$noticeCounts[$today] = self::updateToday();
}
// starts with second-to-last date
while ($snc->fetch()) {
$noticeCounts[$snc->notice_date] = $snc->notice_count;
}
self::cacheSet('sitemap:notice:counts', $noticeCounts);
}
return $noticeCounts;
}
static function initializeCounts()
{
$firstDate = self::getFirstDate(); // awww
$today = self::today();
$counts = array();
for ($d = $firstDate; $d <= $today; $d = self::incrementDay($d)) {
$n = self::getCount($d);
self::insertCount($d, $n);
$counts[$d] = $n;
}
return $counts;
}
static function fillInCounts($lastDate)
{
$today = self::today();
$counts = array();
$n = self::getCount($lastDate);
self::updateCount($lastDate, $n);
$counts[$lastDate] = $n;
for ($d = self::incrementDay($lastDate); $d <= $today; $d = self::incrementDay($d)) {
$n = self::getCount($d);
self::insertCount($d, $n);
}
return $counts;
}
static function updateToday()
{
$today = self::today();
$n = self::getCount($today);
self::updateCount($today, $n);
return $n;
}
static function getCount($d)
{
$notice = new Notice();
$notice->whereAdd('created BETWEEN "'.$d.' 00:00:00" AND "'.self::incrementDay($d).' 00:00:00"');
$notice->whereAdd('is_local = ' . Notice::LOCAL_PUBLIC);
$n = $notice->count();
return $n;
}
static function insertCount($d, $n)
{
$snc = new Sitemap_notice_count();
$snc->notice_date = DB_DataObject_Cast::date($d);
$snc->notice_count = $n;
$snc->created = common_sql_now();
$snc->modified = $snc->created;
if (!$snc->insert()) {
common_log(LOG_WARNING, "Could not save user counts for '$d'");
}
}
static function updateCount($d, $n)
{
$snc = Sitemap_notice_count::staticGet('notice_date', DB_DataObject_Cast::date($d));
if (empty($snc)) {
throw new Exception("No such registration date: $d");
}
$orig = clone($snc);
$snc->notice_date = DB_DataObject_Cast::date($d);
$snc->notice_count = $n;
$snc->created = common_sql_now();
$snc->modified = $snc->created;
if (!$snc->update($orig)) {
common_log(LOG_WARNING, "Could not save user counts for '$d'");
}
}
static function incrementDay($d)
{
$dt = self::dateStrToInt($d);
return self::dateIntToStr($dt + 24 * 60 * 60);
}
static function dateStrToInt($d)
{
return strtotime($d.' 00:00:00');
}
static function dateIntToStr($dt)
{
return date('Y-m-d', $dt);
}
static function getFirstDate()
{
$n = new Notice();
$n->selectAdd();
$n->selectAdd('date(min(created)) as first_date');
if ($n->find(true)) {
return $n->first_date;
} else {
// Is this right?
return self::dateIntToStr(time());
}
}
static function today()
{
return self::dateIntToStr(time());
}
}

View File

@ -0,0 +1,284 @@
<?php
/**
* Data class for counting user registrations by date
*
* PHP version 5
*
* @category Data
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://status.net/
*
* 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')) {
exit(1);
}
require_once INSTALLDIR . '/classes/Memcached_DataObject.php';
/**
* Data class for counting users by date
*
* We make a separate sitemap for each user registered by date.
* To save ourselves some processing effort, we cache this data
*
* @category Action
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl.html AGPLv3
* @link http://status.net/
*
* @see DB_DataObject
*/
class Sitemap_user_count extends Memcached_DataObject
{
public $__table = 'sitemap_user_count'; // table name
public $registration_date; // date primary_key not_null
public $user_count; // int(4)
public $created;
public $modified;
/**
* Get an instance by key
*
* This is a utility method to get a single instance with a given key value.
*
* @param string $k Key to use to lookup (usually 'user_id' for this class)
* @param mixed $v Value to lookup
*
* @return Sitemap_user_count object found, or null for no hits
*
*/
function staticGet($k, $v=null)
{
return Memcached_DataObject::staticGet('Sitemap_user_count', $k, $v);
}
/**
* return table definition for DB_DataObject
*
* DB_DataObject needs to know something about the table to manipulate
* instances. This method provides all the DB_DataObject needs to know.
*
* @return array array of column definitions
*/
function table()
{
return array('registration_date' => DB_DATAOBJECT_DATE + DB_DATAOBJECT_NOTNULL,
'user_count' => DB_DATAOBJECT_INT,
'created' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL,
'modified' => DB_DATAOBJECT_STR + DB_DATAOBJECT_DATE + DB_DATAOBJECT_TIME + DB_DATAOBJECT_NOTNULL);
}
/**
* return key definitions for DB_DataObject
*
* DB_DataObject needs to know about keys that the table has; this function
* defines them.
*
* @return array key definitions
*/
function keys()
{
return array('registration_date' => 'K');
}
function sequenceKey()
{
return array(false, false, false);
}
/**
* return key definitions for Memcached_DataObject
*
* Our caching system uses the same key definitions, but uses a different
* method to get them.
*
* @return array key definitions
*/
function keyTypes()
{
return $this->keys();
}
static function getAll()
{
$userCounts = self::cacheGet('sitemap:user:counts');
if ($userCounts === false) {
$suc = new Sitemap_user_count();
$suc->orderBy('registration_date DESC');
// Fetch the first one to check up-to-date-itude
$n = $suc->find(true);
$today = self::today();
$userCounts = array();
if (!$n) { // No counts saved yet
$userCounts = self::initializeCounts();
} else if ($suc->registration_date < $today) { // There are counts but not up to today
$userCounts = self::fillInCounts($suc->registration_date);
} else if ($suc->registration_date == $today) { // Refresh today's
$userCounts[$today] = self::updateToday();
}
// starts with second-to-last date
while ($suc->fetch()) {
$userCounts[$suc->registration_date] = $suc->user_count;
}
self::cacheSet('sitemap:user:counts', $userCounts);
}
return $userCounts;
}
static function initializeCounts()
{
$firstDate = self::getFirstDate(); // awww
$today = self::today();
$counts = array();
for ($d = $firstDate; $d <= $today; $d = self::incrementDay($d)) {
$n = self::getCount($d);
self::insertCount($d, $n);
$counts[$d] = $n;
}
return $counts;
}
static function fillInCounts($lastDate)
{
$today = self::today();
$counts = array();
$n = self::getCount($lastDate);
self::updateCount($lastDate, $n);
$counts[$lastDate] = $n;
for ($d = self::incrementDay($lastDate); $d <= $today; $d = self::incrementDay($d)) {
$n = self::getCount($d);
self::insertCount($d, $n);
}
return $counts;
}
static function updateToday()
{
$today = self::today();
$n = self::getCount($today);
self::updateCount($today, $n);
return $n;
}
static function getCount($d)
{
$user = new User();
$user->whereAdd('created BETWEEN "'.$d.' 00:00:00" AND "'.self::incrementDay($d).' 00:00:00"');
$n = $user->count();
return $n;
}
static function insertCount($d, $n)
{
$suc = new Sitemap_user_count();
$suc->registration_date = DB_DataObject_Cast::date($d);
$suc->user_count = $n;
$suc->created = common_sql_now();
$suc->modified = $suc->created;
if (!$suc->insert()) {
common_log(LOG_WARNING, "Could not save user counts for '$d'");
}
}
static function updateCount($d, $n)
{
$suc = Sitemap_user_count::staticGet('registration_date', DB_DataObject_Cast::date($d));
if (empty($suc)) {
throw new Exception("No such registration date: $d");
}
$orig = clone($suc);
$suc->registration_date = DB_DataObject_Cast::date($d);
$suc->user_count = $n;
$suc->created = common_sql_now();
$suc->modified = $suc->created;
if (!$suc->update($orig)) {
common_log(LOG_WARNING, "Could not save user counts for '$d'");
}
}
static function incrementDay($d)
{
$dt = self::dateStrToInt($d);
return self::dateIntToStr($dt + 24 * 60 * 60);
}
static function dateStrToInt($d)
{
return strtotime($d.' 00:00:00');
}
static function dateIntToStr($dt)
{
return date('Y-m-d', $dt);
}
static function getFirstDate()
{
$u = new User();
$u->selectAdd();
$u->selectAdd('date(min(created)) as first_date');
if ($u->find(true)) {
return $u->first_date;
} else {
// Is this right?
return self::dateIntToStr(time());
}
}
static function today()
{
return self::dateIntToStr(time());
}
}

View File

@ -0,0 +1,137 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Show list of user pages
*
* 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 Sitemap
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @copyright 2010 StatusNet, Inc.
* @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')) {
exit(1);
}
/**
* sitemap for users
*
* @category Sitemap
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
class NoticesitemapAction extends SitemapAction
{
var $notices = null;
var $j = 0;
function prepare($args)
{
parent::prepare($args);
$y = $this->trimmed('year');
$m = $this->trimmed('month');
$d = $this->trimmed('day');
$i = $this->trimmed('index');
$y += 0;
$m += 0;
$d += 0;
$i += 0;
$this->notices = $this->getNotices($y, $m, $d, $i);
$this->j = 0;
return true;
}
function nextUrl()
{
if ($this->j < count($this->notices)) {
$n = $this->notices[$this->j];
$this->j++;
return array(common_local_url('shownotice', array('notice' => $n[0])),
common_date_w3dtf($n[1]),
'never',
null);
} else {
return null;
}
}
function getNotices($y, $m, $d, $i)
{
$n = Notice::cacheGet("sitemap:notice:$y:$m:$d:$i");
if ($n === false) {
$notice = new Notice();
$begindt = sprintf('%04d-%02d-%02d 00:00:00', $y, $m, $d);
// XXX: estimates 1d == 24h, which screws up days
// with leap seconds (1d == 24h + 1s). Thankfully they're
// few and far between.
$theend = strtotime($begindt) + (24 * 60 * 60);
$enddt = common_sql_date($theend);
$notice->selectAdd();
$notice->selectAdd('id, created');
$notice->whereAdd("created >= '$begindt'");
$notice->whereAdd("created < '$enddt'");
$notice->whereAdd('is_local = ' . Notice::LOCAL_PUBLIC);
$notice->orderBy('created');
$offset = ($i-1) * SitemapPlugin::NOTICES_PER_MAP;
$limit = SitemapPlugin::NOTICES_PER_MAP;
$notice->limit($offset, $limit);
$notice->find();
$n = array();
while ($notice->fetch()) {
$n[] = array($notice->id, $notice->created);
}
$c = Cache::instance();
if (!empty($c)) {
$c->set(Cache::key("sitemap:notice:$y:$m:$d:$i"),
$n,
Cache::COMPRESSED,
((time() > $theend) ? (time() + 90 * 24 * 60 * 60) : (time() + 5 * 60)));
}
}
return $n;
}
}

View File

@ -0,0 +1,95 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Superclass for sitemap-generating actions
*
* 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 Sitemap
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @copyright 2010 StatusNet, Inc.
* @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')) {
exit(1);
}
/**
* superclass for sitemap actions
*
* @category Sitemap
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
class SitemapAction extends Action
{
/**
* handle the action
*
* @param array $args unused.
*
* @return void
*/
function handle($args)
{
header('Content-Type: text/xml; charset=UTF-8');
$this->startXML();
$this->elementStart('urlset', array('xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9'));
while (list($url, $lm, $cf, $p) = $this->nextUrl()) {
$this->showUrl($url, $lm, $cf, $p);
}
$this->elementEnd('urlset');
$this->endXML();
}
function showUrl($url, $lastMod=null, $changeFreq=null, $priority=null)
{
$this->elementStart('url');
$this->element('loc', null, $url);
if (!is_null($lastMod)) {
$this->element('lastmod', null, $lastMod);
}
if (!is_null($changeFreq)) {
$this->element('changefreq', null, $changeFreq);
}
if (!is_null($priority)) {
$this->element('priority', null, $priority);
}
$this->elementEnd('url');
}
function nextUrl()
{
return null;
}
function isReadOnly()
{
return true;
}
}

View File

@ -0,0 +1,128 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Generate sitemap index
*
* 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 Sitemap
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @copyright 2010 StatusNet, Inc.
* @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')) {
exit(1);
}
/**
* Show the sitemap index
*
* @category Sitemap
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
class SitemapindexAction extends Action
{
/**
* handle the action
*
* @param array $args unused.
*
* @return void
*/
function handle($args)
{
header('Content-Type: text/xml; charset=UTF-8');
$this->startXML();
$this->elementStart('sitemapindex', array('xmlns' => 'http://www.sitemaps.org/schemas/sitemap/0.9'));
$this->showNoticeSitemaps();
$this->showUserSitemaps();
$this->elementEnd('sitemapindex');
$this->endXML();
}
function showUserSitemaps()
{
$userCounts = Sitemap_user_count::getAll();
foreach ($userCounts as $dt => $cnt) {
$cnt = $cnt+0;
if ($cnt == 0) {
continue;
}
$n = (int)$cnt / (int)SitemapPlugin::USERS_PER_MAP;
if (($cnt % SitemapPlugin::USERS_PER_MAP) != 0) {
$n++;
}
for ($i = 1; $i <= $n; $i++) {
$this->showSitemap('user', $dt, $i);
}
}
}
function showNoticeSitemaps()
{
$noticeCounts = Sitemap_notice_count::getAll();
foreach ($noticeCounts as $dt => $cnt) {
if ($cnt == 0) {
continue;
}
$n = $cnt / SitemapPlugin::NOTICES_PER_MAP;
if ($cnt % SitemapPlugin::NOTICES_PER_MAP) {
$n++;
}
for ($i = 1; $i <= $n; $i++) {
$this->showSitemap('notice', $dt, $i);
}
}
}
function showSitemap($prefix, $dt, $i)
{
list($y, $m, $d) = explode('-', $dt);
$this->elementStart('sitemap');
$this->element('loc', null, common_local_url($prefix.'sitemap',
array('year' => $y,
'month' => $m,
'day' => $d,
'index' => $i)));
$begdate = strtotime("$y-$m-$d 00:00:00");
$enddate = $begdate + (24 * 60 * 60);
if ($enddate < time()) {
$this->element('lastmod', null, date(DATE_W3C, $enddate));
}
$this->elementEnd('sitemap');
}
}

View File

@ -0,0 +1,128 @@
<?php
/**
* StatusNet, the distributed open-source microblogging tool
*
* Show list of user pages
*
* 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 Sitemap
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @copyright 2010 StatusNet, Inc.
* @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')) {
exit(1);
}
/**
* sitemap for users
*
* @category Sitemap
* @package StatusNet
* @author Evan Prodromou <evan@status.net>
* @license http://www.fsf.org/licensing/licenses/agpl-3.0.html GNU Affero General Public License version 3.0
* @link http://status.net/
*/
class UsersitemapAction extends SitemapAction
{
var $users = null;
var $j = 0;
function prepare($args)
{
parent::prepare($args);
$y = $this->trimmed('year');
$m = $this->trimmed('month');
$d = $this->trimmed('day');
$i = $this->trimmed('index');
$y += 0;
$m += 0;
$d += 0;
$i += 0;
$this->users = $this->getUsers($y, $m, $d, $i);
$this->j = 0;
return true;
}
function nextUrl()
{
if ($this->j < count($this->users)) {
$nickname = $this->users[$this->j];
$this->j++;
return array(common_profile_url($nickname), null, null, '1.0');
} else {
return null;
}
}
function getUsers($y, $m, $d, $i)
{
$u = User::cacheGet("sitemap:user:$y:$m:$d:$i");
if ($u === false) {
$user = new User();
$begindt = sprintf('%04d-%02d-%02d 00:00:00', $y, $m, $d);
// XXX: estimates 1d == 24h, which screws up days
// with leap seconds (1d == 24h + 1s). Thankfully they're
// few and far between.
$theend = strtotime($begindt) + (24 * 60 * 60);
$enddt = common_sql_date($theend);
$user->selectAdd();
$user->selectAdd('nickname');
$user->whereAdd("created >= '$begindt'");
$user->whereAdd("created < '$enddt'");
$user->orderBy('created');
$offset = ($i-1) * SitemapPlugin::USERS_PER_MAP;
$limit = SitemapPlugin::USERS_PER_MAP;
$user->limit($offset, $limit);
$user->find();
while ($user->fetch()) {
$u[] = $user->nickname;
}
$c = Cache::instance();
if (!empty($c)) {
$c->set(Cache::key("sitemap:user:$y:$m:$d:$i"),
$u,
Cache::COMPRESSED,
((time() > $theend) ? (time() + 90 * 24 * 60 * 60) : (time() + 5 * 60)));
}
}
return $u;
}
}