function ouwiki_backup_one_mod($bf, $preferences, $ouwiki)
{
    global $CFG;
    if (is_numeric($ouwiki)) {
        $ouwiki = get_record('ouwiki', 'id', $ouwiki);
    }
    try {
        $xb = new xml_backup($bf, $preferences, 3);
        $xb->tag_start('MOD');
        // Required bits
        $xb->tag_full('ID', $ouwiki->id);
        $xb->tag_full('MODTYPE', 'ouwiki');
        $xb->tag_full('NAME', $ouwiki->name);
        // Backup versioning
        require dirname(__FILE__) . '/version.php';
        $xb->tag_full('OUWIKI_VERSION', $module->version);
        // Wiki-specific
        $xb->tag_full('SUBWIKIS', $ouwiki->subwikis);
        $userdata = backup_userdata_selected($preferences, 'ouwiki', $ouwiki->id);
        $xb->tag_full_notnull('TIMEOUT', $ouwiki->timeout);
        if ($ouwiki->template) {
            // Template setting and actual file
            $xb->tag_full('TEMPLATE', $ouwiki->template);
            $xb->copy_module_file('ouwiki', $ouwiki->id . '/' . $ouwiki->template);
        }
        $xb->tag_full_notnull('SUMMARY', $ouwiki->summary);
        $xb->tag_full_notnull('EDITBEGIN', $ouwiki->editbegin);
        $xb->tag_full_notnull('EDITEND', $ouwiki->editend);
        $xb->tag_full('COMPLETIONPAGES', $ouwiki->completionpages);
        $xb->tag_full('COMPLETIONEDITS', $ouwiki->completionedits);
        $xb->tag_full('COMMENTING', $ouwiki->commenting);
        // Now do the actual subwikis
        $xb->tag_start('SUBS');
        // not SUBWIKIS because that name already used
        // We only back up content when 'user data' is turned on
        if ($userdata) {
            $subwikis = ouwiki_get_record_array('subwikis', 'wikiid', $ouwiki->id);
            foreach ($subwikis as $subwiki) {
                ouwiki_backup_subwiki($xb, $subwiki, $userdata);
            }
        }
        $xb->tag_end('SUBS');
        $xb->tag_end('MOD');
        return true;
    } catch (Exception $e) {
        ouwiki_handle_backup_exception($e);
        return false;
    }
}
function ouwiki_decode_content_links_caller($restore)
{
    // Get all the items that might have links in, from the relevant new course
    try {
        global $CFG, $db;
        // 1. Summaries
        if ($summaries = get_records_select('ouwiki', 'course=' . $restore->course_id . ' AND summary IS NOT NULL', '', 'id,summary')) {
            foreach ($summaries as $summary) {
                $newsummary = restore_decode_content_links_worker($summary->summary, $restore);
                if ($newsummary != $summary->summary) {
                    if (!set_field('ouwiki', 'summary', addslashes($newsummary), 'id', $summary->id)) {
                        throw new Exception("Failed to set summary for wiki {$summary->id}: " . $db->ErrorMsg());
                    }
                }
            }
        }
        // 2. Actual content
        $rs = get_recordset_sql("\nSELECT\n    v.id,v.xhtml     \nFROM\n    {$CFG->prefix}ouwiki w\n    INNER JOIN {$CFG->prefix}ouwiki_subwikis s ON w.id=s.wikiid\n    INNER JOIN {$CFG->prefix}ouwiki_pages p ON s.id=p.subwikiid\n    INNER JOIN {$CFG->prefix}ouwiki_versions v ON p.id=v.pageid     \nWHERE\n    w.course={$restore->course_id} \n");
        if (!$rs) {
            throw new Exception("Failed to query for wiki data: " . $db->ErrorMsg());
        }
        while (!$rs->EOF) {
            $newcontent = restore_decode_content_links_worker($rs->fields['xhtml'], $restore);
            if ($newcontent != $rs->fields['xhtml']) {
                if (!set_field('ouwiki_versions', 'xhtml', addslashes($newcontent), 'id', $rs->fields['id'])) {
                    throw new Exception("Failed to update content {$rs->fields['id']}: " . $db->ErrorMsg());
                }
            }
            $rs->MoveNext();
        }
        // 3. This is a bit crappy, as it isn't directly to do with content links, but
        //    we can't do it until we have a course-module so it can't happen earlier.
        if (ouwiki_search_installed()) {
            ouwiki_ousearch_update_all(false, $restore->course_id);
        }
        return true;
    } catch (Exception $e) {
        ouwiki_handle_backup_exception($e, 'restore');
        return false;
    }
}