Example #1
0
function wiki_entries_restore_mods($old_wiki_id, $new_wiki_id, $info, $restore)
{
    global $CFG, $DB;
    $status = true;
    //Get the entries array
    $entries = $info['MOD']['#']['ENTRIES']['0']['#']['ENTRY'];
    //Iterate over entries
    for ($i = 0; $i < sizeof($entries); $i++) {
        $ent_info = $entries[$i];
        //traverse_xmlize($ent_info);                                                                 //Debug
        //print_object ($GLOBALS['traverse_array']);                                                  //Debug
        //$GLOBALS['traverse_array']="";                                                              //Debug
        //We'll need this later!!
        $oldid = backup_todb($ent_info['#']['ID']['0']['#']);
        //Now, build the wiki_ENTRIES record structure
        $entry = new object();
        $entry->wikiid = $new_wiki_id;
        $entry->course = $restore->course_id;
        $entry->userid = backup_todb($ent_info['#']['USERID']['0']['#']);
        $entry->groupid = backup_todb($ent_info['#']['GROUPID']['0']['#']);
        $entry->pagename = backup_todb($ent_info['#']['PAGENAME']['0']['#']);
        $entry->timemodified = backup_todb($ent_info['#']['TIMEMODIFIED']['0']['#']);
        $entry->timemodified += $restore->course_startdateoffset;
        //We have to recode the userid field
        $user = backup_getid($restore->backup_unique_code, "user", $entry->userid);
        if ($user) {
            $entry->userid = $user->new_id;
        }
        //We have to recode the groupid field
        $group = restore_group_getid($restore, $entry->groupid);
        if ($group) {
            $entry->groupid = $group->new_id;
        }
        //The structure is equal to the db, so insert the wiki_entries
        $newid = $DB->insert_record("wiki_entries", $entry);
        //Do some output
        if (($i + 1) % 50 == 0) {
            if (!defined('RESTORE_SILENTLY')) {
                echo ".";
                if (($i + 1) % 1000 == 0) {
                    echo "<br />";
                }
            }
            backup_flush(300);
        }
        if ($newid) {
            //We have the newid, update backup_ids
            backup_putid($restore->backup_unique_code, "wiki_entries", $oldid, $newid);
            //Restore wiki_pages
            $status = wiki_pages_restore_mods($oldid, $newid, $ent_info, $restore);
            //Now copy moddata associated files
            $status = wiki_restore_files($old_wiki_id, $new_wiki_id, $oldid, $newid, $restore);
        } else {
            $status = false;
        }
    }
    return $status;
}
function wiki_restore_mods($mod, $restore)
{
    global $CFG, $COURSE;
    $status = true;
    $ewiki = false;
    //Get record from backup_ids
    $data = backup_getid($restore->backup_unique_code, $mod->modtype, $mod->id);
    $info = $data->info;
    //Now check if the mod is a ewiki or a newwiki mod, an create the
    //array $data with the info with the same format in both cases.
    if (isset($info["MOD"]["#"]["ENTRIES"])) {
        $data = wiki_read_xml_ewiki($info["MOD"]["#"], $restore);
        $ewiki = true;
        //Use this later.
    } else {
        $data = wiki_read_xml_wiki($info["MOD"]["#"], $restore->course_id);
    }
    if (!isset($data)) {
        $status = false;
    } else {
        $schema = wiki_create_schema();
        $wiki = wiki_validate($data['wiki'], $schema['wiki']);
        $wiki->course = $restore->course_id;
        $id = get_record_sql('SELECT id
					FROM ' . $CFG->prefix . 'wiki WHERE wikicourse=' . addslashes($wiki->course));
        if (empty($id->id) && $wiki->wikicourse != '0') {
            //The course is a wiki format course, then set the correct wikicourse id
            $wiki->wikicourse = $wiki->course;
        } else {
            $wiki->wikicourse = 0;
        }
        //Now insert the wiki record
        $newid = insert_record("wiki", $wiki);
        //Do some output
        echo "<li>" . get_string("modulename", 'wiki') . " \"" . format_string(stripslashes($wiki->name), true) . "\"</li>";
        backup_flush(300);
        if ($newid) {
            //We have the newid, update backup_ids
            backup_putid($restore->backup_unique_code, $mod->modtype, $mod->id, $newid);
            //Now check if want to restore user data and do it.
            if ($restore->mods['wiki']->userinfo) {
                $oldid = backup_todb($info['MOD']['#']['ID']['0']['#']);
                $order = backup_todb($info['MOD']['#']['ORDER']['0']['#']);
                //Now, build the wiki record structure
                $schemapages = $schema['wiki_pages'];
                $datapages = $data['wiki_pages'];
                $numpages = count($datapages);
                for ($i = 0; $i < $numpages; $i++) {
                    //$page=wiki_validate_page($datapages[$i],$schemapages);
                    $page = wiki_validate($datapages[$i], $schemapages);
                    $page->dfwiki = $newid;
                    //We have to recode the userid field
                    $user = backup_getid($restore->backup_unique_code, "user", $page->userid);
                    if ($user) {
                        $page->userid = $user->new_id;
                    }
                    //We have to recode the ownerid field
                    if ($ewiki) {
                        if ($wiki->studentmode != '0') {
                            $page->ownerid = $page->userid;
                        }
                    }
                    $owner = backup_getid($restore->backup_unique_code, "owner", $page->ownerid);
                    if ($owner) {
                        $page->ownerid = $owner->new_id;
                    }
                    //We have to recode the groupid field
                    $group = backup_getid($restore->backup_unique_code, "group", $page->groupid);
                    if ($group) {
                        $page->groupid = $group->new_id;
                    }
                    //The structure is equal to the db
                    $oldpageid = $page->id;
                    $newpageid = insert_record("wiki_pages", $page);
                    $page->id = $oldpageid;
                    //Do some output
                    if (($i + 1) % 50 == 0) {
                        echo ".";
                        if (($i + 1) % 1000 == 0) {
                            echo "<br />";
                        }
                        backup_flush(300);
                    }
                    if ($newpageid) {
                        //We have the newid, update backup_ids
                        //$page->id is now the page old id
                        backup_putid($restore->backup_unique_code, "wiki_pages", $page->id, $newpageid);
                    } else {
                        $status = false;
                    }
                }
                //Restore synonymous for the newwiki mod
                if (!$ewiki && isset($data['wiki_synonymous'])) {
                    $schemasyn = $schema['wiki_synonymous'];
                    $datasyns = $data['wiki_synonymous'];
                    $numsyns = count($datasyns);
                    for ($i = 0; $i < $numsyns; $i++) {
                        $syn = wiki_validate($datasyns[$i], $schemasyn);
                        $syn->dfwiki = $newid;
                        //We have to recode the userid field
                        $owner = backup_getid($restore->backup_unique_code, "owner", $syn->ownerid);
                        if ($owner) {
                            $syn->ownerid = $owner->new_id;
                        }
                        $group = backup_getid($restore->backup_unique_code, "group", $syn->groupid);
                        if ($group) {
                            $syn->groupid = $group->new_id;
                        }
                        //The structure is equal to the db
                        $newsynid = insert_record("wiki_synonymous", $syn);
                        //Do some output
                        if (($i + 1) % 50 == 0) {
                            echo ".";
                            if (($i + 1) % 1000 == 0) {
                                echo "<br />";
                            }
                            backup_flush(300);
                        }
                        if ($newsynid) {
                            //We have the newid, update backup_ids
                            backup_putid($restore->backup_unique_code, "wiki_synonymous", $syn->id, $newsynid);
                        } else {
                            $status = false;
                        }
                    }
                }
                //Now copy moddata associated files
                $e_wiki = $data['wiki_pages'][0];
                wiki_restore_files($oldid, $restore, $order, $e_wiki);
            }
        } else {
            $status = false;
        }
    }
    return $status;
}