Changeset - 1c87613b625c
[Not reviewed]
default
0 2 0
Nathan Brink (binki) - 14 years ago 2012-02-27 10:15:18
ohnobinki@ohnopublishing.net
Add caching support for section autocomplete requests.
2 files changed with 29 insertions and 2 deletions:
0 comments (0 inline, 0 general)
auto.php
Show inline comments
 
@@ -14,49 +14,68 @@
 
 * 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/>.
 
 */
 

	
 
/**
 
 * \file
 
 *   This file's purpose is to autocomplete class names for supporting
 
 *   the autocomplete JS based off of crawling schools' registration
 
 *   websites. This shall only perform the autocompletion of class
 
 *   names.
 
 *
 
 *   Since we output JSON, no special Page classes and stuff
 
 *   :-p. Except we still call the Page class's session_start()
 
 *   function because we apparently need sessions.... oh yeah, for
 
 *   school profile supports ;-).
 
 */
 

	
 
require_once('inc/school.inc');
 
require_once('inc/class.page.php');
 
require_once('inc/class.course.inc');
 

	
 
page::session_start();
 
/*
 
 * Set the Expires and Cache-Control headers -- only if we're getting
 
 * a request which does not rely on the contents of $_SESSION.
 
 */
 
if (!empty($_GET['school']) && !empty($_GET['semester']))
 
  {
 
    header('Expires: ' . gmdate(DATE_RFC1123, time() + 600));
 
    header('Cache-Control: max-age=600, public');
 
    $cache_limiter = 'public';
 
  }
 
else
 
  {
 
    /*
 
     * Tell the caches that the user's cookies affect the cacheability
 
     * since the user did not specify the current semester/school.
 
     */
 
    header('Vary: Cookie');
 
    $cache_limiter = 'private';
 
  }
 
page::session_start($cache_limiter);
 

	
 
if (isset($_REQUEST['txt'])) {
 
  header('Content-Type: text/plain; encoding=utf-8');
 
}
 
else {
 
  header('Content-Type: application/json; encoding=utf-8');
 
}
 

	
 
if (!isset($_REQUEST['term'])) {
 
  clean_empty_exit();
 
}
 

	
 
$getsections = FALSE;
 
if (isset($_REQUEST['getsections'])) {
 
  $getsections = TRUE;
 
}
 

	
 
$term = $_REQUEST['term'];
 
$term_parts = Course::parse($term);
 
if (!count($term_parts)) {
 
  clean_empty_exit();
 
}
 

	
 
/*
 
@@ -67,48 +86,51 @@ if (!count($term_parts)) {
 
$school = school_load_guess(FALSE);
 
if (!$school['crawled']) {
 
  clean_empty_exit();
 
}
 
$semester = school_semester_guess($school, FALSE);
 

	
 
$cache_dir = dirname(__FILE__) . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR . 'auto'
 
  . DIRECTORY_SEPARATOR . $school['id'] . DIRECTORY_SEPARATOR . $semester['id'] . DIRECTORY_SEPARATOR;
 

	
 
/*
 
 * autocomplete the list of departments. If the user has already
 
 * entered a valid department name _and_ delimitted it, however, go on
 
 * to the next autocompletion step.
 
 */
 
$term_strlen = strlen($term);
 
$dept_strlen = strlen($term_parts['department']);
 
$dept = $term_parts['department'];
 
if (!$getsections && count($term_parts) == 1 && $term_strlen == strlen($dept))
 
  {
 
    $dept_file = $cache_dir . '-depts';
 
    if (!file_exists($dept_file)) {
 
      clean_empty_exit();
 
    }
 
    $departments = unserialize(file_get_contents($dept_file));
 
    $departments_mtime = filemtime($dept_file);
 
    if ($departments_mtime)
 
      header('Last-Modified: ' . gmdate(DATE_RFC1123, $departments_mtime));
 
    $json_depts = array();
 
    if (!empty($departments) && is_array($departments[0]))
 
      {
 
	/* New format with department names/labels */
 
	foreach ($departments as $department)
 
	  if (!strncmp($department['value'], $dept, $dept_strlen))
 
	    $json_depts[] = $department;
 
      }
 
    else
 
      {
 
	/* Old format with just department id. */
 
	foreach ($departments as $department)
 
	  if (!strncmp($department, $dept, $dept_strlen))
 
	    $json_depts[] = $department;
 
      }
 

	
 
    /*
 
     * If what the user has entered already can only be completed to a
 
     * particular department, start displaying courses from that
 
     * department instead of just returning that one department.
 
     */
 
    if (count($json_depts) == 1)
 
      {
 
	$dept = $json_depts[0];
inc/class.page.php
Show inline comments
 
@@ -624,55 +624,60 @@ class page
 
   *   the 404 page.
 
   */
 
  public static function show_404($message = 'The page you were looking for cannot be found!.')
 
  {
 
    $page_404 = page::page_create('404: Content Not Found');
 
    $page_404->head();
 

	
 
    echo "<h2>404: Content Not Found</h2>\n"
 
      . "<p>\n"
 
      . '  ' . $message . "\n"
 
      . "</p>\n";
 

	
 
    $page_404->foot();
 

	
 
    exit();
 
  }
 

	
 
  /**
 
   * \brief
 
   *   Start the PHP session by calling session_start().
 
   *
 
   * Used to make sure that different areas of our code don't call
 
   * session_start() multiple times and to make it easier to ensure
 
   * that session_start() is called at least before it's needed.
 
   *
 
   * \param $cache_limiter
 
   *   Specify the sort of session-related cache limitation is used,
 
   *   see session_cache_limiter().
 
   */
 
  public static function session_start()
 
  public static function session_start($cache_limiter = 'nocache')
 
  {
 
    static $session_started = FALSE;
 

	
 
    if (!$session_started)
 
      {
 
	session_cache_limiter($cache_limiter);
 
	session_name('slate_permutate');
 
	session_start();
 
	$session_started = TRUE;
 
      }
 
  }
 

	
 
  /**
 
   * \brief
 
   *   Perform a redirect.
 
   *
 
   * By consolidating all redirects here, we're hopefully able to do
 
   * it in a somewhat compliant and portablish way ;-).
 
   *
 
   * This function does not return. It calls exit().
 
   *
 
   * \param $dest
 
   *   A URL relative to the slate_permutate root. For example,
 
   *   'input.php' or '44' (for clean urls, for example).
 
   * \param $http_code
 
   *   The redirection code to use, if any. For example, this can be
 
   *   used to implement ``permanent'' redirects if necessary.
 
   */
 
  public static function redirect($dest, $http_code = NULL)
 
  {
0 comments (0 inline, 0 general)