2 // PukiWiki - Yet another WikiWikiWeb clone
3 // $Id: link.php,v 1.4 2005/01/29 13:51:44 henoheno Exp $
5 // Backlinks / AutoLinks related functions
8 * CACHE_DIR/encode(¥Ú¡¼¥¸Ì¾).ref
9 * »²¾È¸µ¥Ú¡¼¥¸Ì¾<tab>AutoLink¤Ë¤è¤ë¥ê¥ó¥¯¤Î¤ß¤Î¤È¤1\n
10 * »²¾È¸µ¥Ú¡¼¥¸Ì¾<tab>AutoLink¤Ë¤è¤ë¥ê¥ó¥¯¤Î¤ß¤Î¤È¤1\n
13 * CACHE_DIR/encode(¥Ú¡¼¥¸Ì¾).rel
14 * »²¾ÈÀè¥Ú¡¼¥¸Ì¾<tab>»²¾ÈÀè¥Ú¡¼¥¸Ì¾<tab>...
17 // ¥Ç¡¼¥¿¥Ù¡¼¥¹¤«¤é´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
18 function links_get_related_db($page)
20 $ref_name = CACHE_DIR . encode($page) . '.ref';
21 if (! file_exists($ref_name)) return array();
24 foreach (file($ref_name) as $line) {
25 list($_page) = explode("\t", rtrim($line));
26 $time = get_filetime($_page);
27 if($time != 0) $times[$_page] = $time;
32 //¥Ú¡¼¥¸¤Î´ØÏ¢¤ò¹¹¿·¤¹¤ë
33 function links_update($page)
35 if (PKWK_READONLY) return; // Do nothing
37 if (ini_get('safe_mode') == '0') set_time_limit(0);
39 $time = is_page($page, TRUE) ? get_filetime($page) : 0;
42 $rel_file = CACHE_DIR . encode($page) . '.rel';
43 $rel_file_exist = file_exists($rel_file);
44 if ($rel_file_exist === TRUE) {
45 $lines = file($rel_file);
48 $rel_old = explode("\t", rtrim($lines[0]));
50 $rel_new = array(); // »²¾ÈÀè
51 $rel_auto = array(); // ¥ª¡¼¥È¥ê¥ó¥¯¤·¤Æ¤¤¤ë»²¾ÈÀè
52 $links = links_get_objects($page, TRUE);
53 foreach ($links as $_obj) {
54 if (! isset($_obj->type) || $_obj->type != 'pagename' ||
55 $_obj->name == $page || $_obj->name == '')
58 if (is_a($_obj, 'Link_autolink')) { // ¹Ôµ·¤¬°¤¤
59 $rel_auto[] = $_obj->name;
61 $rel_new[] = $_obj->name;
64 $rel_new = array_unique($rel_new);
66 // autolink¤·¤«¸þ¤¤¤Æ¤¤¤Ê¤¤¥Ú¡¼¥¸
67 $rel_auto = array_diff(array_unique($rel_auto), $rel_new);
70 $rel_new = array_merge($rel_new, $rel_auto);
72 // .rel:$page¤¬»²¾È¤·¤Æ¤¤¤ë¥Ú¡¼¥¸¤Î°ìÍ÷
74 // ¥Ú¡¼¥¸¤¬Â¸ºß¤·¤Æ¤¤¤ë
75 if (! empty($rel_new)) {
76 $fp = fopen($rel_file, 'w')
77 or die_message('cannot write ' . htmlspecialchars($rel_file));
78 fputs($fp, join("\t", $rel_new));
83 // .ref:$_page¤ò»²¾È¤·¤Æ¤¤¤ë¥Ú¡¼¥¸¤Î°ìÍ÷
84 links_add($page, array_diff($rel_new, $rel_old), $rel_auto);
85 links_delete($page, array_diff($rel_old, $rel_new));
87 global $WikiName, $autolink, $nowikiname, $search_non_list;
89 // $page¤¬¿·µ¬ºîÀ®¤µ¤ì¤¿¥Ú¡¼¥¸¤Ç¡¢AutoLink¤ÎÂоݤȤʤêÆÀ¤ë¾ì¹ç
90 if ($time && ! $rel_file_exist && $autolink
91 && (preg_match("/^$WikiName$/", $page) ? $nowikiname : strlen($page) >= $autolink))
93 // $page¤ò»²¾È¤·¤Æ¤¤¤½¤¦¤Ê¥Ú¡¼¥¸¤ò°ìÀƹ¹¿·¤¹¤ë(¤ª¤¤)
95 $pages = do_search($page, 'AND', TRUE);
96 foreach ($pages as $_page) {
101 $ref_file = CACHE_DIR . encode($page) . '.ref';
103 // $page¤¬ºï½ü¤µ¤ì¤¿¤È¤¤Ë¡¢
104 if (! $time && file_exists($ref_file)) {
105 foreach (file($ref_file) as $line) {
106 list($ref_page, $ref_auto) = explode("\t", rtrim($line));
108 // $page¤òAutoLink¤Ç¤·¤«»²¾È¤·¤Æ¤¤¤Ê¤¤¥Ú¡¼¥¸¤ò°ìÀƹ¹¿·¤¹¤ë(¤ª¤¤¤ª¤¤)
110 links_delete($ref_page, array($page));
115 // Init link cache (Called from link plugin)
116 function links_init()
120 if (PKWK_READONLY) return; // Do nothing
122 if (ini_get('safe_mode') == '0') set_time_limit(0);
125 foreach (get_existfiles(CACHE_DIR, '.ref') as $cache)
127 foreach (get_existfiles(CACHE_DIR, '.rel') as $cache)
130 $pages = get_existpages();
131 $ref = array(); // »²¾È¸µ
132 $ref_notauto = array();
133 foreach ($pages as $page) {
134 if ($page == $whatsnew) continue;
136 $rel = array(); // »²¾ÈÀè
137 $links = links_get_objects($page);
138 foreach ($links as $_obj) {
139 if (! isset($_obj->type) || $_obj->type != 'pagename' ||
140 $_obj->name == $page || $_obj->name == '')
143 $rel[] = $_obj->name;
144 if (! is_a($_obj, 'Link_autolink')) {
145 $ref_notauto[$_obj->name][$page] = TRUE;
147 $ref[$_obj->name][] = $page;
149 $rel = array_unique($rel);
151 $fp = fopen(CACHE_DIR . encode($page) . '.rel', 'w')
152 or die_message('cannot write ' . htmlspecialchars(CACHE_DIR . encode($page) . '.rel'));
153 fputs($fp, join("\t", $rel));
158 foreach ($ref as $page=>$arr) {
159 if (empty($arr)) continue;
161 $arr = array_unique($arr);
162 $fp = fopen(CACHE_DIR . encode($page) . '.ref', 'w')
163 or die_message('cannot write ' . htmlspecialchars(CACHE_DIR . encode($page) . '.ref'));
164 foreach ($arr as $ref_page) {
165 $ref_auto = (isset($ref_notauto[$page])
166 && isset($ref_notauto[$page][$ref_page])) ? 0 : 1;
167 fputs($fp, "$ref_page\t$ref_auto\n");
173 function links_add($page, $add, $rel_auto)
175 if (PKWK_READONLY) return; // Do nothing
177 $rel_auto = array_flip($rel_auto);
179 foreach ($add as $_page) {
180 $all_auto = isset($rel_auto[$_page]);
181 $is_page = is_page($_page);
182 $ref = $page . "\t" . ($all_auto ? 1 : 0) . "\n";
184 $ref_file = CACHE_DIR . encode($_page) . '.ref';
185 if (file_exists($ref_file)) {
186 foreach (file($ref_file) as $line) {
187 list($ref_page, $ref_auto) = explode("\t", rtrim($line));
188 if (! $ref_auto) $all_auto = FALSE;
189 if ($ref_page != $page) $ref .= $line;
193 if ($is_page || ! $all_auto) {
194 $fp = fopen($ref_file, 'w')
195 or die_message('cannot write ' . htmlspecialchars($ref_file));
202 function links_delete($page, $del)
204 if (PKWK_READONLY) return; // Do nothing
206 foreach ($del as $_page) {
207 $ref_file = CACHE_DIR . encode($_page) . '.ref';
208 if (! file_exists($ref_file)) continue;
211 $is_page = is_page($_page);
214 foreach (file($ref_file) as $line) {
215 list($ref_page, $ref_auto) = explode("\t", rtrim($line));
216 if ($ref_page != $page) {
217 if (! $ref_auto) $all_auto = FALSE;
222 if ($is_page && ! $all_auto && $ref != '') {
223 $fp = fopen($ref_file, 'w')
224 or die_message('cannot write ' . htmlspecialchars($ref_file));
231 function & links_get_objects($page, $refresh = FALSE)
235 if (! isset($obj) || $refresh)
236 $obj = & new InlineConverter(NULL, array('note'));
238 return $obj->get_objects(join('', preg_grep('/^(?!\/\/|\s)./', get_source($page))), $page);