PDA

View Full Version : HowTo? Move / Delete Threads of one forum after x days


Za4a Tuner
10-26-2017, 11:58 AM
Hello,
i'd like to do two jobs automatically:

1.)
"Delete all threads in forum id=88 after 365 days"

2.)
"Move all threads older than 100 days and the thread-prefix=done from forum-id=10 to forum-id=12"

Can anyone show me a cronjob for this?

kr Chris

MarkFL
10-26-2017, 05:14 PM
Create a plugin hooked at "cron_script_cleanup_daily" with the code:

global $vbulletin, $db;
require_once(DIR . '/includes/functions_databuild.php');
$deleted = false;
$curtime = TIMENOW;
$physicaldel = false;
$del_user = $fetch_userinfo(1);
$delinfo = array(
'userid' => $del_user['userid'],
'username' => $del_user['username'],
'reason' => 'Thread auto-deleted via cron.',
'keepattachments' => true
);
$countposts = $vbulletin->forumcache[88]['options'] & $vbulletin->bf_misc_forumoptions['countposts'];

$query = "SELECT thread.* FROM " . TABLE_PREFIX . "thread AS thread WHERE lastpost <= " . $curtime - 365*86400 . " AND forumid = 88";
$threads_delete = $vbulletin->db->query_read($query);
$mthread =& datamanager_init('Thread', $vbulletin, ERRTYPE_SILENT, 'threadpost');

while ($thread_delete = $vbulletin->db->fetch_array($threads_delete))
{
if (!$deleted)
{
$deleted = true;
}

$mthread->set_existing($thread_delete);
$mthread->delete($countposts, $physicaldel, $delinfo, false);
}

unset($mthread);

if ($deleted)
{
build_forum_counters(88);
}

$vbulletin->db->query_write("
UPDATE " . TABLE_PREFIX . "thread
SET forumid = 12
WHERE forumid = 10
AND lastpost <= " . $curtime - 100*86400 . "
AND prefixid = 'done'
");

build_forum_counters(10);
build_forum_counters(12);


Note: I have not tested this code. Please test this first on your test installation, and then make a backup of your database before running this code the first time on your live site. I cannot emphasize enough how important this is!

As the code is written, the thread deletion is soft-delete. To use a hard-delete, change the line:

$physicaldel = false;

to:

$physicaldel = true;

Za4a Tuner
10-26-2017, 06:33 PM
Hy,
thank you for the code!
I'll try it on my testboard and will give you feedback.

One question:
If i "hard-delete" the threads, than i won't need the line for the attachments?
global $vbulletin, $db;
require_once(DIR . '/includes/functions_databuild.php');
$deleted = false;
$curtime = TIMENOW;
$physicaldel = true;
$del_user = $fetch_userinfo(1);
$delinfo = array(
'userid' => $del_user['1'],
'username' => $del_user['Chris'],
'reason' => 'Thread auto-deleted via cron.',
);

kr Chris

MarkFL
10-26-2017, 09:03 PM
Yeah, I'm not sure but I would just leave the array as is because it won't hurt for it to be there. :)

Za4a Tuner
10-26-2017, 10:48 PM
ok, i will give it a try

MarkFL
10-29-2017, 07:39 PM
ok, i will give it a try

Did it work as you want?

Za4a Tuner
10-29-2017, 07:50 PM
I was on tour this weekend.
The code is running in my testboard since this afternoon.
Tomorrow i give you my feedback.

kr Chris

Za4a Tuner
10-30-2017, 07:35 PM
Hello,
today i found time to test the plugin.

I don't know what's wrong, but it won't work!

When the plugin is active, the cronjob (id 15 - daily clean up) didn't end correct.
156708

Is the plugin inactive, evrything is fine with the cornjob:
156709

This is the code i use fpr my testboard:

- forum-id = 4 => delete threads after 365 days
- forum-id = 10 => move threads with the prefix "Delete" after 100 days to forum-id = 12
- my User-ID = 1
- my Username = Chris

global $vbulletin, $db;
require_once(DIR . '/includes/functions_databuild.php');
$deleted = false;
$curtime = TIMENOW;
$physicaldel = false;
$del_user = $fetch_userinfo(1);
$delinfo = array(
'userid' => $del_user['1'],
'username' => $del_user['Chris'],
'reason' => 'Thema alt entfernt via cron.',
'keepattachments' => false
);
$countposts = $vbulletin->forumcache[4]['options'] & $vbulletin->bf_misc_forumoptions['countposts'];

$query = "SELECT thread.* FROM " . TABLE_PREFIX . "thread AS thread WHERE lastpost <= " . $curtime - 365*86400 . " AND forumid = 4";
$threads_delete = $vbulletin->db->query_read($query);
$mthread =& datamanager_init('Thread', $vbulletin, ERRTYPE_SILENT, 'threadpost');

