Changeset - 830e818d2625
[Not reviewed]
default
0 3 0
Nathan Brink (binki) - 15 years ago 2011-01-01 02:15:45
ohnobinki@ohnopublishing.net
Support rehashing stuffage through the web interface. Only per-school rehashing is tested so far.
3 files changed with 120 insertions and 73 deletions:
0 comments (0 inline, 0 general)
admin.php
Show inline comments
 
@@ -9,25 +9,27 @@
 
 * the Free Software Foundation, either version 3 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * SlatePermutate is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU Affero General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU Affero General Public License
 
 * along with SlatePermutate.  If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
  include_once 'inc/class.page.php';
 
require_once('inc/class.page.php');
 
require_once('inc/admin.inc');
 

	
 

	
 
  $scripts = array('jQuery','jQueryUI'); 
 
  $adminpage = new page('Administration',$scripts,FALSE);
 
  $datepicker = '$(function() {
 
                   $( "#datepicker" ).datepicker();
 
                   $( "#datepicker" ).datepicker( "option", "dateFormat", "yy-mm-dd" );
 
                 });';
 
  $adminpage->headcode_add('datePicker', $adminpage->script_wrap($datepicker), TRUE);
 
  $adminpage->head();
 

	
 
  if(!isset($admin_pass) || !isset($admin_enable) || $admin_enable !== true) {
 
    echo "<p>The administration interface is not enabled or is improperly configured. See config.inc for more information.</p>";
 
@@ -70,69 +72,66 @@
 
          // unlink($dir . '/' . $file->getFilename());
 
          $date = date("Y-m-d",$file->getCTime());
 
          echo "<p>Erased file: " . $dir . '/' . $file->getFilename() . " ({$date})</p>";
 
        } 
 
      }
 
    }
 
  }
 

	
 
  function checkAction() {
 
    $result = '';
 
    if(isset($_GET['rehash'])) {
 
      // Run the rehash
 
      $result = 'Rehash Complete'; 
 

	
 
      $crawl_schools = NULL;
 
      if (isset($_REQUEST['rehash_school']))
 
	$crawl_schools = array($_REQUEST['rehash_school']);
 

	
 
      if (school_cache_recreate($crawl_schools))
 
	$result = 'Rehash Failed';
 
      else
 
	$result = 'Rehash Successful';
 
      if ($crawl_schools !== NULL)
 
	$result .= ': ' . implode(', ', $crawl_schools);
 
    }
 
    else if(isset($_GET['purgetodate'])) {
 
      // Purge saved schedule cache up to date
 
      emptySavedDir($_GET['purgetodate']);
 
      $result = 'Purged all saved schedules up to ' . $_GET['purgetodate'];
 
    }
 
    else if(isset($_GET['purge'])) {
 
      // Purge the saved schedule cache
 
      emptySavedDir();
 
      $result = 'Purge Complete';
 
    }
 
    return $result;
 
  }
 

	
 
  function getLastRehash(){
 
    $stats = stat("cache/schools");
 
    if(!$stats){
 
      return false;
 
    }
 
    return date("F j, Y, g:i a", $stats[9]);
 
  }
 

	
 
  function getSchools() {
 
    if(!stat("cache/schools")){
 
      return false;
 
    }
 
    $schoolsArr = unserialize(file_get_contents("cache/schools"));
 
    return $schoolsArr;
 
  }
 

	
 
  function schoolsDropList(){
 
    $schools = getSchools();
 
    echo '<select>';
 
    foreach($schools['list'] as $school){
 
      if(!$school['name'] != "Generic College") {
 
        echo '<option ';
 
        if(!$school['crawled']) {
 
          echo 'class="bold" ';
 
        }
 
        echo 'value="' . $school['name'] . '">';
 
        echo $school['name'];   
 
        echo "</option>";
 
  function schoolsDropList()
 
  {
 
    $school_ids = school_list();
 
    echo '<select name="rehash_school">';
 
    foreach($school_ids as $school_id)
 
      {
 
	$school = school_load($school_id);
 
	echo '  <option value="' . $school_id . '">' . $school['name'] . '</option>' . "\n";
 
      }
 
    }
 
    echo "</select>";
 
  }
 

	
 
  function getNumSaved(){
 
    return file_get_contents("saved_schedules/lastid");
 
  }
 

	
 
?>
 

	
 

	
 
<?php /* Check if authorized */ 
 

	
 
@@ -149,25 +148,32 @@
 
<p>You are currently running version <?php echo SP_PACKAGE_VERSION; ?>. The latest available release is VERSION.</p>
 

	
 
<h3>Rehash</h3>
 
<?php $lastRehash = getLastRehash();
 
      if($lastRehash) {
 
        echo "<p>Last rehash ocurred on $lastRehash.</p>";
 
      }
 
      else {
 
        echo "<p>This installation has not been rehashed. Please <a href=\"admin.php?rehash\">rehash now</a> to download school scheduling metadata.</p>";
 
      }
 
?>
 
<ul>
 
  <li><a href="admin.php?rehash">Rehash All Institutions</a></li>
 
  <li><form action="admin.php">Rehash schedules for <?php schoolsDropList(); ?> <input type="submit" value="Go &raquo;" /> </form></li>
 
  <li>
 
    <a href="admin.php?rehash">Rehash All Institutions</a>
 
  </li>
 
  <li>
 
    <form action="admin.php">Rehash schedules for <?php schoolsDropList(); ?>
 
      <input type="hidden" name="rehash" value="1" />
 
      <input type="submit" value="Go &raquo;" />
 
    </form>
 
  </li>
 
</ul>
 

	
 
<h3>Purge</h3>
 
<p>The cache currently contains <?php echo getNumSaved(); ?> schedules.</p>
 
<p>The saved schedule fs-db currently contains <?php echo getNumSaved(); ?> schedules.</p>
 
<ul>
 
  <li><a href="admin.php?purge">Purge Entire Cache</a></li>
 
  <li><form action="admin.php">Purge cache up to <input type="text" name="purgetodate" size="8" id="datepicker"/> <input type="submit" value="Go &raquo;" /></form></li>
 
</ul>
 

	
 
<?php
 
$adminpage->foot();
admin/rehash.php
Show inline comments
 
@@ -35,102 +35,63 @@ require_once($inc_base . 'admin.inc');
 
return main($argc, $argv);
 

	
 
function main($argc, $argv)
 
{
 
  $n = test();
 
  if ($n)
 
    {
 
      fprintf(STDERR, "%d tests failed; exiting\n",
 
	      $n);
 
      return 1;
 
    }
 

	
 
  $crawl = TRUE;
 
  $crawl_semester_year = '2011';
 
  $crawl_semester_season = Semester::SEASON_SPRING;
 

	
 
  $opts = getopt('hV:', array('no-crawl', 'crawl-only:', 'help', 'verbosity:'));
 

	
 
  if (isset($opts['help']) || isset($opts['h']))
 
    {
 
      usage($argv[0]);
 
      return 0;
 
    }
 

	
 
  $crawl = TRUE;
 
  if (isset($opts['no-crawl']))
 
    $crawl = FALSE;
 
  if (isset($opts['crawl-only']))
 
    $crawl_only = split(',', $opts['crawl-only']);
 

	
 
  $verbosity = 1;
 
  if (isset($opts['verbosity']))
 
    $verbosity = (int)$opts['verbosity'];
 
  if (isset($opts['V']))
 
    $verbosity = (int)$opts['V'];
 
  if ($verbosity < 0 || $verbosity > 10)
 
    {
 
      fprintf(STDERR, "error: Invalid verbosity level: %d\n", $verbosity);
 
      fprintf(STDERR, "\n");
 
      usage();
 
      return 1;
 
    }
 

	
 
  $school_id_list = school_list();
 
  if (!$school_id_list)
 
    {
 
      fprintf(STDERR, "error: Unable to load schools.\n");
 
      return 1;
 
    }
 

	
 
  $schools = array();
 
  $old_school_cache = _school_cache_load();
 
  foreach ($school_id_list as $school_id)
 
  if ($crawl)
 
    {
 
      $school = school_load($school_id, TRUE);
 
      if (!$school)
 
      $ret = school_cache_recreate($crawl_only);
 
      if ($ret)
 
	{
 
	  fprintf(STDERR, "Error loading school with school_id=%s\n",
 
		  $school_id);
 
	  fprintf(STDERR, "error: Unable to successfully crawl schools.\n");
 
	  return 1;
 
	}
 

	
 
      if ($crawl
 
	  && (!isset($crawl_only) || in_array($school['id'], $crawl_only)))
 
	{
 
	  school_crawl($school, $crawl_semester_year, $crawl_semester_season, $verbosity);
 
	}
 
      else
 
	{
 
	  /*
 
	   * try to allow incremental crawling by not wiping out old
 
	   * data and preserving the cached $school['crawled'].
 
	   */
 
	  if ($old_school_cache && isset($old_school_cache['list'][$school['id']]))
 
	    {
 
	      $old_school = $old_school_cache['list'][$school['id']];
 
	      $school['crawled'] = FALSE;
 
	      if (isset($old_school['crawled']))
 
		$school['crawled'] = $old_school['crawled'];
 
	      if ($school['crawled'])
 
		$school['crawled_notreally'] = TRUE;
 
	    }
 
	  fprintf(STDERR, "Crawling successful.\n");
 
	}
 

	
 
      $schools[] = $school;
 
    }
 

	
 
  if (school_cache($schools))
 
    {
 
      fprintf(STDERR, "Error writing out school cache\n");
 
      return 1;
 
    }
 

	
 
  return 0;
 
}
 

	
 

	
 
/**
 
 * \brief
 
 *   Display CLI interface usage.
 
 */
 
function usage($progname)
 
{
inc/admin.inc
Show inline comments
 
@@ -9,27 +9,30 @@
 
 * the Free Software Foundation, either version 3 of the License, or
 
 * (at your option) any later version.
 
 *
 
 * slate_permutate is distributed in the hope that it will be useful,
 
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
 * GNU Affero General Public License for more details.
 
 *
 
 * You should have received a copy of the GNU Affero General Public License
 
 * along with slate_permutate.  If not, see <http://www.gnu.org/licenses/>.
 
 */
 

	
 
$incdir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'inc' . DIRECTORY_SEPARATOR;
 
$inc_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'inc' . DIRECTORY_SEPARATOR;
 

	
 
require_once($incdir . 'schedule_store.inc');
 
require_once($inc_dir . 'schedule_store.inc');
 
require_once($inc_dir . 'class.semester.inc');
 
require_once($inc_dir . 'school.inc');
 
require_once($inc_dir . 'school.crawl.inc');
 

	
 
/**
 
 * \file
 
 *   Functions which are only needed when recreating the cache.
 
 */
 

	
 
/**
 
 * \brief
 
 *   Returns the list of available school IDs or NULL on error.
 
 */
 
function school_list()
 
{
 
@@ -230,24 +233,101 @@ function school_crawl(&$school, $semeste
 
      fprintf(STDERR, "Crawling %s failed: %s() returned nonzero\n",
 
	      $school['id'], $school_crawl_func);
 
      fwrite(STDERR, "\n");
 
      return;
 
    }
 
  $school['crawled'] = TRUE;
 
  $school['crawled_semester'] = $semester;
 

	
 
  if ($verbosity > 0)
 
    fwrite(STDERR, "\n");
 
}
 

	
 

	
 
/**
 
 * \brief
 
 *   Recreate/update the school section autocomplete cache.
 
 *
 
 * \param $crawl_only
 
 *   If non-NULL, an array of school_ids to limit the cache recreation
 
 *   to. Useful for when developing a certain school's crawling
 
 *   function.
 
 */
 
function school_cache_recreate($crawl_only = NULL)
 
{
 
  $crawl_semester_year = '2011';
 
  $crawl_semester_season = Semester::SEASON_SPRING;
 

	
 
  $school_id_list = school_list();
 
  if (!$school_id_list)
 
    {
 
      fprintf(STDERR, "error: Unable to load schools.\n");
 
      return 1;
 
    }
 

	
 
  if ($crawl_only !== NULL)
 
    foreach ($crawl_only as $crawl_only_school_id)
 
      if (!in_array($crawl_only_school_id, $school_id_list))
 
	{
 
	  fprintf(STDERR, "error: Invalid school_id specified for crawling: %s",
 
		  $crawl_only_school_id);
 
	  return 1;
 
	}
 

	
 
  $schools = array();
 
  $old_school_cache = _school_cache_load();
 
  foreach ($school_id_list as $school_id)
 
    {
 
      $school = school_load($school_id, TRUE);
 
      if (!$school)
 
	{
 
	  fprintf(STDERR, "Error loading school with school_id=%s\n",
 
		  $school_id);
 
	  return 1;
 
	}
 

	
 
      if ($crawl_only === NULL || in_array($school['id'], $crawl_only))
 
	{
 
	  school_crawl($school, $crawl_semester_year, $crawl_semester_season, $verbosity);
 
	}
 
      else
 
	{
 
	  /*
 
	   * try to allow incremental crawling by not wiping out old
 
	   * data and preserving the cached $school['crawled'].
 
	   */
 
	  if ($old_school_cache && isset($old_school_cache['list'][$school['id']]))
 
	    {
 
	      $old_school = $old_school_cache['list'][$school['id']];
 
	      $school['crawled'] = FALSE;
 
	      if (isset($old_school['crawled']))
 
		$school['crawled'] = $old_school['crawled'];
 
	      if ($school['crawled'])
 
		$school['crawled_notreally'] = TRUE;
 
	    }
 
	}
 

	
 
      $schools[] = $school;
 
    }
 

	
 
  if (school_cache($schools))
 
    {
 
      fprintf(STDERR, "Error writing out school cache\n");
 
      return 1;
 
    }
 

	
 
  return 0;
 
}
 

	
 

	
 
/**
 
 * \brief
 
 *   A small testsuite to help developers.
 
 *
 
 * \return
 
 *   Number of failures.
 
 */
 
function test()
 
{
 
  $ideal = array('department' => 'CS',
 
		 'course' => '262',
 
		 'section' => 'A');
0 comments (0 inline, 0 general)