function wiki_upload_url($file, &$WS)
{
    global $CFG;
    if (!isset($WS->dfdir->www)) {
        wiki_upload_config($WS);
    }
    return $WS->dfdir->www . '/' . $file;
}
function wiki_main_setup()
{
    global $CFG, $USER, $COURSE, $WS;
    $wikimanager = wiki_manager_get_instance();
    //recover the value from the same variable if there is not defined any GET or POST:
    $WS->page = optional_param('page', $WS->page, PARAM_CLEANHTML);
    // Wiki Page Name
    //Always strip slashes from page to be 100% sure we can add them back
    //when sending info to DB. This must be done before any action with $page and DB!!
    $WS->page = stripslashes_safe($WS->page);
    // Selected tab
    $WS->selectedtab = '';
    //Define the path of URL, and define the module id if the wiki is a modul
    //or course id if the wiki is a course whit format 'wiki'
    $WS->wikitype = '/mod/wiki/view.php?id=';
    $WS->linkid = $WS->cm->id;
    if (isset($WS->dfcourse)) {
        $WS->wikitype = '/course/view.php?id=';
        $WS->linkid = $COURSE->id;
    }
    //look for $editor_size
    $WS->editor_size = wiki_get_editor_size($WS);
    //look for $page
    if (!$WS->page) {
        //if there's no page caught the wiki instance first page
        if (!empty($WS->page)) {
            $WS->page = wiki_get_real_pagename($WS->page);
        } else {
            $WS->page = !empty($WS->dfwiki->pagename) ? $WS->dfwiki->pagename : get_string('firstpage', 'wiki');
        }
        //students are considerated only by group (like old dfwiki version).
        if ($WS->dfwiki->studentmode == 0) {
            //if the first page exists put action in view, otherwise put in edit
            $pageid = new wiki_pageid($WS->dfwiki->id, $WS->page, null, $WS->groupmember->groupid, null);
            if ($wikimanager->page_exists($pageid)) {
                if (substr($WS->page, 0, strlen('discussion:')) != 'discussion:') {
                    $WS->pageaction = 'view';
                } else {
                    $WS->pageaction = 'discussion';
                }
            } else {
                if (substr($WS->page, 0, strlen('discussion:')) != 'discussion:') {
                    $WS->pageaction = 'edit';
                } else {
                    $WS->pageaction = 'editdiscussion';
                }
            }
        } else {
            //case "without groups", with visible or separate students
            if ($WS->cm->groupmode == 0) {
                //if the first page exists put action in view, otherwise put in edit
                $pageid = new wiki_pageid($WS->dfwiki->id, $WS->page, null, null, $WS->member->id);
                if ($wikimanager->page_exists($pageid)) {
                    //if the first page exists put action in view, otherwise put in edit
                    if (substr($WS->page, 0, strlen('discussion:')) != 'discussion:') {
                        $WS->pageaction = 'view';
                    } else {
                        $WS->pageaction = 'discussion';
                    }
                } else {
                    if (substr($WS->page, 0, strlen('discussion:')) != 'discussion:') {
                        $WS->pageaction = 'edit';
                    } else {
                        $WS->pageaction = 'editdiscussion';
                    }
                }
                //case "visible groups" or "separate groups", with visble or separate students
            } else {
                //if the first page exists put action in view, otherwise put in edit
                $pageid = new wiki_pageid($WS->dfwiki->id, $WS->page, null, $WS->groupmember->groupid, $WS->member->id);
                if ($wikimanager->page_exists($pageid)) {
                    //if the first page exists put action in view, otherwise put in edit
                    if (substr($WS->page, 0, strlen('discussion:')) != 'discussion:') {
                        $WS->pageaction = 'view';
                    } else {
                        $WS->pageaction = 'discussion';
                    }
                } else {
                    if (substr($WS->page, 0, strlen('discussion:')) != 'discussion:') {
                        $WS->pageaction = 'edit';
                    } else {
                        $WS->pageaction = 'editdiscussion';
                    }
                }
            }
        }
    } else {
        /* The page variable can contain the name of the page and the page action. These two parameterers
         * are separated by the character '/'. If the page doesn't have the separations character it's
         * suppossed to be just the page name, and the action will be 'view' if the page exists or 'edit'
         * in other cases.
         *
         * For example, to view a page named 'eddy' that already exists, we can do:
         *      page = 'eddy' or page = 'view/eddy'
         * But if we want to edit the 'eddy' page that already exists, we can do only this:
         *      page = 'edit/eddy'
         */
        $pageexplode = explode('/', $WS->page);
        switch (count($pageexplode)) {
            case 1:
                //only the page name is given
                $WS->page = wiki_get_real_pagename($pageexplode[0]);
                //checkes if the page name exists
                if ($WS->dfwiki->studentmode == '0' && $WS->cm->groupmode != '0') {
                    //only by groups
                    $pageid = new wiki_pageid($WS->dfwiki->id, $WS->page, null, $WS->groupmember->groupid, null);
                    if ($wikimanager->page_exists($pageid)) {
                        if (substr($WS->page, 0, strlen('discussion:')) != 'discussion:') {
                            $WS->pageaction = 'view';
                        } else {
                            $WS->pageaction = 'discussion';
                        }
                    } else {
                        if (substr($WS->page, 0, strlen('discussion:')) != 'discussion:') {
                            $WS->pageaction = 'edit';
                        } else {
                            $WS->pageaction = 'editdiscussion';
                        }
                    }
                } else {
                    //by students and their groups
                    $pageid = new wiki_pageid($WS->dfwiki->id, $WS->page, null, $WS->groupmember->groupid, $WS->member->id);
                    if ($wikimanager->page_exists($pageid)) {
                        if (substr($WS->page, 0, strlen('discussion:')) != 'discussion:') {
                            $WS->pageaction = 'view';
                        } else {
                            $WS->pageaction = 'discussion';
                        }
                    } else {
                        if (substr($WS->page, 0, strlen('discussion:')) != 'discussion:') {
                            $WS->pageaction = 'edit';
                        } else {
                            $WS->pageaction = 'editdiscussion';
                        }
                    }
                }
                break;
            case 2:
                //the page name and the action are given
                $WS->page = wiki_get_real_pagename($pageexplode[1]);
                $WS->pageaction = $pageexplode[0];
                // Control action tabs when we are doing preview of discussions
                if (substr($WS->page, 0, strlen('discussion:')) == 'discussion:') {
                    if ($WS->pageaction == 'view') {
                        if (isset($WS->dfform['addtitle'])) {
                            $WS->selectedtab = 'adddiscussion';
                        } else {
                            $WS->selectedtab = 'editdiscussion';
                        }
                    }
                }
                break;
            default:
                //Error
                error('Locallib: necessary parameters needed');
                break;
        }
    }
    // Tab needed for the discussions...
    if ($WS->selectedtab == '') {
        $WS->selectedtab = $WS->pageaction;
    }
    //configure wikieditable
    //configure permissions
    wiki_load_permissions($WS);
    if (!$WS->dfperms['edit'] && $WS->pageaction == 'edit') {
        $WS->pageaction = 'view';
        $WS->selectedtab = 'view';
    }
    //configure update data
    wiki_upload_config($WS);
    //treats the form
    wiki_form_treatment($WS);
    //fetch the latest page version
    // Loads the data of the wiki page, depending on the group mode
    $WS->load_page_data();
    // Selects the editor
    $WS->select_editor();
    // If the page exists, adds the action to log
    add_action_to_log();
}
function wiki_delete_instance($id)
{
    /// Given an ID of an instance of this module,
    /// this function will permanently delete the instance
    /// and any data that depends on it.
    global $WS;
    $wikimanager = wiki_manager_get_instance();
    //get dfwiki entry
    if (!$wikimanager->get_wiki_by_id($id)) {
        return false;
    }
    //get modules id
    if (!($module = get_record("modules", "name", 'wiki'))) {
        return false;
    }
    $WS->dfwiki->course = $wikimanager->persistencemanager->wikis[$id]->course;
    $WS->dfwiki->id = $id;
    //get cm id
    if (!($WS->cm = get_record("course_modules", "course", $WS->dfwiki->course, "module", $module->id, "instance", $id))) {
        return false;
    }
    //delete uploaded files
    wiki_upload_config($WS);
    if (!wiki_upload_deldir($WS)) {
        return false;
    }
    # Delete any dependent records here #
    return $wikimanager->delete_wiki($WS->dfwiki->id);
}
function wiki_sintax_create_internal_link_bis(&$linktext)
{
    global $WS, $USER, $itinerary;
    $WS->member->id = $USER->id;
    if ($WS->dfwiki->studentmode == '0') {
        $WS->member->id = '0';
    }
    $res = '';
    //separate type link from link text
    $parts = explode(":", $linktext);
    if (count($parts) == 1) {
        $linktype = 'internal';
        $linkname = $parts[0];
    } else {
        $linktype = $parts[0];
        $linkname = $parts[1];
    }
    switch ($linktype) {
        case 'internal':
            //normal internal links
            //separate linktext into pagename and text
            $parts = explode("|", $linkname);
            if (count($parts) == 1) {
                $linkpage = $parts[0];
                $linktext = $parts[0];
            } else {
                $linkpage = $parts[0];
                $linktext = $parts[1];
            }
            $cleanpagename = clean_filename($linkpage);
            if (isset($itinerary)) {
                if (!wiki_page_exists($WS, $linkpage)) {
                    $res = '<b><u>' . $linktext . '</u></b><a target="popup" href="' . $linkpage . '.html">?</a>';
                } else {
                    $res = '<a href="' . $cleanpagename . '.html">' . $linkpage . '</a>';
                }
            } else {
                if (wiki_page_exists($WS, $linkpage)) {
                    //if the page already exists
                    $res = '<a href="' . $cleanpagename . '.html">' . $linktext . '</a>';
                } else {
                    //to create the page
                    $res = '<b><u>' . $linktext . '</u></b><a href="' . $linkpage . '.html">?</a>';
                }
            }
            //save link into log
            if (!isset($WS->parser_logs['internal'])) {
                $WS->parser_logs['internal'] = array();
            }
            if (!in_array($linkpage, $WS->parser_logs['internal'])) {
                $WS->parser_logs['internal'][] = $linkpage;
            }
            break;
        case 'user':
            $res = wiki_get_user_info($linkname, 25);
            break;
        case 'attach':
            wiki_upload_config($WS);
            $res = '[' . wiki_upload_url($linkname, $WS) . ' ' . $linkname . ']';
            break;
        default:
            //error
    }
    return $res;
}