function getTagWithLabel($label)
{
    assert(labelExists($label));
    global $database;
    $sql = $database->prepare('SELECT tag FROM tags WHERE label = :label AND active = "TRUE"');
    $sql->bindParam(':label', $label);
    if ($sql->execute()) {
        return $sql->fetchColumn();
    }
    return "ZZZZ";
}
function parseReferences($string)
{
    // look for \ref before MathJax can and see if they point to existing tags
    $references = array();
    preg_match_all('/\\\\ref{[\\w-]*}/', $string, $references);
    foreach ($references[0] as $reference) {
        // get the label or tag we're referring to, nothing more
        $target = substr($reference, 5, -1);
        // we're referring to a tag
        if (isValidTag($target)) {
            // regardless of whether the tag exists we insert the link, the user is responsible for meaningful content
            $string = str_replace($reference, '[`' . $target . '`](' . href('tag/' . $target) . ')', $string);
        } else {
            // might it be that he is referring to a "local" label, i.e. in the same chapter as the tag?
            // TODO: Is it worth it to do this?
            if (!labelExists($target)) {
                $tag = getTag(strtoupper($_GET['tag']));
                // let's try it with the current chapter in front of the label
                $target = $tag["file"] . '-' . $target;
            }
            // the label (potentially modified) exists in the database (and it is active), so the user is probably referring to it
            // if he declared a \label{} in his string with this particular label value he's out of luck
            if (labelExists($target)) {
                $tag = getTagWithLabel($target);
                $string = str_replace($reference, '[`' . $tag . '`](' . href('tag/' . $tag) . ')', $string);
            }
        }
    }
    return $string;
}