Load one inforef.xml file to backup_ids table for future reference

@param string $restoreid Restore id
@param string $inforeffile File path
@param \core\progress\base $progress Progress tracker
767830
public static function load_inforef_to_tempids($restoreid, $inforeffile,
            \core\progress\base $progress = null) {

        if (!file_exists($inforeffile)) { // Shouldn't happen ever, but...
            throw new backup_helper_exception('missing_inforef_xml_file', $inforeffile);
        }

        // Set up progress tracking (indeterminate).
        if (!$progress) {
            $progress = new \core\progress\none();
        }
        $progress->start_progress('Loading inforef.xml file');

        // Let's parse, custom processor will do its work, sending info to DB
        $xmlparser = new progressive_parser();
        $xmlparser->set_file($inforeffile);
        $xmlprocessor = new restore_inforef_parser_processor($restoreid);
        $xmlparser->set_processor($xmlprocessor);
        $xmlparser->set_progress($progress);
        $xmlparser->process();

        // Finish progress
        $progress->end_progress();
    }
154592
protected static function get_backup_ids_cached($restoreid, $itemname, $itemid) {
        global $DB;

        $key = "$itemid $itemname $restoreid";

        // If record exists in cache then return.
        if (isset(self::$backupidsexist[$key]) && isset(self::$backupidscache[$key])) {
            // Return a copy of cached data, to avoid any alterations in cached data.
            return clone self::$backupidscache[$key];
        }

        // Clean cache, if it's full.
        if (self::$backupidscachesize <= 0) {
            // Remove some records, to keep memory in limit.
            self::$backupidscache = array_slice(self::$backupidscache, self::$backupidsslice, null, true);
            self::$backupidscachesize = self::$backupidscachesize + self::$backupidsslice;
        }
        if (self::$backupidsexistsize <= 0) {
            self::$backupidsexist = array_slice(self::$backupidsexist, self::$backupidsslice, null, true);
            self::$backupidsexistsize = self::$backupidsexistsize + self::$backupidsslice;
        }

        // Retrive record from database.
        $record = array(
            'backupid' => $restoreid,
            'itemname' => $itemname,
            'itemid'   => $itemid
        );
        if ($dbrec = $DB->get_record('backup_ids_temp', $record)) {
            self::$backupidsexist[$key] = $dbrec->id;
            self::$backupidscache[$key] = $dbrec;
            self::$backupidscachesize--;
            self::$backupidsexistsize--;
            return $dbrec;
        } else {
            return false;
        }
    }
756733
public static function set_course_role_names($restoreid, $courseid) {
        global $DB;

        // Get the course context
        $coursectx = context_course::instance($courseid);
        // Get all the mapped roles we have
        $rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $restoreid, 'itemname' => 'role'), '', 'itemid, info, newitemid');
        foreach ($rs as $recrole) {
            $info = backup_controller_dbops::decode_backup_temp_info($recrole->info);
            // If it's one mapped role and we have one name for it
            if (!empty($recrole->newitemid) && !empty($info['nameincourse'])) {
                // If role name doesn't exist, add it
                $rolename = new stdclass();
                $rolename->roleid = $recrole->newitemid;
                $rolename->contextid = $coursectx->id;
                if (!$DB->record_exists('role_names', (array)$rolename)) {
                    $rolename->name = $info['nameincourse'];
                    $DB->insert_record('role_names', $rolename);
                }
            }
        }
        $rs->close();
    }
1080410
public function process_file($data) {

        $data = (object)$data; // handy

        // load it if needed:
        //   - it it is one of the annotated inforef files (course/section/activity/block)
        //   - it is one "user", "group", "grouping", "grade", "question" or "qtype_xxxx" component file (that aren't sent to inforef ever)
        // TODO: qtype_xxx should be replaced by proper backup_qtype_plugin::get_components_and_fileareas() use,
        //       but then we'll need to change it to load plugins itself (because this is executed too early in restore)
        $isfileref   = restore_dbops::get_backup_ids_record($this->get_restoreid(), 'fileref', $data->id);
        $iscomponent = ($data->component == 'user' || $data->component == 'group' || $data->component == 'badges' ||
                        $data->component == 'grouping' || $data->component == 'grade' ||
                        $data->component == 'question' || substr($data->component, 0, 5) == 'qtype');
        if ($isfileref || $iscomponent) {
            restore_dbops::set_backup_files_record($this->get_restoreid(), $data);
        }
    }
