Commit 7a83c153 authored by Malcolm Blaney's avatar Malcolm Blaney

parse_hcard in microformats.php now returns the old name found in

the nickname cache, so that if it's changed the Reader module can
update the author.
parent ba587fe4
Pipeline #46015973 failed with stage
in 1 minute and 20 seconds
......@@ -221,6 +221,7 @@ class Post extends Base {
// should not be shown yet and return false so that it's hidden.
if ($title === '' && $description === '') return false;
if ($author === '') $author = $this->owner;
$author = $this->LookupUser($author, $base_url);
// Permalink page requires h-entry class on the parent group div so that
// comments can be nested. This is handled by the Page class.
......
......@@ -527,6 +527,8 @@ class Reader extends Base {
'"",".reader-item a.author-name","font-size","1.2em"',
'"",".reader-item a.author-name ","color","#222222"',
'"",".reader-item a.author-name:hover","color","#999999"',
'"",".reader-item .options","overflow","auto"',
'"",".reader-item .options","margin-top","5px"',
'"",".reader-reset","float","right"',
'"",".reader-clear","clear","both"',
'"",".reader h4","margin","10px 0"',
......@@ -703,42 +705,44 @@ class Reader extends Base {
$feed_author = '';
$h_feed_list = [];
$mf = Mf2\fetch($us_xml_url);
foreach ($mf['items'] as $mf_item) {
if (!isset($mf_item['type'])) continue;
if (isset($mf['items'])) {
foreach ($mf['items'] as $mf_item) {
if (!isset($mf_item['type'])) continue;
if (in_array('h-feed', $mf_item['type'])) {
if (isset($mf_item['properties']['name'][0])) {
$h_feed_list[] = $mf_item['properties']['name'][0];
}
else if (isset($mf_item['properties']['uid'][0])) {
$h_feed_list[] = $mf_item['properties']['uid'][0];
}
else if (!in_array('', $h_feed_list)) {
// Assume an h-feed without a name is the main feed on the page,
// and push it onto the front of the list.
array_unshift($h_feed_list, '');
}
}
// Also look for h-feed on children.
if (!isset($mf_item['children'][0]['type'])) continue;
foreach ($mf_item['children'] as $child) {
if (in_array('h-feed', $child['type'])) {
// The parent of this h-feed may be an h-card, so use the name as
// the feed author if a name isn't given for the h-feed.
if (in_array('h-card', $mf_item['type'])) {
$feed_author = $mf_item['properties']['name'][0];
if (in_array('h-feed', $mf_item['type'])) {
if (isset($mf_item['properties']['name'][0])) {
$h_feed_list[] = $mf_item['properties']['name'][0];
}
if (isset($child['properties']['name'][0])) {
$h_feed_list[] = $child['properties']['name'][0];
}
else if (isset($child['properties']['uid'][0])) {
$h_feed_list[] = $child['properties']['uid'][0];
else if (isset($mf_item['properties']['uid'][0])) {
$h_feed_list[] = $mf_item['properties']['uid'][0];
}
else if (!in_array('', $h_feed_list)) {
// Assume an h-feed without a name is the main feed on the page,
// and push it onto the front of the list.
array_unshift($h_feed_list, '');
}
}
// Also look for h-feed on children.
if (!isset($mf_item['children'][0]['type'])) continue;
foreach ($mf_item['children'] as $child) {
if (in_array('h-feed', $child['type'])) {
// The parent of this h-feed may be an h-card, so use the name as
// the feed author if a name isn't given for the h-feed.
if (in_array('h-card', $mf_item['type'])) {
$feed_author = $mf_item['properties']['name'][0];
}
if (isset($child['properties']['name'][0])) {
$h_feed_list[] = $child['properties']['name'][0];
}
else if (isset($child['properties']['uid'][0])) {
$h_feed_list[] = $child['properties']['uid'][0];
}
else if (!in_array('', $h_feed_list)) {
array_unshift($h_feed_list, '');
}
}
}
}
}
// get_all_discovered_feeds() returns an array of SimplePie_File objects.
......@@ -1797,7 +1801,7 @@ class Reader extends Base {
if (stripos($author_name, 'http') === 0) {
$regex = '/^https?:\/\/([^\/]+\/?[^\/]*)/i';
if (preg_match($regex, $author_name, $match)) {
$author_name = $match[1];
$author_name = trim($match[1], ' /');
}
}
if ($author_photo === '') {
......@@ -2737,6 +2741,8 @@ class Reader extends Base {
}
// html isn't escaped for author and categories to support h-card.
$us_author = $purifier->purify(htmlspecialchars_decode($us_author));
$us_author_name = '';
$us_old_name = '';
// Special case for items generated by facebook-atom.appspot.com, the
// author is always the first link in the content. Pass it to parse_hcard
// so that it gets stored in the nickname cache.
......@@ -2748,7 +2754,8 @@ class Reader extends Base {
$h_card = ['type' => ['h-card'],
'properties' => ['url' => [$us_author_url],
'name' => [$us_author_name]]];
$us_author = parse_hcard($h_card, true);
list($us_author_name, $us_author_photo,
$us_author_url, $us_old_name) = parse_hcard($h_card, true);
// Also remove the author from content.
$us_content = preg_replace($regex, '', $us_content, 1);
$content = $mysqli->escape_string($purifier->purify($us_content));
......@@ -2759,14 +2766,26 @@ class Reader extends Base {
// nickname cache.
$mf = Mf2\parse($us_author);
foreach ($mf['items'] as $mf_item) {
$us_author_name = parse_hcard($mf_item, true);
if ($us_author_name !== '') {
$us_author = $us_author_name;
break;
}
list($us_author_name, $us_author_photo,
$us_author_url, $us_old_name) = parse_hcard($mf_item, true);
if ($us_author_name !== '') break;
}
}
$author = $mysqli->escape_string($us_author);
$author = $mysqli->escape_string($us_author_name);
if ($us_old_name !== '' && $us_old_name !== $us_author_name) {
$old_name = $mysqli->escape_string($us_old_name);
$query = 'UPDATE reader_items SET author = "' . $author . '" WHERE ' .
'author = "' . $old_name . '" AND xml_url = "' . $xml_url . '"';
if (!$mysqli->query($query)) {
$this->Log('Reader->SaveItem 1: ' . $mysqli->error);
}
$query = 'UPDATE reader_channels SET author = "' . $author . '" ' .
'WHERE author = "' . $old_name . '" AND xml_url = "' . $xml_url . '"';
if (!$mysqli->query($query)) {
$this->Log('Reader->SaveItem 2: ' . $mysqli->error);
}
}
$us_category_list = [];
if ($all_categories = $item->get_categories()) {
foreach ($all_categories as $category) {
......@@ -2819,14 +2838,14 @@ class Reader extends Base {
$updated = $mysqli->affected_rows !== 0;
}
else {
$this->Log('Reader->SaveItem 1: ' . $mysqli->error);
$this->Log('Reader->SaveItem 3: ' . $mysqli->error);
}
// SimplePie creates it's own guid from the data if one wasn't explicitly
// set, so old items with the same permalink need to be removed.
$query = 'DELETE FROM reader_items WHERE permalink = "' . $permalink . '" '.
'AND xml_url = "' . $xml_url . '" AND guid != "' . $guid . '"';
if (!$mysqli->query($query)) {
$this->Log('Reader->SaveItem 2: ' . $mysqli->error);
$this->Log('Reader->SaveItem 4: ' . $mysqli->error);
}
$mysqli->close();
return $updated;
......
......@@ -78,7 +78,8 @@ var formData=new FormData();if(!formData){dobrado.log('Your browser doesn\'t sup
dobrado.log('Importing file...','info');var file=$('#reader-file-import').get(0).files[0];if(file){formData.append('file',file);formData.append('request','reader');formData.append('action','import');formData.append('url',location.href);formData.append('token',dobrado.token);$.ajax({url:'/php/request.php',data:formData,contentType:false,processData:false,type:'POST',success:function(response){if(dobrado.checkResponseError(response,'reader importFile')){return;}
importList=JSON.parse(response);importCount=0;if(importList.length!==0){var text='<p>Found <b>'+importList.length+'</b> feeds in imported file:</p>';$('.reader-discovered').html(text).dialog('open');addImportedFeed();}}});}
return false;}
function newFeedEvents(){(function tooltipEvents(){var tooltipClick=false;var channelOptions=$('#reader-channel-select').html();function tooltipContent(){var name=$(this).html();var link=$(this).attr('href');var shortLink=link;if(shortLink.length>50){shortLink=shortLink.substring(0,50)+'...';}
function newFeedEvents(){(function tooltipEvents(){var tooltipClick=false;var channelOptions=$('#reader-channel-select').html();function tooltipContent(){var name=$(this).html();var link=$(this).attr('href');var shortLink=link;var fields=shortLink.match(/^https?:\/\/(.*)$/);if(fields&&fields.length===2){shortLink=fields[1];}
if(shortLink.length>50){shortLink=shortLink.substring(0,50)+'...';}
var feed=$(this).parents('.reader-group').find('a.reader-group-link');var feedUrl=feed.attr('href');var photo=$(this).siblings('.author-photo-url');var content='';if(photo.length!==0){content+='<a class="tooltip-photo" href="'+
photo.attr('href')+'">'+photo.html()+'</a>';}
content+='<a class="tooltip-name" href="'+link+'">'+
......
......@@ -461,6 +461,10 @@ if (!this.dobrado.reader) {
var name = $(this).html();
var link = $(this).attr('href');
var shortLink = link;
var fields = shortLink.match(/^https?:\/\/(.*)$/);
if (fields && fields.length === 2) {
shortLink = fields[1];
}
if (shortLink.length > 50) {
shortLink = shortLink.substring(0, 50) + '...';
}
......
......@@ -237,7 +237,8 @@ function create_tables() {
'visitor VARCHAR(50),' .
'page VARCHAR(200),' .
'timestamp INT(10) UNSIGNED NOT NULL,' .
'message TEXT' .
'message TEXT,' .
'PRIMARY KEY(user, visitor, timestamp)' .
') ENGINE=MyISAM';
if (!$mysqli->query($query)) {
error_log('create log: ' . $mysqli->error);
......
......@@ -332,8 +332,9 @@ function parse_happ($source) {
return ['name' => $name, 'logo' => $logo, 'url' => $url];
}
function parse_hcard($author, $name_only = false) {
function parse_hcard($author, $show_repost = false) {
$us_author_name = '';
$us_old_name = '';
$us_photo = '';
$us_cache = '';
$us_author_photo = '';
......@@ -353,10 +354,13 @@ function parse_hcard($author, $name_only = false) {
$us_author_url = 'http://' . $us_author_url;
}
$url = $mysqli->escape_string($us_author_url);
// Check if there's an existing photo to avoid caching it again.
$query = 'SELECT photo, cache FROM nickname WHERE url = "' . $url . '"';
// Check if there's an existing photo to avoid caching it again. Also
// want to return the old name for the Reader module in case it changes.
$query = 'SELECT name, photo, cache FROM nickname WHERE ' .
'url = "' . $url . '"';
if ($mysqli_result = $mysqli->query($query)) {
if ($nickname = $mysqli_result->fetch_assoc()) {
$us_old_name = $nickname['name'];
$us_photo = $nickname['photo'];
$us_cache = $nickname['cache'];
}
......@@ -459,8 +463,8 @@ function parse_hcard($author, $name_only = false) {
if ($us_cache !== '') {
$us_author_photo = '<img class="thumb u-photo" src="' . $us_cache . '">';
}
return $name_only ? $us_author_name . $us_repost_name :
[$us_author_name, $us_author_photo, $us_author_url];
if ($show_repost) $us_author_name .= $us_repost_name;
return [$us_author_name, $us_author_photo, $us_author_url, $us_old_name];
}
function parse_hcite($target, $hcite_list) {
......
......@@ -333,14 +333,14 @@ class Control extends Base {
'"",".control .username","margin","6px 10px"',
'"' . $media . '",".control .username","display","none"',
'"",".control .info","position","absolute"',
'"",".control .info","width","18em"',
'"",".control .info","max-width","20em"',
'"",".control .info","padding","6px"',
'"",".control .info","z-index","1"',
'"",".control .info .message","padding-left","0.2em"',
'"",".control .info .message","font-family","Helvetica"',
'"",".control .error .message","font-family","Helvetica"',
'"",".control .error","position","absolute"',
'"",".control .error","width","18em"',
'"",".control .error","max-width","20em"',
'"",".control .error","padding","6px"',
'"",".control .error","z-index","1"',
'"",".control .error .message","padding-left","0.2em"'];
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment