Differences Between: [Versions 311 and 401] [Versions 311 and 402] [Versions 311 and 403]
1 <?php 2 3 // This file is part of Moodle - http://moodle.org/ 4 // 5 // Moodle is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // Moodle is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 17 18 /** 19 * @package moodlecore 20 * @subpackage backup-dbops 21 * @copyright 2010 onwards Eloy Lafuente (stronk7) {@link http://stronk7.com} 22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 23 */ 24 25 /** 26 * Non instantiable helper class providing DB support to the backup_structure stuff 27 * 28 * This class contains various static methods available for all the DB operations 29 * performed by the backup_structure stuff (mainly @backup_nested_element class) 30 * 31 * TODO: Finish phpdocs 32 */ 33 abstract class backup_structure_dbops extends backup_dbops { 34 35 public static function get_iterator($element, $params, $processor) { 36 global $DB; 37 38 // Check we are going to get_iterator for one backup_nested_element 39 if (! $element instanceof backup_nested_element) { 40 throw new base_element_struct_exception('backup_nested_element_expected'); 41 } 42 43 // If var_array, table and sql are null, and element has no final elements it is one nested element without source 44 // Just return one 1 element iterator without information 45 if ($element->get_source_array() === null && $element->get_source_table() === null && 46 $element->get_source_sql() === null && count($element->get_final_elements()) == 0) { 47 return new backup_array_iterator(array(0 => null)); 48 49 } else if ($element->get_source_array() !== null) { // It's one array, return array_iterator 50 return new backup_array_iterator($element->get_source_array()); 51 52 } else if ($element->get_source_table() !== null) { // It's one table, return recordset iterator 53 return $DB->get_recordset($element->get_source_table(), self::convert_params_to_values($params, $processor), $element->get_source_table_sortby()); 54 55 } else if ($element->get_source_sql() !== null) { // It's one sql, return recordset iterator 56 return $DB->get_recordset_sql($element->get_source_sql(), self::convert_params_to_values($params, $processor)); 57 58 } else { // No sources, supress completely, using null iterator 59 return new backup_null_iterator(); 60 } 61 } 62 63 public static function convert_params_to_values($params, $processor) { 64 $newparams = array(); 65 foreach ($params as $key => $param) { 66 $newvalue = null; 67 // If we have a base element, get its current value, exception if not set 68 if ($param instanceof base_atom) { 69 if ($param->is_set()) { 70 $newvalue = $param->get_value(); 71 } else { 72 throw new base_element_struct_exception('valueofparamelementnotset', $param->get_name()); 73 } 74 75 } else if ($param < 0) { // Possibly one processor variable, let's process it 76 // See @backup class for all the VAR_XXX variables available. 77 // Note1: backup::VAR_PARENTID is handled by nested elements themselves 78 // Note2: trying to use one non-existing var will throw exception 79 $newvalue = $processor->get_var($param); 80 81 // Else we have one raw param value, use it 82 } else { 83 $newvalue = $param; 84 } 85 86 $newparams[$key] = $newvalue; 87 } 88 return $newparams; 89 } 90 91 public static function insert_backup_ids_record($backupid, $itemname, $itemid) { 92 global $DB; 93 // We need to do some magic with scales (that are stored in negative way) 94 if ($itemname == 'scale') { 95 $itemid = -($itemid); 96 } 97 // Now, we skip any annotation with negatives/zero/nulls, ids table only stores true id (always > 0) 98 if ($itemid <= 0 || is_null($itemid)) { 99 return; 100 } 101 // TODO: Analyze if some static (and limited) cache by the 3 params could save us a bunch of record_exists() calls 102 // Note: Sure it will! 103 if (!$DB->record_exists('backup_ids_temp', array('backupid' => $backupid, 'itemname' => $itemname, 'itemid' => $itemid))) { 104 $DB->insert_record('backup_ids_temp', array('backupid' => $backupid, 'itemname' => $itemname, 'itemid' => $itemid)); 105 } 106 } 107 108 /** 109 * Adds backup id database record for all files in the given file area. 110 * 111 * @param string $backupid Backup ID 112 * @param int $contextid Context id 113 * @param string $component Component 114 * @param string $filearea File area 115 * @param int $itemid Item id 116 * @param \core\progress\base $progress 117 */ 118 public static function annotate_files($backupid, $contextid, $component, $filearea, $itemid, 119 \core\progress\base $progress = null) { 120 global $DB; 121 $sql = 'SELECT id 122 FROM {files} 123 WHERE contextid = ? 124 AND component = ?'; 125 $params = array($contextid, $component); 126 127 if (!is_null($filearea)) { // Add filearea to query and params if necessary 128 $sql .= ' AND filearea = ?'; 129 $params[] = $filearea; 130 } 131 132 if (!is_null($itemid)) { // Add itemid to query and params if necessary 133 $sql .= ' AND itemid = ?'; 134 $params[] = $itemid; 135 } 136 if ($progress) { 137 $progress->start_progress(''); 138 } 139 $rs = $DB->get_recordset_sql($sql, $params); 140 foreach ($rs as $record) { 141 if ($progress) { 142 $progress->progress(); 143 } 144 self::insert_backup_ids_record($backupid, 'file', $record->id); 145 } 146 if ($progress) { 147 $progress->end_progress(); 148 } 149 $rs->close(); 150 } 151 152 /** 153 * Moves all the existing 'item' annotations to their final 'itemfinal' ones 154 * for a given backup. 155 * 156 * @param string $backupid Backup ID 157 * @param string $itemname Item name 158 * @param \core\progress\base $progress Progress tracker 159 */ 160 public static function move_annotations_to_final($backupid, $itemname, \core\progress\base $progress) { 161 global $DB; 162 $progress->start_progress('move_annotations_to_final'); 163 $rs = $DB->get_recordset('backup_ids_temp', array('backupid' => $backupid, 'itemname' => $itemname)); 164 $progress->progress(); 165 foreach($rs as $annotation) { 166 // If corresponding 'itemfinal' annotation does not exist, update 'item' to 'itemfinal' 167 if (! $DB->record_exists('backup_ids_temp', array('backupid' => $backupid, 168 'itemname' => $itemname . 'final', 169 'itemid' => $annotation->itemid))) { 170 $DB->set_field('backup_ids_temp', 'itemname', $itemname . 'final', array('id' => $annotation->id)); 171 } 172 $progress->progress(); 173 } 174 $rs->close(); 175 // All the remaining $itemname annotations can be safely deleted 176 $DB->delete_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => $itemname)); 177 $progress->end_progress(); 178 } 179 180 /** 181 * Returns true/false if there are annotations for a given item 182 */ 183 public static function annotations_exist($backupid, $itemname) { 184 global $DB; 185 return (bool)$DB->count_records('backup_ids_temp', array('backupid' => $backupid, 'itemname' => $itemname)); 186 } 187 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body