From 55ee9a1c72554d41aa5005e0d1a2c3d1f622a20a Mon Sep 17 00:00:00 2001 From: Adam Magness Date: Wed, 10 Jan 2018 11:46:05 -0500 Subject: [PATCH 1/5] Move files to src Move files.php and create Term class --- include/files.php | 55 ---------------------------------- src/Model/Term.php | 74 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 74 insertions(+), 55 deletions(-) delete mode 100644 include/files.php create mode 100644 src/Model/Term.php diff --git a/include/files.php b/include/files.php deleted file mode 100644 index 64fe6a310..000000000 --- a/include/files.php +++ /dev/null @@ -1,55 +0,0 @@ -/ism", $message["file"], $files)) { - foreach ($files[1] as $file) { - q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`) VALUES (%d, %d, %d, %d, '%s')", - intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), dbesc($file)); - } - } -} - -function create_files_from_itemuri($itemuri, $uid) -{ - $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid)); - - if (count($messages)) { - foreach ($messages as $message) - create_files_from_item($message["id"]); - } -} - -function update_files_for_items() -{ - $messages = q("SELECT `id` FROM `item` where file !=''"); - - foreach ($messages as $message) { - echo $message["id"] . "\n"; - create_files_from_item($message["id"]); - } -} diff --git a/src/Model/Term.php b/src/Model/Term.php new file mode 100644 index 000000000..3b327b582 --- /dev/null +++ b/src/Model/Term.php @@ -0,0 +1,74 @@ +/ism", $message["file"], $files)) { + foreach ($files[1] as $file) { + q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`) VALUES (%d, %d, %d, %d, '%s')", + intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), dbesc($file)); + } + } + } + + /** + * @param string $itemuri item uri + * @param integer $uid uid + * @return void + */ + function create_files_from_itemuri($itemuri, $uid) + { + $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid)); + + if (count($messages)) { + foreach ($messages as $message) + create_files_from_item($message["id"]); + } + } + + /** + * @return void + */ + function update_files_for_items() + { + $messages = q("SELECT `id` FROM `item` where file !=''"); + + foreach ($messages as $message) { + echo $message["id"] . "\n"; + create_files_from_item($message["id"]); + } + } +} \ No newline at end of file From dc175b8e12823927a4119d17272e771e8d7033ea Mon Sep 17 00:00:00 2001 From: Adam Magness Date: Wed, 10 Jan 2018 12:05:20 -0500 Subject: [PATCH 2/5] Update functions and calls Change function names, calls and use dba functions. --- include/items.php | 8 ++++---- include/text.php | 10 +++++----- src/Model/Item.php | 4 ++-- src/Model/Term.php | 20 ++++++++++---------- src/Protocol/DFRN.php | 6 +++--- 5 files changed, 24 insertions(+), 24 deletions(-) diff --git a/include/items.php b/include/items.php index 3ddb1f279..83f7f76a8 100644 --- a/include/items.php +++ b/include/items.php @@ -12,6 +12,7 @@ use Friendica\Database\DBM; use Friendica\Model\Contact; use Friendica\Model\GContact; use Friendica\Model\Group; +use Friendica\Model\Term; use Friendica\Model\User; use Friendica\Object\Image; use Friendica\Protocol\DFRN; @@ -21,7 +22,6 @@ use Friendica\Util\ParseUrl; require_once 'include/bbcode.php'; require_once 'include/tags.php'; -require_once 'include/files.php'; require_once 'include/text.php'; require_once 'include/threads.php'; require_once 'include/plaintext.php'; @@ -1109,7 +1109,7 @@ function item_store($arr, $force_parent = false, $notify = false, $dontcache = f * This is not perfect - but a workable solution until we found the reason for the problem. */ create_tags_from_item($current_post); - create_files_from_item($current_post); + Term::createFromItem($current_post); /* * If this is now the last-child, force all _other_ children of this parent to *not* be last-child @@ -2157,7 +2157,7 @@ function drop_item($id, $interactive = true) { array('id' => $item['id'])); create_tags_from_item($item['id']); - create_files_from_item($item['id']); + Term::createFromItem($item['id']); delete_thread($item['id'], $item['parent-uri']); // clean up categories and tags so they don't end up as orphans @@ -2230,7 +2230,7 @@ function drop_item($id, $interactive = true) { array('parent-uri' => $item['parent-uri'], 'uid' => $item['uid'])); create_tags_from_itemuri($item['parent-uri'], $item['uid']); - create_files_from_itemuri($item['parent-uri'], $item['uid']); + Term::createFromItemURI($item['parent-uri'], $item['uid']); delete_thread_uri($item['parent-uri'], $item['uid']); // ignore the result } else { diff --git a/include/text.php b/include/text.php index 7ed561bbb..f82a2ea3d 100644 --- a/include/text.php +++ b/include/text.php @@ -10,6 +10,7 @@ use Friendica\Core\Config; use Friendica\Core\PConfig; use Friendica\Core\System; use Friendica\Database\DBM; +use Friendica\Model\Term; use Friendica\Util\Map; require_once "include/friendica_smarty.php"; @@ -1895,9 +1896,8 @@ function file_tag_update_pconfig($uid, $file_old, $file_new, $type = 'file') { return true; } -function file_tag_save_file($uid, $item, $file) { - require_once "include/files.php"; - +function file_tag_save_file($uid, $item, $file) +{ if (! intval($uid)) { return false; } @@ -1915,7 +1915,7 @@ function file_tag_save_file($uid, $item, $file) { ); } - create_files_from_item($item); + Term::createFromItem($item); $saved = PConfig::get($uid, 'system', 'filetags'); if (!strlen($saved) || !stristr($saved, '[' . file_tag_encode($file) . ']')) { @@ -1955,7 +1955,7 @@ function file_tag_unsave_file($uid, $item, $file, $cat = false) { intval($uid) ); - create_files_from_item($item); + Term::createFromItem($item); $r = q("SELECT `oid` FROM `term` WHERE `term` = '%s' AND `otype` = %d AND `type` = %d AND `uid` = %d", dbesc($file), diff --git a/src/Model/Item.php b/src/Model/Item.php index 49cb499c3..2ad610cca 100644 --- a/src/Model/Item.php +++ b/src/Model/Item.php @@ -7,10 +7,10 @@ namespace Friendica\Model; use Friendica\Core\Worker; +use Friendica\Model\Term; use dba; require_once 'include/tags.php'; -require_once 'include/files.php'; require_once 'include/threads.php'; class Item @@ -52,7 +52,7 @@ class Item } create_tags_from_item($item['id']); - create_files_from_item($item['id']); + Term::createFromItem($item['id']); update_thread($item['id']); Worker::add(PRIORITY_HIGH, "Notifier", 'edit_post', $item['id']); diff --git a/src/Model/Term.php b/src/Model/Term.php index 3b327b582..8b007a93a 100644 --- a/src/Model/Term.php +++ b/src/Model/Term.php @@ -4,15 +4,17 @@ */ namespace Friendica\Model; +use dba; + class Term { /** * @param integer $itemid item id * @return void */ - function create_files_from_item($itemid) + public static function createFromItem($itemid) { - $messages = q("SELECT `guid`, `uid`, `id`, `edited`, `deleted`, `file`, `parent` FROM `item` WHERE `id` = %d LIMIT 1", intval($itemid)); + $messages = dba::selectFirst('item', ['guid', 'uid', 'id', 'edited', 'deleted', 'file', 'parent'], ['id' => $itemid]); if (!$messages) { return; } @@ -31,15 +33,13 @@ class Term if (preg_match_all("/\[(.*?)\]/ism", $message["file"], $files)) { foreach ($files[1] as $file) { - q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`) VALUES (%d, %d, %d, %d, '%s')", - intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_FILE), dbesc($file)); + dba::insert('term', ['uid' => $message["uid"], 'oid' => $itemid, 'otype' => TERM_OBJ_POST, 'type' => TERM_FILE, 'term' => $file]); } } if (preg_match_all("/\<(.*?)\>/ism", $message["file"], $files)) { foreach ($files[1] as $file) { - q("INSERT INTO `term` (`uid`, `oid`, `otype`, `type`, `term`) VALUES (%d, %d, %d, %d, '%s')", - intval($message["uid"]), intval($itemid), intval(TERM_OBJ_POST), intval(TERM_CATEGORY), dbesc($file)); + dba::insert('term', ['uid' => $message["uid"], 'oid' => $itemid, 'otype' => TERM_OBJ_POST, 'type' => TERM_CATEGORY, 'term' => $file]); } } } @@ -49,26 +49,26 @@ class Term * @param integer $uid uid * @return void */ - function create_files_from_itemuri($itemuri, $uid) + public static function createFromItemURI($itemuri, $uid) { $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid)); if (count($messages)) { foreach ($messages as $message) - create_files_from_item($message["id"]); + self::createFromItem($message["id"]); } } /** * @return void */ - function update_files_for_items() + private function update_files_for_items() { $messages = q("SELECT `id` FROM `item` where file !=''"); foreach ($messages as $message) { echo $message["id"] . "\n"; - create_files_from_item($message["id"]); + self::createFromItem($message["id"]); } } } \ No newline at end of file diff --git a/src/Protocol/DFRN.php b/src/Protocol/DFRN.php index 4d63f2b0b..509d05e6c 100644 --- a/src/Protocol/DFRN.php +++ b/src/Protocol/DFRN.php @@ -17,6 +17,7 @@ use Friendica\Model\Contact; use Friendica\Model\GContact; use Friendica\Model\Group; use Friendica\Model\Profile; +use Friendica\Model\Term; use Friendica\Model\User; use Friendica\Object\Image; use Friendica\Protocol\OStatus; @@ -32,7 +33,6 @@ require_once "include/enotify.php"; require_once "include/threads.php"; require_once "include/items.php"; require_once "include/tags.php"; -require_once "include/files.php"; require_once "include/event.php"; require_once "include/text.php"; require_once "include/html2bbcode.php"; @@ -2912,7 +2912,7 @@ class DFRN intval($importer["uid"]) ); create_tags_from_itemuri($uri, $importer["uid"]); - create_files_from_itemuri($uri, $importer["uid"]); + Term::createFromItemURI($uri, $importer["uid"]); update_thread_uri($uri, $importer["uid"]); } else { $r = q( @@ -2925,7 +2925,7 @@ class DFRN intval($importer["uid"]) ); create_tags_from_itemuri($uri, $importer["uid"]); - create_files_from_itemuri($uri, $importer["uid"]); + Term::createFromItemURI($uri, $importer["uid"]); update_thread_uri($uri, $importer["importer_uid"]); if ($item["last-child"]) { // ensure that last-child is set in case the comment that had it just got wiped. From 483c4e380316d779a2f069efec6f081da64f61f2 Mon Sep 17 00:00:00 2001 From: Adam Magness Date: Wed, 10 Jan 2018 12:08:56 -0500 Subject: [PATCH 3/5] selectFirst dba::selectFirst hasn't been merged yet! --- src/Model/Term.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Model/Term.php b/src/Model/Term.php index 8b007a93a..fa9574610 100644 --- a/src/Model/Term.php +++ b/src/Model/Term.php @@ -14,7 +14,7 @@ class Term */ public static function createFromItem($itemid) { - $messages = dba::selectFirst('item', ['guid', 'uid', 'id', 'edited', 'deleted', 'file', 'parent'], ['id' => $itemid]); + $messages = dba::select('item', ['guid', 'uid', 'id', 'edited', 'deleted', 'file', 'parent'], ['id' => $itemid], ['limit' => 1]); if (!$messages) { return; } From 7723309310c33107cac0ca680e7057589361e898 Mon Sep 17 00:00:00 2001 From: Adam Magness Date: Wed, 10 Jan 2018 12:11:11 -0500 Subject: [PATCH 4/5] Remove requires Remove left over require_once statements. --- include/text.php | 5 ++--- mod/item.php | 1 - 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/include/text.php b/include/text.php index f82a2ea3d..232cbc5c9 100644 --- a/include/text.php +++ b/include/text.php @@ -1926,9 +1926,8 @@ function file_tag_save_file($uid, $item, $file) return true; } -function file_tag_unsave_file($uid, $item, $file, $cat = false) { - require_once "include/files.php"; - +function file_tag_unsave_file($uid, $item, $file, $cat = false) +{ if (! intval($uid)) { return false; } diff --git a/mod/item.php b/mod/item.php index 868ed54ad..e234ada98 100644 --- a/mod/item.php +++ b/mod/item.php @@ -29,7 +29,6 @@ use Friendica\Util\Emailer; require_once 'include/enotify.php'; require_once 'include/tags.php'; -require_once 'include/files.php'; require_once 'include/threads.php'; require_once 'include/text.php'; require_once 'include/items.php'; From 594461189b3e996e39186a9d5c62ab133601790d Mon Sep 17 00:00:00 2001 From: Adam Magness Date: Wed, 10 Jan 2018 12:51:51 -0500 Subject: [PATCH 5/5] Review updates require_once, foreach braces, new line EOF, and remove unneeded function. --- src/Model/Term.php | 22 ++++++---------------- 1 file changed, 6 insertions(+), 16 deletions(-) diff --git a/src/Model/Term.php b/src/Model/Term.php index fa9574610..07dec5213 100644 --- a/src/Model/Term.php +++ b/src/Model/Term.php @@ -6,6 +6,8 @@ namespace Friendica\Model; use dba; +require_once "include/dba.php"; + class Term { /** @@ -14,7 +16,7 @@ class Term */ public static function createFromItem($itemid) { - $messages = dba::select('item', ['guid', 'uid', 'id', 'edited', 'deleted', 'file', 'parent'], ['id' => $itemid], ['limit' => 1]); + $messages = dba::select('item', ['uid', 'deleted', 'file'], ['id' => $itemid], ['limit' => 1]); if (!$messages) { return; } @@ -54,21 +56,9 @@ class Term $messages = q("SELECT `id` FROM `item` WHERE uri ='%s' AND uid=%d", dbesc($itemuri), intval($uid)); if (count($messages)) { - foreach ($messages as $message) + foreach ($messages as $message) { self::createFromItem($message["id"]); + } } } - - /** - * @return void - */ - private function update_files_for_items() - { - $messages = q("SELECT `id` FROM `item` where file !=''"); - - foreach ($messages as $message) { - echo $message["id"] . "\n"; - self::createFromItem($message["id"]); - } - } -} \ No newline at end of file +}