Merge pull request #5003 from annando/fix-author-id

Update script to set author-id and owner-id
This commit is contained in:
Hypolite Petovan 2018-05-08 17:03:32 -04:00 committed by GitHub
commit f8139d1179
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 101 additions and 13 deletions

View File

@ -41,7 +41,7 @@ define('FRIENDICA_PLATFORM', 'Friendica');
define('FRIENDICA_CODENAME', 'The Tazmans Flax-lily');
define('FRIENDICA_VERSION', '2018.05-rc');
define('DFRN_PROTOCOL_VERSION', '2.23');
define('DB_UPDATE_VERSION', 1259);
define('DB_UPDATE_VERSION', 1260);
define('NEW_UPDATE_ROUTINE_VERSION', 1170);
/**

View File

@ -207,7 +207,7 @@ class DBStructure
public static function update($verbose, $action, $install = false, array $tables = null, array $definition = null) {
if ($action && !$install) {
Config::set('system', 'maintenance', 1);
Config::set('system', 'maintenance_reason', L10n::t(': Database update', DBM::date().' '.date('e')));
Config::set('system', 'maintenance_reason', L10n::t('%s: Database update', DBM::date().' '.date('e')));
}
$errors = '';

View File

@ -752,10 +752,11 @@ class Contact extends BaseObject
* @param string $url Contact URL
* @param integer $uid The user id for the contact (0 = public contact)
* @param boolean $no_update Don't update the contact
* @param array $default Default value for creating the contact when every else fails
*
* @return integer Contact ID
*/
public static function getIdForURL($url, $uid = 0, $no_update = false)
public static function getIdForURL($url, $uid = 0, $no_update = false, $default = [])
{
logger("Get contact data for url " . $url . " and user " . $uid . " - " . System::callstack(), LOGGER_DEBUG);
@ -810,12 +811,42 @@ class Contact extends BaseObject
}
// Get data from the gcontact table
$gcontact = dba::selectFirst('gcontact', ['name', 'nick', 'url', 'photo', 'addr', 'alias', 'network'], ['nurl' => normalise_link($url)]);
if (!DBM::is_result($gcontact)) {
return 0;
$fields = ['name', 'nick', 'url', 'photo', 'addr', 'alias', 'network'];
$contact = dba::selectFirst('gcontact', $fields, ['nurl' => normalise_link($url)]);
if (!DBM::is_result($contact)) {
$contact = dba::selectFirst('contact', $fields, ['nurl' => normalise_link($url)]);
}
$data = array_merge($data, $gcontact);
if (!DBM::is_result($contact)) {
$fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick',
'photo', 'keywords', 'location', 'about', 'network',
'priority', 'batch', 'request', 'confirm', 'poco'];
$contact = dba::selectFirst('contact', $fields, ['addr' => $url]);
}
if (!DBM::is_result($contact)) {
// The link could be provided as http although we stored it as https
$ssl_url = str_replace('http://', 'https://', $url);
$condition = ['alias' => [$url, normalise_link($url), $ssl_url]];
$contact = dba::selectFirst('contact', $fields, $condition);
}
if (!DBM::is_result($contact)) {
$fields = ['url', 'addr', 'alias', 'notify', 'poll', 'name', 'nick',
'photo', 'network', 'priority', 'batch', 'request', 'confirm'];
$condition = ['url' => [$url, normalise_link($url), $ssl_url]];
$contact = dba::selectFirst('fcontact', $fields, $condition);
}
if (!empty($default)) {
$contact = $default;
}
if (!DBM::is_result($contact)) {
return 0;
} else {
$data = array_merge($data, $contact);
}
}
if (!$contact_id && ($data["alias"] != '') && ($data["alias"] != $url)) {

View File

@ -52,17 +52,24 @@ class Item extends BaseObject
return false;
}
// To ensure the data integrity we do it in an transaction
dba::transaction();
// We cannot simply expand the condition to check for origin entries
// The condition needn't to be a simple array but could be a complex condition.
// And we have to execute this query before the update to ensure to fetch the same data.
$items = dba::select('item', ['id', 'origin'], $condition);
$success = dba::update('item', $fields, $condition);
if (!$success) {
dba::close($items);
dba::rollback();
return false;
}
$rows = dba::affected_rows();
// We cannot simply expand the condition to check for origin entries
// The condition needn't to be a simple array but could be a complex condition.
$items = dba::select('item', ['id', 'origin'], $condition);
while ($item = dba::fetch($items)) {
Term::insertFromTagFieldByItemId($item['id']);
Term::insertFromFileFieldByItemId($item['id']);
@ -74,6 +81,8 @@ class Item extends BaseObject
}
}
dba::close($items);
dba::commit();
return $rows;
}
@ -479,14 +488,20 @@ class Item extends BaseObject
// The contact-id should be set before "self::insert" was called - but there seems to be issues sometimes
$item["contact-id"] = self::contactId($item);
$item['author-id'] = defaults($item, 'author-id', Contact::getIdForURL($item["author-link"]));
$default = ['url' => $item['author-link'], 'name' => $item['author-name'],
'avatar' => $item['author-avatar'], 'network' => $item['network']];
$item['author-id'] = defaults($item, 'author-id', Contact::getIdForURL($item["author-link"], 0, false, $default));
if (Contact::isBlocked($item["author-id"])) {
logger('Contact '.$item["author-id"].' is blocked, item '.$item["uri"].' will not be stored');
return 0;
}
$item['owner-id'] = defaults($item, 'owner-id', Contact::getIdForURL($item["owner-link"]));
$default = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
'avatar' => $item['owner-avatar'], 'network' => $item['network']];
$item['owner-id'] = defaults($item, 'owner-id', Contact::getIdForURL($item["owner-link"], 0, false, $default));
if (Contact::isBlocked($item["owner-id"])) {
logger('Contact '.$item["owner-id"].' is blocked, item '.$item["uri"].' will not be stored');
@ -2008,7 +2023,7 @@ EOT;
{
$fields = ['uid', 'guid', 'title', 'body', 'created', 'edited', 'commented', 'received', 'changed',
'wall', 'private', 'pubmail', 'moderated', 'visible', 'spam', 'starred', 'bookmark', 'contact-id',
'deleted', 'origin', 'forum_mode', 'network', 'rendered-html', 'rendered-hash'];
'deleted', 'origin', 'forum_mode', 'network', 'author-id', 'owner-id', 'rendered-html', 'rendered-hash'];
$condition = ["`id` = ? AND (`parent` = ? OR `parent` = 0)", $itemid, $itemid];
$item = dba::selectFirst('item', $fields, $condition);

View File

@ -5,6 +5,8 @@ use Friendica\Core\Config;
use Friendica\Core\PConfig;
use Friendica\Core\Worker;
use Friendica\Database\DBM;
use Friendica\Model\Contact;
use Friendica\Model\Item;
use Friendica\Model\User;
require_once 'include/dba.php';
@ -184,3 +186,43 @@ WHERE `hook` LIKE 'plugin_%'");
// Make sure we install the new renamed ones
Addon::reload();
}
function update_1260() {
Config::set('system', 'maintenance', 1);
Config::set('system', 'maintenance_reason', L10n::t('%s: Updating author-id and owner-id in item and thread table. ', DBM::date().' '.date('e')));
$items = dba::p("SELECT `id`, `owner-link`, `owner-name`, `owner-avatar`, `network` FROM `item`
WHERE `owner-id` = 0 AND `owner-link` != ''");
while ($item = dba::fetch($items)) {
$contact = ['url' => $item['owner-link'], 'name' => $item['owner-name'],
'avatar' => $item['owner-avatar'], 'network' => $item['network']];
$cid = Contact::getIdForURL($item['owner-link'], 0, false, $contact);
if (empty($cid)) {
continue;
}
Item::update(['owner-id' => $cid], ['id' => $item['id']]);
}
dba::close($items);
dba::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
SET `thread`.`owner-id` = `item`.`owner-id` WHERE `thread`.`owner-id` = 0");
$items = dba::p("SELECT `id`, `author-link`, `author-name`, `author-avatar`, `network` FROM `item`
WHERE `author-id` = 0 AND `author-link` != ''");
while ($item = dba::fetch($items)) {
$contact = ['url' => $item['author-link'], 'name' => $item['author-name'],
'avatar' => $item['author-avatar'], 'network' => $item['network']];
$cid = Contact::getIdForURL($item['author-link'], 0, false, $contact);
if (empty($cid)) {
continue;
}
Item::update(['author-id' => $cid], ['id' => $item['id']]);
}
dba::close($items);
dba::e("UPDATE `thread` INNER JOIN `item` ON `thread`.`iid` = `item`.`id`
SET `thread`.`author-id` = `item`.`author-id` WHERE `thread`.`author-id` = 0");
Config::set('system', 'maintenance', 0);
return UPDATE_SUCCESS;
}