Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

MDL-27120 backup: fixed docblock and code cleanup

  • Loading branch information...
commit f2bfe26aea52b918df5147d7b2b911b0eca12f59 1 parent 0c99b72
Rajesh Taneja rajeshtaneja authored
33 backup/moodle2/backup_xml_transformer.class.php
@@ -27,17 +27,18 @@
27 27
28 28 defined('MOODLE_INTERNAL') || die();
29 29
  30 +// Cache for storing link encoders, so that we don't need to call
  31 +// register_link_encoders each time backup_xml_transformer is constructed
  32 +// TODO MDL-25290 replace global with MUC code.
  33 +global $LINKS_ENCODERS_CACHE;
  34 +
  35 +$LINKS_ENCODERS_CACHE = array();
  36 +
30 37 /**
31 38 * Class implementing the @xml_contenttrasnformed logic to be applied in moodle2 backups
32 39 *
33 40 * TODO: Finish phpdocs
34 41 */
35   -
36   -// cache for storing link encoders, so that we don't need to call
37   -// register_link_encoders each time backup_xml_transformer is constructed
38   -global $LINKS_ENCODERS_CACHE;
39   -$LINKS_ENCODERS_CACHE = array();
40   -
41 42 class backup_xml_transformer extends xml_contenttransformer {
42 43
43 44 private $absolute_links_encoders; // array of static methods to be called in order to
@@ -47,18 +48,13 @@ class backup_xml_transformer extends xml_contenttransformer {
47 48 private $unicoderegexp; // to know if the site supports unicode regexp
48 49
49 50 public function __construct($courseid) {
50   - global $LINKS_ENCODERS_CACHE;
51   -
52 51 $this->absolute_links_encoders = array();
53 52 $this->courseid = $courseid;
54 53 // Check if we support unicode modifiers in regular expressions
55 54 $this->unicoderegexp = @preg_match('/\pL/u', 'a'); // This will fail silently, returning false,
56 55 // if regexp libraries don't support unicode
57 56 // Register all the available content link encoders
58   - if (empty($LINKS_ENCODERS_CACHE)) {
59   - $LINKS_ENCODERS_CACHE = $this->register_link_encoders();
60   - }
61   - $this->absolute_links_encoders = $LINKS_ENCODERS_CACHE;
  57 + $this->absolute_links_encoders = $this->register_link_encoders();
62 58 }
63 59
64 60 public function process($content) {
@@ -142,7 +138,19 @@ static private function process_filephp_uses($matches) {
142 138 return $result;
143 139 }
144 140
  141 + /**
  142 + * Register all available content link encoders
  143 + *
  144 + * @return array encoder
  145 + * @todo MDL-25290 replace LINKS_ENCODERS_CACHE global with MUC code
  146 + */
145 147 private function register_link_encoders() {
  148 + global $LINKS_ENCODERS_CACHE;
  149 + // If encoder is linked, then return cached encoder.
  150 + if (!empty($LINKS_ENCODERS_CACHE)) {
  151 + return $LINKS_ENCODERS_CACHE;
  152 + }
  153 +
146 154 $encoders = array();
147 155
148 156 // Add the course encoder
@@ -171,6 +179,7 @@ private function register_link_encoders() {
171 179 // Add local encodes
172 180 // TODO: Any interest? 1.9 never had that.
173 181
  182 + $LINKS_ENCODERS_CACHE = $encoders;
174 183 return $encoders;
175 184 }
176 185 }
163 backup/util/dbops/restore_dbops.class.php
@@ -28,8 +28,38 @@
28 28 * TODO: Finish phpdocs
29 29 */
30 30 abstract class restore_dbops {
31   - static $BACKUP_IDS_CACHE = array();
32   - static $BACKUP_IDS_EXIST = array();
  31 + /**
  32 + * Keep cache of backup records.
  33 + * @var array
  34 + * @todo MDL-25290 static should be replaced with MUC code.
  35 + */
  36 + private static $backupidscache = array();
  37 + /**
  38 + * Keep track of backup ids which are cached.
  39 + * @var array
  40 + * @todo MDL-25290 static should be replaced with MUC code.
  41 + */
  42 + private static $backupidsexist = array();
  43 + /**
  44 + * Count is expensive, so manually keeping track of
  45 + * backupidscache, to avoid memory issues.
  46 + * @var int
  47 + * @todo MDL-25290 static should be replaced with MUC code.
  48 + */
  49 + private static $backupidscachesize = 2048;
  50 + /**
  51 + * Count is expensive, so manually keeping track of
  52 + * backupidsexist, to avoid memory issues.
  53 + * @var int
  54 + * @todo MDL-25290 static should be replaced with MUC code.
  55 + */
  56 + private static $backupidsexistsize = 10240;
  57 + /**
  58 + * Slice backupids cache to add more data.
  59 + * @var int
  60 + * @todo MDL-25290 static should be replaced with MUC code.
  61 + */
  62 + private static $backupidsslice = 512;
33 63
34 64 /**
35 65 * Return one array containing all the tasks that have been included
@@ -153,61 +183,130 @@ public static function precheck_included_roles($restoreid, $courseid, $userid, $
153 183 return $problems;
154 184 }
155 185
  186 + /**
  187 + * Return cached backup id's
  188 + *
  189 + * @param int $restoreid id of backup
  190 + * @param string $itemname name of the item
  191 + * @param int $itemid id of item
  192 + * @return array backup id's
  193 + * @todo MDL-25290 replace static backupids* with MUC code
  194 + */
156 195 protected static function get_backup_ids_cached($restoreid, $itemname, $itemid) {
157 196 global $DB;
158 197
159 198 $key = "$itemid $itemname $restoreid";
160 199
161   - if (!isset(self::$BACKUP_IDS_EXIST[$key])) {
162   - return false;
  200 + // If record exists in cache then return.
  201 + if (isset(self::$backupidsexist[$key]) && isset(self::$backupidscache[$key])) {
  202 + return self::$backupidscache[$key];
163 203 }
164 204
165   - if (isset(self::$BACKUP_IDS_CACHE[$key])) {
166   - return self::$BACKUP_IDS_CACHE[$key];
  205 + // Clean cache, if it's full.
  206 + if (self::$backupidscachesize <= 0) {
  207 + // Remove some records, to keep memory in limit.
  208 + self::$backupidscache = array_slice(self::$backupidscache, self::$backupidsslice, null, true);
  209 + self::$backupidscachesize = self::$backupidscachesize + self::$backupidsslice;
  210 + }
  211 + if (self::$backupidsexistsize <= 0) {
  212 + self::$backupidsexist = array_slice(self::$backupidsexist, self::$backupidsslice, null, true);
  213 + self::$backupidsexistsize = self::$backupidsexistsize + self::$backupidsslice;
167 214 }
168 215
  216 + // Retrive record from database.
169 217 $record = array(
170 218 'backupid' => $restoreid,
171 219 'itemname' => $itemname,
172 220 'itemid' => $itemid
173 221 );
174   -
175   - if (count(self::$BACKUP_IDS_CACHE) > 2048) {
176   - $BACKUP_IDS_CACHE = array();
  222 + if ($dbrec = $DB->get_record('backup_ids_temp', $record)) {
  223 + self::$backupidsexist[$key] = $dbrec->id;
  224 + self::$backupidscache[$key] = $dbrec;
  225 + self::$backupidscachesize--;
  226 + self::$backupidsexistsize--;
  227 + return self::$backupidscache[$key];
  228 + } else {
  229 + return false;
177 230 }
178   -
179   - return self::$BACKUP_IDS_CACHE[$key] = $DB->get_record('backup_ids_temp', $record);
180 231 }
181 232
  233 + /**
  234 + * Cache backup ids'
  235 + *
  236 + * @param int $restoreid id of backup
  237 + * @param string $itemname name of the item
  238 + * @param int $itemid id of item
  239 + * @param array $extrarecord extra record which needs to be updated
  240 + * @return void
  241 + * @todo MDL-25290 replace static BACKUP_IDS_* with MUC code
  242 + */
182 243 protected static function set_backup_ids_cached($restoreid, $itemname, $itemid, $extrarecord) {
183 244 global $DB;
184 245
185 246 $key = "$itemid $itemname $restoreid";
186 247
187   - if (!isset(self::$BACKUP_IDS_EXIST[$key])) {
188   - $record = array(
189   - 'backupid' => $restoreid,
190   - 'itemname' => $itemname,
191   - 'itemid' => $itemid,
192   - 'newitemid' => 0,
193   - 'parentitemid' => null,
194   - 'info' => null
195   - );
196   - $record = array_merge($record, $extrarecord);
197   - $record['id'] = $DB->insert_record_raw('backup_ids_temp', $record);
198   - self::$BACKUP_IDS_EXIST[$key] = $record['id'];
199   - if (count(self::$BACKUP_IDS_CACHE) < 2048) {
200   - // cache new records if we haven't got many yet
201   - self::$BACKUP_IDS_CACHE[$key] = (object) $record;
  248 + $record = array(
  249 + 'backupid' => $restoreid,
  250 + 'itemname' => $itemname,
  251 + 'itemid' => $itemid,
  252 + );
  253 +
  254 + // If record is not cached then add one.
  255 + if (!isset(self::$backupidsexist[$key])) {
  256 + // If we have this record in db, then just update this.
  257 + if ($existingrecord = $DB->get_record('backup_ids_temp', $record)) {
  258 + self::$backupidsexist[$key] = $existingrecord->id;
  259 + self::$backupidsexistsize--;
  260 + self::update_backup_cached_record($record, $extrarecord, $key, $existingrecord);
  261 + } else {
  262 + // Add new record to cache and db.
  263 + $recorddefault = array (
  264 + 'newitemid' => 0,
  265 + 'parentitemid' => null,
  266 + 'info' => null);
  267 + $record = array_merge($record, $recorddefault, $extrarecord);
  268 + $record['id'] = $DB->insert_record('backup_ids_temp', $record);
  269 + self::$backupidsexist[$key] = $record['id'];
  270 + self::$backupidsexistsize--;
  271 + if (self::$backupidscachesize > 0) {
  272 + // Cache new records if we haven't got many yet.
  273 + self::$backupidscache[$key] = (object) $record;
  274 + self::$backupidscachesize--;
  275 + }
202 276 }
203 277 } else {
204   - if (!empty($extrarecord)) {
205   - $extrarecord['id'] = self::$BACKUP_IDS_EXIST[$key];
206   - $DB->update_record('backup_ids_temp', $extrarecord);
207   - if (isset(self::$BACKUP_IDS_CACHE[$key])) {
208   - $record = array_merge((array)self::$BACKUP_IDS_CACHE[$key], $extrarecord);
209   - self::$BACKUP_IDS_CACHE[$key] = (object) $record;
  278 + self::update_backup_cached_record($record, $extrarecord, $key);
  279 + }
  280 + }
  281 +
  282 + /**
  283 + * Updates existing backup record
  284 + *
  285 + * @param array $record record which needs to be updated
  286 + * @param array $extrarecord extra record which needs to be updated
  287 + * @param string $key unique key which is used to identify cached record
  288 + * @param stdClass $existingrecord (optional) existing record
  289 + */
  290 + protected static function update_backup_cached_record($record, $extrarecord, $key, $existingrecord = null) {
  291 + global $DB;
  292 + // Update only if extrarecord is not empty.
  293 + if (!empty($extrarecord)) {
  294 + $extrarecord['id'] = self::$backupidsexist[$key];
  295 + $DB->update_record('backup_ids_temp', $extrarecord);
  296 + // Update existing cache or add new record to cache.
  297 + if (isset(self::$backupidscache[$key])) {
  298 + $record = array_merge((array)self::$backupidscache[$key], $extrarecord);
  299 + self::$backupidscache[$key] = (object) $record;
  300 + } else if (self::$backupidscachesize > 0) {
  301 + if ($existingrecord) {
  302 + self::$backupidscache[$key] = $existingrecord;
  303 + } else {
  304 + // Retrive record from database and cache updated records.
  305 + self::$backupidscache[$key] = $DB->get_record('backup_ids_temp', $record);
210 306 }
  307 + $record = array_merge((array)self::$backupidscache[$key], $extrarecord);
  308 + self::$backupidscache[$key] = (object) $record;
  309 + self::$backupidscachesize--;
211 310 }
212 311 }
213 312 }
14 backup/util/plan/base_plan.class.php
@@ -62,7 +62,7 @@ public function add_task($task) {
62 62 foreach ($task->get_settings() as $key => $setting) {
63 63 if (!in_array($setting, $this->settings)) {
64 64 $name = $setting->get_name();
65   - if(!isset($this->settings[$name])) {
  65 + if (!isset($this->settings[$name])) {
66 66 $this->settings[$name] = $setting;
67 67 } else {
68 68 throw new base_plan_exception('multiple_settings_by_name_found', $name);
@@ -89,17 +89,17 @@ public function get_settings() {
89 89
90 90 /**
91 91 * return one setting by name, useful to request root/course settings
92   - * that are, by definition, unique by name. Throws exception if multiple
93   - * are found
  92 + * that are, by definition, unique by name.
94 93 *
95   - * TODO: Change this to string indexed array for quicker lookup. Not critical
  94 + * @param string $name name of the setting
  95 + * @throws base_plan_exception if setting name is not found.
96 96 */
97 97 public function get_setting($name) {
98 98 $result = null;
99   - if(isset($this->settings[$name])) {
100   - $result = $this->settings[$name];
  99 + if (isset($this->settings[$name])) {
  100 + $result = $this->settings[$name];
101 101 } else {
102   - throw new base_plan_exception('setting_by_name_not_found', $name);
  102 + throw new base_plan_exception('setting_by_name_not_found', $name);
103 103 }
104 104 return $result;
105 105 }
8 backup/util/ui/backup_ui_stage.class.php
@@ -147,9 +147,9 @@ protected function initialise_stage_form() {
147 147 }
148 148 }
149 149 }
150   - // Add all settings at once
  150 + // Add all settings at once.
151 151 $form->add_settings($add_settings);
152   - // Add dependencies
  152 + // Add dependencies.
153 153 foreach ($dependencies as $depsetting) {
154 154 $form->add_dependencies($depsetting);
155 155 }
@@ -265,8 +265,8 @@ protected function initialise_stage_form() {
265 265 }
266 266 }
267 267 $form->add_settings($add_settings);
268   - foreach($dependencies as $depsetting) {
269   - $form->add_dependencies($depsetting);
  268 + foreach ($dependencies as $depsetting) {
  269 + $form->add_dependencies($depsetting);
270 270 }
271 271 $this->stageform = $form;
272 272 }
22 backup/util/ui/base_moodleform.class.php
@@ -136,32 +136,14 @@ function close_task_divs() {
136 136 * @return bool
137 137 */
138 138 function add_setting(backup_setting $setting, base_task $task=null) {
139   - global $OUTPUT;
140   -
141   - // If the setting cant be changed or isn't visible then add it as a fixed setting.
142   - if (!$setting->get_ui()->is_changeable() || $setting->get_visibility() != backup_setting::VISIBLE) {
143   - return $this->add_fixed_setting($setting, $task);
144   - }
145   -
146   - // First add the formatting for this setting
147   - $this->add_html_formatting($setting);
148   -
149   - // The call the add method with the get_element_properties array
150   - call_user_func_array(array($this->_form, 'addElement'), $setting->get_ui()->get_element_properties($task, $OUTPUT));
151   - $this->_form->setDefault($setting->get_ui_name(), $setting->get_value());
152   - if ($setting->has_help()) {
153   - list($identifier, $component) = $setting->get_help();
154   - $this->_form->addHelpButton($setting->get_ui_name(), $identifier, $component);
155   - }
156   - $this->_form->addElement('html', html_writer::end_tag('div'));
157   - return true;
  139 + return $this->add_settings(array(array($setting, $task)));
158 140 }
159 141 /**
160 142 * Adds multiple backup_settings as elements to the form
161 143 * @param array $settingstasks Consists of array($setting, $task) elements
162 144 * @return bool
163 145 */
164   - function add_settings(array $settingstasks) {
  146 + public function add_settings(array $settingstasks) {
165 147 global $OUTPUT;
166 148
167 149 $defaults = array();
10 backup/util/xml/parser/progressive_parser.class.php
@@ -149,15 +149,7 @@ public function process() {
149 149 * handling parser paths, see MDL-24381
150 150 */
151 151 public static function dirname($path) {
152   - static $cache = array();
153   -
154   - if (!isset($cache[$path])) {
155   - if (count($cache) > 4096)
156   - $cache = array();
157   - $cache[$path] = str_replace('\\', '/', dirname($path));
158   - }
159   -
160   - return $cache[$path];
  152 + return str_replace('\\', '/', dirname($path));
161 153 }
162 154
163 155 // Protected API starts here

0 comments on commit f2bfe26

Please sign in to comment.
Something went wrong with that request. Please try again.