2013-04-14 29 views
0

有沒有一種方法可以設置cron在我的Drupal站點上運行視圖?我有一個jQuery腳本,我希望每小時運行一次。使用cron獲取包含jQuery內容的視圖?

我試過了;

php http://mysite/myview 

但我想我是waaay的標誌。我得到;

Could not open input file: 

這裏是我在我的RSS飼料使用的JS;

// $Id 
(function ($) { 
Drupal.behaviors.tweetcast = { 

attach: function (context, settings) { 

$('div.tagspeak:not(.tags-processed)', context).addClass('tags-processed').each(function() { 

var mynid, sinceid, updateinfo, sinceurl, author, tweethtml; 
var currentsinceid = $('.currentsinceid').val(); 

var firstring = $(this).html(); 
var twostring = firstring.replace(/\s/g,'').replace(/^%20OR%20/gim,'').replace(/%20OR$/gim,'').replace(/%20OR%20%$/gim,'').replace(/%20OR%20$/gim,'').replace(/%$/gim,'').replace(/%20OR$/gim,'').toLowerCase(); 
var threestring = twostring.replace(/%20or%20/gim,'%20OR%20'); 
$(this).text(threestring); 
var fourstring = threestring.replace(/%20OR%20/gim,' '); 
var fivestring = fourstring.replace(/%23/gim,'#'); 

$(this).closest('.views-row').append('<div class="views-field views-field-replies"></div>');  
tweethtml = $(this).closest('.views-row').find('div.views-field-replies').eq(0); 

var twitter_api_url = 'http://search.twitter.com/search.json'; 
$.ajaxSetup({ cache: true }); 

$.getJSON(twitter_api_url + '?callback=?&rpp=1&q=' + threestring + '&since_id=' + currentsinceid, function(data) { 
$.each(data.results, function(i, tweet) {console.log(tweet); 

    if(tweet.text !== undefined) { 

    var tweet_html = '<div class="tweetuser">' + tweet.from_user + '</div>'; 
    tweet_html += ' <div class="sinceid">' + tweet.id + '</div>'; 
    tweethtml.append(tweet_html); 

    } 
}); 
}); 
    $.fn.delay = function(time, callback){ 
    jQuery.fx.step.delay = function(){}; 
    return this.animate({delay:1}, time, callback); 
    } 

    $(this).delay(2000, function(){ 
    title = $(this).closest('.views-row').find('div.views-field-title').eq(0).find('span.field-content').text(); 
    link = $(this).closest('.views-row').find('div.views-field-path').eq(0).find('span.field-content').text(); 
    author = $(this).closest('.views-row').find('div.tweetuser').eq(0).text(); 
    mynid = $(this).closest('.views-row').find('div.mynid').text(); 
    sinceid = $(this).closest('.views-row').find('div.sinceid').eq(0).text(); 
    updateinfo = {sinceid: sinceid, mynid: mynid, author: author, title: title, link: link, tags: fivestring}; 
    sinceurl = Drupal.settings.basePath + 'sinceid'; 

    $.ajax({ 
    type: "POST", 
    url: sinceurl, 
    data: updateinfo, 
    success: function(){} 
    }); 
    }); 
}); 
}} 
}) 
(jQuery); 

這裏是php我的模塊;

<?php 
// $id: 
function sinceid_menu() { 
    $items = array(); 
    $items['sinceid'] = array(
    'title' => 'sinceid', 
    'page callback' => 'sinceid_count', 
    'description' => 'sinceid', 
    'access arguments' => array('access content'), 
    'type' => MENU_CALLBACK, 
); 
return $items; 
} 
function sinceid_count() 
{ 
$sinceid=$_POST['sinceid']; 
$mynid=$_POST['mynid']; 
$author=$_POST['author']; 
$title=$_POST['title']; 
$link=$_POST['link']; 
$tags=$_POST['tags']; 

db_update('node') 
->expression('sinceid', $sinceid) 
->condition('nid', $mynid) 
->execute(); 

$sql = db_query("SELECT author FROM {authordupe} WHERE author = $author"); 
$result = db_query($sql); 
$how_many_rows = $result->rowCount(); 
if($how_many_rows == 0) { 

db_insert('authordupe') 
->fields(array(
'author' => $author, 
'title' => $title, 
'link' => $link, 
'tags' => $tags, 
)) 
->execute(); 



} 
} 

回答

1

的基本代碼在Drupal運行的觀點是

$view = views_get_view('<your_view_machine_name>'); 
$view->set_display('<optional_display_name>'); 
$view->execute(); 

這就是說,你說有一個視圖中運行的jQuery腳本。真的嗎?你的劇本在做什麼?我想說,當你想在客戶端運行腳本時,最好使用jQuery。你不能寫PHP代碼來完成你需要的任何東西,並把它放在cron鉤子中嗎?

+0

嗯...非常尷尬,但...我不是一個PHP程序員。我知道一點jquery。我知道這很醜陋,但我的能力有限......我會編輯我原來的帖子並添加代碼。我有意見創建一個rss飼料,我用jquery中的正則表達式修改併發布到twitter。 – Meggy

+0

這裏的問題是,你正試圖執行一個通常在瀏覽器中運行的jQuery腳本。我仍然相信你最好在你的模塊中使用一個cron鉤來加載合適的節點並建立rs來發布到twitter。或者你可以嘗試[PhantomJS](http://phantomjs.org/),如果你仍然認爲你更喜歡jQuery解決方案(我從來沒有用過自己,但看起來很有前途...) – pamatt

+0

感謝您指點我正確的方式!我會採納你的建議,寫一個模塊,我也會研究使用PhantomJS,它看起來很有前途。 – Meggy