closure.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. <?php
  2. /**
  3. * <https://y.st./>
  4. * Copyright © 2016-2019 Alex Yst <mailto:copyright@y.st>
  5. *
  6. * This program is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org./licenses/>.
  18. **/
  19. /**
  20. * Closures are for writing functions that aren't fit to be reused in
  21. * other projects. If a function isn't fit to be used in other
  22. * projects, it clearly hasn't been generalized enough. As such, the
  23. * code in this file really should be generalized more. I'll rewrite
  24. * this code as I figure out how to better generalize it.
  25. *
  26. * I'm stashing closures in a pseudo-namespace by prefixing their names
  27. * with "$¢_". It's ugly, clearly, but then again, so are the closures
  28. * here themselves.
  29. **/
  30. $¢_build_term_update_schedule = function($year, $month, $day) {
  31. $update = new \st\y\datetime($year, $month, $day);
  32. $update->add(new DateInterval('P6D'));
  33. $week = new DateInterval('P7D');
  34. $return = '';
  35. for($i=0;$i<7;$i++):
  36. $return .= <<<END
  37. <li>
  38. <time>{$update->format('l, Y-m-d')}</time>
  39. </li>
  40. END;
  41. $update->add($week);
  42. endfor;
  43. $update->add(new DateInterval('P1D'));
  44. $return .= <<<END
  45. <li>
  46. <time>{$update->format('l, Y-m-d')}</time>
  47. </li>
  48. END;
  49. return $return; };
  50. $¢_set_data_from_path = function(string $filename, array $xhtml): array {
  51. global $a;
  52. global $coursework_nav;
  53. global $latest_entry;
  54. global $interval;
  55. global $entry;
  56. if(substr('/'.$filename, -12) == '/index.xhtml'):
  57. $xhtml['<{URI path minus slash}>'] = substr($filename, 0, -11);
  58. else:
  59. $xhtml['<{URI path minus slash}>'] = $filename;
  60. endif;
  61. $xhtml['<{stripped title}>'] = strip_tags($xhtml['<{title}>']);
  62. $xhtml['<{encoded path minus slash}>'] = urlencode($xhtml['<{URI path minus slash}>']);
  63. $xhtml['<{nav}>'] = "<nav>\n";
  64. if(substr($filename, 0, 3) == 'en/'):
  65. $xhtml['<{nav}>'] .= <<<END
  66. <p>
  67. <a href="/en/coursework/">Coursework</a> |
  68. <a href="/en/take-down/">Take-down requests</a> |
  69. <a href="/en/">Home</a> |
  70. <a href="/en/a/about.xhtml">About</a> |
  71. <a href="/en/a/contact.xhtml">Contact</a> |
  72. <a href="/a/canary.txt">Canary</a> |
  73. <a href="/en/URI_research/">$a[URI] research</a> |
  74. <a href="/en/opinion/">Opinions</a> |
  75. <a href="/en/law/">Law</a> |
  76. <a href="/en/recipe/">Recipes</a> |
  77. <a href="/en/a/links.xhtml">Links</a> |
  78. <a href="/$filename.asc">{this page}.asc</a>
  79. </p>
  80. <hr/>
  81. END;
  82. if($filename != 'en/coursework/index.xhtml' and substr($filename, 0, 14) == 'en/coursework/'):
  83. $xhtml['<{nav}>'] .= <<<'END'
  84. <p>
  85. <span class="hyperlink_unavailable" title="Art History">AHIST 1401</span> |
  86. <span class="hyperlink_unavailable" title="Biology 1 for Health Studies Majors">BIOL 1121</span> |
  87. <span class="hyperlink_unavailable" title="Introduction to Biology">BIOL 1301</span> |
  88. <a href="/en/coursework/BUS1101/" title="Principles of Business Management">BUS 1101</a> |
  89. <a href="/en/coursework/CS1101/" title="Programming Fundamentals">CS 1101</a> |
  90. <a href="/en/coursework/CS1102/" title="Programming 1">CS 1102</a> |
  91. <a href="/en/coursework/CS1103/" title="Programming 2">CS 1103</a> |
  92. <a href="/en/coursework/CS1104/" title="Computer Systems">CS 1104</a> |
  93. <a href="/en/coursework/CS2203/" title="Databases 1">CS 2203</a> |
  94. <a href="/en/coursework/CS2204/" title="Communications and Networking">CS 2204</a> |
  95. <a href="/en/coursework/CS2205/" title="Web Programming 1">CS 2205</a> |
  96. <a href="/en/coursework/CS2301/" title="Operating Systems 1">CS 2301</a> |
  97. <a href="/en/coursework/CS2401/" title="Software Engineering 1">CS 2401</a> |
  98. <a href="/en/coursework/CS3303/" title="Data Structures">CS 3303</a> |
  99. <a href="/en/coursework/CS3304/" title="Analysis of Algorithms">CS 3304</a> |
  100. <a href="/en/coursework/CS3305/" title="Web Programming 2">CS 3305</a> |
  101. <a href="/en/coursework/CS3306/" title="Databases 2">CS 3306</a> |
  102. <a href="/en/coursework/CS3307/" title="Operating Systems 2">CS 3307</a> |
  103. <a href="/en/coursework/CS3308/" title="Information Retrieval">CS 3308</a> |
  104. <a href="/en/coursework/CS4402/" title="Comparative Programming Languages">CS 4402</a> |
  105. <a href="/en/coursework/CS4403/" title="Software Engineering 2">CS 4403</a> |
  106. <a href="/en/coursework/CS4404/" title="Advanced Networking and Data Security">CS 4404</a> |
  107. <a href="/en/coursework/CS4405/" title="Mobile Applications">CS 4405</a> |
  108. <a href="/en/coursework/CS4406/" title="Computer Graphics">CS 4406</a> |
  109. <a href="/en/coursework/CS4407/" title="Data Mining and Machine Learning">CS 4407</a> |
  110. <a href="/en/coursework/CS4408/" title="Artificial Intelligence">CS 4408</a> |
  111. <span class="hyperlink_unavailable" title="Introduction to Economics">ECON 1580</span> |
  112. <a href="/en/coursework/ENGL1102/" title="English Composition 2">ENGL 1102</a> |
  113. <a href="/en/coursework/ENGL1405/" title="World Literature">ENGL 1405</a> |
  114. <a href="/en/coursework/ENVS1301/" title="Introduction to Environmental Sciences">ENVS 1301</a> |
  115. <a href="/en/coursework/HIST1421/" title="Greek and Roman Civilization">HIST 1421</a> |
  116. <a href="/en/coursework/MATH1201/" title="College Algebra">MATH 1201</a> |
  117. <a href="/en/coursework/MATH1211/" title="Calculus">MATH 1211</a> |
  118. <a href="/en/coursework/MATH1280/" title="Introduction to Statistics">MATH 1280</a> |
  119. <span class="hyperlink_unavailable" title="Statistical Inference">MATH 1281</span> |
  120. <a href="/en/coursework/MATH1302/" title="Discrete Mathematics">MATH 1302</a> |
  121. <a href="/en/coursework/PHIL1402/" title="Introduction to Philosophy">PHIL 1402</a> |
  122. <a href="/en/coursework/PHIL1404/" title="Ethics and Social Responsibility">PHIL 1404</a> |
  123. <a href="/en/coursework/POLS1503/" title="Globalization">POLS 1503</a> |
  124. <a href="/en/coursework/PSYC1111/" title="Introduction to Health Psychology (previously known as Introduction to Human Psychology)">PSYC 1111</a> |
  125. <a href="/en/coursework/PSYC1205/" title="Emotional intelligence (EI)">PSYC 1205</a> |
  126. <a href="/en/coursework/PSYC1504/" title="Introduction to Psychology">PSYC 1504</a> |
  127. <a href="/en/coursework/SOC1502/" title="Introduction to Sociology">SOC 1502</a> |
  128. <a href="/en/coursework/UNIV1001/" title="Online Education Strategies">UNIV 1001</a>
  129. </p>
  130. <hr/>
  131. END;
  132. // It's less work to maintain this if we have the script dynamically
  133. // decide what the course code must be.
  134. $course_path_parts = explode('/', $filename);
  135. // If there's an element number three, element two is the course code.
  136. // Otherwise, we have no course code at all because we're not dealing
  137. // with a course subdirectory.
  138. if(isset($course_path_parts[3])):
  139. $xhtml['<{nav}>'] .= "\t<p>\n\t\t{$coursework_nav[$course_path_parts[2]]}\n\t</p>\n\t<hr/>\n";
  140. endif;
  141. endif;
  142. if($filename != 'en/opinion/index.xhtml' and substr($filename, 0, 11) == 'en/opinion/'):
  143. $xhtml['<{nav}>'] .= <<<END
  144. <p>
  145. <a href="/en/opinion/XML.xhtml">$a[XML]</a> |
  146. <a href="/en/opinion/code_indentation.xhtml">Code indentation</a> |
  147. <a href="/en/opinion/free_culture.xhtml">Free culture</a> |
  148. <a href="/en/opinion/free_software.xhtml">Free software</a> |
  149. <a href="/en/opinion/holidays.xhtml">Holidays</a> |
  150. <a href="/en/opinion/misuse_of_words.xhtml">Misuse of words</a>
  151. </p>
  152. <hr/>
  153. END;
  154. endif;
  155. if(substr($filename, 0, 10) == 'en/weblog/'):
  156. $current_entry = substr($xhtml['<{URI path minus slash}>'], 10);
  157. $current_entry = explode('/', $current_entry);
  158. $xhtml['<{nav}>'] .= <<<END
  159. <p>
  160. Weblog index:
  161. <a href="/en/weblog/memories">Memories</a> |
  162. <a href="/en/weblog/">$a[ASCII] calendars</a> |
  163. <a href="/en/weblog/index_ol_ascending.xhtml">Ascending list</a> |
  164. <a href="/en/weblog/index_ol_descending.xhtml">Descending list</a>
  165. </p>
  166. <hr/>
  167. END;
  168. if(is_numeric($current_entry[0])):
  169. switch(count($current_entry)):
  170. // Main weblog index:
  171. case 1:
  172. break;
  173. // Weblog year index:
  174. case 2:
  175. $datetime = new DateTimeImmutable("$current_entry[0]-01-01");
  176. $previous = $datetime->sub($interval['year'])->format('Y');
  177. $next = $datetime->add($interval['year'])->format('Y');
  178. $xhtml['<{nav}>'] .= <<<END
  179. <p>
  180. Jump to year:
  181. END;
  182. if($filename != 'en/weblog/2015/index.xhtml'):
  183. $xhtml['<{nav}>'] .= <<<END
  184. <a href="/en/weblog/2015/">&lt;&lt;First</a>
  185. END;
  186. endif;
  187. if(is_dir(__DIR__."/pages/en/weblog/$previous/")):
  188. $xhtml['<{nav}>'] .= <<<END
  189. <a rel="prev" href="/en/weblog/$previous/">&lt;Previous</a>
  190. END;
  191. endif;
  192. if(is_dir(__DIR__."/pages/en/weblog/$next/")):
  193. $xhtml['<{nav}>'] .= <<<END
  194. <a rel="next" href="/en/weblog/$next/">Next&gt;</a>
  195. END;
  196. endif;
  197. if("/$filename" != "$latest_entry[year]index.xhtml"):
  198. $xhtml['<{nav}>'] .= <<<END
  199. <a href="/en/weblog/latest_year.xhtml">Latest&gt;&gt;</a>
  200. END;
  201. endif;
  202. $xhtml['<{nav}>'] .= <<<END
  203. </p>
  204. <hr/>
  205. END;
  206. break;
  207. case 3:
  208. // Weblog month index:
  209. if($current_entry[2] == ''):
  210. $current_entry[1] = explode('-', $current_entry[1])[0];
  211. $datetime = new DateTimeImmutable("$current_entry[0]-$current_entry[1]-01");
  212. $previous = $datetime->sub($interval['month'])->format('Y/m-F');
  213. $next = $datetime->add($interval['month'])->format('Y/m-F');
  214. $xhtml['<{nav}>'] .= <<<'END'
  215. <p>
  216. Jump to month:
  217. END;
  218. if($filename != 'en/weblog/2015/03-March/index.xhtml'):
  219. $xhtml['<{nav}>'] .= <<<'END'
  220. <a href="/en/weblog/2015/03-March/">&lt;&lt;First</a>
  221. END;
  222. endif;
  223. if(is_dir(__DIR__."/pages/en/weblog/$previous/")):
  224. $xhtml['<{nav}>'] .= <<<END
  225. <a rel="prev" href="/en/weblog/$previous/">&lt;Previous</a>
  226. END;
  227. endif;
  228. if(is_dir(__DIR__."/pages/en/weblog/$next/")):
  229. $xhtml['<{nav}>'] .= <<<END
  230. <a rel="next" href="/en/weblog/$next/">Next&gt;</a>
  231. END;
  232. endif;
  233. if("/$filename" != "$latest_entry[month]index.xhtml"):
  234. $xhtml['<{nav}>'] .= <<<'END'
  235. <a href="/en/weblog/latest_month.xhtml">Latest&gt;&gt;</a>
  236. END;
  237. endif;
  238. $xhtml['<{nav}>'] .= <<<'END'
  239. </p>
  240. <hr/>
  241. END;
  242. // Weblog day entry:
  243. else:
  244. $current_entry[1] = explode('-', $current_entry[1])[0];
  245. $current_entry[2] = explode('.', $current_entry[2])[0];
  246. $datetime = new DateTimeImmutable("$current_entry[0]-$current_entry[1]-$current_entry[2]");
  247. $previous = $datetime->sub($interval['day'])->format('Y/m-F/d');
  248. $next = $datetime->add($interval['day'])->format('Y/m-F/d');
  249. $xhtml['<{subtitle}>'] = $entry($current_entry[0], $current_entry[1], $current_entry[2]);
  250. $xhtml['<{nav}>'] .= <<<'END'
  251. <p>
  252. Jump to entry:
  253. END;
  254. if($filename != 'en/weblog/2015/03-March/07.xhtml'):
  255. $xhtml['<{nav}>'] .= <<<'END'
  256. <a href="/en/weblog/2015/03-March/07.xhtml">&lt;&lt;First</a>
  257. END;
  258. endif;
  259. if(file_exists(__DIR__."/pages/en/weblog/$previous.xhtml")):
  260. $xhtml['<{nav}>'] .= <<<END
  261. <a rel="prev" href="/en/weblog/$previous.xhtml">&lt;Previous</a>
  262. END;
  263. endif;
  264. if(file_exists(__DIR__."/pages/en/weblog/$next.xhtml")):
  265. $xhtml['<{nav}>'] .= <<<END
  266. <a rel="next" href="/en/weblog/$next.xhtml">Next&gt;</a>
  267. END;
  268. endif;
  269. if("/$filename" != $latest_entry['day']):
  270. $xhtml['<{nav}>'] .= <<<'END'
  271. <a href="/en/weblog/latest.xhtml">Latest&gt;&gt;</a>
  272. END;
  273. endif;
  274. $xhtml['<{nav}>'] .= <<<'END'
  275. </p>
  276. <hr/>
  277. END;
  278. endif;
  279. break;
  280. default:
  281. throw new \st\y\exception\unexpected_value("The weblog section of the source directory tree has an anomaly. File path \"$filename\" detected.", '[000000]');
  282. endswitch;
  283. // Automatic copyright dating of journal entries:
  284. if(empty($xhtml['<{copyright year}>']) and !empty($current_entry[0])):
  285. $xhtml['<{copyright year}>'] = $current_entry[0];
  286. endif;
  287. endif;
  288. endif;
  289. endif;
  290. $xhtml['<{nav}>'] .= '</nav>';
  291. if(!isset(
  292. $xhtml['<{copyright year}>'],
  293. $xhtml['<{URI path minus slash}>'],
  294. $xhtml['<{encoded path minus slash}>'],
  295. $xhtml['<{title}>'],
  296. $xhtml['<{subtitle}>'],
  297. $xhtml['<{stripped title}>'],
  298. $xhtml['<{nav}>'],
  299. $xhtml['<{body}>']
  300. ) or count($xhtml) != 8):
  301. var_dump(array_keys($xhtml));
  302. throw new \st\y\exception\unexpected_value("File '$filename' has invalid data.", '[000000]');
  303. endif;
  304. return $xhtml; };
  305. $¢_build_page = function(string $filename, array $xhtml) {
  306. global $pgp;
  307. global $should_exist;
  308. global $¢_set_data_from_path;
  309. global $template;
  310. $xhtml = $¢_set_data_from_path($filename, $xhtml);
  311. $page = strtr($template, $xhtml);
  312. /*
  313. // Automated page validation
  314. $validate = new DOMDocument;
  315. $validate->load(__DIR__."/../compiled/$filename");
  316. var_dump($filename);
  317. if(!$validate->validate()):
  318. // Due to an error in PHP's DOM module, it throws errors when a
  319. // document is invalid instead of simply returning false. Our error
  320. // handler deliberately makes all errors fatal, so this exception will
  321. // never be thrown. However, we have it here anyway in case the error
  322. // in PHP's DOM module is ever corrected.
  323. throw new Exception("File '$filename' seems to produce invalid XML.");
  324. endif;
  325. */
  326. $should_exist[$filename] = true;
  327. $should_exist["$filename.asc"] = true;
  328. $dirname = dirname(__DIR__."/../compiled/$filename");
  329. if(!is_dir($dirname)):
  330. mkdir($dirname, 0777, true);
  331. endif;
  332. if(\st\y\xml_is_clean($page)):
  333. \st\y\update_file(__DIR__."/../compiled/$filename", $page, $pgp);
  334. else:
  335. throw new \st\y\exception\unexpected_value("UNCLEAN PAGE MARKUP: $filename", '[000000]');
  336. endif;
  337. };