Skip to content

Commit

Permalink
Massage the index before shipping it. This safes us almost 3mb
Browse files Browse the repository at this point in the history
  • Loading branch information
bjori committed Dec 27, 2013
1 parent 7d16193 commit ff9eabe
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 62 deletions.
72 changes: 72 additions & 0 deletions js/search-index.php
@@ -0,0 +1,72 @@
<?php
$_GET["lang"] = "en";
if (!isset($_GET["lang"])) {
header("Location: http://php.net");
exit;
}
if (empty($_SERVER["DOCUMENT_ROOT"])) {
$_SERVER["DOCUMENT_ROOT"] = __DIR__ . "/../";
}
include $_SERVER['DOCUMENT_ROOT'] . '/include/prepend.inc';
if (!isset($ACTIVE_ONLINE_LANGUAGES[$_GET["lang"]])) {
header("Location: http://php.net");
}
$lang = $_GET["lang"];

/*
$types = array(
"phpdoc:varentry",
"refentry",
"phpdoc:exceptionref",
"phpdoc:classref",
"section",
"chapter",
"book",
"reference",
"set",
"appendix",
"article",
);
*/

$indexfile = $_SERVER["DOCUMENT_ROOT"] . "/manual/$lang/search-index.json";
$descfile = $_SERVER["DOCUMENT_ROOT"] . "/manual/$lang/search-description.json";

/* {{{ Cache this */
$time = max(filemtime($indexfile), filemtime($descfile));
$tsstring = gmdate("D, d M Y H:i:s ", $time) . "GMT";
if (isset($_SERVER["HTTP_IF_MODIFIED_SINCE"]) &&
($_SERVER["HTTP_IF_MODIFIED_SINCE"] == $tsstring)) {
header("HTTP/1.1 304 Not Modified");
exit;
}

header("Last-Modified: " . $tsstring);
header("Content-Type: application/javascript");
/* }}} */



$s = file_get_contents($indexfile);
$js = json_decode($s, true);

$index = array();
foreach($js as $item) {
if ($item[0]) {
/* key: ID/filename, 0=>*/
$index[$item[1]] = array($item[0], "", $item[2]);
}
}

$s = file_get_contents($descfile);
$js = json_decode($s, true);

foreach($js as $k => $item) {
if ($item && isset($index[$k])) {
$index[$k][1] = $item;
}
}


echo json_encode($index);

80 changes: 18 additions & 62 deletions js/search.js
Expand Up @@ -16,18 +16,6 @@
this.elements = {};
};

/**
* Adds a description to the given item. If the ID doesn't exist, this
* method will do nothing, successfully.
*
* @param {String} id The ID to match.
* @param {String} description The description to add.
*/
Backend.prototype.addDescription = function (id, description) {
if (id in this.elements) {
this.elements[id].description = description;
}
};

/**
* Adds an item to the backend.
Expand All @@ -36,12 +24,12 @@
* @param {String} name The item name to use as a label.
* @param {Array} tokens An array of tokens that should match this item.
*/
Backend.prototype.addItem = function (id, name, tokens) {
Backend.prototype.addItem = function (id, name, description, tokens) {
this.elements[id] = {
tokens: tokens,
id: id,
name: name,
description: null
description: description
};
};

Expand Down Expand Up @@ -134,26 +122,7 @@
};

/**
* Given a data structure in the format of our search-description.json
* files, augments the given backends with descriptions.
*
* @param {Object} backends An object or array containing one or more
* Backend objects.
* @param {Object} desc A description array, keyed by page ID.
* @return {Object} The updated backends.
*/
var processDescription = function (backends, desc) {
$.each(desc, function (id, description) {
$.each(backends, function (_, backend) {
backend.addDescription(id, description);
});
});

return backends;
};

/**
* Processes a data structure in the format of our search-index.json
* Processes a data structure in the format of our search-index.php
* files and returns an object containing multiple Backend objects.
*
* @param {Object} index
Expand All @@ -167,7 +136,7 @@
"general": new Backend("Other Matches")
};

$.each(index, function (_, item) {
$.each(index, function (id, item) {
/* If the item has a name, then we should figure out what type
* of data this is, and hence which backend this should go
* into. */
Expand All @@ -179,7 +148,7 @@
tokens.push(item[0].replace("_", ""));
}

if (item[1].match(/^function\./)) {
if (id.match(/^function\./)) {
type = "function";
} else if (item[0].indexOf("::") != -1) {
type = "function";
Expand All @@ -198,9 +167,10 @@
* other matches category. */
type = "general";
}
/* item[2] contains the XML element name.. may be better? */

if (type) {
backends[type].addItem(item[1], item[0], tokens);
backends[type].addItem(id, item[0], item[1], tokens);
}
}
});
Expand Down Expand Up @@ -252,32 +222,18 @@
success: function (data) {
// Transform the data into something useful.
var backends = processIndex(data);

// See if we can augment this with description data.
$.ajax({
dataType: "json",
error: function () {
// Return the data without descriptions but don't
// cache it.
success(backends);
},
success: function (data) {
backends = processDescription(backends, data);

// Cache the data if we can.
if (canCache()) {
window.localStorage.setItem(key, JSON.stringify({
data: backends,
time: new Date().getTime()
}));
}

success(backends);
},
url: "/cached.php?f=/manual/" + language + "/search-description.json"
});
// Cache the data if we can.
if (canCache()) {
window.localStorage.setItem(key,
JSON.stringify({
data: backends,
time: new Date().getTime()
})
);
}
success(backends);
},
url: "/cached.php?f=/manual/" + language + "/search-index.json"
url: "/js/search-index.php?lang=" + language
});
};

Expand Down

0 comments on commit ff9eabe

Please sign in to comment.