while ($thread_delete = $vbulletin->db->fetch_array($threads_delete))
{
if (!$deleted)
{
$deleted = true;
}

$mthread->set_existing($thread_delete);
$mthread->delete($countposts, $physicaldel, $delinfo, false);
}

unset($mthread);

if ($deleted)
{
build_forum_counters(4);
}

$vbulletin->db->query_write("
UPDATE " . TABLE_PREFIX . "thread
SET forumid = 12
WHERE forumid = 10
AND lastpost <= " . $curtime - 100*86400 . "
AND prefixid = 'Delete'
");

build_forum_counters(10);
build_forum_counters(12);

kr Chris

MarkFL
10-30-2017, 07:43 PM
I don't read German, so the screenshots aren't of much use to me. Sorry :(

You don't want to edit the 'userid' and 'username' keys of the $delinfo array...that could be the cause of the issue.

Za4a Tuner
10-30-2017, 08:11 PM
I don't read German, so the screenshots aren't of much use to me. Sorry :(would the screenshots be helpfull if the language is english? I can install the english phrases, this woul'd be not a problem

You don't want to edit the 'userid' and 'username' keys of the $delinfo array...that could be the cause of the issue.that don't make a difference, i tried it right now.

MarkFL
10-30-2017, 10:06 PM
I will run the code on my local dev site to see what I find. :)

--------------- Added 1509428993 at 1509428993 ---------------

Okay, this should work:

global $vbulletin;
require_once(DIR . '/includes/functions_databuild.php');

$deleted = 0;
$curtime = TIMENOW;
$physicaldel = false;
$del_user = fetch_userinfo(1);
$del_forumid = '4';
$reason = 'Thema alt entfernt via cron.';

$delinfo = array(
'userid' => $del_user['userid'],
'username' => $del_user['username'],
'reason' => $reason,
'keepattachments' => true
);

$countposts = ($vbulletin->forumcache["$del_forumid"]['options'] & $vbulletin->bf_misc_forumoptions['countposts']);

$query = "SELECT thread.* FROM " . TABLE_PREFIX . "thread AS thread WHERE lastpost <= " . ($curtime - 365*86400) . " AND forumid = " . $del_forumid;

$threads_delete = $vbulletin->db->query_read($query);
$mthread =& datamanager_init('Thread', $vbulletin, ERRTYPE_SILENT, 'threadpost');

while ($thread_delete = $vbulletin->db->fetch_array($threads_delete))
{
if (!$deleted)
{
$deleted = true;
}

$mthread->set_existing($thread_delete);
$mthread->delete($countposts, $physicaldel, $delinfo, false);
}

unset($mthread);

if ($deleted)
{
build_forum_counters($del_forumid);
}

$move_prefixid = 'reportthread_solved';
$source_forumid = 10;
$destination_forumid = 12;

$vbulletin->db->query_write("
UPDATE " . TABLE_PREFIX . "thread
SET forumid = " . $destination_forumid . "
WHERE forumid = " . $source_forumid . "
AND lastpost <= " . ($curtime - 100*86400) . "
AND prefixid = '" . $move_prefixid . "'
");

build_forum_counters($source_forumid);
build_forum_counters($destination_forumid);

--------------- Added 1509429187 at 1509429187 ---------------

I move that these double-post merges go away. :)

Za4a Tuner
10-31-2017, 06:39 AM
Thanks for your help and spending time on my problem,
but the code from the post above won't work in my board :(
I used it, as you posted it, or have i to do some changes?

kr Chris

MarkFL
10-31-2017, 01:44 PM
It should work exactly as I posted it, but it could be that you have other plugins hooked at "threaddata_delete" that are causing an issue. Do you have any plugins hooked there?

Za4a Tuner
11-01-2017, 10:58 AM
It should work exactly as I posted it, but it could be that you have other plugins hooked at "threaddata_delete" that are causing an issue. Do you have any plugins hooked there?in the plug-in list is nothing hooked at "threaddata_delete".

MarkFL
11-01-2017, 02:50 PM
When you manually run the daily cleanup, what error(s) do you get?

Za4a Tuner
11-04-2017, 06:35 PM
Hy,
sorry for the late answer, but i wasn't at home till now.

All the shedulded tasks even don't run!
156714

The plugin is disabled, and the task for "daily cleanup" run:
156715

but when i open the logfile, there is no entry for the task!
156716

I don't know whats wrong

MarkFL
11-04-2017, 07:02 PM
When you manually run the daily cleanup, what error messages do you get?

Za4a Tuner
11-04-2017, 07:10 PM
this is the error message:
[Sat Nov 04 21:06:43 2017] [error] [client 95.91.250.110] PHP Fatal error: Only variables can be passed by reference in .../includes/cron/dailycleanup.php(126) : eval()'d code on line 12, referer: .../admincp/cronadmin.php?do=modify