07872467b544f17160d38da202691afe7003a651
[atutor.git] / mods / wiki / plugins / linking / tcn.php
1 <?php
2
3 /*
4   This plugin does minimal HTTP language negotiation (RFC2095) by selecting
5   the best matching variant of a page.
6   Language codes are always only two-letters in ewiki (country abbreviations
7   are ignored or break it).
8   Allowed page names are "PageName.en" or "PageName*en"
9 */
10
11 define("EWIKI_DEFAULT_LANG", "en");
12 define("EWIKI_AUTO_CHOOSE", 1);
13 define("EWIKI_UP_FORCE_LANG", "lang");
14 $ewiki_config["langnames"] = array(
15    "English" => "en",
16    "Spanish" => "es",
17    "German" => "de",
18    "Dutch" => "nl",
19 );
20
21
22 $ewiki_plugins["handler"][] = "ewiki_tcn_handler_select_best";
23 $ewiki_plugins["view_final"][] = "ewiki_tcn_view_final_add_variants";
24
25
26 function ewiki_tcn_which_variant($id) {
27
28    $langnames = &$GLOBALS["ewiki_config"]["langnames"];
29
30    $n = strlen($id);
31    $r = array($id, EWIKI_DEFAULT_LANG);
32
33    if (($id[$n-3] == ".") || ($id[$n-3] == "*")) {
34       $r = array(substr($id, 0, $n-3), substr($id, $n-2, 2));
35    }
36    else {
37       foreach ($langnames as $str=>$cd) {
38          if (substr($id, $n-strlen($str)) == $str) {
39             $r = array(substr($id, 0, $n-strlen($str)), $cd);
40          }
41       }
42    }
43    return($r);
44 }
45
46
47 function ewiki_tcn_find_variants($id) {
48
49    list($base_id, $id_lang) = ewiki_tcn_which_variant($id);
50    $variants = array(
51       "$id" => "$id_lang",
52    );
53
54    $result = ewiki_db::SEARCH("id",$base_id);
55    while ($result && ($row = $result->get())) {
56       if (substr($row["id"], 0, strlen($base_id)) == $base_id) {
57          list($i, $l) = ewiki_tcn_which_variant($row["id"]);
58          if ($i == $base_id) {
59             $variants[$row["id"]] = $l;
60          }
61       }
62    }
63
64    return($variants);
65 }
66
67
68 function ewiki_tcn_handler_select_best(&$id, &$data, &$action) {
69
70    global $ewiki_variants, $ewiki_t;
71
72    $ewiki_variants = ewiki_tcn_find_variants($id);
73
74    if ($action == "view") {
75       if (count($ewiki_variants) >= 2) {
76
77          $wanted_langs = $ewiki_t["languages"];
78          if ($force_lang = $_REQUEST[EWIKI_UP_FORCE_LANG]) {
79             array_unshift($wanted_langs, $force_lang);
80          }         
81
82          foreach ($wanted_langs as $l) {
83             foreach ($ewiki_variants as $v_id=>$v_l) {
84                if ($l==$v_l) {
85                   $id = $v_id;
86                   $data = ewiki_db::GET($id);
87                   break 2;
88                }
89             }
90          }#--langs
91
92       }
93    }
94
95 }
96
97
98 function ewiki_tcn_view_final_add_variants(&$o, $id, &$data, &$action) {
99
100    global $ewiki_variants;
101
102    if (count($ewiki_variants) >= 2) {
103
104       $add = '<div class="language-variants">';
105       foreach ($ewiki_variants as $v_id=>$v_l) {
106          $add .= " <a href=\""
107               . ewiki_script($action, $v_id, array(EWIKI_UP_FORCE_LANG=>$v_l))
108               . "\" class=\"lang {$v_l}\" lang=\"{$v_l}\">{$v_l}</a> ";
109       }
110       $add .= '</div>'."\n";
111
112       $o = $add . $o;
113    }
114 }
115
116
117 ?>