db39ac9d9f4602bb197c0b825ebfaf6c9f8388bf
[atutor.git] / mods / wiki / tools / xt_searchcache_staticf.php
1 <?php
2    include("t_config.php");
3 ?>
4 <html>
5 <head>
6  <title>XT: cache static files' text into database</title>
7  <link rel="stylesheet" type="text/css" href="t_config.css">
8 </head>
9 <body bgcolor="#ffffff" text="#000000">
10 <h1>cache static files</h1>
11 <?php
12
13
14 #-- action
15 if ($_REQUEST["go"]) {
16
17    $dir = $_REQUEST["dir"];
18    $prefix = $_REQUEST["prefix"];
19    $glob = $_REQUEST["glob"];
20    
21    #-- scan for files
22    $ls = scan_dir($dir);
23    foreach ($ls as $fn) if (fnmatch($glob, $fn)) {
24       $id = "$prefix$fn";
25
26       #-- checking for entry
27       if ($data = ewiki_db::GET($id)) {
28          // our cache entries don't have any flag set
29          if ($data["flags"] & EWIKI_DB_F_TYPE) {
30             continue;  // real page exists, definetely won't overwrite
31          }
32       }
33
34       #-- read
35       $content = file_get_contents("$dir/$fn");
36       $data = ewiki_db::CREATE($id, 0x000);
37       $data["content"] = $content;
38       $data["meta"]["class"] = "search";
39
40       #-- write
41       ewiki_db::WRITE($id, "_OVERWRITE=1");
42    }
43    
44
45    #-- returns only filenames (no dirs) with leading dirspec omitted   
46    function scan_dir($dir, $pfix="") {
47       $r = array();
48       $dh = opendir($dir);
49       while ($fn = readdir($dh)) {
50          if (is_dir("$dir/$pfix$fn")) {
51             $r += scan_dir($dir, "$pfix$fn/");
52          }
53          else {
54             $r[] = "$pfix$fn";
55          }
56       }
57       return($r);
58    }
59
60 }
61
62 #-- help
63 else {
64
65    ?>
66      This tool is useful if 'yoursite.php' loads from static files and from the Wiki
67      database concurrently and the URLs are therefore similar enough to warrant
68      caching of static .html files into the database to use the unified search
69      feature.<br>
70      This is the case if you use <b>mod_rewrite</b> and have URLs like
71      <tt>http://example.org/WikiPage</tt> and <tt>http://example.org/otherpage.html</tt>
72      and both get displayed by your index.php script depending on if such a static page
73      exists. In this case EWIKI_SCRIPT is most always set to "/".
74      <br>
75      <br>
76      Else don't use this tool, it would generate silly database entries then
77      for your setup.
78      <br>
79      <br>
80      <form action="xt_searchcache_staticf.php" method="POST">
81        directory <input type="text" name="dir" value="./static/"><br>
82        (we're currently in "<?php echo getcwd(); ?>")
83        <br><br>
84        maps to <input type="text" name="prefix" value="static/"><br>
85        (don't forget that EWIKI_SCRIPT is also prepended to this)       
86        <br><br>
87        file types <input type="text" name="glob" value="*.htm*"><br>
88        (unix filename globbing!)
89        <br><br>
90        <input type="submit" value="Generate" name="go">
91      </form>
92      <?php 
93
94 }
95
96
97
98 ?>
99 </body>
100 </html>