/**
 * Divides a wiki page content in 3 parts to allow a proper section
 * viewing/editing. We need the name of the section and the number
 * to allow sections with same name.
 *
 * The parts are:
 * 1. prev_part:    the text previous to the section
 * 2. current_part: the text of the section
 * 3. next_part:    the text after the section
 *
 * @param  String $text       Wiki page text
 * @param  String $section    Name of the section to cut
 * @param  String $sectionnum Number of the section to cut
 * @param  String $check      'nocheck' implies a weaker section validation
 * @return Array  $res        Array composed of 3 parts
 */
function wiki_split_sections($text, $section, $sectionnum)
{
    global $WS;
    $res->prev_part = '';
    $res->current_part = $text;
    $res->next_part = '';
    $res->error = '';
    // param checking
    if ($text == '') {
        return $res;
    }
    $res->current_part = '';
    $prev_part = '';
    $next_part = '';
    $current_depth = -1;
    $matched_section = false;
    $num_sections = 0;
    // analyze the text to divide the section in the 3 parts
    $lines = explode("\n", $text);
    $numlines = 0;
    foreach ($lines as $current_line) {
        $numlines++;
        if (!isset($current_part)) {
            // we haven't found the section yet
            $current_depth = wiki_get_section_depth($current_line, $section);
            if (wiki_get_section_depth($current_line, '(.*)+') > 0) {
                $num_sections++;
            }
            if ($current_depth > 0) {
                // probably section found
                if ($num_sections == $sectionnum) {
                    // if it's really the one we want
                    $current_part = $current_line . "\n";
                    $matched_depth = $current_depth;
                    $matched_section = true;
                } else {
                    // it's a section with the same name but not the one we want
                    $prev_part .= $current_line . "\n";
                }
            } else {
                // section not found, so it's text previous to it
                $prev_part .= $current_line . "\n";
            }
        } else {
            $new_depth = wiki_get_section_depth($current_line, '(.*)+');
            if ($new_depth > 0) {
                $num_sections++;
                $current_depth = $new_depth;
                if ($current_depth <= $matched_depth) {
                    $matched_section = false;
                }
            }
            if ($matched_section) {
                // belongs to the section
                $current_part .= $current_line . "\n";
            } else {
                // doesn't belong to the section, so it's after
                $next_part .= $current_line . "\n";
            }
        }
    }
    // could find the section content ?
    if (!isset($current_part)) {
        $urls = wiki_view_page_url($WS->page, $section, 0, '');
        $res->error = get_string('sectionerror', 'wiki', $section) . '. ' . get_string('sectionchanged', 'wiki') . ': ' . '<a href="' . $urls[0] . '">' . $WS->page . '</a>';
        return $res;
    }
    // construct and return the result
    $res->prev_part = $prev_part;
    $res->current_part = $current_part . "\n";
    $res->next_part = $next_part;
    return $res;
}
function parse_internal_link($matches)
{
    global $WS, $CFG, $COURSE;
    // allow spaces before and after the internal link name
    $matches[1] = trim($matches[1]);
    $matches[1] = wiki_clean_name($matches[1]);
    $parts = explode('|', $matches[1]);
    $target = $parts[0];
    $label = $target;
    if (count($parts) > 1) {
        $label = $parts[1];
    }
    // internal links to sections
    $page = $target;
    $anchor = '';
    $anchortype = 0;
    $pos = strpos($target, '#');
    if ($pos > 0) {
        $page = substr($target, 0, $pos);
        if (substr($target, $pos + 1, 1) == "#") {
            // [[page##section]]: partial view of the section
            $anchortype = 2;
            $anchor = substr($target, $pos + 2, strlen($target) - $pos);
        } else {
            // [[page#section]]: page view plus scroll to section
            $anchortype = 1;
            $anchor = substr($target, $pos + 1, strlen($target) - $pos);
        }
    }
    // wikibook
    $wikibookname = '';
    if (count($parts) > 2 && preg_match("/^wikibook:(.*)/", $parts[2], $match)) {
        $wikibookname = $match[1];
    }
    $wikibookparam = '';
    if ($wikibookname) {
        $wikibookparam = '&amp;wikibook=' . urlencode($wikibookname);
    }
    // we can have more than one section with the same name, so we link
    // to all of them in a list after the link.
    $res = '';
    $urls = wiki_view_page_url($page, $anchor, $anchortype, $wikibookname);
    $num_urls = count($urls);
    for ($i = 0; $i < $num_urls; $i++) {
        if ($i == 0) {
            // we render the first matched section as a normal link
            $res = '<a href="' . $urls[$i] . '">' . $label . '</a>';
        } elseif ($i == 1) {
            $res .= ' [<a href="' . $urls[$i] . '">' . ($i + 1) . '</a>';
        } else {
            $res .= ', <a href="' . $urls[$i] . '">' . ($i + 1) . '</a>';
        }
        if ($i == $num_urls - 1 && $i > 0) {
            $res .= ']';
        }
    }
    if (!wiki_page_exists($WS, $page, true)) {
        $res = '<span class="nwikiwanted">' . $res . '</span>';
    } else {
        if ($anchor != '') {
            if (!wiki_section_exists($page, $anchor)) {
                $res = '<span class="nwikiwanted">' . $res . '</span>';
            }
        }
    }
    return $res;
}