/** * Insert the highilight in the database. */ function deleteHighlight($uid, $declarationId, $startWord, $endWord, $content) { // Find the declaration link. $declarationLink = getDeclarationLink($declarationId); if (!$declarationLink) { die("Wrong declaration id"); } $sql = "\n DELETE FROM people_declarations_highlights\n WHERE\n source = '{$declarationLink}' AND\n user_id = {$uid} AND\n start_word = {$startWord} AND\n end_word = {$endWord} AND\n user_id = {$uid}"; mysql_query($sql); }
/** * Returns the array of highlights for this declaration. The separate ranges * will all be merged together and only continuous disjoint ranges will be * returned. So if the real highlights are like this: * +-------+ +---------+ +-----+ * +----------+ * What will be returned will look like this: * +---------------------+ +-----+ * * @param {Number} $declarationId * @param {Number} $uid * @return {Array} */ function getHighlightsForDeclaration($declarationId, $include = 0, $exclude = 0) { global $highlightsPerDeclaration; $declarationLink = getDeclarationLink($declarationId); if ($include != 0) { $sql = "\n SELECT *\n FROM people_declarations_highlights\n WHERE source='{$declarationLink}' AND user_id = {$include}"; } else { if ($exclude != 0) { $sql = "\n SELECT *\n FROM people_declarations_highlights\n WHERE source='{$declarationLink}' AND user_id != {$exclude}"; } else { $sql = "\n SELECT *\n FROM people_declarations_highlights\n WHERE source='{$declarationLink}'"; } } //echo $sql; // Keep it simple and inefficient, that's okay. $map = array(); $maxIndex = 0; $s = mysql_query($sql); while ($r = mysql_fetch_array($s)) { for ($i = $r['start_word']; $i <= $r['end_word']; $i++) { $map[$i] = 1; $maxIndex = max($maxIndex, $i); } } $map[$maxIndex + 1] = 0; $state = 0; $start = 0; $ranges = array(); for ($i = 0; $i < sizeof($map); $i++) { $map[$i] = $map[$i] ? $map[$i] : 0; if ($map[$i] != $state) { if ($state != 0) { $newRange = array('start' => $start, 'end' => $i - 1, 'declarationId' => $declarationId); $ranges[] = $newRange; $highlightsPerDeclaration['declaration-' . $declarationId] = true; } $start = $i; $state = $map[$i]; } } return $ranges; }