2 /////////////////////////////////////////////////
3 // PukiWiki - Yet another WikiWikiWeb clone.
5 // $Id: file.php,v 1.11 2003/03/02 16:08:04 panda Exp $
9 function get_source($page)
11 if (!is_page($page)) {
14 return str_replace("\r",'',file(get_filename($page)));
17 // ¥Ú¡¼¥¸¤Î¹¹¿·»þ¹ï¤òÆÀ¤ë
18 function get_filetime($page)
20 return filemtime(get_filename($page)) - LOCALZONE;
23 // ¥Ú¡¼¥¸¤Î¥Õ¥¡¥¤¥ë̾¤òÆÀ¤ë
24 function get_filename($page)
26 return DATA_DIR.encode($page).'.txt';
30 function page_write($page,$postdata)
32 $postdata = make_str_rules($postdata);
35 $oldpostdata = is_page($page) ? join('',get_source($page)) : '';
36 $diffdata = do_diff($oldpostdata,$postdata);
37 file_write(DIFF_DIR,$page,$diffdata);
40 make_backup($page,$postdata == '');
43 file_write(DATA_DIR,$page,$postdata);
45 // is_page¤Î¥¥ã¥Ã¥·¥å¤ò¥¯¥ê¥¢¤¹¤ë¡£
48 // link¥Ç¡¼¥¿¥Ù¡¼¥¹¤ò¹¹¿·
52 // ¥æ¡¼¥¶ÄêµÁ¥ë¡¼¥ë(¥½¡¼¥¹¤òÃÖ´¹¤¹¤ë)
53 function make_str_rules($str)
57 $arr = explode("\n",$str);
60 foreach ($arr as $str)
62 if ($str{0} != ' ' and $str{0} != "\t")
64 foreach ($str_rules as $rule => $replace)
66 $str = preg_replace("/$rule/",$replace,$str);
72 return join("\n",$retvars);
76 function file_write($dir,$page,$str)
78 global $post,$update_exec;
79 global $_msg_invalidiwn;
81 if (!is_pagename($page))
84 str_replace('$1',htmlspecialchars($page),
85 str_replace('$2','WikiName',$_msg_invalidiwn)
89 $page = strip_bracket($page);
91 $file = $dir.encode($page).'.txt';
93 if ($dir == DATA_DIR and $str == '' and file_exists($file)) {
97 $str = preg_replace("/\r/",'',$str);
98 $str = rtrim($str)."\n";
100 if (!empty($post['notimestamp']) and file_exists($file)) {
101 $timestamp = filemtime($file) - LOCALZONE;
104 $fp = fopen($file,'w')
105 or die_message('cannot write page file or diff file or other'.htmlspecialchars($page).'<br>maybe permission is not writable or filename is too long');
111 touch($file,$timestamp + LOCALZONE);
119 if ($update_exec and $dir == DATA_DIR) {
120 system($update_exec.' > /dev/null &');
124 // ºÇ½ª¹¹¿·¥Ú¡¼¥¸¤Î¹¹¿·
125 function put_lastmodified()
127 global $script,$post,$maxshow,$whatsnew,$non_list,$autolink;
129 $pages = get_existpages();
130 $recent_pages = array();
131 foreach($pages as $page)
133 if ($page != $whatsnew and !preg_match("/$non_list/",$page))
135 $recent_pages[$page] = get_filetime($page);
140 arsort($recent_pages,SORT_NUMERIC);
142 // create recent.dat (for recent.inc.php)
143 $fp = fopen(CACHE_DIR.'recent.dat','w')
144 or die_message('cannot write cache file '.CACHE_DIR.'recent.dat<br>maybe permission is not writable or filename is too long');
146 foreach ($recent_pages as $page=>$time)
148 fputs($fp,"$time\t$page\n");
153 // create RecentChanges
154 $fp = fopen(get_filename($whatsnew),'w')
155 or die_message('cannot write page file '.htmlspecialchars($whatsnew).'<br>maybe permission is not writable or filename is too long');
157 foreach (array_splice($recent_pages,0,$maxshow) as $page=>$time)
159 $s_lastmod = htmlspecialchars(format_date($time));
160 $s_page = htmlspecialchars($page);
161 fputs($fp, "-$s_lastmod - [[$s_page]]\n");
163 fputs($fp,"#norelated\n"); // :)
170 $fp = fopen(CACHE_DIR.'autolink.dat','w')
171 or die_message('cannot write autolink file '.CACHE_DIR.'/autolink.dat<br>maybe permission is not writable');
173 fputs($fp,get_autolink_pattern($pages));
179 // »ØÄꤵ¤ì¤¿¥Ú¡¼¥¸¤Î·Ð²á»þ¹ï
180 function get_pg_passage($page,$sw=TRUE)
182 global $show_passage;
185 if (!$show_passage) {
189 if (!isset($pg_passage)) {
190 $pg_passage = array();
193 if (!array_key_exists($page,$pg_passage)) {
194 $pg_passage[$page] = (is_page($page) and $time = get_filetime($page)) ? get_passage($time) : '';
197 return $sw ? "<small>{$pg_passage[$page]}</small>" : " {$pg_passage[$page]}";
200 // Last-Modified ¥Ø¥Ã¥À
201 function header_lastmod()
205 if ($lastmod and is_page($page)) {
206 header('Last-Modified: '.date('D, d M Y H:i:s',get_filetime($page)).' GMT');
210 // Á´¥Ú¡¼¥¸Ì¾¤òÇÛÎó¤Ë
211 function get_existpages($dir=DATA_DIR,$ext='.txt')
215 $pattern = '^([0-9A-F]+)';
217 $pattern .= preg_quote($ext,'/').'$';
220 or die_message($dir. ' is not found or not readable.');
221 while ($file = readdir($dp)) {
222 if (preg_match("/$pattern/",$file,$matches)) {
223 $aryret[] = decode($matches[1]);
229 //¥Õ¥¡¥¤¥ë̾¤Î°ìÍ÷¤òÇÛÎó¤Ë(¥¨¥ó¥³¡¼¥ÉºÑ¤ß¡¢³ÈÄ¥»Ò¤ò»ØÄê)
230 function get_existfiles($dir,$ext)
234 $pattern = '^[0-9A-F]+'.preg_quote($ext,'/').'$';
236 or die_message($dir. ' is not found or not readable.');
237 while ($file = readdir($dp)) {
238 if (preg_match("/$pattern/",$file)) {
239 $aryret[] = $dir.$file;
245 function links_update($page)
249 // link¥Ç¡¼¥¿¥Ù¡¼¥¹¤ò¹¹¿·
250 if (exist_plugin_action('links')) {
252 $tmp = $vars['page'];
253 $vars['page'] = $page;
254 do_plugin_action('links');
255 $vars['page'] = $tmp;
258 //¤¢¤ë¥Ú¡¼¥¸¤Î´ØÏ¢¥Ú¡¼¥¸¤òÆÀ¤ë
259 function links_get_related($page)
261 global $vars,$related;
264 if (!isset($links)) {
268 if (array_key_exists($page,$links)) {
269 return $links[$page];
272 // ²Äǽ¤Ê¤émake_link()¤ÇÀ¸À®¤·¤¿´ØÏ¢¥Ú¡¼¥¸¤ò¼è¤ê¹þ¤à
273 $links[$page] = ($page == $vars['page']) ? $related : array();
275 $a_page = addslashes($page);
277 if (defined('LINK_DB')) {
279 SELECT refpage.name,refpage.lastmod FROM page left join link on page.id = page_id left join page as refpage on ref_id = refpage.id where page.name = '$a_page'
281 SELECT DISTINCT refpage.name,refpage.lastmod FROM page left join link on page.id = ref_id left join page as refpage on page_id = refpage.id where page.name = '$a_page';
283 $rows = db_query($sql);
285 foreach ($rows as $row) {
286 if (empty($row['name']) or substr($row['name'],0,1) == ':') {
289 $links[$page][$row['name']] = $row['lastmod'];
293 // $links[$page] = array_merge($links[$page],do_search($page,'OR',1));
294 $ref_name = CACHE_DIR.encode($page).'.ref';
295 if (file_exists($ref_name)) {
296 foreach (file($ref_name) as $line) {
297 list($_page,$time) = explode("\t",rtrim($line));
298 $links[$page][$_page] = $time;
303 return $links[$page];