/**
 * Returns an array with the URLs of a page (plus optinal section if it's a
 * a partial editing) to view.
 * @param  String $pagename
 * @param  String $anchor Section name
 * @param  String $anchortype (0, 1, 2)<br>
 *                   0: no anchor<br>
 *                   1: [[page#section]]<br>
 *                   2: [[page##section]]
 * @return Array  URLs Contains a list (if there are more than one section
 *                     with the same name in the same page) of URLs that
 *                     match the page and section
 */
function wiki_view_page_url($pagename, $anchor = '', $anchortype = 0, $wikibook = '')
{
    global $CFG, $COURSE, $WS;
    if ($wikibook) {
        $wikibook = '&amp;wikibook=' . urlencode($wikibook);
    }
    // support page synonyms
    $pagename = wiki_get_real_pagename($pagename);
    $urls = array();
    $page = wiki_page_last_version($pagename);
    if ($page) {
        $sectionnums = wiki_get_section_positions($page->content, $anchor);
    }
    if ($page && $anchortype > 0 && count($sectionnums) > 0) {
        if ($anchortype == 1) {
            // [[page#section]]
            foreach ($sectionnums as $sectionnum) {
                $newurl = 'view.php?id=$COURSE->id&amp;page=view/' . urlencode($pagename) . '&amp;gid=$gid&amp;uid=$uid' . $wikibook . '#' . $sectionnum;
                $newurl = wiki_format_url($newurl);
                $urls[] = $newurl;
            }
        } else {
            if ($anchortype == 2) {
                // [[page##section]]
                foreach ($sectionnums as $sectionnum) {
                    $newurl = 'view.php?id=$COURSE->id&amp;page=view/' . urlencode($pagename) . '&amp;gid=$gid&amp;uid=$uid' . $wikibook . '&amp;section=' . urlencode($anchor) . '&amp;sectionnum=' . $sectionnum;
                    $newurl = wiki_format_url($newurl);
                    $urls[] = $newurl;
                }
            }
        }
    } else {
        // no anchor
        $newurl = 'view.php?id=' . $COURSE->id . '&amp;page=' . urlencode($pagename) . '&amp;gid=$gid&amp;uid=$uid' . $wikibook;
        $newurl = wiki_format_url($newurl);
        $urls[] = $newurl;
    }
    return $urls;
}
/**
 * Returns true if the section exist in the page
 *
 * @param $pagename Page name
 * @param $section  Section name
 */
function wiki_section_exists($pagename, $section)
{
    $pagename = wiki_get_real_pagename($pagename);
    $page = wiki_page_last_version($pagename);
    $sectionnums = wiki_get_section_positions($page->content, $section);
    if (count($sectionnums) < 1) {
        return false;
    }
    return true;
}
function parse_nwiki_text($text)
{
    global $regex, $nowikitext, $USER, $tocheaders, $WS;
    // Security Patch !!!
    // $find will contain the regex to find
    // $find   = array_keys($regex['than']);
    // $replace will contain the replacements of items in $find
    // $replace = array_values($regex['than']);
    // $text    = preg_replace($find, $replace, $text);
    $section = optional_param('section', '', PARAM_TEXT);
    $sectionnum = optional_param('sectionnum', 0, PARAM_INTEGER);
    $sectionhash = optional_param('sectionhash', '', PARAM_TEXT);
    $preview = optional_param('dfformpreview', NULL, PARAM_TEXT);
    $action = optional_param('dfformaction', NULL, PARAM_TEXT);
    if (!($section == '' && $sectionnum == 0) && $preview == '' && $action == '') {
        // check if the section exists or it has been deleted
        $section = stripslashes($section);
        $section_positions = wiki_get_section_positions($text, $section);
        if (!in_array($sectionnum, $section_positions)) {
            $num_positions = count($section_positions);
            if ($num_positions == 0) {
                // section has been deleted
                $error_text = get_string('sectionerror', 'wiki', $section) . ': ' . strtolower(get_string('sectiondeleted', 'wiki'));
                error($error_text);
            } elseif ($num_positions == 1) {
                // section position has changed
                $sectionnum = $section_positions[0];
            } else {
                // there are more than one section with this name
                $error_text = get_string('sectionerror', 'wiki', $section) . ': ' . strtolower(get_string('sectionmultiple', 'wiki')) . ': ';
                if (isset($WS->dfcourse)) {
                    $type = 'course';
                } else {
                    $type = 'mod';
                }
                $urls = wiki_view_page_url($WS->page, $section, 2, '', $type);
                $num_urls = count($urls);
                for ($i = 0; $i < $num_urls; $i++) {
                    if ($i == 0) {
                        $error_text .= '<a href="' . $urls[$i] . '">[[' . $WS->page . '##' . $section . '</a>';
                    } elseif ($i == $num_urls - 1) {
                        $error_text .= ', <a href="' . $urls[$i] . '">' . ($i + 1) . ']]</a>';
                    } else {
                        $error_text .= ', <a href="' . $urls[$i] . '">' . ($i + 1) . '</a>';
                    }
                }
                error($error_text);
            }
        }
        $res = wiki_split_sections($text, $section, $sectionnum);
        if ($res->error != '') {
            $text = $res->error;
        } else {
            $text = $res->current_part;
        }
    }
    // Remove carriage returns
    $text = str_replace("\r", "", $text);
    foreach ($regex['nowiki'] as $match => $func) {
        $text = preg_replace_callback($match, $func, $text);
    }
    foreach ($regex['variable'] as $match => $func) {
        $text = preg_replace_callback($match, $func, $text);
    }
    // $find will contain the regex to find
    $find = array_keys($regex['replace']);
    // $replace will contain the replacements of items in $find
    $replace = array_values($regex['replace']);
    $text2 = preg_replace($find, $replace, $text);
    foreach ($nowikitext as $index => $singlenowikitext) {
        $text2 = preg_replace("/<<ThisIsThePlaceOfTheNoWikiText-{$index}>>/", $singlenowikitext, $text2);
    }
    // here comes the TOC, search for --TOC-- and include the real TOC
    $toc = parse_toc($tocheaders);
    $text2 = str_replace("NWikiTableOfContents", $toc, $text2);
    $text2 = preg_replace('/^\\n/m', '</p><p>', $text2);
    // remove empty paragraphs
    $text2 = preg_replace('/<p>\\s*<\\/p>/s', '', "<p>{$text2}</p>");
    // join consecutive pre blocks
    $text2 = preg_replace('/<\\/pre>\\s*<pre class="quote">/s', '<br />', $text2);
    $options->para = false;
    return format_text($text2, FORMAT_HTML, $options);
    //return $text2;
}