Go Back   vb.org Archive > vBulletin 3 Discussion > vB3 General Discussions
FAQ Community Calendar Today's Posts Search

Reply
 
Thread Tools Display Modes
  #1  
Old 02-17-2006, 03:58 PM
CyberRanger's Avatar
CyberRanger CyberRanger is offline
 
Join Date: Mar 2004
Posts: 1,319
Благодарил(а): 0 раз(а)
Поблагодарили: 0 раз(а) в 0 сообщениях
Default 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"
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 06:00 PM.


Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2024, vBulletin Solutions Inc.
X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.03943 seconds
  • Memory Usage 2,298KB
  • Queries Executed 13 (?)
More Information
Template Usage:
  • (1)SHOWTHREAD
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (1)ad_showthread_beforeqr
  • (1)ad_showthread_firstpost
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (3)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)navbar
  • (3)navbar_link
  • (120)option
  • (1)post_thanks_box
  • (1)post_thanks_button
  • (1)post_thanks_javascript
  • (1)post_thanks_navbar_search
  • (1)post_thanks_postbit_info
  • (1)postbit
  • (1)postbit_onlinestatus
  • (1)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available:
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files:
  • ./showthread.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/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_post_thanks.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • forumjump
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • post_thanks_function_post_thanks_off_start
  • post_thanks_function_post_thanks_off_end
  • post_thanks_function_fetch_thanks_start
  • post_thanks_function_fetch_thanks_end
  • post_thanks_function_thanked_already_start
  • post_thanks_function_thanked_already_end
  • fetch_musername
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • post_thanks_function_can_thank_this_post_start
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • showthread_complete