From 078dd7f3350170f31c8d44c4e8783cba417d5630 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Sol=C3=ADs?= Date: Wed, 12 Feb 2025 21:27:45 +0000 Subject: [PATCH 1/4] feat: Detect MariaDB vs MySQL automatically --- friendica-clean-database.sh | 16 ++++++++++++++-- friendica-delete-old-users.sh | 18 ++++++++++-------- friendica-remove-old-photos-parallel.sh | 14 +++++++++++--- 3 files changed, 35 insertions(+), 13 deletions(-) diff --git a/friendica-clean-database.sh b/friendica-clean-database.sh index 2aa808c..f622cf8 100755 --- a/friendica-clean-database.sh +++ b/friendica-clean-database.sh @@ -4,9 +4,21 @@ limit=1000 folder=/var/www/friendica user=friendica phpversion=php8.2 -dbengine=mariadb +dbengine="" +if [[ -n $(type mariadb) ]]; then + dbengine="mariadb" +elif [[ -n $(type mysql) ]]; then + dbengine="mysql" +else + exit +fi db=friendica -dboptimizer=mariadb-optimize +dboptimizer="" +if [[ -n $(type mariadb-optimize) ]]; then + dboptimizer="mariadb-optimize" +elif [[ -n $(type mysqloptimize) ]]; then + dbengine="mysqloptimize" +fi intense_optimizations=${1:-"0"} bash -c "cd ${folder} && sudo -u ${user} ${phpversion} bin/console.php maintenance 1 \"Database maintenance\"" #&> /dev/null diff --git a/friendica-delete-old-users.sh b/friendica-delete-old-users.sh index 70ddeda..52daa1d 100755 --- a/friendica-delete-old-users.sh +++ b/friendica-delete-old-users.sh @@ -1,4 +1,13 @@ #!/bin/bash +#Check for mariadb vs. mysql +dbengine="" +if [[ -n $(type mariadb) ]]; then + dbengine="mariadb" +elif [[ -n $(type mysql) ]]; then + dbengine="mysql" +else + exit +fi db="friendica" url=friendica.example.net avatarfolder=/var/www/friendica/avatar @@ -23,21 +32,14 @@ loop() { "${dbengine}" "${db}" -N -B -q -e "delete from \`post-thread-user\` where \`author-id\` = ${lineb} or \`causer-id\` = ${lineb} or \`owner-id\` = ${lineb}" "${dbengine}" "${db}" -N -B -q -e "delete from \`post-user\` where \`author-id\` = ${lineb} or \`causer-id\` = ${lineb} or \`owner-id\` = ${lineb}" "${dbengine}" "${db}" -N -B -q -e "delete from \`post-tag\` where cid = ${lineb}" + "${dbengine}" "${db}" -N -B -q -e "create temporary table tmp_post (select \`uri-id\` from \`post\` where \`owner-id\` = ${lineb} or \`author-id\` = ${lineb} or \`causer-id\` = ${lineb}); delete p.* from \`post-content\` p inner join \`tmp_post\` t where p.\`uri-id\` = t.\`uri-id\`;" "${dbengine}" "${db}" -N -B -q -e "delete from \`post\` where \`owner-id\` = ${lineb} or \`author-id\` = ${lineb} or \`causer-id\` = ${lineb}" - "${dbengine}" "${db}" -N -B -q -e "delete from \`post-content\` where \`uri-id\` = ${lineb}" "${dbengine}" "${db}" -N -B -q -e "delete from \`photo\` where \`contact-id\` = ${lineb}" "${dbengine}" "${db}" -N -B -q -e "delete from \`contact\` where \`id\` = ${lineb}" "${dbengine}" "${db}" -N -B -q -e "delete from \`apcontact\` where \`uri-id\` = ${lineb}" "${dbengine}" "${db}" -N -B -q -e "delete from \`diaspora-contact\` where \`uri-id\` = ${lineb}" } -#Check for mariadb vs. mysql -dbengine="" -if [[ -n $(type mariadb) ]]; then - dbengine="mariadb" -elif [[ -n $(type mysql) ]]; then - dbengine="mysql" -fi #Check if our dependencies are installed if [[ -n $(type curl) && -n "${dbengine}" && -n $(type "${dbengine}") && -n $(type date) ]]; then date diff --git a/friendica-remove-old-photos-parallel.sh b/friendica-remove-old-photos-parallel.sh index dfb7d39..05bc27a 100755 --- a/friendica-remove-old-photos-parallel.sh +++ b/friendica-remove-old-photos-parallel.sh @@ -1,6 +1,14 @@ #!/bin/bash IFS=" " +dbengine="" +if [[ -n $(type mariadb) ]]; then + dbengine="mariadb" +elif [[ -n $(type mysql) ]]; then + dbengine="mysql" +else + exit +fi #Set your parameters here url=friendica.example.net db=friendica @@ -10,7 +18,7 @@ loop() { #Parse each file in folder folderescaped=${folder////\\/} ky=$(echo "${y}" | sed -e "s/${folderescaped}/https:\/\/${url}/g" -e "s/-[0-9]*\..*\$//g") - f=$(mariadb "${db}" -N -B -q -e "select photo from contact where photo like '${ky}%' limit 1") + f=$("${dbengine}" "${db}" -N -B -q -e "select photo from contact where photo like '${ky}%' limit 1") if [[ $? -eq 0 && -z ${f} && -f ${y} ]]; then rm -rvf "${y}" & if [[ $(jobs -r -p | wc -l) -ge $(($(getconf _NPROCESSORS_ONLN) * 2)) ]]; then @@ -28,7 +36,7 @@ date #Go to the Friendica installation cd "${folderavatar}" || exit #indexlength=$((49 + ${#url})) -#mariadb "${db}" -e "alter table contact add index if not exists photo_index (photo(${indexlength}))" +#"${dbengine}" "${db}" -e "alter table contact add index if not exists photo_index (photo(${indexlength}))" n=0 d=0 while read -r x; do @@ -45,5 +53,5 @@ while read -r x; do done < <(find "${x}" -type f) fi done < <(find "${folderavatar}" -depth -mindepth 1 -maxdepth 1 -type d) -#mariadb "${db}" -e "alter table contact drop index photo_index" +#"${dbengine}" "${db}" -e "alter table contact drop index photo_index" date From 337fb7c15507b5ff3b1738a964660a0d1b87ebe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Sol=C3=ADs?= Date: Wed, 12 Feb 2025 21:29:26 +0000 Subject: [PATCH 2/4] feat: Add the ability to remove spurious AddContact jobs; minor fixes --- friendica-non-follower-featured-posts.sh | 34 +++++++++++++++--------- 1 file changed, 22 insertions(+), 12 deletions(-) diff --git a/friendica-non-follower-featured-posts.sh b/friendica-non-follower-featured-posts.sh index ebbf79a..aaf5005 100755 --- a/friendica-non-follower-featured-posts.sh +++ b/friendica-non-follower-featured-posts.sh @@ -22,12 +22,12 @@ printf "\rContactDiscovery\t%s\n\r" "${cbmax}" cc=100 ccmax=0 while [[ ${cc} -gt 0 ]]; do - cc=$(sudo mariadb friendica -B -N -q -e "create temporary table tmp_fetchfeaturedposts (select \`url\` from \`contact\` where \`id\` in (select \`contact-id\` from \`group_member\`) or \`id\` in (select \`cid\` from \`user-contact\`) or \`id\` in (select \`uid\` from \`user\`)); delete from workerqueue where \`command\`= \"FetchFeaturedPosts\" and regexp_replace(substring_index(substring_index(\`parameter\`, '\\\"', -2), '\\\"', 1), '\\\\\\\\', '') not in (select \`url\` from tmp_fetchfeaturedposts) limit ${cc}; select row_count();") + cc=$(sudo mariadb friendica -B -N -q -e "create temporary table tmp_addcontact (select \`url\` from \`contact\` where \`id\` in (select \`contact-id\` from \`group_member\`) or \`id\` in (select \`cid\` from \`user-contact\`) or \`id\` in (select \`uid\` from \`user\`)); delete from workerqueue where \`command\`= \"AddContact\" and regexp_replace(substring_index(substring_index(\`parameter\`, '\\\"', -2), '\\\"', 1), '\\\\\\\\', '') not in (select \`url\` from tmp_addcontact) limit ${cc}; select row_count();") ccmax=$((ccmax + cc)) - printf "\rFetchFeaturedPosts\t%s\r" "${ccmax}" + printf "\rAddContact\t%s\r" "${ccmax}" done -printf "\rFetchFeaturedPosts\t%s\n\r" "${ccmax}" -#echo "FetchFeaturedPosts $ccmax" +printf "\rAddContact\t%s\n\r" "${ccmax}" +#echo "AddContact $ccmax" cd=100 cdmax=0 @@ -42,19 +42,29 @@ printf "\rUpdateGServer\t\t%s\n\r" "${cdmax}" ce=100 cemax=0 while [[ ${ce} -gt 0 ]]; do - ce=$(sudo mariadb friendica -B -N -q -e "delete from workerqueue where command=\"ProcessQueue\" and pid=0 and done=0 limit ${ce}; select row_count();") + ce=$(sudo mariadb friendica -B -N -q -e "create temporary table tmp_fetchfeaturedposts (select \`url\` from \`contact\` where \`id\` in (select \`contact-id\` from \`group_member\`) or \`id\` in (select \`cid\` from \`user-contact\`) or \`id\` in (select \`uid\` from \`user\`)); delete from workerqueue where \`command\`= \"FetchFeaturedPosts\" and regexp_replace(substring_index(substring_index(\`parameter\`, '\\\"', -2), '\\\"', 1), '\\\\\\\\', '') not in (select \`url\` from tmp_fetchfeaturedposts) limit ${ce}; select row_count();") cemax=$((cemax + ce)) - printf "\rProcessQueue\t\t%s\r" "${cemax}" + printf "\rFetchFeaturedPosts\t%s\r" "${cemax}" done -printf "\rProcessQueue\t\t%s\n\r" "${cemax}" -#echo "ProcessQueue $cemax" +printf "\rFetchFeaturedPosts\t%s\n\r" "${cemax}" +#echo "FetchFeaturedPosts $cemax" cf=100 cfmax=0 while [[ ${cf} -gt 0 ]]; do - cf=$(sudo mariadb friendica -B -N -q -e "delete from workerqueue where \`id\` in (select distinct w2.\`id\` from workerqueue w1 inner join workerqueue w2 where w1.\`id\` > w2.\`id\` and w1.\`parameter\` = w2.\`parameter\` and w1.command = \"UpdateContact\" and w1.\`pid\` = 0 and w1.\`done\` = 0) limit ${cf}; select row_count();") + cf=$(sudo mariadb friendica -B -N -q -e "delete from workerqueue where command=\"ProcessQueue\" and pid=0 and done=0 limit ${cf}; select row_count();") cfmax=$((cfmax + cf)) - printf "\rWorkerQueue\t\t%s\r" "${cfmax}" + printf "\rProcessQueue\t\t%s\r" "${cfmax}" done -printf "\rWorkerQueue\t\t%s\n\r" "${cfmax}" -#echo "WorkerQueue $cfmax" +printf "\rProcessQueue\t\t%s\n\r" "${cfmax}" +#echo "ProcessQueue $cfmax" + +cg=100 +cgmax=0 +while [[ ${cg} -gt 0 ]]; do + cg=$(sudo mariadb friendica -B -N -q -e "delete from workerqueue where \`id\` in (select distinct w2.\`id\` from workerqueue w1 inner join workerqueue w2 where w1.\`id\` > w2.\`id\` and w1.\`parameter\` = w2.\`parameter\` and w1.command = \"UpdateContact\" and w1.\`pid\` = 0 and w1.\`done\` = 0) limit ${cg}; select row_count();") + cgmax=$((cgmax + cg)) + printf "\rWorkerQueue\t\t%s\r" "${cgmax}" +done +printf "\rWorkerQueue\t\t%s\n\r" "${cgmax}" +#echo "WorkerQueue $cgmax" From da644bf4c18caf2250b1af24052ed3620fe9df1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Sol=C3=ADs?= Date: Wed, 12 Feb 2025 21:31:53 +0000 Subject: [PATCH 3/4] feat: Detect MariaDB vs MySQL automatically; add specific check for ActivityPub; fix some checks --- friendica-find-missing-servers.sh | 67 ++++++++++++++++++------------- 1 file changed, 40 insertions(+), 27 deletions(-) diff --git a/friendica-find-missing-servers.sh b/friendica-find-missing-servers.sh index 56b2143..6ac35a5 100755 --- a/friendica-find-missing-servers.sh +++ b/friendica-find-missing-servers.sh @@ -1,4 +1,13 @@ #!/bin/bash +#Check for mariadb vs. mysql +dbengine="" +if [[ -n $(type mariadb) ]]; then + dbengine="mariadb" +elif [[ -n $(type mysql) ]]; then + dbengine="mysql" +else + exit +fi db="friendica" tmpfile="/tmp/sitesdown.txt" idsdownfile="/tmp/idsdown.txt" @@ -6,17 +15,31 @@ url=friendica.example.net avatarfolder=/var/www/friendica/avatar avatarfolderescaped=${avatarfolder////\\/} loop_1() { - sitereq=$(curl -s -L --head -m 30 --request GET "${a}") + site=$(echo "${sites}" | sed -e "s/http[s]*:\/\///g") + if [[ "${protocol}" == "apub" ]]; then + #For ActivityPub sites, we test the well-known Webfinger + #We also need a valid (known) user for the Webfinger test + user=$("${dbengine}" "${db}" -N -B -q -e "select \`addr\` from contact where baseurl = \"http://${site}\" or url = \"http://${site}\" or baseurl = \"https://${site}\" or url = \"https://${site}\" limit 1") + site_test="https://${site}/.well-known/webfinger?resource=acct:${user}" + #If the return message is in "application/jrd+json" format, the site is still up + #If the message contains a reference to Cloudflare, we don't add it to the list either, just in case + if ! grep -q -e "application/jrd+json" -e "HTTP.*200" -e "cloudflare" <(curl -s -L -I -m 30 -X HEAD "${site_test}"); then + echo "${site}" >>"${tmpfile}" + echo "Added ${site}" + fi + fi + #This is mostly for RSS feeds, we only check whether the site itself is up #Skip check if the message contains a reference to Cloudflare - status=$(echo "${sitereq}" | grep -e "200" -e "cloudflare") - if [[ -z ${status} ]]; then - echo "${a}" >>"${tmpfile}" - echo "Added ${a}" + if [[ "${protocol}" != "bsky" ]]; then + if ! grep -q -e "HTTP.*200" -e "cloudflare" <(curl -s -L -I -m 30 -X HEAD "https://${site}"); then + echo "${site}" >>"${tmpfile}" + echo "Added ${site}" + fi fi } loop_2() { echo "Finding users for ${b}" - "${dbengine}" "${db}" -N -B -q -e "select \`id\`, \`nick\`, \`baseurl\` from contact c where c.\`id\` not in (select \`contact-id\` from group_member) and (c.baseurl = \"${b}\" or c.url = \"${b}\")" | sudo tee -a "${idsdownfile}" #&> /dev/null + "${dbengine}" "${db}" -N -B -q -e "select \`id\`, \`nick\`, \`baseurl\` from contact c where c.\`id\` not in (select \`id\` from \`contact\` where \`id\` in (select \`contact-id\` from \`group_member\`) or \`id\` in (select \`cid\` from \`user-contact\`) or \`id\` in (select \`uid\` from \`user\`)) and (c.baseurl = \"http://${b}\" or c.url = \"http://${b}\" or c.baseurl = \"https://${b}\" or c.url = \"https://${b}\")" | sudo tee -a "${idsdownfile}" #&> /dev/null } loop_3() { @@ -28,7 +51,6 @@ loop_3() { if grep -v -q "${url}/avatar" <(echo "${photo}"); then #if [[ -z "${isavatar}" ]] phototrimmed=$(echo "${photo}" | sed -e "s/https:\/\/${url}\/avatar/${avatarfolderescaped}/g" -e "s/\?ts.*//g") - echo "${phototrimmed}" rm -rfv "${phototrimmed}" thumbtrimmed=$(echo "${thumb}" | sed -e "s/https:\/\/${url}\/avatar/${avatarfolderescaped}/g" -e "s/\?ts.*//g") rm -rfv "${thumbtrimmed}" @@ -40,43 +62,36 @@ loop_3() { "${dbengine}" "${db}" -N -B -q -e "delete from \`post-thread-user\` where \`author-id\` = ${lineb} or \`causer-id\` = ${lineb} or \`owner-id\` = ${lineb}" "${dbengine}" "${db}" -N -B -q -e "delete from \`post-user\` where \`author-id\` = ${lineb} or \`causer-id\` = ${lineb} or \`owner-id\` = ${lineb}" "${dbengine}" "${db}" -N -B -q -e "delete from \`post-tag\` where cid = ${lineb}" + "${dbengine}" "${db}" -N -B -q -e "create temporary table tmp_post (select \`uri-id\` from \`post\` where \`owner-id\` = ${lineb} or \`author-id\` = ${lineb} or \`causer-id\` = ${lineb}); delete p.* from \`post-content\` p inner join \`tmp_post\` t where p.\`uri-id\` = t.\`uri-id\`;" "${dbengine}" "${db}" -N -B -q -e "delete from \`post\` where \`owner-id\` = ${lineb} or \`author-id\` = ${lineb} or \`causer-id\` = ${lineb}" - "${dbengine}" "${db}" -N -B -q -e "delete from \`post-content\` where \`uri-id\` = ${lineb}" "${dbengine}" "${db}" -N -B -q -e "delete from \`photo\` where \`contact-id\` = ${lineb}" "${dbengine}" "${db}" -N -B -q -e "delete from \`contact\` where \`id\` = ${lineb}" "${dbengine}" "${db}" -N -B -q -e "delete from \`apcontact\` where \`uri-id\` = ${lineb}" "${dbengine}" "${db}" -N -B -q -e "delete from \`diaspora-contact\` where \`uri-id\` = ${lineb}" } -#Check for mariadb vs. mysql -dbengine="" -if [[ -n $(type mariadb) ]]; then - dbengine="mariadb" -elif [[ -n $(type mysql) ]]; then - dbengine="mysql" -fi #Check if our dependencies are installed if [[ -n $(type curl) && -n "${dbengine}" && -n $(type "${dbengine}") && -n $(type date) ]]; then date if [[ ! -f "${tmpfile}" ]]; then echo "Listing sites" - #sites=($("${dbengine}" "${db}" -N -B -q -e "select distinct baseurl from contact where baseurl != \"\"" | sort -n | uniq )) - sites=() - mapfile -t sites < <("${dbengine}" "${db}" -N -B -q -e "select distinct baseurl from contact where baseurl != \"\"" | sort -b -f -n | uniq -i) - echo "Amount of unique sites: ${#sites[@]}" - for a in "${sites[@]}"; do - loop_1 "${a}" & + siteslist=$("${dbengine}" "${db}" -N -B -q -e "select distinct baseurl, protocol from contact where baseurl != ''" | sort -b -f -n | sed -e "s/http:/https:/g" | uniq -i) + echo "Amount of unique sites: ${#siteslist[@]}" + while read -r sites protocol; do + loop_1 "${sites}" "${protocol}" & if [[ $(jobs -r -p | wc -l) -ge $(($(getconf _NPROCESSORS_ONLN) * 2)) ]]; then wait -n fi - done + done < <(echo "${siteslist}") wait fi sitesdown=() while read -r line; do sitesdown+=("${line}") done <"${tmpfile}" - echo "Amount of sites down: ${#sitesdown[@]} / ${#sites[@]}" + t=$(sort -n "${tmpfile}" | uniq) + echo "${t}" >"${tmpfile}" + echo "Amount of sites down: ${#sitesdown[@]} / ${#siteslist[@]}" if [[ ! -f "${idsdownfile}" ]]; then for b in "${sitesdown[@]}"; do loop_2 "${b}" & @@ -85,12 +100,10 @@ if [[ -n $(type curl) && -n "${dbengine}" && -n $(type "${dbengine}") && -n $(ty fi done wait - #cat "$idsdownfile" | sort -n | uniq > "$idsdownfile" + u=$(sort -n "${idsdownfile}" | uniq) + echo "${u}" >"${idsdownfile}" fi - #idsdown=() - #echo "$idsdownfile" | sort | uniq > "$idsdownfile" while read -r lineb nick baseurl; do - #idsdown+=($lineb) #The community no longer exists, delete loop_3 "${lineb}" "${nick}" "${baseurl}" & if [[ $(jobs -r -p | wc -l) -ge $(($(getconf _NPROCESSORS_ONLN) / 2)) ]]; then From b2427c2e75506e6729cd3e8766b3312694871fc5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Carlos=20Sol=C3=ADs?= Date: Wed, 12 Feb 2025 21:33:02 +0000 Subject: [PATCH 4/4] feat: Detect MariaDB vs MySQL automatically; remove some optimizations causing locks; fix some checks --- friendica-remove-invalid-photos.sh | 118 +++++++++++++++-------------- 1 file changed, 63 insertions(+), 55 deletions(-) diff --git a/friendica-remove-invalid-photos.sh b/friendica-remove-invalid-photos.sh index 0f90b60..e2ec812 100755 --- a/friendica-remove-invalid-photos.sh +++ b/friendica-remove-invalid-photos.sh @@ -4,7 +4,14 @@ url=friendica.example.net user=friendica group=www-data fileperm=660 -dbengine=mariadb +dbengine="" +if [[ -n $(type mariadb) ]]; then + dbengine="mariadb" +elif [[ -n $(type mysql) ]]; then + dbengine="mysql" +else + exit +fi db=friendica folder=/var/www/friendica timeout=60 @@ -52,16 +59,17 @@ nt=0 #Highest possible ID known maxid=$("${dbengine}" "${db}" -B -N -q -e "select max(\`id\`) from contact") #Limit per batch -limit=$(((maxid / 1000) + 1)) +#limit=$(((maxid / 1000) + 1)) +limit="${maxid}" dbcount=0 idcount=0 if [[ "${intensive_optimizations}" -gt 0 ]]; then #https:// = 8 characters | /avatar/ = 8 characters indexlength=$(("${#url}" + 16)) "${dbengine}" "${db}" -e "alter table contact add index if not exists photo_index (photo(${indexlength}))" - dbcount=$("${dbengine}" "${db}" -B -N -q -e "select count(\`id\`) from \`contact\` where (\`photo\` like 'https:\/\/${url}/avatar/%' or \`photo\` like '')") + dbcount=$("${dbengine}" "${db}" -B -N -q -e "select count(\`id\`) from \`contact\` where (\`photo\` like 'https:\/\/${url}/avatar/%' or (\`photo\` = '' and not \`avatar\` = '') or (\`avatar\` = '' and not \`photo\` = ''))") else - dbcount=$("${dbengine}" "${db}" -B -N -q -e "select count(\`id\`) from \`contact\` where (\`photo\` like 'https:\/\/${url}/avatar/%' or \`photo\` like '') and (\`id\` in (select \`cid\` from \`user-contact\`) or \`id\` in (select \`uid\` from \`user\`) or \`id\` in (select \`contact-id\` from \`group_member\`))") + dbcount=$("${dbengine}" "${db}" -B -N -q -e "select count(\`id\`) from \`contact\` where (\`photo\` like 'https:\/\/${url}/avatar/%' or (\`photo\` = '' and not \`avatar\` = '') or (\`avatar\` = '' and not \`photo\` = '')) and (\`id\` in (select \`cid\` from \`user-contact\`) or \`id\` in (select \`uid\` from \`user\`) or \`id\` in (select \`contact-id\` from \`group_member\`))") fi loop() { @@ -295,63 +303,63 @@ loop() { #Go to the Friendica installation cd "${folder}" || exit echo "${n} ${nt}" >"${nfile}" -until [[ "${idcount}" -ge "${dbcount}" || "${nt}" -gt "${dbcount}" || "${lastid}" -gt "${maxid}" ]]; do - c="" - if [[ "${intensive_optimizations}" -gt 0 ]]; then - c=$("${dbengine}" "${db}" -B -N -q -e "select \`id\` from \`contact\` where \`id\` > ${lastid} and (\`photo\` like \"https:\/\/${url}/avatar/%\" or \`photo\` like \"\") order by id limit ${limit}") - else - c=$("${dbengine}" "${db}" -B -N -q -e "select \`id\` from \`contact\` where \`id\` > ${lastid} and (\`photo\` like \"https:\/\/${url}/avatar/%\" or \`photo\` like \"\") and (id in (select cid from \`user-contact\`) or id in (select \`uid\` from \`user\`) or \`id\` in (select \`contact-id\` from \`group_member\`)) order by id limit ${limit}") +#until [[ "${idcount}" -ge "${dbcount}" || "${nt}" -gt "${dbcount}" || "${lastid}" -gt "${maxid}" ]]; do +c="" +if [[ "${intensive_optimizations}" -gt 0 ]]; then + c=$("${dbengine}" "${db}" -B -N -q -e "select \`id\` from \`contact\` where \`id\` > ${lastid} and (\`photo\` like 'https:\/\/${url}/avatar/%' or (\`photo\` = '' and not \`avatar\` = '') or (\`avatar\` = '' and not \`photo\` = '')) order by id limit ${limit}") +else + c=$("${dbengine}" "${db}" -B -N -q -e "select \`id\` from \`contact\` where \`id\` > ${lastid} and (\`photo\` like 'https:\/\/${url}/avatar/%' or (\`photo\` = '' and not \`avatar\` = '') or (\`avatar\` = '' and not \`photo\` = '')) and (id in (select cid from \`user-contact\`) or id in (select \`uid\` from \`user\`) or \`id\` in (select \`contact-id\` from \`group_member\`)) order by id limit ${limit}") +fi +while read -r id; do + if [[ -n "${id}" && $((10#${id})) -ge "${lastid}" ]]; then + lastid=$((10#${id})) + id=$((10#${id})) fi - while read -r id; do - if [[ -n "${id}" && $((10#${id})) -ge "${lastid}" ]]; then - lastid=$((10#${id})) - id=$((10#${id})) - fi - if [[ -n "${lastid}" ]]; then - idcount=$((idcount + 1)) - loop & - fi - until [[ $(jobs -r -p | wc -l) -lt $(($(getconf _NPROCESSORS_ONLN) * thread_multiplier)) ]]; do - wait -n - done - done < <(echo "${c}") - wait - #Read data before next iteration - rl=0 - ( - sleep 60s - if [[ "${rl}" -eq 0 ]]; then rm -rf "${nlock}"; fi - ) & - while [[ "${rl}" -eq 0 ]]; do - if [[ ! -f "${nlock}" ]]; then - touch "${nlock}" - fi - if [[ -f "${nlock}" && $(cat "${nlock}" 2>/dev/null || echo "") == "" ]]; then - rm -rf "${nlock}" && touch "${nlock}" && echo "${lastid}" | tee "${nlock}" &>/dev/null - if [[ -f "${nlock}" && $(grep -e "[0-9]" "${nlock}" 2>/dev/null || echo 0) == "${lastid}" ]]; then - read -r n_tmp_l nt_tmp_l <"${nfile}" || break - if [[ -n "${n_tmp_l}" && -n "${nt_tmp_l}" ]]; then - n="${n_tmp_l}" - nt="${nt_tmp_l}" - if [[ -f "${nlock}" ]]; then - rm -rf "${nlock}" && touch "${nlock}" && echo "" >"${nlock}" - fi - rl=1 - fi - elif [[ -f "${nlock}" ]]; then - nlm=$(grep -e "[0-9]" "${nlock}" 2>/dev/null || echo 0) - if [[ -n "${nlm}" ]]; then - nlmt=$((10#${nlm})) - if [[ "${nlmt}" -lt $((id + limit)) ]]; then - rm -rf "${nlock}" && touch "${nlock}" && echo "" >"${nlock}" - fi - else + if [[ -n "${lastid}" ]]; then + idcount=$((idcount + 1)) + loop & + fi + until [[ $(jobs -r -p | wc -l) -lt $(($(getconf _NPROCESSORS_ONLN) * thread_multiplier)) ]]; do + wait -n + done +done < <(echo "${c}") +wait +#Read data before next iteration +rl=0 +( + sleep 60s + if [[ "${rl}" -eq 0 ]]; then rm -rf "${nlock}"; fi +) & +while [[ "${rl}" -eq 0 ]]; do + if [[ ! -f "${nlock}" ]]; then + touch "${nlock}" + fi + if [[ -f "${nlock}" && $(cat "${nlock}" 2>/dev/null || echo "") == "" ]]; then + rm -rf "${nlock}" && touch "${nlock}" && echo "${lastid}" | tee "${nlock}" &>/dev/null + if [[ -f "${nlock}" && $(grep -e "[0-9]" "${nlock}" 2>/dev/null || echo 0) == "${lastid}" ]]; then + read -r n_tmp_l nt_tmp_l <"${nfile}" || break + if [[ -n "${n_tmp_l}" && -n "${nt_tmp_l}" ]]; then + n="${n_tmp_l}" + nt="${nt_tmp_l}" + if [[ -f "${nlock}" ]]; then rm -rf "${nlock}" && touch "${nlock}" && echo "" >"${nlock}" fi + rl=1 + fi + elif [[ -f "${nlock}" ]]; then + nlm=$(grep -e "[0-9]" "${nlock}" 2>/dev/null || echo 0) + if [[ -n "${nlm}" ]]; then + nlmt=$((10#${nlm})) + if [[ "${nlmt}" -lt $((id + limit)) ]]; then + rm -rf "${nlock}" && touch "${nlock}" && echo "" >"${nlock}" + fi + else + rm -rf "${nlock}" && touch "${nlock}" && echo "" >"${nlock}" fi fi - done + fi done +#done if [[ -f "${nfile}" ]]; then rm -rf "${nfile}" fi