Merge branch 'testing' of git@gitorious.org:statusnet/mainline into testing

This commit is contained in:
Evan Prodromou 2010-03-25 13:50:49 -04:00
commit 920acf8d96
17 changed files with 439 additions and 132 deletions

View File

@ -123,7 +123,7 @@ class ApiFavoriteCreateAction extends ApiAuthAction
return; return;
} }
$fave = Fave::addNew($this->user, $this->notice); $fave = Fave::addNew($this->user->getProfile(), $this->notice);
if (empty($fave)) { if (empty($fave)) {
$this->clientError( $this->clientError(

View File

@ -162,40 +162,29 @@ class FoafAction extends Action
if ($sub->find()) { if ($sub->find()) {
while ($sub->fetch()) { while ($sub->fetch()) {
if ($sub->token) {
$other = Remote_profile::staticGet('id', $sub->subscriber);
$profile = Profile::staticGet('id', $sub->subscriber); $profile = Profile::staticGet('id', $sub->subscriber);
} else { if (empty($profile)) {
$other = User::staticGet('id', $sub->subscriber);
$profile = Profile::staticGet('id', $sub->subscriber);
}
if (!$other) {
common_debug('Got a bad subscription: '.print_r($sub,true)); common_debug('Got a bad subscription: '.print_r($sub,true));
continue; continue;
} }
if (array_key_exists($other->uri, $person)) { $user = $profile->getUser();
$person[$other->uri][0] = BOTH; $other_uri = $profile->getUri();
if (array_key_exists($other_uri, $person)) {
$person[$other_uri][0] = BOTH;
} else { } else {
$person[$other->uri] = array(LISTENER, $person[$other_uri] = array(LISTENER,
$other->id, $profile->id,
$profile->nickname, $profile->nickname,
(empty($sub->token)) ? 'User' : 'Remote_profile'); $user ? 'local' : 'remote');
} }
$other->free();
$other = null;
unset($other);
$profile->free();
$profile = null;
unset($profile); unset($profile);
} }
} }
$sub->free();
$sub = null;
unset($sub); unset($sub);
foreach ($person as $uri => $p) { foreach ($person as $uri => $p) {
list($type, $id, $nickname, $cls) = $p; list($type, $id, $nickname, $local) = $p;
if ($type == BOTH) { if ($type == BOTH) {
$this->element('knows', array('rdf:resource' => $uri)); $this->element('knows', array('rdf:resource' => $uri));
} }
@ -206,8 +195,8 @@ class FoafAction extends Action
foreach ($person as $uri => $p) { foreach ($person as $uri => $p) {
$foaf_url = null; $foaf_url = null;
list($type, $id, $nickname, $cls) = $p; list($type, $id, $nickname, $local) = $p;
if ($cls == 'User') { if ($local == 'local') {
$foaf_url = common_local_url('foaf', array('nickname' => $nickname)); $foaf_url = common_local_url('foaf', array('nickname' => $nickname));
} }
$profile = Profile::staticGet($id); $profile = Profile::staticGet($id);
@ -216,7 +205,7 @@ class FoafAction extends Action
$this->element('knows', array('rdf:resource' => $this->user->uri)); $this->element('knows', array('rdf:resource' => $this->user->uri));
} }
$this->showMicrobloggingAccount($profile, $this->showMicrobloggingAccount($profile,
($cls == 'User') ? common_root_url() : null, ($local == 'local') ? common_root_url() : null,
$uri, $uri,
true); true);
if ($foaf_url) { if ($foaf_url) {
@ -275,33 +264,22 @@ class FoafAction extends Action
if ($sub->find()) { if ($sub->find()) {
while ($sub->fetch()) { while ($sub->fetch()) {
if (!empty($sub->token)) {
$other = Remote_profile::staticGet('id', $sub->subscribed);
$profile = Profile::staticGet('id', $sub->subscribed); $profile = Profile::staticGet('id', $sub->subscribed);
} else { if (empty($profile)) {
$other = User::staticGet('id', $sub->subscribed);
$profile = Profile::staticGet('id', $sub->subscribed);
}
if (empty($other)) {
common_debug('Got a bad subscription: '.print_r($sub,true)); common_debug('Got a bad subscription: '.print_r($sub,true));
continue; continue;
} }
$this->element('sioc:follows', array('rdf:resource' => $other->uri.'#acct')); $user = $profile->getUser();
$person[$other->uri] = array(LISTENEE, $other_uri = $profile->getUri();
$other->id, $this->element('sioc:follows', array('rdf:resource' => $other_uri.'#acct'));
$person[$other_uri] = array(LISTENEE,
$profile->id,
$profile->nickname, $profile->nickname,
(empty($sub->token)) ? 'User' : 'Remote_profile'); $user ? 'local' : 'remote');
$other->free();
$other = null;
unset($other);
$profile->free();
$profile = null;
unset($profile); unset($profile);
} }
} }
$sub->free();
$sub = null;
unset($sub); unset($sub);
} }

View File

@ -63,7 +63,8 @@ class Conversation extends Memcached_DataObject
} }
$orig = clone($conv); $orig = clone($conv);
$orig->uri = common_local_url('conversation', array('id' => $id)); $orig->uri = common_local_url('conversation', array('id' => $id),
null, null, false);
$result = $orig->update($conv); $result = $orig->update($conv);
if (empty($result)) { if (empty($result)) {

View File

@ -21,7 +21,15 @@ class Fave extends Memcached_DataObject
/* the code above is auto generated do not remove the tag below */ /* the code above is auto generated do not remove the tag below */
###END_AUTOCODE ###END_AUTOCODE
static function addNew($profile, $notice) { /**
* Save a favorite record.
* @fixme post-author notification should be moved here
*
* @param Profile $profile the local or remote user who likes
* @param Notice $notice the notice that is liked
* @return mixed false on failure, or Fave record on success
*/
static function addNew(Profile $profile, Notice $notice) {
$fave = null; $fave = null;

View File

@ -421,8 +421,10 @@ class Notice extends Memcached_DataObject
} }
$profile = Profile::staticGet($this->profile_id); $profile = Profile::staticGet($this->profile_id);
if (!empty($profile)) {
$profile->blowNoticeCount(); $profile->blowNoticeCount();
} }
}
/** /**
* Clear cache entries related to this notice at delete time. * Clear cache entries related to this notice at delete time.

View File

@ -1273,7 +1273,7 @@ class ApiAction extends Action
if (empty($local)) { if (empty($local)) {
return null; return null;
} else { } else {
return User_group::staticGet('id', $local->id); return User_group::staticGet('id', $local->group_id);
} }
} }

View File

@ -273,7 +273,7 @@ class FavCommand extends Command
function handle($channel) function handle($channel)
{ {
$notice = $this->getNotice($this->other); $notice = $this->getNotice($this->other);
$fave = Fave::addNew($this->user, $notice); $fave = Fave::addNew($this->user->getProfile(), $notice);
if (!$fave) { if (!$fave) {
$channel->error($this->user, _('Could not create favorite.')); $channel->error($this->user, _('Could not create favorite.'));

View File

@ -1529,7 +1529,8 @@ function common_user_uri(&$user)
function common_notice_uri(&$notice) function common_notice_uri(&$notice)
{ {
return common_local_url('shownotice', return common_local_url('shownotice',
array('notice' => $notice->id)); array('notice' => $notice->id),
null, null, false);
} }
// 36 alphanums - lookalikes (0, O, 1, I) = 32 chars = 5 bits // 36 alphanums - lookalikes (0, O, 1, I) = 32 chars = 5 bits

View File

@ -92,7 +92,7 @@ class UsersalmonAction extends SalmonAction
throw new ClientException("Not to anyone in reply to anything!"); throw new ClientException("Not to anyone in reply to anything!");
} }
$existing = Notice::staticGet('uri', $this->act->object->id); $existing = Notice::staticGet('uri', $this->act->objects[0]->id);
if (!empty($existing)) { if (!empty($existing)) {
common_log(LOG_ERR, "Not saving notice '{$existing->uri}'; already exists."); common_log(LOG_ERR, "Not saving notice '{$existing->uri}'; already exists.");
@ -143,7 +143,7 @@ class UsersalmonAction extends SalmonAction
function handleFavorite() function handleFavorite()
{ {
$notice = $this->getNotice($this->act->object); $notice = $this->getNotice($this->act->objects[0]);
$profile = $this->ensureProfile()->localProfile(); $profile = $this->ensureProfile()->localProfile();
$old = Fave::pkeyGet(array('user_id' => $profile->id, $old = Fave::pkeyGet(array('user_id' => $profile->id,
@ -164,7 +164,7 @@ class UsersalmonAction extends SalmonAction
*/ */
function handleUnfavorite() function handleUnfavorite()
{ {
$notice = $this->getNotice($this->act->object); $notice = $this->getNotice($this->act->objects[0]);
$profile = $this->ensureProfile()->localProfile(); $profile = $this->ensureProfile()->localProfile();
$fave = Fave::pkeyGet(array('user_id' => $profile->id, $fave = Fave::pkeyGet(array('user_id' => $profile->id,

View File

@ -27,6 +27,8 @@
* @link http://status.net/ * @link http://status.net/
*/ */
require_once 'Crypt/RSA.php';
class Magicsig extends Memcached_DataObject class Magicsig extends Memcached_DataObject
{ {
@ -50,7 +52,15 @@ class Magicsig extends Memcached_DataObject
{ {
$obj = parent::staticGet(__CLASS__, $k, $v); $obj = parent::staticGet(__CLASS__, $k, $v);
if (!empty($obj)) { if (!empty($obj)) {
return Magicsig::fromString($obj->keypair); $obj = Magicsig::fromString($obj->keypair);
// Double check keys: Crypt_RSA did not
// consistently generate good keypairs.
// We've also moved to 1024 bit keys.
if (strlen($obj->publicKey->modulus->toBits()) != 1024) {
$obj->delete();
return false;
}
} }
return $obj; return $obj;
@ -100,16 +110,16 @@ class Magicsig extends Memcached_DataObject
public function generate($user_id) public function generate($user_id)
{ {
$rsa = new SafeCrypt_RSA(); $rsa = new Crypt_RSA();
$keypair = $rsa->createKey(); $keypair = $rsa->createKey();
$rsa->loadKey($keypair['privatekey']); $rsa->loadKey($keypair['privatekey']);
$this->privateKey = new SafeCrypt_RSA(); $this->privateKey = new Crypt_RSA();
$this->privateKey->loadKey($keypair['privatekey']); $this->privateKey->loadKey($keypair['privatekey']);
$this->publicKey = new SafeCrypt_RSA(); $this->publicKey = new Crypt_RSA();
$this->publicKey->loadKey($keypair['publickey']); $this->publicKey->loadKey($keypair['publickey']);
$this->user_id = $user_id; $this->user_id = $user_id;
@ -161,7 +171,7 @@ class Magicsig extends Memcached_DataObject
{ {
common_log(LOG_DEBUG, "Adding ".$type." key: (".$mod .', '. $exp .")"); common_log(LOG_DEBUG, "Adding ".$type." key: (".$mod .', '. $exp .")");
$rsa = new SafeCrypt_RSA(); $rsa = new Crypt_RSA();
$rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1; $rsa->signatureMode = CRYPT_RSA_SIGNATURE_PKCS1;
$rsa->setHash('sha256'); $rsa->setHash('sha256');
$rsa->modulus = new Math_BigInteger(base64_url_decode($mod), 256); $rsa->modulus = new Math_BigInteger(base64_url_decode($mod), 256);

View File

@ -67,7 +67,7 @@
* @author Jim Wigginton <terrafrost@php.net> * @author Jim Wigginton <terrafrost@php.net>
* @copyright MMVI Jim Wigginton * @copyright MMVI Jim Wigginton
* @license http://www.gnu.org/licenses/lgpl.txt * @license http://www.gnu.org/licenses/lgpl.txt
* @version $Id: BigInteger.php,v 1.31 2010/03/01 17:28:19 terrafrost Exp $ * @version $Id: BigInteger.php,v 1.33 2010/03/22 22:32:03 terrafrost Exp $
* @link http://pear.php.net/package/Math_BigInteger * @link http://pear.php.net/package/Math_BigInteger
*/ */
@ -294,7 +294,7 @@ class Math_BigInteger {
$this->value = array(); $this->value = array();
} }
if ($x === 0) { if (empty($x)) {
return; return;
} }
@ -718,7 +718,7 @@ class Math_BigInteger {
* *
* Will be called, automatically, when serialize() is called on a Math_BigInteger object. * Will be called, automatically, when serialize() is called on a Math_BigInteger object.
* *
* @see __wakeup * @see __wakeup()
* @access public * @access public
*/ */
function __sleep() function __sleep()
@ -740,7 +740,7 @@ class Math_BigInteger {
* *
* Will be called, automatically, when unserialize() is called on a Math_BigInteger object. * Will be called, automatically, when unserialize() is called on a Math_BigInteger object.
* *
* @see __sleep * @see __sleep()
* @access public * @access public
*/ */
function __wakeup() function __wakeup()

View File

@ -59,14 +59,23 @@ class MagicEnvelope
} }
if ($xrd->links) { if ($xrd->links) {
if ($link = Discovery::getService($xrd->links, Magicsig::PUBLICKEYREL)) { if ($link = Discovery::getService($xrd->links, Magicsig::PUBLICKEYREL)) {
list($type, $keypair) = explode(',', $link['href']); $keypair = false;
if (empty($keypair)) { $parts = explode(',', $link['href']);
if (count($parts) == 2) {
$keypair = $parts[1];
} else {
// Backwards compatibility check for separator bug in 0.9.0 // Backwards compatibility check for separator bug in 0.9.0
list($type, $keypair) = explode(';', $link['href']); $parts = explode(';', $link['href']);
if (count($parts) == 2) {
$keypair = $parts[1];
} }
}
if ($keypair) {
return $keypair; return $keypair;
} }
} }
}
throw new Exception('Unable to locate signer public key'); throw new Exception('Unable to locate signer public key');
} }

View File

@ -1,18 +0,0 @@
<?php
require_once 'Crypt/RSA.php';
/**
* Crypt_RSA stores a Math_BigInteger with value 0, which triggers a bug
* in Math_BigInteger's wakeup function which spews notices to log or output.
* This wrapper replaces it with a version that survives serialization.
*/
class SafeCrypt_RSA extends Crypt_RSA
{
function __construct()
{
parent::__construct();
$this->zero = new SafeMath_BigInteger();
}
}

View File

@ -1,20 +0,0 @@
<?php
require_once 'Math/BigInteger.php';
/**
* Crypt_RSA stores a Math_BigInteger with value 0, which triggers a bug
* in Math_BigInteger's wakeup function which spews notices to log or output.
* This wrapper replaces it with a version that survives serialization.
*/
class SafeMath_BigInteger extends Math_BigInteger
{
function __wakeup()
{
if ($this->hex == '') {
$this->hex = '0';
}
parent::__wakeup();
}
}

View File

@ -53,7 +53,14 @@ class XRD
$xrd = new XRD(); $xrd = new XRD();
$dom = new DOMDocument(); $dom = new DOMDocument();
if (!$dom->loadXML($xml)) {
// Don't spew XML warnings to output
$old = error_reporting();
error_reporting($old & ~E_WARNING);
$ok = $dom->loadXML($xml);
error_reporting($old);
if (!$ok) {
throw new Exception("Invalid XML"); throw new Exception("Invalid XML");
} }
$xrd_element = $dom->getElementsByTagName('XRD')->item(0); $xrd_element = $dom->getElementsByTagName('XRD')->item(0);

View File

@ -40,6 +40,20 @@ class TestBase
} }
return true; return true;
} }
function assertTrue($a)
{
if (!$a) {
throw new Exception("Failed to assert true: got false");
}
}
function assertFalse($a)
{
if ($a) {
throw new Exception("Failed to assert false: got true");
}
}
} }
class OStatusTester extends TestBase class OStatusTester extends TestBase
@ -60,8 +74,17 @@ class OStatusTester extends TestBase
function run() function run()
{ {
$this->setup(); $this->setup();
$this->testLocalPost();
$this->testMentionUrl(); $methods = get_class_methods($this);
foreach ($methods as $method) {
if (strtolower(substr($method, 0, 4)) == 'test') {
print "\n";
print "== $method ==\n";
call_user_func(array($this, $method));
}
}
print "\n";
$this->log("DONE!"); $this->log("DONE!");
} }
@ -98,6 +121,45 @@ class OStatusTester extends TestBase
$post = $this->pub->post("@$base/$name should have this in home and replies"); $post = $this->pub->post("@$base/$name should have this in home and replies");
$this->sub->assertReceived($post); $this->sub->assertReceived($post);
} }
function testSubscribe()
{
$this->assertFalse($this->sub->hasSubscription($this->pub->getProfileUri()));
$this->assertFalse($this->pub->hasSubscriber($this->sub->getProfileUri()));
$this->sub->subscribe($this->pub->getProfileLink());
$this->assertTrue($this->sub->hasSubscription($this->pub->getProfileUri()));
$this->assertTrue($this->pub->hasSubscriber($this->sub->getProfileUri()));
}
function testPush()
{
$this->assertTrue($this->sub->hasSubscription($this->pub->getProfileUri()));
$this->assertTrue($this->pub->hasSubscriber($this->sub->getProfileUri()));
$name = $this->sub->username;
$post = $this->pub->post("Regular post, which $name should get via PuSH");
$this->sub->assertReceived($post);
}
function testMentionSubscribee()
{
$this->assertTrue($this->sub->hasSubscription($this->pub->getProfileUri()));
$this->assertFalse($this->pub->hasSubscription($this->sub->getProfileUri()));
$name = $this->pub->username;
$post = $this->sub->post("Just a quick note back to my remote subscribee @$name");
$this->pub->assertReceived($post);
}
function testUnsubscribe()
{
$this->assertTrue($this->sub->hasSubscription($this->pub->getProfileUri()));
$this->assertTrue($this->pub->hasSubscriber($this->sub->getProfileUri()));
$this->sub->unsubscribe($this->pub->getProfileLink());
$this->assertFalse($this->sub->hasSubscription($this->pub->getProfileUri()));
$this->assertFalse($this->pub->hasSubscriber($this->sub->getProfileUri()));
}
} }
class SNTestClient extends TestBase class SNTestClient extends TestBase
@ -202,6 +264,43 @@ class SNTestClient extends TestBase
return $dom; return $dom;
} }
protected function parseXml($path, $body)
{
$dom = new DOMDocument();
if ($dom->loadXML($body)) {
return $dom;
} else {
throw new Exception("Bogus XML data from $path:\n$body");
}
}
/**
* Make a hit to a REST-y XML page on the site, without authentication.
* @param string $path URL fragment for something relative to base
* @param array $params POST parameters to send
* @return DOMDocument
* @throws Exception on low-level error conditions
*/
protected function xml($path, $params=array())
{
$response = $this->hit($path, $params, true);
$body = $response->getBody();
return $this->parseXml($path, $body);
}
protected function parseJson($path, $body)
{
$data = json_decode($body, true);
if ($data !== null) {
if (!empty($data['error'])) {
throw new Exception("JSON API returned error: " . $data['error']);
}
return $data;
} else {
throw new Exception("Bogus JSON data from $path:\n$body");
}
}
/** /**
* Make an API hit to this site, with authentication. * Make an API hit to this site, with authentication.
* @param string $path URL fragment for something under 'api' folder * @param string $path URL fragment for something under 'api' folder
@ -215,22 +314,9 @@ class SNTestClient extends TestBase
$response = $this->hit("api/$path.$style", $params, true); $response = $this->hit("api/$path.$style", $params, true);
$body = $response->getBody(); $body = $response->getBody();
if ($style == 'json') { if ($style == 'json') {
$data = json_decode($body, true); return $this->parseJson($path, $body);
if ($data !== null) {
if (!empty($data['error'])) {
throw new Exception("JSON API returned error: " . $data['error']);
}
return $data;
} else {
throw new Exception("Bogus JSON data from $path:\n$body");
}
} else if ($style == 'xml' || $style == 'atom') { } else if ($style == 'xml' || $style == 'atom') {
$dom = new DOMDocument(); return $this->parseXml($path, $body);
if ($dom->loadXML($body)) {
return $dom;
} else {
throw new Exception("Bogus XML data from $path:\n$body");
}
} else { } else {
throw new Exception("API needs to be JSON, XML, or Atom"); throw new Exception("API needs to be JSON, XML, or Atom");
} }
@ -257,6 +343,24 @@ class SNTestClient extends TestBase
'submit' => 'Register')); 'submit' => 'Register'));
} }
/**
* @return string canonical URI/URL to profile page
*/
function getProfileUri()
{
$data = $this->api('account/verify_credentials', 'json');
$id = $data['id'];
return $this->basepath . '/user/' . $id;
}
/**
* @return string human-friendly URL to profile page
*/
function getProfileLink()
{
return $this->basepath . '/' . $this->username;
}
/** /**
* Check that the account has been registered and can be used. * Check that the account has been registered and can be used.
* On failure, throws a test failure exception. * On failure, throws a test failure exception.
@ -271,6 +375,7 @@ class SNTestClient extends TestBase
$this->assertEqual($this->fullname, $data['name']); $this->assertEqual($this->fullname, $data['name']);
$this->assertEqual($this->homepage, $data['url']); $this->assertEqual($this->homepage, $data['url']);
$this->assertEqual($this->bio, $data['description']); $this->assertEqual($this->bio, $data['description']);
$this->log(" looks good!");
} }
/** /**
@ -307,11 +412,11 @@ class SNTestClient extends TestBase
} }
$tries--; $tries--;
if ($tries) { if ($tries) {
$this->log("Didn't see it yet, waiting $timeout seconds"); $this->log(" didn't see it yet, waiting $timeout seconds");
sleep($timeout); sleep($timeout);
} }
} }
throw new Exception("Message $notice_uri not received by $this->username"); throw new Exception(" message $notice_uri not received by $this->username");
} }
/** /**
@ -341,30 +446,88 @@ class SNTestClient extends TestBase
} }
foreach ($entries as $entry) { foreach ($entries as $entry) {
if ($entry->id == $notice_uri) { if ($entry->id == $notice_uri) {
$this->log("found it $notice_uri"); $this->log(" found it $notice_uri");
return true; return true;
} }
//$this->log("nope... " . $entry->id);
} }
return false; return false;
} }
/**
* @param string $profile user page link or webfinger
*/
function subscribe($profile)
{
// This uses the command interface, since there's not currently
// a friendly Twit-API way to do a fresh remote subscription and
// the web form's a pain to use.
$this->post('follow ' . $profile);
}
/**
* @param string $profile user page link or webfinger
*/
function unsubscribe($profile)
{
// This uses the command interface, since there's not currently
// a friendly Twit-API way to do a fresh remote subscription and
// the web form's a pain to use.
$this->post('leave ' . $profile);
}
/** /**
* Check that this account is subscribed to the given profile. * Check that this account is subscribed to the given profile.
* @param string $profile_uri URI for the profile to check for * @param string $profile_uri URI for the profile to check for
* @return boolean
*/ */
function assertHasSubscription($profile_uri) function hasSubscription($profile_uri)
{ {
throw new Exception('tbi'); $this->log("Checking if $this->username has a subscription to $profile_uri");
$me = $this->getProfileUri();
return $this->checkSubscription($me, $profile_uri);
} }
/** /**
* Check that this account is subscribed to by the given profile. * Check that this account is subscribed to by the given profile.
* @param string $profile_uri URI for the profile to check for * @param string $profile_uri URI for the profile to check for
* @return boolean
*/ */
function assertHasSubscriber($profile_uri) function hasSubscriber($profile_uri)
{ {
throw new Exception('tbi'); $this->log("Checking if $this->username is subscribed to by $profile_uri");
$me = $this->getProfileUri();
return $this->checkSubscription($profile_uri, $me);
}
protected function checkSubscription($subscriber, $subscribed)
{
// Using FOAF as the API methods for checking the social graph
// currently are unfriendly to remote profiles
$ns_foaf = 'http://xmlns.com/foaf/0.1/';
$ns_sioc = 'http://rdfs.org/sioc/ns#';
$ns_rdf = 'http://www.w3.org/1999/02/22-rdf-syntax-ns#';
$dom = $this->xml($this->username . '/foaf');
$agents = $dom->getElementsByTagNameNS($ns_foaf, 'Agent');
foreach ($agents as $agent) {
$agent_uri = $agent->getAttributeNS($ns_rdf, 'about');
if ($agent_uri == $subscriber) {
$follows = $agent->getElementsByTagNameNS($ns_sioc, 'follows');
foreach ($follows as $follow) {
$target = $follow->getAttributeNS($ns_rdf, 'resource');
if ($target == ($subscribed . '#acct')) {
$this->log(" confirmed $subscriber subscribed to $subscribed");
return true;
}
}
$this->log(" we found $subscriber but they don't follow $subscribed");
return false;
}
}
$this->log(" can't find $subscriber in {$this->username}'s social graph.");
return false;
} }
} }

166
scripts/fixup_deletions.php Executable file
View File

@ -0,0 +1,166 @@
#!/usr/bin/env php
<?php
/*
* StatusNet - a 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/>.
*/
define('INSTALLDIR', realpath(dirname(__FILE__) . '/..'));
$longoptions = array('dry-run', 'start=', 'end=');
$helptext = <<<END_OF_USERROLE_HELP
fixup_deletions.php [options]
Finds notices posted by deleted users and cleans them up.
Stray incompletely deleted items cause various fun problems!
--dry-run look but don't touch
--start=N start looking at profile_id N instead of 1
--end=N end looking at profile_id N instead of the max
END_OF_USERROLE_HELP;
require_once INSTALLDIR.'/scripts/commandline.inc';
/**
* Find the highest profile_id currently listed in the notice table;
* this field is indexed and should return very quickly.
*
* We check notice.profile_id rather than profile.id because we're
* looking for notices left behind after deletion; if the most recent
* accounts were deleted, we wouldn't have them from profile.
*
* @return int
* @access private
*/
function get_max_profile_id()
{
$query = 'SELECT MAX(profile_id) AS id FROM notice';
$profile = new Profile();
$profile->query($query);
if ($profile->fetch()) {
return intval($profile->id);
} else {
die("Something went awry; could not look up max used profile_id.");
}
}
/**
* Check for profiles in the given id range that are missing, presumed deleted.
*
* @param int $start beginning profile.id, inclusive
* @param int $end final profile.id, inclusive
* @return array of integer profile.ids
* @access private
*/
function get_missing_profiles($start, $end)
{
$query = sprintf("SELECT id FROM profile WHERE id BETWEEN %d AND %d",
$start, $end);
$profile = new Profile();
$profile->query($query);
$all = range($start, $end);
$known = array();
while ($row = $profile->fetch()) {
$known[] = intval($profile->id);
}
unset($profile);
$missing = array_diff($all, $known);
return $missing;
}
/**
* Look for stray notices from this profile and, if present, kill them.
*
* @param int $profile_id
* @param bool $dry if true, we won't delete anything
*/
function cleanup_missing_profile($profile_id, $dry)
{
$notice = new Notice();
$notice->profile_id = $profile_id;
$notice->find();
if ($notice->N == 0) {
return;
}
$s = ($notice->N == 1) ? '' : 's';
print "Deleted profile $profile_id has $notice->N stray notice$s:\n";
while ($notice->fetch()) {
print " notice $notice->id";
if ($dry) {
print " (skipped; dry run)\n";
} else {
$victim = clone($notice);
try {
$victim->delete();
print " (deleted)\n";
} catch (Exception $e) {
print " FAILED: ";
print $e->getMessage();
print "\n";
}
}
}
}
$dry = have_option('dry-run');
$max_profile_id = get_max_profile_id();
$chunk = 1000;
if (have_option('start')) {
$begin = intval(get_option_value('start'));
} else {
$begin = 1;
}
if (have_option('end')) {
$final = min($max_profile_id, intval(get_option_value('end')));
} else {
$final = $max_profile_id;
}
if ($begin < 1) {
die("Silly human, you can't begin before profile number 1!\n");
}
if ($final < $begin) {
die("Silly human, you can't end at $final if it's before $begin!\n");
}
// Identify missing profiles...
for ($start = $begin; $start <= $final; $start += $chunk) {
$end = min($start + $chunk - 1, $final);
print "Checking for missing profiles between id $start and $end";
if ($dry) {
print " (dry run)";
}
print "...\n";
$missing = get_missing_profiles($start, $end);
foreach ($missing as $profile_id) {
cleanup_missing_profile($profile_id, $dry);
}
}
echo "done.\n";