Moodle PHP Documentation 4.2
Moodle 4.2.8 (Build: 20240610) (2d41ac46f45)
core\update\code_manager Class Reference

General purpose class managing the plugins source code files deployment. More...

Public Member Functions

 __construct ($dirroot=null, $temproot=null)
 Instantiate the class instance.
 
 archive_plugin_version ($folderpath, $component, $version, $overwrite=false)
 Archive the current plugin on-disk version.
 
 get_archived_plugin_version ($component, $version)
 Return the path to the ZIP file with the archive of the given plugin version.
 
 get_plugin_zip_root_dir ($zipfilepath)
 Detects the plugin's name from its ZIP file.
 
 get_remote_plugin_zip ($url, $md5)
 Obtain the plugin ZIP file from the given URL.
 
 list_plugin_folder_files ($folderpath)
 Returns list of all files in the given directory.
 
 unzip_plugin_file ($zipfilepath, $targetdir, $rootdir='')
 Extracts the saved plugin ZIP file.
 
 zip_plugin_folder ($folderpath, $targetzip)
 Make an archive backup of the existing plugin folder.
 

Protected Member Functions

 debug ($msg)
 Raise developer debugging level message.
 
 download_file_content ($url, $tofile)
 Thin wrapper for the core's download_file_content() function.
 
 download_plugin_zip_file ($url, $tofile)
 Download the ZIP file with the plugin package from the given location.
 
 init_temp_directories ()
 Makes sure all temp directories exist and are writable.
 
 move_extracted_plugin_files ($sourcedir, $targetdir, array $files)
 Moves the extracted contents of the plugin ZIP into the target location.
 
 rename_extracted_rootdir ($dirname, $rootdir, array $files)
 Renames the root directory of the extracted ZIP package.
 
 set_plugin_files_permissions ($targetdir, array $files)
 Sets the permissions of extracted subdirs and files.
 

Protected Attributes

string $dirroot
 full path to the Moodle app directory root
 
string $temproot
 full path to the temp directory root
 

Detailed Description

General purpose class managing the plugins source code files deployment.

The class is able and supposed to

  • fetch and cache ZIP files distributed via the Moodle Plugins directory
  • unpack the ZIP files in a temporary storage
  • archive existing version of the plugin source code
  • move (deploy) the plugin source code into the $CFG->dirroot
License
http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later

Constructor & Destructor Documentation

◆ __construct()

core\update\code_manager::__construct ( $dirroot = null,
$temproot = null )

Instantiate the class instance.

Parameters
string$dirrootfull path to the moodle app directory root
string$temprootfull path to our temp directory

Member Function Documentation

◆ archive_plugin_version()

core\update\code_manager::archive_plugin_version ( $folderpath,
$component,
$version,
$overwrite = false )

Archive the current plugin on-disk version.

Parameters
string$folderpathfull path to the plugin folder
string$component
int$version
bool$overwriteoverwrite existing archive if found
Return values
bool

◆ debug()

core\update\code_manager::debug ( $msg)
protected

Raise developer debugging level message.

Parameters
string$msg

◆ download_file_content()

core\update\code_manager::download_file_content ( $url,
$tofile )
protected

Thin wrapper for the core's download_file_content() function.

Parameters
string$urlURL to the file
string$tofilefull path to where to store the downloaded file
Return values
bool

◆ download_plugin_zip_file()

core\update\code_manager::download_plugin_zip_file ( $url,
$tofile )
protected

Download the ZIP file with the plugin package from the given location.

Parameters
string$urlURL to the file
string$tofilefull path to where to store the downloaded file
Return values
boolfalse on error

◆ get_archived_plugin_version()

core\update\code_manager::get_archived_plugin_version ( $component,
$version )

Return the path to the ZIP file with the archive of the given plugin version.

Parameters
string$component
int$version
Return values
string|boolfalse if not found, full path otherwise

◆ get_plugin_zip_root_dir()

