The plugin below uses the hook cron_script_cleanup. When the scheduled task "Hourly Cleanup #1" is run, the plugin updates two fields in a custom table.
Here's the problem:
- If I manually run "Hourly Cleanup #1" from the admincp using "Run Now", it works great. My database fields are updated as I'd expect.
- However, when the scheduled task for "Hourly Cleanup #1" is actually called due to the time being reached, my database fields are update with empty values.
Any ideas on what could cause this behavior? Apparently, being in the admincp is setting "something" that I need to set in the plugin for it work when truly called as a scheduled task. If you look at the very bottom, could how I'm using $vbulletin->db->sql_prepare have some difference if I'm not running it from the admincp?
(BTW - the below script checks RSS Feeds and then updates info in a custom table)
PHP Code:
global $vbulletin;
if ($vbulletin->options['histtddisabledrss'] == 0)
{
// RSS FUNCTIONS
function startElement($parser, $name, $attrs) {
global $rss_channel, $currently_writing, $main;
switch($name) {
case "RSS":
case "RDF:RDF":
case "ITEMS":
$currently_writing = "";
break;
case "CHANNEL":
$main = "CHANNEL";
break;
case "IMAGE":
$main = "IMAGE";
$rss_channel["IMAGE"] = array();
break;
case "ITEM":
$main = "ITEMS";
break;
default:
$currently_writing = $name;
break;
}
}
function endElement($parser, $name) {
global $rss_channel, $currently_writing, $item_counter;
$currently_writing = "";
if ($name == "ITEM") {
$item_counter++;
}
}
function characterData($parser, $data) {
global $rss_channel, $currently_writing, $main, $item_counter;
if ($currently_writing!= "") {
switch($main) {
case "CHANNEL":
if (isset($rss_channel[$currently_writing])) {
$rss_channel[$currently_writing] .= $data;
} else {
$rss_channel[$currently_writing] = $data;
}
break;
case "IMAGE":
if (isset($rss_channel[$main][$currently_writing])) {
$rss_channel[$main][$currently_writing] .= $data;
} else {
$rss_channel[$main][$currently_writing] = $data;
}
break;
case "ITEMS":
if (isset($rss_channel[$main][$item_counter][$currently_writing])) {
$rss_channel[$main][$item_counter][$currently_writing] .= $data;
} else {
$rss_channel[$main][$item_counter][$currently_writing] = $data;
}
break;
}
}
}
$result = $vbulletin->db->query_read("SELECT id, datelastupdate, mainnumber, maxlengthevent, useimage, title, todaynumber, url, name
FROM " . TABLE_PREFIX . "histtd_rss
WHERE enabled = '0' AND ((datelastupdate + (updatetime * 60)) < ".TIMENOW.")
ORDER BY name");
while ($arr = $vbulletin->db->fetch_array($result))
{
$dmainx = '';
$rssopened = 'no';
while( list($key, $val ) = each( $arr) )
{
if ($key == "id")
{
$rssid = $arr['id'];
}
if ($key == "datelastupdate")
{
$updatetime = vbdate($vbulletin->options['dateformat'],$arr['datelastupdate'],true);
$updatetime .= " at ".vbdate($vbulletin->options['timeformat'],$arr['datelastupdate'],true);
$sincelastupdate = TIMENOW - $arr['datelastupdate'];
}
if ($key == "mainnumber")
{
$n = $arr['mainnumber'];
}
if ($key == "maxlengthevent")
{
$maxlengthevent = $arr['maxlengthevent'];
}
if ($key == "useimage")
{
$useimage = $arr['useimage'];
}
if ($key == "title")
{
$title = $arr['title'];
}
if ($key == "todaynumber")
{
$todaynumber = $arr['todaynumber'];
}
if ($key == "url")
{
set_time_limit(0);
$file = $arr['url'];
$rss_channel = array();
$currently_writing = "";
$main = "";
$item_counter = 0;
if ($n == '')
{
$n = 4;
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
if (!($fp = @fopen($file, "r"))) {
$rssopened = 'no';
} else {
$rssopened = 'yes';
}
while ($data = @fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp)))
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
// output HTML
if (isset($rss_channel["ITEMS"]))
{
if (count($rss_channel["ITEMS"]) > 0)
{
if (count($rss_channel["ITEMS"]) > $n)
{
$NumberOfItems = $n;
}
else
{
$NumberOfItems = count($rss_channel["ITEMS"]);
}
for($i = 0;$i < $NumberOfItems;$i++)
{
if (isset($rss_channel["ITEMS"][$i]["LINK"]))
{
if ($i < 1)
{
$dmainx = "<br /><table cellpadding=\"4\" cellspacing=\"4\" border=\"0\" class=\"tborder\" width=\"".$stylevar['tablewidth']."\" align=\"center\"><tr><td class=\"thead\" valign=\"top\" align=\"center\"><strong>".iif($useimage == 1 AND $rss_channel["IMAGE"]["URL"] != '',"<img name='". iif($title == '',$rss_channel["TITLE"],$title) ."'alt='". iif($title == '',$rss_channel["TITLE"],$title) ."' src='".$rss_channel["IMAGE"]["URL"]."'>",iif($title == '',$rss_channel["TITLE"],$title)) . "</strong><br />Published: ".$rss_channel["PUBDATE"]."</td></tr>
";
if ($todaynumber > 0 & $i < 1)
{
$dtodayx = "<div><strong><a href='".$vbulletin->options['bburl']."/abouttoday.php?do=rss&id=".$rssid."'>".iif($title == '',$rss_channel["TITLE"],$title) . "</a></strong>";
$j = 1;
}
else
{
$dtodayx = '';
}
}
$dmainx .= "<tr><td class=\"alt1\" valign=\"top\" align=\"left\" width=\"100%\"><ul><li><a href=\"" . $rss_channel["ITEMS"][$i]["LINK"] . "\" target=\"_blank\">" . $rss_channel["ITEMS"][$i]["TITLE"] . "</a>";
}
else
{
$dmainx .= $rss_channel["ITEMS"][$i]["TITLE"];
}
$dmainx .= "<br />".iif($maxlengthevent>0,substr($rss_channel["ITEMS"][$i]["DESCRIPTION"],0,$maxlengthevent)." ...[<a href=\"" . $rss_channel["ITEMS"][$i]["LINK"] . "\" target=\"_blank\">more</a>]",$rss_channel["ITEMS"][$i]["DESCRIPTION"]) . "</li></ul></td></tr>";
if ($todaynumber >= $j)
{
$dtodayx .= "<br /> ? ".$rss_channel["ITEMS"][$i]["TITLE"];
$j++;
}
}
}
else
{
$dmainx .= "<b>There are no articles in this feed.</b>";
}
}
}
if (($dmainx != '' || $mainnumber == 0) AND $rssopened == 'yes')
{
if ($dmainx != '')
{
$dmainx .= "</table>";
}
if ($dtodayx != '')
{
$dtodayx .= "</div>";
}
$vbulletin->db->query_write("UPDATE " . TABLE_PREFIX . "histtd_rss
SET `desc`=".$vbulletin->db->sql_prepare($dmainx).",
`datelastupdate`=".$vbulletin->db->sql_prepare(TIMENOW)." ,
`todaytitles`=".$vbulletin->db->sql_prepare($dtodayx)."
WHERE `id`=".$vbulletin->db->sql_prepare($rssid));
}
}
}
$vbulletin->db->free_result($result);
log_cron_action('RSS Feeds Updated', $nextitem);
//End RSS FEED
}
Solved!
Ends up I need to global most of the variables in the plugin. Once I changed
PHP Code:
global $vbulletin;
to:
PHP Code:
global $vbulletin, $dmainx, $xml_parser, $parser, $name, $attrs, $data, $rss_channel, $currently_writing, $main, $item_counter;
the plugin works correctly when run by cron and when run by doing "Run Now"