| 1 |
<?php |
<?php |
| 2 |
/************************************* |
/************************************* |
| 3 |
* Demexp module for a Drupal demexp client |
* Demexp module for a Drupal demexp client |
| 4 |
* $Id: demexp.module,v 1.24 2006/10/13 04:38:23 augustin Exp $ |
* $Id: demexp.module,v 1.25 2006/10/13 10:18:03 augustin Exp $ |
| 5 |
* Copyright Augustin Masquilier, 2006. |
* Copyright Augustin Masquilier, 2006. |
| 6 |
* This module is released under the terms of the GPL. |
* This module is released under the terms of the GPL. |
| 7 |
*************************************/ |
*************************************/ |
| 710 |
* |
* |
| 711 |
*************************************/ |
*************************************/ |
| 712 |
function demexp_cron() { |
function demexp_cron() { |
| 713 |
|
|
| 714 |
// check if there are new tags to retrieve. |
// check if there are new tags to retrieve. |
| 715 |
$demexp_max_tag_id = variable_get('demexp_max_tag_id', 0); |
$demexp_max_tag_id = variable_get('demexp_max_tag_id', 0); |
| 716 |
echo '<p>' . $demexp_max_tag_id . 'last tag retrieved <br />'; |
echo '<p>' . $demexp_max_tag_id . 'last tag retrieved <br />'; |
| 717 |
$ds_max_tag_id = _contact_demexp_server('max_tag_id', 0); |
$ds_max_tag_id = _contact_demexp_server('max_tag_id', 0); |
| 718 |
echo $ds_max_tag_id . 'server max<p>'; |
echo $ds_max_tag_id . 'server max<p>'; |
| 719 |
|
|
| 720 |
// connection failed or syntax problem. Aborting cron. |
// connection failed. Aborting cron. |
| 721 |
if ($ds_max_tag_id === FALSE) { |
if ($ds_max_tag_id === FALSE) { |
| 722 |
echo "bye"; |
echo "bye"; |
| 723 |
return FALSE; |
return FALSE; |
| 735 |
$ds_tags = _contact_demexp_server('tag_info', 0, $demexp_max_tag_id, $nb); |
$ds_tags = _contact_demexp_server('tag_info', 0, $demexp_max_tag_id, $nb); |
| 736 |
_demexp_update_tags($ds_tags); |
_demexp_update_tags($ds_tags); |
| 737 |
variable_set('demexp_max_tag_id', $demexp_max_tag_id + $nb); |
variable_set('demexp_max_tag_id', $demexp_max_tag_id + $nb); |
| 738 |
|
|
| 739 |
// all the tags are supposed to be imported before the questions can be imported, |
// all the tags are supposed to be imported before the questions can be imported, |
| 740 |
// so that the questions' tags can be set properly. |
// so that the questions' tags can be set properly. |
| 741 |
if ($demexp_max_tag_id + $nb < $ds_max_tag_id) { |
if ($demexp_max_tag_id + $nb < $ds_max_tag_id) { |
| 742 |
return; |
return; |
| 743 |
} |
} |
| 744 |
} |
} |
| 745 |
|
|
| 746 |
|
|
| 747 |
// check if there are new questions to retrieve. |
// check if there are new questions to retrieve. |
| 748 |
$demexp_max_question_id = variable_get('demexp_max_question_id', 0); |
$demexp_max_question_id = variable_get('demexp_max_question_id', 0); |
| 749 |
echo $demexp_max_question_id . 'last retrieved <br />'; |
echo $demexp_max_question_id . 'last retrieved <br />'; |
| 750 |
$ds_max_question_id = _contact_demexp_server('max_question_id', 0); |
$ds_max_question_id = _contact_demexp_server('max_question_id', 0); |
| 751 |
echo $ds_max_question_id . 'server max'; |
echo $ds_max_question_id . 'server max'; |
| 752 |
|
|
| 753 |
|
|
| 754 |
// $demexp_max_question_id is never supposed to be superior to $ds_max_question_id ; |
// $demexp_max_question_id is never supposed to be superior to $ds_max_question_id ; |
| 755 |
if ($ds_max_question_id != $demexp_max_question_id) { |
if ($ds_max_question_id != $demexp_max_question_id) { |
| 764 |
_demexp_update_question_node($ds_questions); |
_demexp_update_question_node($ds_questions); |
| 765 |
variable_set('demexp_max_question_id', $demexp_max_question_id + $nb); |
variable_set('demexp_max_question_id', $demexp_max_question_id + $nb); |
| 766 |
} |
} |
| 767 |
|
|
| 768 |
|
|
| 769 |
// get the timestamps to check for updates: |
// get the timestamps to check for updates: |
| 770 |
// TODO: use get_timestamps() instead of get_timestamps_decoded(). |
// TODO: use get_timestamps() instead of get_timestamps_decoded(). |
| 771 |
$ds_timestamps = _contact_demexp_server('get_timestamps_decoded', 0); |
$ds_timestamps = _contact_demexp_server('get_timestamps_decoded', 0); |
| 807 |
|
|
| 808 |
if ($update) { |
if ($update) { |
| 809 |
foreach ($ds_tags AS $tag) { |
foreach ($ds_tags AS $tag) { |
| 810 |
|
// demexp tags come in the form "abc: economy". "abc" is the key, designing the hierarchy, and "economy" is the tag proper. |
| 811 |
|
$key = preg_replace_callback('/^([a-z]*):(.*)$/', create_function('$matches', 'return $matches[1];'), $tag['a_tag_label']); |
| 812 |
|
$len = strlen($key); |
| 813 |
|
$tag['a_tag_label'] = substr($tag['a_tag_label'], $len+2); |
| 814 |
$tid = db_result(db_query('SELECT tid FROM {demexp_tags} WHERE ds_a_tag_id = %d', $tag['a_tag_id'])); |
$tid = db_result(db_query('SELECT tid FROM {demexp_tags} WHERE ds_a_tag_id = %d', $tag['a_tag_id'])); |
| 815 |
db_query('UPDATE {term_data} SET name = "%s" WHERE tid = %d', $tag['a_tag_label'], $tid); |
db_query('UPDATE {term_data} SET name = "%s" WHERE tid = %d', $tag['a_tag_label'], $tid); |
| 816 |
|
db_query("UPDATE {demexp_tags} SET tag_hierarchy = '%s' WHERE ds_a_tag_id = %d", $key, $tag['a_tag_id']); |
| 817 |
|
_demexp_tag_hierarchy($tid, $key); |
| 818 |
} |
} |
| 819 |
} |
} |
| 820 |
else { |
else { |
| 821 |
foreach ($ds_tags AS $tag) { |
foreach ($ds_tags AS $tag) { |
| 822 |
|
// see similar code above. |
| 823 |
|
$key = preg_replace_callback('/^([a-z]*):(.*)$/', create_function('$matches', 'return $matches[1];'), $tag['a_tag_label']); |
| 824 |
|
$len = strlen($key); |
| 825 |
|
$tag['a_tag_label'] = substr($tag['a_tag_label'], $len+2); |
| 826 |
$values = array(); |
$values = array(); |
| 827 |
$values['name'] = $tag['a_tag_label']; |
$values['name'] = $tag['a_tag_label']; |
| 828 |
drupal_execute('taxonomy_form_term', $values, 2); |
drupal_execute('taxonomy_form_term', $values, 2); |
| 829 |
// http://drupal.org/node/84068 programmatically submitting form: drupal_execute should return useful data. |
// http://drupal.org/node/84068 programmatically submitting form: drupal_execute should return useful data. |
| 830 |
$tid = db_result(db_query('SELECT tid FROM {term_data} WHERE vid = 2 AND name = "%s" ORDER BY tid DESC', $tag['a_tag_label'])); |
$tid = db_result(db_query('SELECT tid FROM {term_data} WHERE vid = 2 AND name = "%s" ORDER BY tid DESC', $tag['a_tag_label'])); |
| 831 |
db_query('INSERT INTO {demexp_tags} (ds_a_tag_id, tid, ds_a_tag_timestamp) VALUES (%d, %d, %d)', $tag['a_tag_id'], $tid, $tag['a_tag_timestamp']); |
db_query("INSERT INTO {demexp_tags} (ds_a_tag_id, tid, ds_a_tag_timestamp, tag_hierarchy) VALUES (%d, %d, %d, '%s')", $tag['a_tag_id'], $tid, $tag['a_tag_timestamp'], $key); |
| 832 |
|
_demexp_tag_hierarchy($tid, $key); |
| 833 |
} |
} |
| 834 |
} |
} |
| 835 |
} |
} |
| 836 |
|
|
| 837 |
|
/************************************* |
| 838 |
|
* Update the hierarchy between tags. |
| 839 |
|
* Official demexp tags have a single hierarchy. |
| 840 |
|
* |
| 841 |
|
*************************************/ |
| 842 |
|
function _demexp_tag_hierarchy($tid, $key) { |
| 843 |
|
$len = strlen($key); |
| 844 |
|
|
| 845 |
|
// update the parent first. |
| 846 |
|
if ($len > 1) { |
| 847 |
|
$parent_key = substr($key, $len - 1); |
| 848 |
|
$parent_tid = db_result(db_query("SELECT tid FROM {demexp_tags} WHERE tag_hierarchy = '%s'", $parent_key)); |
| 849 |
|
// make sure the the parent is up to date: |
| 850 |
|
// 1- remove the old one, if any (there should only be one row with the same tid in the table. |
| 851 |
|
// 2 - and put back the new relationship. |
| 852 |
|
db_query('DELETE FROM {term_hierarchy} WHERE tid = %d ', $tid); |
| 853 |
|
db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $tid, $parent_tid); |
| 854 |
|
} |
| 855 |
|
|
| 856 |
|
// update the children. |
| 857 |
|
$result = db_query("SELECT * FROM {demexp_tags} WHERE LEFT(tag_hierarchy, %d) = '%s' AND LENGTH(tag_hierarchy) = %d", $len, $key, $len +1); |
| 858 |
|
while($child = db_fetch_object($result)) { |
| 859 |
|
db_query('DELETE FROM {term_hierarchy} WHERE tid = %d ', $child->tid); |
| 860 |
|
db_query('INSERT INTO {term_hierarchy} (tid, parent) VALUES (%d, %d)', $child->tid, $tid); |
| 861 |
|
} |
| 862 |
|
} |
| 863 |
|
|
| 864 |
/************************************* |
/************************************* |
| 865 |
* Function to theme the body of a question node. |
* Function to theme the body of a question node. |