core\update\code_manager::get_plugin_zip_root_dir ( $zipfilepath)

Detects the plugin's name from its ZIP file.

Plugin ZIP packages are expected to contain a single directory and the directory name would become the plugin name once extracted to the Moodle dirroot.

Parameters
string$zipfilepathfull path to the ZIP files
Return values
string|boolfalse on error

◆ get_remote_plugin_zip()

core\update\code_manager::get_remote_plugin_zip ( $url,
$md5 )

Obtain the plugin ZIP file from the given URL.

The caller is supposed to know both downloads URL and the MD5 hash of the ZIP contents in advance, typically by using the API requests against the plugins directory.

Parameters
string$url
string$md5
Return values
string|boolfull path to the file, false on error

◆ list_plugin_folder_files()

core\update\code_manager::list_plugin_folder_files ( $folderpath)

Returns list of all files in the given directory.

Given a path like /full/path/to/mod/workshop, it returns array like

[workshop/] => /full/path/to/mod/workshop [workshop/lang/] => /full/path/to/mod/workshop/lang [workshop/lang/workshop.php] => /full/path/to/mod/workshop/lang/workshop.php ...

Which mathes the format used by Moodle file packers.

Parameters
string$folderpathfull path to the plugin directory
Return values
array(string)relpath => (string)fullpath

◆ move_extracted_plugin_files()

core\update\code_manager::move_extracted_plugin_files ( $sourcedir,
$targetdir,
array $files )
protected

Moves the extracted contents of the plugin ZIP into the target location.

Parameters
string$sourcedirfull path to the directory the ZIP file was extracted to
mixed$targetdirfull path to the directory where the files should be moved to
array$fileslist of extracted files

◆ rename_extracted_rootdir()

core\update\code_manager::rename_extracted_rootdir ( $dirname,
$rootdir,
array $files )
protected

Renames the root directory of the extracted ZIP package.

This internal helper method assumes that the plugin ZIP package has been extracted into a temporary empty directory so the plugin folder is the only folder there. The ZIP package is supposed to be validated so that it contains just a single root folder.

Parameters
string$dirnamefullpath location of the extracted ZIP package
string$rootdirthe requested name of the root directory
array$fileslist of extracted files
Return values
arrayeventually amended list of extracted files

◆ set_plugin_files_permissions()

core\update\code_manager::set_plugin_files_permissions ( $targetdir,
array $files )
protected

Sets the permissions of extracted subdirs and files.

As a result of unzipping, the subdirs and files are created with permissions set to $CFG->directorypermissions and $CFG->filepermissions. These are too benevolent by default (777 and 666 respectively) for PHP scripts and may lead to HTTP 500 errors in some environments.

To fix this behaviour, we inherit the permissions of the plugin root directory itself.

Parameters
string$targetdirfull path to the directory the ZIP file was extracted to
array$fileslist of extracted files

◆ unzip_plugin_file()

core\update\code_manager::unzip_plugin_file ( $zipfilepath,
$targetdir,
$rootdir = '' )

Extracts the saved plugin ZIP file.

Returns the list of files found in the ZIP. The format of that list is array of (string)filerelpath => (bool|string) where the array value is either true or a string describing the problematic file.

See also
zip_packer\extract_to_pathname()
Parameters
string$zipfilepathfull path to the saved ZIP file
string$targetdirfull path to the directory to extract the ZIP file to
string$rootdirexplicitly rename the root directory of the ZIP into this non-empty value
Return values
arraylist of extracted files as returned by zip_packer::extract_to_pathname()

◆ zip_plugin_folder()

core\update\code_manager::zip_plugin_folder ( $folderpath,
$targetzip )

Make an archive backup of the existing plugin folder.

Parameters
string$folderpathfull path to the plugin folder
string$targetzipfull path to the zip file to be created
Return values
booltrue if file created, false if not

The documentation for this class was generated from the following file: