PDA

View Full Version : How do I automatically turn on/off specific forums at a set time?


SemperFideles
11-24-2008, 07:20 PM
I looked for some cron hacks that would do this but couldn't find any.

Here's what I want to do:

1. Automatically turn off certain forums on Saturday night.
2. Automatically turn them back on on Monday AM.

If I can't do it via the cron tab then could somebody give me an example of an SQL query that would turn off/on a forum so I could perform them all en masse at once if I have to do it manually?

CypherSTL
11-25-2008, 10:16 AM
If you use Linux, and have access to create local cron job to execute a script at said times.

With the forums offline, I dont think vBulletin Cron Jobs will still function, so you would have to look "external of vBulletin" I would imagine.

SemperFideles
11-25-2008, 10:48 AM
If you use Linux, and have access to create local cron job to execute a script at said times.

With the forums offline, I dont think vBulletin Cron Jobs will still function, so you would have to look "external of vBulletin" I would imagine.
Thanks for the reply.

All the forums would not be offline but just the few forums that I want to turn off. I have about 150 forums and would be turning off only about 8 of them during the period.

My problem is not knowing that a cron job could be set up in Linux or vBulletin but how.

CypherSTL
11-25-2008, 10:55 AM
Read over the vBulletin Manual for scheduled tasks here: http://www.vbulletin.com/docs/html/scheduled_tasks

Looks like all you have to do is write a PHP Script to modify your database to make changes to the forum sections in question.

ReCom
11-25-2008, 11:13 AM
I spent about 30 minutes cracking this up:

<?php
require_once('global.php');
require_once(DIR . '/includes/adminfunctions.php');

$vbulletin->input->clean_gpc('g', 'enable', TYPE_BOOL);
$enable = $vbulletin->GPC['enable'];

$sql = "SELECT `forumid`, `options` FROM `".TABLE_PREFIX."forum` WHERE `forumid` = '57'";
$res = $vbulletin->db->query_read_slave($sql);
while ($row = $vbulletin->db->fetch_array($res))
{
$forumid = $row['forumid'];
$optionbits = $row['options'];
$newoptionbits = ($enable? $optionbits | 3 : $optionbits - ($optionbits & 3));
$vbulletin->db->query_write("UPDATE `".TABLE_PREFIX."forum` SET `options` = '$newoptionbits' WHERE `forumid` = '$forumid'");
}

build_forum_permissions();
?>

Modify the SQL query $sql to put the forumids you are interested in. Then save as forumswitch.php in your vbulletin directory.

Set two Scheduled Tasks in AdminCP that call forumswitch.php?enable=0 and forumswitch.php?enable=1 respectively.

SemperFideles
11-25-2008, 11:46 AM
Thanks so much ReCom!

Just so I'm clear, it seems like this line is t[he one with the forumid:

$sql = "SELECT `forumid`, `options` FROM `".TABLE_PREFIX."forum` WHERE `forumid` = '57'";

Do I replace the 57 with my forum id?
I have about 10 forums to turn off, do I replace the '57' with something like '1,2,3,4,5,6,7,8,9,10'?

ReCom
11-25-2008, 11:53 AM
Thanks so much ReCom!

Just so I'm clear, it seems like this line is t[he one with the forumid:

$sql = "SELECT `forumid`, `options` FROM `".TABLE_PREFIX."forum` WHERE `forumid` = '57'";

Do I replace the 57 with my forum id?
I have about 10 forums to turn off, do I replace the '57' with something like '1,2,3,4,5,6,7,8,9,10'?
Nope, you use IN, as in:
$sql = "SELECT `forumid`, `options` FROM `".TABLE_PREFIX."forum` WHERE `forumid` IN ('1','2','3','4','5','6','7','8','9','10') ";

SemperFideles
11-25-2008, 01:53 PM
I did a test run and get this error:
Turn off Forum

Warning: include_once([path]/./includes/cron/forumswitch.php?enable=0) [function.include-once]: failed to open stream: No such file or directory in [path]/admincp/cronadmin.php on line 113

Warning: include_once() [function.include]: Failed opening '[path]/./includes/cron/forumswitch.php?enable=0' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in [path]/admincp/cronadmin.php on line 113

Done

Lynne
11-25-2008, 02:05 PM
Did you save the script where suggested? Have you renamed your admincp? What path did you put for your Scheduled Task Filename?

SemperFideles
11-25-2008, 02:20 PM
Lynne,

Are you ReCom?

I saved it both in my vBulletin directory (same error) and in my includes/cron directory within my vBulletin directory (same error).

I have not renamed admincp.

--------------- Added 1227630218 at 1227630218 ---------------

Here is the error when run from the vBulletin directory:

Turn off Forum

Warning: include_once([path]/./forumswitch.php?enable=0) [function.include-once]: failed to open stream: No such file or directory in [path]/admincp/cronadmin.php on line 113

Warning: include_once() [function.include]: Failed opening '[path]/./forumswitch.php?enable=0' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in [path]/admincp/cronadmin.php on line 113

Done

Lynne
11-25-2008, 03:40 PM
No, I am not ReCom. I just thought I would put out some comments to try to help because it looked like there was a path wrong somewhere along the lines. But, if you think the paths are all correct, then fine.

ReCom
11-25-2008, 10:04 PM
I did a test run and get this error:
Turn off Forum

Warning: include_once([path]/./includes/cron/forumswitch.php?enable=0) [function.include-once]: failed to open stream: No such file or directory in [path]/admincp/cronadmin.php on line 113

Warning: include_once() [function.include]: Failed opening '[path]/./includes/cron/forumswitch.php?enable=0' for inclusion (include_path='.:/usr/lib/php:/usr/local/lib/php') in [path]/admincp/cronadmin.php on line 113

Done
Hmmm, seels like Scheduled Tasks don't accept filenames with query string attached ...

Two alternatives:

Either forget about scheduled tasks and simply invoke the forumswitch.php manually at specified time
Or wrap the forumswitch.php code into a function that takes the $enable value (true/false), and then create two separate files that include the forumswitch.php and call the function with either true or false respectively. Use these separate files in the scheduled tasks.

SemperFideles
12-02-2008, 03:57 AM
I ended up simplifying this:

forumswitchoff.php looks like this:
<?php
$vbulletin->db->query_read("UPDATE ".TABLE_PREFIX."forum SET options=options - 2 WHERE (options & 2) AND `forumid` IN ('111','81','33','39','105','125','84','52','75',' 86','51','76')");
build_forum_permissions();
?>

forumswitchon.php looks like this:
<?php
$vbulletin->db->query_read("UPDATE ".TABLE_PREFIX."forum SET options=options + 2 WHERE NOT (options & 2) AND `forumid` IN ('111','81','33','39','105','125','84','52','75',' 86','51','76')");
build_forum_permissions();
?>

ReCom
12-02-2008, 04:28 AM
Cool. I didn't know we can do math in SQL queries :o

SemperFideles
12-02-2008, 12:52 PM
The format you gave me was critical for the IN command as well as how to put a db query inside a PHP file. The format for the actual SQL query to turn a forum on or off was in the VBulletin 3.7 tricks forum at vbulletin.com. I really appreciate your help or I wouldn't have been able to do it.