76828
public function getAvailablePackageStats($packageHandle, $packageVersion, $progressLimit = null)
    {
        $allLocales = [];
        if ($progressLimit === null) {
            $progressLimit = $this->getProgressLimit();
        }
        $data = null;
        $cacheLifetime = $this->getCacheLifetime();
        if ($cacheLifetime > 0 && $this->cache->isEnabled()) {
            $cacheItem = $this->cache->getItem('community_translation/' . $packageHandle . '@' . $packageVersion . 'L' . $progressLimit);
            /* @var \Stash\Item $cacheItem */
            if (!$cacheItem->isMiss()) {
                $data = $cacheItem->get();
            }
        } else {
            $cacheItem = null;
        }
        if ($data === null) {
            $request = $this->buildRequest('package/' . rawurlencode($packageHandle) . '/best-match-version/locales/' . $progressLimit . '/?v=' . rawurlencode($packageVersion));
            $this->httpClient->reset();
            $response = $this->httpClient->send($request);
            $this->httpClient->reset();
            if ($response->getStatusCode() === 404) {
                $data = [];
            } else {
                $data = $this->getJsonFromResponse($response);
            }
            if ($cacheItem !== null) {
                $cacheItem->set($data)->expiresAfter($cacheLifetime)->save();
            }
        }

        $result = [];
        if (!empty($data)) {
            foreach ($data['locales'] as $localeStats) {
                $result[$localeStats['id']] = new Stats(
                    $data['versionHandle'],
                    $localeStats['total'],
                    $localeStats['translated'],
                    empty($localeStats['translated']) ? null : new DateTime($localeStats['updated'])
                );
            }
        }

        return $result;
    }
259484
public void incrementalRestore(File incrementalBackupFile) throws FileNotFoundException, IOException,
      ClassNotFoundException, RepositoryException
   {
      ObjectInputStream ois = null;
      try
      {
         ois = new ObjectInputStream(PrivilegedFileHelper.fileInputStream(incrementalBackupFile));

         while (true)
         {
            TransactionChangesLog changesLog = readExternal(ois);
            changesLog.setSystemId(Constants.JCR_CORE_RESTORE_WORKSPACE_INITIALIZER_SYSTEM_ID); // mark changes

            ChangesLogIterator cli = changesLog.getLogIterator();
            while (cli.hasNextLog())
            {
               if (cli.nextLog().getEventType() == ExtendedEvent.LOCK)
               {
                  cli.removeLog();
               }
            }

            saveChangesLog(changesLog);
         }
      }
      catch (EOFException ioe)
      {
         if (LOG.isTraceEnabled())
         {
            LOG.trace("An exception occurred: " + ioe.getMessage());
         }
      }
   }
1299566
def set_progress(self, scan_id, progress):
        

        if progress > 0 and progress <= 100:
            self.scans_table[scan_id]['progress'] = progress
        if progress == 100:
            self.scans_table[scan_id]['end_time'] = int(time.time())
1161701
public void loadContentDefinition(final String entityId, final Command callback) {

        AsyncCallback<CmsContentDefinition> asyncCallback = new AsyncCallback<CmsContentDefinition>() {

            public void onFailure(Throwable caught) {

                onRpcError(caught);
            }

            public void onSuccess(CmsContentDefinition result) {

                registerContentDefinition(result);
                callback.execute();
            }
        };
        getService().loadContentDefinition(entityId, asyncCallback);
    }
90442
public static function get_async_backup_progress($backupids, $contextid) {
        global $CFG;
        require_once($CFG->dirroot . '/backup/util/includes/backup_includes.php');
        require_once($CFG->dirroot . '/backup/util/includes/restore_includes.php');

        // Parameter validation.
        self::validate_parameters(
                self::get_async_backup_progress_parameters(),
                array(
                    'backupids' => $backupids,
                    'contextid' => $contextid
                )
        );

        // Context validation.
        list($context, $course, $cm) = get_context_info_array($contextid);
        self::validate_context($context);

        if ($cm) {
            require_capability('moodle/backup:backupactivity', $context);
        } else {
            require_capability('moodle/backup:backupcourse', $context);
            $instanceid = $course->id;
        }

        $results = array();
        foreach ($backupids as $backupid) {
            $results[] = backup_controller_dbops::get_progress($backupid);
        }

        return $results;
    }
