This commit is contained in:
friendica 2012-07-05 16:06:43 -07:00
commit 8af78079cb
8 changed files with 235 additions and 44 deletions

View File

@ -12,7 +12,7 @@ require_once('include/cache.php');
define ( 'FRIENDICA_PLATFORM', 'Friendica');
define ( 'FRIENDICA_VERSION', '3.0.1395' );
define ( 'DFRN_PROTOCOL_VERSION', '2.23' );
define ( 'DB_UPDATE_VERSION', 1151 );
define ( 'DB_UPDATE_VERSION', 1152 );
define ( 'EOL', "<br />\r\n" );
define ( 'ATOM_TIME', 'Y-m-d\TH:i:s\Z' );

View File

@ -598,6 +598,19 @@ CREATE TABLE IF NOT EXISTS `item_id` (
-- --------------------------------------------------------
--
-- Table structure for table `locks`
--
CREATE TABLE `locks` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` char(128) NOT NULL,
`locked` tinyint(1) NOT NULL DEFAULT '0',
PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;
-- --------------------------------------------------------
--
-- Table structure for table `mail`
--

View File

@ -432,7 +432,7 @@ function probe_url($url, $mode = PROBE_NORMAL) {
intval(local_user())
);
if(count($x) && count($r)) {
$mailbox = construct_mailbox_name($r[0]);
$mailbox = construct_mailbox_name($r[0]);
$password = '';
openssl_private_decrypt(hex2bin($r[0]['pass']),$password,$x[0]['prvkey']);
$mbox = email_connect($mailbox,$r[0]['user'],$password);

View File

@ -70,6 +70,12 @@ function stripdcode_br_cb($s) {
}
//////////////////////
// The following "diaspora_ul" and "diaspora_ol" are only appropriate for the
// pre-Markdownify conversion. If Markdownify isn't used, use the non-Markdownify
// versions below
//////////////////////
/*
function diaspora_ul($s) {
// Replace "[*]" followed by any number (including zero) of
// spaces by "* " to match Diaspora's list format
@ -103,10 +109,38 @@ function diaspora_ol($s) {
else
return $s[0];
}
*/
//////////////////////
// Non-Markdownify versions of "diaspora_ol" and "diaspora_ul"
//////////////////////
function diaspora_ul($s) {
// Replace "[\\*]" followed by any number (including zero) of
// spaces by "* " to match Diaspora's list format
return preg_replace("/\[\\\\\*\]( *)/", "* ", $s[1]);
}
function diaspora_ol($s) {
// A hack: Diaspora will create a properly-numbered ordered list even
// if you use '1.' for each element of the list, like:
// 1. First element
// 1. Second element
// 1. Third element
return preg_replace("/\[\\\\\*\]( *)/", "1. ", $s[1]);
}
function bb2diaspora($Text,$preserve_nl = false) {
//////////////////////
// An attempt was made to convert bbcode to html and then to markdown
// consisting of the following lines.
// I'm undoing this as we have a lot of bbcode constructs which
// were simply getting lost, for instance bookmark, vimeo, video, youtube, events, etc.
// We can try this again, but need a very good test sequence to verify
// all the major bbcode constructs that we use are getting through.
//////////////////////
/*
// bbcode() will convert "[*]" into "<li>" with no closing "</li>"
// Markdownify() is unable to handle these, as it makes each new
// "<li>" into a deeper nested element until it crashes. So pre-format
@ -115,6 +149,7 @@ function bb2diaspora($Text,$preserve_nl = false) {
// Note that to get nested lists to work for Diaspora, we would need
// to define the closing tag for the list elements. So nested lists
// are going to be flattened out in Diaspora for now
$endlessloop = 0;
while ((((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false)) ||
((strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false)) ||
@ -129,16 +164,7 @@ function bb2diaspora($Text,$preserve_nl = false) {
$Text = preg_replace_callback("/\[ol\](.*?)\[\/ol\]/is", 'diaspora_ol', $Text);
}
//////////////////////
// An attempt was made to convert bbcode to html and then to markdown
// consisting of the following lines.
// I'm undoing this as we have a lot of bbcode constructs which
// were simply getting lost, for instance bookmark, vimeo, video, youtube, events, etc.
// We can try this again, but need a very good test sequence to verify
// all the major bbcode constructs that we use are getting through.
//////////////////////
*/
// Convert it to HTML - don't try oembed
// $Text = bbcode($Text, $preserve_nl, false);
@ -244,15 +270,21 @@ function bb2diaspora($Text,$preserve_nl = false) {
$Text = preg_replace("(\[size=(.*?)\](.*?)\[\/size\])is","$2",$Text);
// Check for list text
$Text = preg_replace_callback("/\[list\](.*?)\[\/list\]/is", 'diaspora_ul', $Text);
$Text = preg_replace_callback("/\[ul\](.*?)\[\/ul\]/is", 'diaspora_ul', $Text);
$Text = preg_replace_callback("/\[list=1\](.*?)\[\/list\]/is", 'diaspora_ol', $Text);
$Text = preg_replace_callback("/\[list=i\](.*?)\[\/list\]/s",'diaspora_ol', $Text);
$Text = preg_replace_callback("/\[list=I\](.*?)\[\/list\]/s", 'diaspora_ol', $Text);
$Text = preg_replace_callback("/\[list=a\](.*?)\[\/list\]/s", 'diaspora_ol', $Text);
$Text = preg_replace_callback("/\[list=A\](.*?)\[\/list\]/s", 'diaspora_ol', $Text);
$Text = preg_replace_callback("/\[ol\](.*?)\[\/ol\]/is", 'diaspora_ol', $Text);
// $Text = preg_replace("/\[li\](.*?)\[\/li\]/s", '<li>$1</li>' ,$Text);
$endlessloop = 0;
while ((((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false)) ||
((strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false)) ||
((strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false)) ||
((strpos($Text, "[/li]") !== false) && (strpos($Text, "[li]") !== false))) && (++$endlessloop < 20)) {
$Text = preg_replace_callback("/\[list\](.*?)\[\/list\]/is", 'diaspora_ul', $Text);
$Text = preg_replace_callback("/\[list=1\](.*?)\[\/list\]/is", 'diaspora_ol', $Text);
$Text = preg_replace_callback("/\[list=i\](.*?)\[\/list\]/s",'diaspora_ol', $Text);
$Text = preg_replace_callback("/\[list=I\](.*?)\[\/list\]/s", 'diaspora_ol', $Text);
$Text = preg_replace_callback("/\[list=a\](.*?)\[\/list\]/s", 'diaspora_ol', $Text);
$Text = preg_replace_callback("/\[list=A\](.*?)\[\/list\]/s", 'diaspora_ol', $Text);
$Text = preg_replace_callback("/\[ul\](.*?)\[\/ul\]/is", 'diaspora_ul', $Text);
$Text = preg_replace_callback("/\[ol\](.*?)\[\/ol\]/is", 'diaspora_ol', $Text);
$Text = preg_replace("/\[li\]( *)(.*?)\[\/li\]/s", '* $2' ,$Text);
}
// Just get rid of table tags since Diaspora doesn't support tables
$Text = preg_replace("/\[th\](.*?)\[\/th\]/s", '$1' ,$Text);

View File

@ -166,14 +166,14 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
// Check for list text
$Text = str_replace("[*]", "<li>", $Text);
$Text = preg_replace("/\[li\](.*?)\[\/li\]/ism", '<li>$1</li>' ,$Text);
// handle nested lists
$endlessloop = 0;
while ((((strpos($Text, "[/list]") !== false) && (strpos($Text, "[list") !== false)) ||
((strpos($Text, "[/ol]") !== false) && (strpos($Text, "[ol]") !== false)) ||
((strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false))) && (++$endlessloop < 20)) {
((strpos($Text, "[/ul]") !== false) && (strpos($Text, "[ul]") !== false)) ||
((strpos($Text, "[/li]") !== false) && (strpos($Text, "[li]") !== false))) && (++$endlessloop < 20)) {
$Text = preg_replace("/\[list\](.*?)\[\/list\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
$Text = preg_replace("/\[list=\](.*?)\[\/list\]/ism", '<ul class="listnone" style="list-style-type: none;">$1</ul>' ,$Text);
$Text = preg_replace("/\[list=1\](.*?)\[\/list\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
@ -183,6 +183,7 @@ function bbcode($Text,$preserve_nl = false, $tryoembed = true) {
$Text = preg_replace("/\[list=((?-i)A)\](.*?)\[\/list\]/ism", '<ul class="listupperalpha" style="list-style-type: upper-alpha;">$2</ul>' ,$Text);
$Text = preg_replace("/\[ul\](.*?)\[\/ul\]/ism", '<ul class="listbullet" style="list-style-type: circle;">$1</ul>' ,$Text);
$Text = preg_replace("/\[ol\](.*?)\[\/ol\]/ism", '<ul class="listdecimal" style="list-style-type: decimal;">$1</ul>' ,$Text);
$Text = preg_replace("/\[li\](.*?)\[\/li\]/ism", '<li>$1</li>' ,$Text);
}
$Text = preg_replace("/\[th\](.*?)\[\/th\]/sm", '<th>$1</th>' ,$Text);

View File

@ -5,6 +5,7 @@ require_once('include/items.php');
require_once('include/bb2diaspora.php');
require_once('include/contact_selectors.php');
require_once('include/queue_fn.php');
require_once('include/lock.php');
function diaspora_dispatch_public($msg) {
@ -113,27 +114,83 @@ function diaspora_get_contact_by_handle($uid,$handle) {
}
function find_diaspora_person_by_handle($handle) {
$person = false;
$update = false;
$r = q("select * from fcontact where network = '%s' and addr = '%s' limit 1",
dbesc(NETWORK_DIASPORA),
dbesc($handle)
);
if(count($r)) {
logger('find_diaspora_person_by handle: in cache ' . print_r($r,true), LOGGER_DEBUG);
// update record occasionally so it doesn't get stale
$d = strtotime($r[0]['updated'] . ' +00:00');
if($d > strtotime('now - 14 days'))
return $r[0];
$update = true;
}
logger('find_diaspora_person_by_handle: refresh',LOGGER_DEBUG);
require_once('include/Scrape.php');
$r = probe_url($handle, PROBE_DIASPORA);
if((count($r)) && ($r['network'] === NETWORK_DIASPORA)) {
add_fcontact($r,$update);
return ($r);
}
return false;
$got_lock = false;
do {
$r = q("select * from fcontact where network = '%s' and addr = '%s' limit 1",
dbesc(NETWORK_DIASPORA),
dbesc($handle)
);
if(count($r)) {
$person = $r[0];
logger('find_diaspora_person_by handle: in cache ' . print_r($r,true), LOGGER_DEBUG);
// update record occasionally so it doesn't get stale
$d = strtotime($person['updated'] . ' +00:00');
if($d < strtotime('now - 14 days'))
$update = true;
}
// FETCHING PERSON INFORMATION FROM REMOTE SERVER
//
// If the person isn't in our 'fcontact' table, or if he/she is but
// his/her information hasn't been updated for more than 14 days, then
// we want to fetch the person's information from the remote server.
//
// Note that $person isn't changed by this block of code unless the
// person's information has been successfully fetched from the remote
// server. So if $person was 'false' to begin with (because he/she wasn't
// in the local cache), it'll stay false, and if $person held the local
// cache information to begin with, it'll keep that information. That way
// if there's a problem with the remote fetch, we can at least use our
// cached information--it's better than nothing.
if((! $person) || ($update)) {
// Lock the function to prevent race conditions if multiple items
// come in at the same time from a person who doesn't exist in
// fcontact
$got_lock = lock_function('find_diaspora_person_by_handle', false);
if($got_lock) {
logger('find_diaspora_person_by_handle: create or refresh', LOGGER_DEBUG);
require_once('include/Scrape.php');
$r = probe_url($handle, PROBE_DIASPORA);
// Note that Friendica contacts can return a "Diaspora person"
// if Diaspora connectivity is enabled on their server
if((count($r)) && ($r['network'] === NETWORK_DIASPORA)) {
add_fcontact($r,$update);
$person = ($r);
}
unlock_function('find_diaspora_person_by_handle');
}
else {
logger('find_diaspora_person_by_handle: couldn\'t lock function', LOGGER_DEBUG);
if(! $person)
block_on_function_lock('find_diaspora_person_by_handle');
}
}
} while((! $person) && (! $got_lock));
// We need to try again if the person wasn't in 'fcontact' but the function was locked.
// The fact that the function was locked may mean that another process was creating the
// person's record. It could also mean another process was creating or updating an unrelated
// person.
//
// At any rate, we need to keep trying until we've either got the person or had a chance to
// try to fetch his/her remote information. But we don't want to block on locking the
// function, because if the other process is creating the record, then when we acquire the lock
// we'll dive right into creating another, duplicate record. We DO want to at least wait
// until the lock is released, so we don't flood the database with requests.
//
// If the person was in the 'fcontact' table, don't try again. It's not worth the time, since
// we do have some information for the person
return $person;
}

76
include/lock.php Normal file
View File

@ -0,0 +1,76 @@
<?php
// Provide some ability to lock a PHP function so that multiple processes
// can't run the function concurrently
if(! function_exists('lock_function')) {
function lock_function($fn_name, $block = true, $wait_sec = 2) {
if( $wait_sec == 0 )
$wait_sec = 2; // don't let the user pick a value that's likely to crash the system
$got_lock = false;
do {
q("LOCK TABLE locks WRITE");
$r = q("SELECT locked FROM locks WHERE name = '%s' LIMIT 1",
dbesc($fn_name)
);
if((count($r)) && (! $r[0]['locked'])) {
q("UPDATE locks SET locked = 1 WHERE name = '%s' LIMIT 1",
dbesc($fn_name)
);
$got_lock = true;
}
elseif(! $r) { // the Boolean value for count($r) should be equivalent to the Boolean value of $r
q("INSERT INTO locks ( name, locked ) VALUES ( '%s', 1 )",
dbesc($fn_name)
);
$got_lock = true;
}
q("UNLOCK TABLES");
if(($block) && (! $got_lock))
sleep($wait_sec);
} while(($block) && (! $got_lock));
logger('lock_function: function ' . $fn_name . ' with blocking = ' . $block . ' got_lock = ' . $got_lock, LOGGER_DEBUG);
return $got_lock;
}}
if(! function_exists('block_on_function_lock')) {
function block_on_function_lock($fn_name, $wait_sec = 2) {
if( $wait_sec == 0 )
$wait_sec = 2; // don't let the user pick a value that's likely to crash the system
do {
$r = q("SELECT locked FROM locks WHERE name = '%s' LIMIT 1",
dbesc($fn_name)
);
if(count($r) && $r[0]['locked'])
sleep($wait_sec);
} while(count($r) && $r[0]['locked']);
return;
}}
if(! function_exists('unlock_function')) {
function unlock_function($fn_name) {
//$r = q("LOCK TABLE lock WRITE");
$r = q("UPDATE locks SET locked = 0 WHERE name = '%s' LIMIT 1",
dbesc($fn_name)
);
//$r = q("UNLOCK TABLES");
logger('unlock_function: released lock for function ' . $fn_name, LOGGER_DEBUG);
return;
}}
?>

View File

@ -1,6 +1,6 @@
<?php
define( 'UPDATE_VERSION' , 1151 );
define( 'UPDATE_VERSION' , 1152 );
/**
*
@ -1307,3 +1307,15 @@ function update_1150() {
return UPDATE_SUCCESS;
}
function update_1151() {
$r = q("CREATE TABLE IF NOT EXISTS locks (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,
name CHAR( 128 ) NOT NULL ,
locked TINYINT( 1 ) NOT NULL DEFAULT '0'
) ENGINE = MYISAM DEFAULT CHARSET=utf8 ");
if (!$r)
return UPDATE_FAILED;
return UPDATE_SUCCESS;
}