build.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. #!/usr/bin/php
  2. <?php
  3. /**
  4. * <https://y.st./>
  5. * Copyright © 2015-2019 Alex Yst <mailto:copyright@y.st>
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org./licenses/>.
  19. **/
  20. /**
  21. * This source code requires PHP to compile. It was tested with PHP
  22. * 5.6.6-2, but will likely work with anything at least as recent as
  23. * PHP 5.3. Any PHP version older than that lacks name spaces, and will
  24. * not work with this code. To install PHP without installing a Web
  25. * server, install the "php5-cli" on Debian. The package may be under a
  26. * different name on other systems and distributions. Additionally, you
  27. * will need PECL's gnupg_*() functions and the DOM extension.
  28. *
  29. * sudo aptitude install php7-cli php7-gnupg php7-dom
  30. *
  31. * You will need to install the include.d PHP library, which is not
  32. * available from the Debian repository. It can be found at
  33. * <https://notabug.org./y.st./include.d>.
  34. **/
  35. // Used for end-of-run statistics output
  36. $time_script_started = time();
  37. // A little setup ...
  38. spl_autoload_register();
  39. require 'st/y/function/error_handler.php';
  40. require 'st/y/function/highlight_string.php';
  41. require 'st/y/function/recurse_dir.php';
  42. require 'st/y/function/update_file.php';
  43. require 'st/y/function/xml_is_clean.php';
  44. set_error_handler('\\st\\y\\error_handler');
  45. require __DIR__.'/source/variables.php';
  46. require __DIR__.'/source/closure.php';
  47. // Load the previously-saved state value:
  48. $saved_state = unserialize(file_get_contents(__DIR__.'/source/saved state.txt'));
  49. // If invalid arguments are used, the whole compilation process should
  50. // halt and an error message should be presented.
  51. if(isset($invalid_arguments)):
  52. die("\nInvalid arguments: ".implode(' ', $invalid_arguments)."\n\n");
  53. endif;
  54. // If the canary chirps ...
  55. if(in_array('--canary', $argv)):
  56. ob_start();
  57. require __DIR__.'/source/canary.php';
  58. $canary = $pgp->sign(ob_get_clean());
  59. \st\y\update_file(__DIR__.'/static/a/canary.txt', $canary);
  60. $saved_state['days_until_freedom_as_of_last_canary_chirp'] = $days_until_freedom;
  61. $saved_state['days_until_freedom_as_of_last_canary_chirp_extended'] = $days_until_freedom + 365;
  62. $saved_state['days_until_partial_freedom_as_of_last_canary_chirp'] = $days_until_partial_freedom;
  63. else:
  64. echo "\nYou did not run this script using the \"--canary\" flag. Is everything okay?\n\n";
  65. endif;
  66. // If there's new course data, parse it and replace the old data.
  67. // Then remove the useless, unparsed data.
  68. if(file_exists(__DIR__.'/source/input/courses.txt')):
  69. $input = explode("\n", file_get_contents(__DIR__.'/source/input/courses.txt'));
  70. $new_courses = array();
  71. for($i=0; isset($input[$i]); $i+=3):
  72. $key = explode("\t", $input[$i+1])[1];
  73. $key = str_replace(' ', '', $key);
  74. $new_courses[$key] = $input[$i];
  75. if(empty($saved_state['courses'][$key])):
  76. $saved_state['courses'][$key] = $input[$i];
  77. endif;
  78. endfor;
  79. ksort($saved_state['courses']);
  80. $courses_lost = false;
  81. foreach($saved_state['courses'] as $key => $value):
  82. if(empty($new_courses[$key])):
  83. echo "$key removed from the list of available courses!!\n";
  84. $courses_lost = true;
  85. endif;
  86. endforeach;
  87. if($courses_lost):
  88. echo "\nSome courses seem to have been removed!\nUpdate the course schedule!\n\n";
  89. else:
  90. unlink(__DIR__.'/source/input/courses.txt');
  91. endif;
  92. endif;
  93. // A loading bar to track my remaining time at this wretched school:
  94. $temp = 0;
  95. foreach($coursework as $key => $value):
  96. if(empty($value['assignments']['Unit_7_essay'])):
  97. ++$temp;
  98. endif;
  99. endforeach;
  100. echo "Schooling completed:\n";
  101. echo str_repeat('#', $temp), '--', str_repeat('_', max(0, 44 - $temp - 2));
  102. echo "\n| YEAR 0 || YEAR 1 || YEAR 2 || YEAR 3 ||??|\n\n";
  103. /*
  104. $needed_courses = array(
  105. 'CS3304' => true,
  106. 'CS3306' => true,
  107. 'CS3307' => true,
  108. 'CS3308' => true,
  109. 'CS4403' => true,
  110. 'CS4404' => true,
  111. 'CS4405' => true,
  112. 'CS4406' => true,
  113. 'CS4408' => true,
  114. 'MATH1211' => true,
  115. 'MATH1302' => true,
  116. );
  117. $needed_course_list = '';
  118. $elective_course_list = '';
  119. foreach($saved_state['courses'] as $key => $value):
  120. if(isset($coursework[$key])):
  121. echo "\t\t<a href=\"/en/coursework/$key/\" title=\"$value\">$key</a> |\n";
  122. else:
  123. echo "\t\t<span class=\"hyperlink_unavailable\" title=\"$value\">$key</span> |\n";
  124. endif;
  125. if(!isset($coursework[$key])):
  126. if(isset($needed_courses[$key])):
  127. $needed_course_list .= "=> $key: $value\n";
  128. else:
  129. $elective_course_list .= "-> $key: $value\n";
  130. endif;
  131. endif;
  132. endforeach;
  133. echo "Remaining courses:\n", $needed_course_list, $elective_course_list, "\n";
  134. */
  135. // Tell PHP to use detached signatures, so the XHTML pages will still validate.
  136. $pgp->setsignmode(GNUPG_SIG_MODE_DETACH);
  137. // Some files are static, and only need to be copied, not compiled.
  138. foreach(\st\y\recurse_dir(__DIR__.'/static') as $filename):
  139. // TO DO: This conditional is hacky; fix it. Check path prefixes
  140. // against keys or values in an array or something.
  141. if(substr($filename, 0, 34) != 'img/CC_BY-SA_4.0/y.st./coursework/'):
  142. $dirname = dirname(__DIR__."/compiled/$filename");
  143. if(!is_dir($dirname)):
  144. mkdir($dirname, 0777, true);
  145. endif;
  146. \st\y\update_file(__DIR__."/compiled/$filename", file_get_contents(__DIR__."/static/$filename"));
  147. $should_exist[$filename] = true;
  148. endif;
  149. endforeach;
  150. // Other files are compiled.
  151. foreach(\st\y\recurse_dir(__DIR__."/source/pages") as $filename):
  152. require __DIR__."/source/pages/$filename";
  153. // Some pages have been censored by third parties.
  154. if(isset($xhtml['takedown'])):
  155. require __DIR__."/source/pages/en/take-down/$xhtml[takedown].xhtml";
  156. $censored_pages++;
  157. endif;
  158. $¢_build_page($filename, $xhtml);
  159. endforeach;
  160. // Account for moved pages.
  161. require __DIR__.'/source/redirect.php';
  162. // Directory indexes should be built.
  163. require __DIR__.'/source/weblog_index.php';
  164. // Copy the English index to the main index because the plan to set up
  165. // an Esperanto version of the website sort of flopped.
  166. //copy(__DIR__.'/compiled/en/index.xhtml', __DIR__.'/compiled/index.xhtml');
  167. //$should_exist['index.xhtml'] = true;
  168. // The current host doesn't handle index files with a ".xhtml"
  169. // extension, but it does serve Content-Type headers base on the
  170. // content of the file instead of the file extension. The index files
  171. // will therefore be served as XHTML even with the wrong file
  172. // extension. To get them to also be treated as index files, let's copy
  173. // all "index.xhtml" files to "index.html".
  174. //
  175. // Actually, never mind. That host's administrator got frustrated that
  176. // I didn't want to use the host name or TLS certificate "authority"
  177. // they like, and decided to stop helping me with unrelated issues
  178. // regarding their server, such as that the server does use file
  179. // extensions after all to decided on Content-Type headers to send and
  180. // that "index.xhtml" isn't recognised as an index file name by the
  181. // server. That hosting isn't going to work out, so there's no reason
  182. // to copy the index files to a new location. Comment this out until a
  183. // day it might one day be needed again.
  184. #foreach(\st\y\recurse_dir(__DIR__.'/compiled') as $filename):
  185. # if(substr($filename, -11) == 'index.xhtml'):
  186. # $temp_filename = substr($filename, 0, -5).'html';
  187. # $should_exist[$temp_filename] = true;
  188. # $temp_file = file_get_contents(__DIR__.'/compiled/'.$filename);
  189. # if(!file_exists(__DIR__.'/compiled/'.$temp_filename) or
  190. # $temp_file != file_get_contents(__DIR__.'/compiled/'.$temp_filename)):
  191. # file_put_contents(__DIR__.'/compiled/'.$temp_filename, $temp_file);
  192. # endif;
  193. # endif;
  194. #endforeach;
  195. foreach(\st\y\recurse_dir(__DIR__.'/compiled') as $filename):
  196. if(!isset($should_exist[$filename]) and substr($filename, 0, 5) != '.git/'):
  197. unlink(__DIR__."/compiled/$filename");
  198. endif;
  199. endforeach;
  200. // Save the state of some values for next time:
  201. $saved_state['censored_pages'] = $censored_pages;
  202. file_put_contents(__DIR__.'/source/saved state.txt', serialize($saved_state));
  203. // End-of-run statistics:
  204. $time_until_script_ended = time() - $time_script_started;
  205. echo <<<END
  206. The script completed in $time_until_script_ended seconds.
  207. $censored_pages pages have been censored.
  208. END;