1369698
public static function restore_get_questions($restoreid, $qcatid) {
        global $DB;

        $results = array();
        $qs = $DB->get_recordset_sql("SELECT itemid, info
                                      FROM {backup_ids_temp}
                                     WHERE backupid = ?
                                       AND itemname = 'question'
                                       AND parentitemid = ?", array($restoreid, $qcatid));
        foreach ($qs as $q) {
            $results[$q->itemid] = backup_controller_dbops::decode_backup_temp_info($q->info);
        }
        $qs->close();
        return $results;
    }
1008591
public static function copy_file_moodle2backup($backupid, $filerecorid) {
        global $DB;

        if (!backup_controller_dbops::backup_includes_files($backupid)) {
            // Only include the files if required by the controller.
            return;
        }

        // Normalise param
        if (!is_object($filerecorid)) {
            $filerecorid = $DB->get_record('files', array('id' => $filerecorid));
        }

        // Directory, nothing to do
        if ($filerecorid->filename === '.') {
            return;
        }

        $fs = get_file_storage();
        $file = $fs->get_file_instance($filerecorid);
        // If the file is external file, skip copying.
        if ($file->is_external_file()) {
            return;
        }

        // Calculate source and target paths (use same subdirs strategy for both)
        $targetfilepath = self::get_backup_storage_base_dir($backupid) . '/' .
                          self::get_backup_content_file_location($filerecorid->contenthash);

        // Create target dir if necessary
        if (!file_exists(dirname($targetfilepath))) {
            if (!check_dir_exists(dirname($targetfilepath), true, true)) {
                throw new backup_helper_exception('cannot_create_directory', dirname($targetfilepath));
            }
        }

        // And copy the file (if doesn't exist already)
        if (!file_exists($targetfilepath)) {
            $file->copy_content_to($targetfilepath);
        }
    }
100322
public static function require_gradebook_backup($courseid, $backupid) {
        global $DB;

        $sql = "SELECT count(id)
                  FROM {grade_items}
                 WHERE courseid=:courseid
                   AND itemtype = 'mod'
                   AND id NOT IN (
                       SELECT bi.itemid
                         FROM {backup_ids_temp} bi
                        WHERE bi.itemname = 'grade_itemfinal'
                          AND bi.backupid = :backupid)";
        $params = array('courseid'=>$courseid, 'backupid'=>$backupid);


        $count = $DB->count_records_sql($sql, $params);

        //if there are 0 activity grade items not already included in the backup
        return $count == 0;
    }
705947
def upload_progress(request):
    
    if 'X-Progress-ID' in request.GET:
        progress_id = request.GET['X-Progress-ID']
    elif 'X-Progress-ID' in request.META:
        progress_id = request.META['X-Progress-ID']
    if 'logfilename' in request.GET:
        logfilename = request.GET['logfilename']
    elif 'logfilename' in request.META:
        logfilename = request.META['logfilename']
    cache_key = "%s_%s" % (request.META['REMOTE_ADDR'], progress_id)
    data = cache.get(cache_key)
    if not data:
        data = cache.get(logfilename.replace(' ','_'))
    return HttpResponse(json.dumps(data))
10200
public static function get_async_backup_progress_parameters() {
        return new external_function_parameters(
            array(
                'backupids' => new external_multiple_structure(
                        new external_value(PARAM_ALPHANUM, 'Backup id to get progress for', VALUE_REQUIRED, null, NULL_ALLOWED),
                        'Backup id to get progress for', VALUE_REQUIRED
                 ),
                'contextid' => new external_value(PARAM_INT, 'Context id', VALUE_REQUIRED, null, NULL_NOT_ALLOWED),
            )
        );
    }
1371650
public void resetCallbacksForInProgressTasks(String workflowId) {
        Preconditions.checkArgument(StringUtils.isNotBlank(workflowId), "workflow id cannot be blank");
        postForEntityWithUriVariablesOnly("workflow/{workflowId}/resetcallbacks", workflowId);
    }
673554
public static function restore_get_question_categories($restoreid, $contextid) {
        global $DB;

        $results = array();
        $qcats = $DB->get_recordset_sql("SELECT itemid, info
                                         FROM {backup_ids_temp}
                                        WHERE backupid = ?
                                          AND itemname = 'question_category'
                                          AND parentitemid = ?", array($restoreid, $contextid));
        foreach ($qcats as $qcat) {
            $results[$qcat->itemid] = backup_controller_dbops::decode_backup_temp_info($qcat->info);
        }
        $qcats->close();

        return $results;
    }
106139
public function execute() {
        if ($this->progress >= self::PROGRESS_EXECUTED) {
            throw new restore_ui_exception('restoreuialreadyexecuted');
        }
        if ($this->stage->get_stage() < self::STAGE_PROCESS) {
            throw new restore_ui_exception('restoreuifinalisedbeforeexecute');
        }
        if ($this->controller->get_target() == backup::TARGET_CURRENT_DELETING || $this->controller->get_target() == backup::TARGET_EXISTING_DELETING) {
            $options = array();
            $options['keep_roles_and_enrolments'] = $this->get_setting_value('keep_roles_and_enrolments');
            $options['keep_groups_and_groupings'] = $this->get_setting_value('keep_groups_and_groupings');
            restore_dbops::delete_course_content($this->controller->get_courseid(), $options);
        }
        $this->controller->execute_plan();
        $this->progress = self::PROGRESS_EXECUTED;
        $this->stage = new restore_ui_stage_complete($this, $this->stage->get_params(), $this->controller->get_results());
        return true;
    }