Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

Provides core\update\code_manager class.

Copyright: 2012, 2013, 2015 David Mudrak <david@moodle.com>
License: http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
File Size: 561 lines (20 kb)
Included or required:0 times
Referenced: 0 times
Includes or requires: 0 files

Defines 1 class


Class: code_manager  - X-Ref

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

__construct($dirroot=null, $temproot=null)   X-Ref
Instantiate the class instance

param: string $dirroot full path to the moodle app directory root
param: string $temproot full path to our temp directory

get_remote_plugin_zip($url, $md5)   X-Ref
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.

param: string $url
param: string $md5
return: string|bool full path to the file, false on error

unzip_plugin_file($zipfilepath, $targetdir, $rootdir = '')   X-Ref
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.

param: string $zipfilepath full path to the saved ZIP file
param: string $targetdir full path to the directory to extract the ZIP file to
param: string $rootdir explicitly rename the root directory of the ZIP into this non-empty value
return: array list of extracted files as returned by {@link zip_packer::extract_to_pathname()}

zip_plugin_folder($folderpath, $targetzip)   X-Ref
Make an archive backup of the existing plugin folder.

param: string $folderpath full path to the plugin folder
param: string $targetzip full path to the zip file to be created
return: bool true if file created, false if not

archive_plugin_version($folderpath, $component, $version, $overwrite=false)   X-Ref
Archive the current plugin on-disk version.

param: string $folderpath full path to the plugin folder
param: string $component
param: int $version
param: bool $overwrite overwrite existing archive if found
return: bool

get_archived_plugin_version($component, $version)   X-Ref
Return the path to the ZIP file with the archive of the given plugin version.

param: string $component
param: int $version
return: string|bool false if not found, full path otherwise

list_plugin_folder_files($folderpath)   X-Ref
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.

param: string $folderpath full path to the plugin directory
return: array (string)relpath => (string)fullpath

get_plugin_zip_root_dir($zipfilepath)   X-Ref
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.

param: string $zipfilepath full path to the ZIP files
return: string|bool false on error

init_temp_directories()   X-Ref
Makes sure all temp directories exist and are writable.


debug($msg)   X-Ref
Raise developer debugging level message.

param: string $msg

download_plugin_zip_file($url, $tofile)   X-Ref
Download the ZIP file with the plugin package from the given location

param: string $url URL to the file
param: string $tofile full path to where to store the downloaded file
return: bool false on error

download_file_content($url, $tofile)   X-Ref
Thin wrapper for the core's download_file_content() function.

param: string $url URL to the file
param: string $tofile full path to where to store the downloaded file
return: bool

rename_extracted_rootdir($dirname, $rootdir, array $files)   X-Ref
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.

param: string $dirname fullpath location of the extracted ZIP package
param: string $rootdir the requested name of the root directory
param: array $files list of extracted files
return: array eventually amended list of extracted files

set_plugin_files_permissions($targetdir, array $files)   X-Ref
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.

param: string $targetdir full path to the directory the ZIP file was extracted to
param: array $files list of extracted files

move_extracted_plugin_files($sourcedir, $targetdir, array $files)   X-Ref
Moves the extracted contents of the plugin ZIP into the target location.

param: string $sourcedir full path to the directory the ZIP file was extracted to
param: mixed $targetdir full path to the directory where the files should be moved to
param: array $files list of extracted files