Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Provides classes used by the moodle1 converter

Copyright: 2011 Mark Nielsen <mark@moodlerooms.com>
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 1562 lines (55 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 1 file
 backup/converter/moodle1/handlerlib.php

Defines 10 classes

moodle1_converter:: (24 methods):
  log()
  detect_format()
  init()
  execute()
  register_handler()
  grouped_parent_exists()
  process_chunk()
  path_start_reached()
  path_end_reached()
  create_stash_storage()
  drop_stash_storage()
  set_stash()
  get_stash()
  get_stash_or_default()
  get_stash_names()
  get_stash_itemids()
  get_contextid()
  get_nextid()
  get_file_manager()
  get_inforef_manager()
  migrate_referenced_files()
  find_referenced_files()
  rewrite_filephp_usage()
  description()

moodle1_convert_exception:: (0 methods):

moodle1_convert_storage_exception:: (0 methods):

moodle1_convert_empty_storage_exception:: (0 methods):

moodle1_parser_processor:: (5 methods):
  __construct()
  process_cdata()
  dispatch_chunk()
  notify_path_start()
  notify_path_end()

moodle1_xml_transformer:: (1 method):
  process()

convert_path:: (20 methods):
  __construct()
  set_processing_object()
  set_processing_method()
  set_start_method()
  set_end_method()
  set_tags()
  set_dropped_fields()
  set_renamed_fields()
  set_new_fields()
  apply_recipes()
  get_name()
  get_path()
  is_grouped()
  get_processing_object()
  get_processing_method()
  get_start_method()
  get_end_method()
  get_tags()
  validate_name()
  validate_pobject()

convert_path_exception:: (1 method):
  __construct()

moodle1_file_manager:: (9 methods):
  __construct()
  migrate_file()
  migrate_directory()
  get_fileids()
  reset_fileids()
  log()
  make_file_record()
  add_file_to_pool()
  stash_file()

moodle1_inforef_manager:: (5 methods):
  __construct()
  add_ref()
  add_refs()
  write_refs()
  validate_item()


Class: moodle1_converter  - X-Ref

Converter of Moodle 1.9 backup into Moodle 2.x format

log($message, $level, $a = null, $depth = null, $display = false)   X-Ref
Log a message

param: string $message message text
param: int $level message level {@example backup::LOG_WARNING}
param: null|mixed $a additional information
param: null|int $depth the message depth
param: bool $display whether the message should be sent to the output, too

detect_format($tempdir)   X-Ref
Detects the Moodle 1.9 format of the backup directory

param: string $tempdir the name of the backup directory
return: null|string backup::FORMAT_MOODLE1 if the Moodle 1.9 is detected, null otherwise

init()   X-Ref
Initialize the instance if needed, called by the constructor

Here we create objects we need before the execution.

execute()   X-Ref
Converts the contents of the tempdir into the target format in the workdir


register_handler(moodle1_handler $handler, array $elements)   X-Ref
Register a handler for the given path elements


grouped_parent_exists($pelement, $elements)   X-Ref
Helper method used by {@link self::register_handler()}

param: convert_path $pelement path element
param: array of convert_path instances
return: bool true if grouped parent was found, false otherwise

process_chunk($data)   X-Ref
Process the data obtained from the XML parser processor

This methods receives one chunk of information from the XML parser
processor and dispatches it, following the naming rules.
We are expanding the modules and blocks paths here to include the plugin's name.

param: array $data

path_start_reached($path)   X-Ref
Executes operations required at the start of a watched path

For MOD and BLOCK paths, this is supported only for the sub-paths, not the root
module/block element. For the illustration:

You CAN'T attach on_xxx_start() listener to a path like
/MOODLE_BACKUP/COURSE/MODULES/MOD/WORKSHOP because the <MOD> must
be processed first in {@link self::process_chunk()} where $this->currentmod
is set.

You CAN attach some on_xxx_start() listener to a path like
/MOODLE_BACKUP/COURSE/MODULES/MOD/WORKSHOP/SUBMISSIONS because it is
a sub-path under <MOD> and we have $this->currentmod already set when the
<SUBMISSIONS> is reached.

param: string $path in the original file

path_end_reached($path)   X-Ref
Executes operations required at the end of a watched path

param: string $path in the original file

create_stash_storage()   X-Ref
Creates the temporary storage for stashed data

This implementation uses backup_ids_temp table.

drop_stash_storage()   X-Ref
Drops the temporary storage of stashed data

This implementation uses backup_ids_temp table.

set_stash($stashname, $info, $itemid = 0)   X-Ref
Stores some information for later processing

This implementation uses backup_ids_temp table to store data. Make
sure that the $stashname + $itemid combo is unique.

param: string $stashname name of the stash
param: mixed $info information to stash
param: int $itemid optional id for multiple infos within the same stashname

get_stash($stashname, $itemid = 0)   X-Ref
Restores a given stash stored previously by {@link self::set_stash()}

param: string $stashname name of the stash
param: int $itemid optional id for multiple infos within the same stashname
return: mixed stashed data

get_stash_or_default($stashname, $itemid = 0, $default = null)   X-Ref
Restores a given stash or returns the given default if there is no such stash

param: string $stashname name of the stash
param: int $itemid optional id for multiple infos within the same stashname
param: mixed $default information to return if the info has not been stashed previously
return: mixed stashed data or the default value

get_stash_names()   X-Ref
Returns the list of existing stashes

return: array

get_stash_itemids($stashname)   X-Ref
Returns the list of stashed $itemids in the given stash

param: string $stashname
return: array

get_contextid($level, $instance = 0)   X-Ref
Generates an artificial context id

Moodle 1.9 backups do not contain any context information. But we need them
in Moodle 2.x format so here we generate fictive context id for every given
context level + instance combo.

CONTEXT_SYSTEM and CONTEXT_COURSE ignore the $instance as they represent a
single system or the course being restored.

param: int $level the context level, like CONTEXT_COURSE or CONTEXT_MODULE
param: int $instance the instance id, for example $course->id for courses or $cm->id for activity modules
return: int the context id

get_nextid()   X-Ref
Simple autoincrement generator

return: int the next number in a row of numbers

get_file_manager($contextid = null, $component = null, $filearea = null, $itemid = 0, $userid = null)   X-Ref
Creates and returns new instance of the file manager

param: int $contextid the default context id of the files being migrated
param: string $component the default component name of the files being migrated
param: string $filearea the default file area of the files being migrated
param: int $itemid the default item id of the files being migrated
param: int $userid initial user id of the files being migrated
return: moodle1_file_manager

get_inforef_manager($name, $id = 0)   X-Ref
Creates and returns new instance of the inforef manager

param: string $name the name of the annotator (like course, section, activity, block)
param: int $id the id of the annotator if required
return: moodle1_inforef_manager

migrate_referenced_files($text, moodle1_file_manager $fileman)   X-Ref
Migrates all course files referenced from the hypertext using the given filemanager

This is typically used to convert images embedded into the intro fields.

param: string $text hypertext containing $@FILEPHP@$ referenced
param: moodle1_file_manager $fileman file manager to use for the file migration
return: string the original $text with $@FILEPHP@$ references replaced with the new @@PLUGINFILE@@

find_referenced_files($text)   X-Ref
Detects all links to file.php encoded via $@FILEPHP@$ and returns the files to migrate

param: string $text
return: array

rewrite_filephp_usage($text, array $files)   X-Ref
Given the list of migrated files, rewrites references to them from $@FILEPHP@$ form to the @@PLUGINFILE@@ one

param: string $text
param: array $files
return: string

description()   X-Ref


Class: moodle1_convert_exception  - X-Ref

Exception thrown by this converter

Class: moodle1_convert_storage_exception  - X-Ref

Exception thrown by the temporary storage subsystem of moodle1_converter

Class: moodle1_convert_empty_storage_exception  - X-Ref

Exception thrown by the temporary storage subsystem of moodle1_converter

Class: moodle1_parser_processor  - X-Ref

XML parser processor used for processing parsed moodle.xml

__construct(moodle1_converter $converter)   X-Ref
No description

process_cdata($cdata)   X-Ref
Provides NULL decoding

Note that we do not decode $@FILEPHP@$ and friends here as we are going to write them
back immediately into another XML file.

dispatch_chunk($data)   X-Ref
Dispatches the data chunk to the converter class

param: array $data the chunk of parsed data

notify_path_start($path)   X-Ref
Informs the converter at the start of a watched path

param: string $path

notify_path_end($path)   X-Ref
Informs the converter at the end of a watched path

param: string $path

Class: moodle1_xml_transformer  - X-Ref

XML transformer that modifies the content of the files being written during the conversion

process($content)   X-Ref
Modify the content before it is writter to a file

param: string|mixed $content

Class: convert_path  - X-Ref

Class representing a path to be converted from XML file

This was created as a copy of {@link restore_path_element} and should be refactored
probably.
__construct($name, $path, array $recipe = array()   X-Ref
Constructor

The optional recipe array can have three keys, and for each key, the value is another array.
- newfields    => array fieldname => defaultvalue indicates fields that have been added to the table,
and so should be added to the XML.
- dropfields   => array fieldname                 indicates fieldsthat have been dropped from the table,
and so can be dropped from the XML.
- renamefields => array oldname => newname        indicates fieldsthat have been renamed in the table,
and so should be renamed in the XML.
{@line moodle1_course_outline_handler} is a good example that uses all of these.

param: string $name name of the element
param: string $path path of the element
param: array $recipe basic description of the structure conversion
param: bool $grouped to gather information in grouped mode or no

set_processing_object($pobject)   X-Ref
Validates and sets the given processing object

param: object $pobject processing object, must provide a method to be called

set_processing_method($pmethod)   X-Ref
Sets the name of the processing method

param: string $pmethod

set_start_method($smethod)   X-Ref
Sets the name of the path start event listener

param: string $smethod

set_end_method($emethod)   X-Ref
Sets the name of the path end event listener

param: string $emethod

set_tags($tags)   X-Ref
Sets the element tags

param: array $tags

set_dropped_fields(array $fields)   X-Ref
Sets the list of deprecated fields to drop

param: array $fields

set_renamed_fields(array $fields)   X-Ref
Sets the required new names of the current fields

param: array $fields (string)$currentname => (string)$newname

set_new_fields(array $fields)   X-Ref
Sets the new fields and their values

param: array $fields (string)$field => (mixed)value

apply_recipes(array $data)   X-Ref
Cooks the parsed tags data by applying known recipes

Recipes are used for common trivial operations like adding new fields
or renaming fields. The handler's processing method receives cooked
data.

param: array $data the contents of the element
return: array

get_name()   X-Ref

return: string the element given name

get_path()   X-Ref

return: string the path to the element

is_grouped()   X-Ref

return: bool flag to define if this element will get child ones grouped or no

get_processing_object()   X-Ref

return: object the processing object providing the processing method

get_processing_method()   X-Ref

return: string the name of the method to call to process the element

get_start_method()   X-Ref

return: string the name of the path start event listener

get_end_method()   X-Ref

return: string the name of the path end event listener

get_tags()   X-Ref

return: mixed the element data

validate_name($name)   X-Ref
Makes sure the given name is a valid element name

Note it may look as if we used exceptions for code flow control here. That's not the case
as we actually validate the code, not the user data. And the code is supposed to be
correct.

param: string @name the element given name
return: void

validate_pobject($pobject)   X-Ref
Makes sure that the given object is a valid processing object

The processing object must be an object providing at least element's processing method
or path-reached-end event listener or path-reached-start listener method.

Note it may look as if we used exceptions for code flow control here. That's not the case
as we actually validate the code, not the user data. And the code is supposed to be
correct.

param: object $pobject
return: void

Class: convert_path_exception  - X-Ref

Exception being thrown by {@link convert_path} methods

__construct($errorcode, $a = null, $debuginfo = null)   X-Ref
Constructor

param: string $errorcode key for the corresponding error string
param: mixed $a extra words and phrases that might be required by the error string
param: string $debuginfo optional debugging information

Class: moodle1_file_manager  - X-Ref

The class responsible for files migration

The files in Moodle 1.9 backup are stored in moddata, user_files, group_files,
course_files and site_files folders.
__construct(moodle1_converter $converter, $contextid = null, $component = null, $filearea = null, $itemid = 0, $userid = null)   X-Ref
Constructor optionally accepting some default values for the migrated files

param: moodle1_converter $converter the converter instance we serve to
param: int $contextid initial context id of the files being migrated
param: string $component initial component name of the files being migrated
param: string $filearea initial file area of the files being migrated
param: int $itemid initial item id of the files being migrated
param: int $userid initial user id of the files being migrated

migrate_file($sourcepath, $filepath = '/', $filename = null, $sortorder = 0, $timecreated = null, $timemodified = null)   X-Ref
Migrates one given file stored on disk

param: string $sourcepath the path to the source local file within the backup archive {@example 'moddata/foobar/file.ext'}
param: string $filepath the file path of the migrated file, defaults to the root directory '/' {@example '/sub/dir/'}
param: string $filename the name of the migrated file, defaults to the same as the source file has
param: int $sortorder the sortorder of the file (main files have sortorder set to 1)
param: int $timecreated override the timestamp of when the migrated file should appear as created
param: int $timemodified override the timestamp of when the migrated file should appear as modified
return: int id of the migrated file

migrate_directory($rootpath, $relpath='/')   X-Ref
Migrates all files in the given directory

param: string $rootpath path within the backup archive to the root directory containing the files {@example 'course_files'}
param: string $relpath relative path used during the recursion - do not provide when calling this!
return: array ids of the migrated files, empty array if the $rootpath not found

get_fileids()   X-Ref
Returns the list of all file ids migrated by this instance so far

return: array of int

reset_fileids()   X-Ref
Explicitly clear the list of file ids migrated by this instance so far


log($message, $level, $a = null, $depth = null, $display = false)   X-Ref
Log a message using the converter's logging mechanism

param: string $message message text
param: int $level message level {@example backup::LOG_WARNING}
param: null|mixed $a additional information
param: null|int $depth the message depth
param: bool $display whether the message should be sent to the output, too

make_file_record(array $fileinfo)   X-Ref
Prepares a fake record from the files table

param: array $fileinfo explicit file data
return: array

add_file_to_pool($pathname)   X-Ref
Copies the given file to the pool directory

Returns an array containing SHA1 hash of the file contents, the file size
and a flag indicating whether the file was actually added to the pool or whether
it was already there.

param: string $pathname the full path to the file
return: array with keys (string)contenthash, (int)filesize, (bool)newfile

stash_file(array $filerecord)   X-Ref
Stashes the file record into 'files' stash and adds the record id to list of migrated files

param: array $filerecord

Class: moodle1_inforef_manager  - X-Ref

Helper class that handles ids annotations for inforef.xml files

__construct(moodle1_converter $converter, $name, $id = 0)   X-Ref
Creates new instance of the manager for the given annotator

The identification of the annotator we serve to may be important in the future
when we move the actual storage of the references from memory to a persistent storage.

param: moodle1_converter $converter
param: string $name the name of the annotator (like course, section, activity, block)
param: int $id the id of the annotator if required

add_ref($item, $id)   X-Ref
Adds a reference

param: string $item the name of referenced item (like user, file, scale, outcome or grade_item)
param: int $id the value of the reference

add_refs($item, array $ids)   X-Ref
Adds a bulk of references

param: string $item the name of referenced item (like user, file, scale, outcome or grade_item)
param: array $ids the list of referenced ids

write_refs(xml_writer $xmlwriter)   X-Ref
Writes the current references using a given opened xml writer

param: xml_writer $xmlwriter

validate_item($item)   X-Ref
Makes sure that the given name is a valid citizen of inforef.xml file

param: string $item the name of reference (like user, file, scale, outcome or grade_item)