vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 General Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=111)
-   -   Problems with using the hook location cron_script_cleanup (https://vborg.vbsupport.ru/showthread.php?t=108232)

CyberRanger 02-17-2006 03:58 PM

Problems with using the hook location cron_script_cleanup
 
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($fp4096)) {
                    if (!
xml_parse($xml_parser$datafeof($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 == 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 $i 1)
                                     {
                                         
$dtodayx "<div><strong><a href='".$vbulletin->options['bburl']."/abouttoday.php?do=rss&amp;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 />&nbsp;?&nbsp;".$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"


All times are GMT. The time now is 05:05 PM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.02181 seconds
  • Memory Usage 1,865KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (3)bbcode_php_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (1)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete