/[drupal]/contributions/modules/hatena_module/hatena.module
ViewVC logotype

Diff of /contributions/modules/hatena_module/hatena.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.3, Fri Sep 5 06:36:45 2008 UTC revision 1.3.2.1, Tue Sep 9 12:31:57 2008 UTC
# Line 5  Line 5 
5   * Written by Takashi Ikebe (iktaka@gmail.com).   * Written by Takashi Ikebe (iktaka@gmail.com).
6   */   */
7    
8    $hatena_debug=0;
9    
10  /**  /**
11   * Implementation of hook_help().   * Implementation of hook_help().
# Line 76  function hatena_admin_settings() { Line 77  function hatena_admin_settings() {
77      '#attributes' => NULL,      '#attributes' => NULL,
78      '#required' => FALSE,      '#required' => FALSE,
79    );    );
80    
81      $form['hatena_keyword_settings'] = array(
82        '#type' => 'fieldset',
83        '#title' => t('hatena keyword automatic link settings'),
84        '#description' => t('Set the details of hatena keyword automatic link.'),
85      );
86    
87        $form['hatena_keyword_settings']['hatena_keyword_show'] = array(
88        '#type' => 'checkbox',
89        '#title' => t('Enable Hatena Keyword Filter'),
90        '#return_value' => 1,
91        '#default_value' => variable_get('hatena_keyword_show', 0),
92    
93      );
94        $form['hatena_keyword_settings']['hatena_keyword_timeout'] = array(
95        '#type' => 'textfield',
96        '#title' => t('Hatena keyword cahche TTL(seconds)'),
97        '#default_value' => variable_get('hatena_keyword_timeout', '86400'),
98        '#size' => 10,
99        '#maxlength' => 16,
100        '#description' => t('Set the cache timeout as second. the default cache TTL is 1 day(86400)'),
101        '#attributes' => NULL,
102        '#required' => TRUE,
103      );
104        $form['hatena_keyword_settings']['hatena_keyword_score'] = array(
105        '#type' => 'textfield',
106        '#title' => t('Hatena keyword score (0 - 50)'),
107        '#default_value' => variable_get('hatena_keyword_score', '25'),
108        '#size' => 2,
109        '#maxlength' => 2,
110        '#description' => t('Set the keyword score(Min:0 Max:50). Lower score results many keyword links.'),
111        '#attributes' => NULL,
112        '#required' => TRUE,
113      );
114    
115    $form['how_to_show_the_links'] = array(    $form['how_to_show_the_links'] = array(
116      '#type' => 'fieldset',      '#type' => 'fieldset',
# Line 84  function hatena_admin_settings() { Line 119  function hatena_admin_settings() {
119    $form['how_to_show_the_links']['hatena_in_node'] = array(    $form['how_to_show_the_links']['hatena_in_node'] = array(
120      '#type' => 'select',      '#type' => 'select',
121      '#title' => t('hatena in nodes'),      '#title' => t('hatena in nodes'),
122      '#default_value' => variable_get('hatena_in_node', 2),      '#default_value' => variable_get('hatena_in_node', 3),
123      '#options' => array(0 => t('Disabled'), 1 => t('Teaser view'), 2 => t('Full-page view'), 3 => t('Teasers and full-page view')),      '#options' => array(0 => t('Disabled'), 1 => t('Teaser view'), 2 => t('Full-page view'), 3 => t('Teasers and full-page view')),
124      '#description' => t('When to display the hatena after the node text.'),      '#description' => t('When to display the hatena after the node text.'),
125    );    );
126    $form['how_to_show_the_links']['hatena_style'] = array(    $form['how_to_show_the_links']['hatena_style'] = array(
127      '#type' => 'select',      '#type' => 'select',
128      '#title' => t('hatena link style'),      '#title' => t('hatena link style'),
129      '#default_value' => variable_get('hatena_style', 1),      '#default_value' => variable_get('hatena_style', 2),
130      '#options' => array(1 => t('Text links'), 2 => t('Image links'), 3 => t('Image and text links')),      '#options' => array(1 => t('Text links'), 2 => t('Image links'), 3 => t('Image and text links')),
131    );    );
132    return system_settings_form($form);    return system_settings_form($form);
# Line 345  function theme_hatena_block_format($item Line 380  function theme_hatena_block_format($item
380  }  }
381    
382    
383    
384    
385    
386    /**
387     * Implementation of hook_filter(), which enables hatena keyword automatic link.
388     */
389    
390    function hatena_filter($op, $delta = 0, $format = -1, $text = '') {
391            switch ($op) {
392                    case 'list':
393                            return array(
394                                    0 => t('hatena_module'),
395                            );
396    
397                    case 'no cache':
398                            switch ($delta) {
399                                    case 0:
400                                            return 0;
401                                    default:
402                                            return 0;
403                            }
404    
405                    case 'description':
406                            switch ($delta) {
407                                    case 0:
408                                            return t('Enable Hatena Keyword Link.');
409                                    default:
410                                      return;
411                            }
412    
413                    case 'process':
414                            switch ($delta) {
415                                    case 0:
416                                            if(($text != NULL)&&variable_get('hatena_keyword_show','')){
417                                                    $temp= hatena_keyword_process($text, $format);
418                                                    if($temp == NULL){
419                                                            //error! may be server is down!
420                                                            return $text;
421                                                    }
422                                                    return $temp;
423                                            }
424                                    default:
425                                            return $text;
426                            }
427    
428                    case 'settings':
429                            switch ($delta) {
430                                    case 0:
431                                            return hatena_admin_settings($format);
432                                    default:
433                                            return;
434                            }
435    
436                    default:
437                            return $text;
438            }
439    }
440    
441    
442    /**
443     * Core part of hatena keyword link. Use hatena keyword automatic link API.
444     * (http://d.hatena.ne.jp/keyword/%a4%cf%a4%c6%a4%ca%a5%ad%a1%bc%a5%ef%a1%bc%a5%c9%bc%ab%c6%b0%a5%ea%a5%f3%a5%afAPI)
445     * Use XML-RPC interface with drupal internal cacheing mechanism. Requre pear XML_RPC package.
446     */
447    function hatena_keyword_process($text, $format = -1) {
448    
449      $outputtext=NULL;
450    
451      $timeout = variable_get('hatena_keyword_timeout', '');
452      if(!is_int($timeout) && ($timeout < 0)){
453        $timeout=60*60*24;  //use default.
454      }
455      $score =  variable_get('hatena_keyword_score', '');
456      if(!is_int($score) && ($score > 50) && ($score < 0)){
457        $score=25;  //use default.
458      }
459    
460      $digest = "hatena_module" . hash("sha256",$text);
461    
462      $cache=cache_get($digest, 'cache');
463      if($hatena_debug){
464        drupal_set_message("digest=" . $digest . "\n");
465        drupal_set_message("cache->data=" . $cache->data . "\n");
466        drupal_set_message("timeout=" . $timeout . "\n");
467        drupal_set_message("isset=" . isset($cache->data) . "\n");
468      }
469      if (isset($cache->data)) {
470        // Found a fresh entry in the cache
471        $outputtext=$cache->data;
472       // if($hatena_debug){
473          drupal_set_message("get from cache\n");
474       // }
475      }
476      else{
477        if($hatena_debug){
478          $time=date("Y/m/d H:i:s");
479          drupal_set_message("hatena_keyword_process start" . date("Y/m/d H:i:s")  . "\n");
480          drupal_set_message("text=" . $text . "\n");
481        }
482    
483        if (module_exists('locale')) {
484         global $language;
485         if($language->language=="ja"){ //Currently Japanese supported.
486           mb_language('Japanese');
487           mb_internal_encoding("UTF-8");
488         }
489        }
490        require_once("XML/RPC.php");
491        $GLOBALS['XML_RPC_defencoding'] = "UTF-8";
492    
493        //Make clien connection.
494        $xmlrpc_host = "http://d.hatena.ne.jp";  //host name
495        $xmlrpc_path = "/xmlrpc"; // path
496        $c = new XML_RPC_client( $xmlrpc_path, $xmlrpc_host, 80 );
497    
498        $struct = array();
499        $struct['body']=new XML_RPC_Value(mb_convert_encoding($text,"UTF-8","auto"),"string");
500        $struct['score']=new XML_RPC_Value($score,"int");
501        //$struct['cname']=new XML_RPC_Value(array( new XML_RPC_Value("book","string"),new XML_RPC_Value("movie","string")),"array");
502        $struct['a_target']=new XML_RPC_Value("_blank","string");
503        $struct['a_class']=new XML_RPC_Value("keyword","string");
504    
505        $message = new XML_RPC_Message('hatena.setKeywordLink',array(new XML_RPC_Value($struct,"struct")));
506        $response = $c->send($message);
507    
508        // Process the response
509        if (!$response->faultCode()) {
510          $v = $response->value();
511          $outputtext=$v->getval();
512       //   if($hatena_debug){
513            drupal_set_message("get from hatena\n");
514       //   }
515          cache_set($digest, $outputtext,'cache',time()+$timeout);
516        }else{
517          drupal_set_message("Hatena keyword query failed! May be server down?\n");
518          drupal_set_message("Fault Code:   " . $response->faultCode()   . "\n");
519          drupal_set_message("Fault Reason: " . $response->faultString() . "\n");
520        }
521      }
522      if($hatena_debug){
523        drupal_set_message("outputtext=" . $outputtext . "\n");
524      }
525    
526      return $outputtext;
527    
528    }
529    
530    
531  /**  /**
532   * Check if the hatena functions should be displayed.   * Check if the hatena functions should be enabled.
533   */   */
534  function _hatena_functions_show($node) {  function _hatena_functions_show($node) {
535    $bookmark_show = FALSE;    $bookmark_show = FALSE;
# Line 355  function _hatena_functions_show($node) { Line 538  function _hatena_functions_show($node) {
538    if ($ret) {    if ($ret) {
539      $bookmark_show = TRUE;      $bookmark_show = TRUE;
540    }    }
541    
542    return $bookmark_show;    return $bookmark_show;
543  }  }

Legend:
Removed from v.1.3  
changed lines
  Added in v.1.3.2.1

  ViewVC Help
Powered by ViewVC 1.1.2