changed git call from https to git readonly
[atutor.git] / mods / wiki / tools / t_checklinks.php
1 <?php
2   include("t_config.php");
3 ?>
4 <html>
5 <head>
6  <title>check http:// links of a wiki page</title>
7  <link rel="stylesheet" type="text/css" href="t_config.css">
8 </head>
9 <body bgcolor="#ffffff" text="#000000">
10 <h1>link check</h1>
11 <?php
12
13
14   if (empty($_REQUEST["page"])) {
15
16      echo '
17 This tool checks all http:// links for availability, afterwards resaves the
18 wiki page with the dead links marked for easier editing.
19 <br><br>
20 <form action="t_checklinks.php" method="POST">
21
22 <select name="page_pattern">
23   <option value="1" selected>only page</option>
24   <option value="2">page pattern</option>
25 </select>
26 <input name="page" size="40" value="LinkDirectory"><br><br>
27
28 <input type="submit" value="check http:// links"><br><br>
29
30 <input type="checkbox" name="opt[modify]" checked value="1" id="opt-modify"><label for="opt-modify"> modify pages, mark bad links</label><br>
31 <input type="checkbox" name="opt[404]" checked value="1" id="opt-404"><label for="opt-404"> extened error 404 checks (to detect text-only/human-visible error messages of buggy portal scripts)</label><br>
32 <br>
33 </form>
34 ';
35
36   }
37   else {
38   
39      echo "<h4>link checking starts...</h4>\n";
40      set_time_limit(0);
41   
42      $ext404 = $_REQUEST["opt"]["404"];
43      $modify = $_REQUEST["opt"]["modify"];
44      
45      #-- single page mode
46      if ($_REQUEST["page_pattern"]==1) {
47         $id = $_REQUEST["page"];
48         check_links_for($id, $ext404, $modify);
49      }
50      
51      #-- multiple pages
52      else {
53         $pat = trim($_REQUEST["page"], "*");
54         $result = ewiki_db::SEARCH("id", $pat);
55         while ($row = $get->result) {
56            check_links_for($row["id"], $ext404, $modify);
57         }
58      }
59
60      echo "<br><b>done.</b><br><br>";
61   }
62   
63   
64 function check_links_for($id, $ext404=1, $modify=1) {
65
66      echo "<b><tt>$id</tt></b><br>\n";
67
68      $get = ewiki_db::GET($id);
69      $content = $get["content"];
70      
71      preg_match_all('_(http://[^\s"\'<>#,;]+[^\s"\'<>#,;.])_', $content, $links);
72      $badlinks = array();
73      foreach ($links[1] as $href) {
74
75         set_time_limit(20);
76         $d = @implode("", @file($href));
77         
78         if ($ext404) {
79            if (strstr($d, "Apache/") && (stristr($d, "file not found") || stristr($d, "error 404"))) {
80               $d = "";
81            }
82         }
83         if (empty($d) || !strlen(trim($d))) {
84            echo "[DEAD] $href<br>\n";
85            $badlinks[] = $href;
86         }
87         else {
88            echo "[OK] $href<br>\n";
89         }
90      }
91
92      #-- if sometihng found, modify on request
93      if ($modify) {
94          #-- replace dead links
95          foreach ($badlinks as $href) {
96             $content = preg_replace("\377^(.*$href)\377m", ' µµ__~[OFFLINE]__µµ   $1', $content);
97          }
98
99          #-- compare against db content
100          if ($content != $get["content"]) {
101
102             $get["content"] = $content;
103             $get["version"]++;
104             $get["author"] = ewiki_author("ewiki_checklinks");
105             $get["lastmodified"] = time();
106
107             ewiki_db::WRITE($get);
108             echo "<sup>(updated)</sup><br>";
109          }
110      }
111 }
112
113
114 ?>
115 </body>
116 </html>