Proyectos de Subversion Moodle

Rev

Rev 1 | Mostrar el archivo completo | | | Autoría | Ultima modificación | Ver Log |

Rev 1 Rev 1441
Línea 12... Línea 12...
12
// GNU General Public License for more details.
12
// GNU General Public License for more details.
13
//
13
//
14
// You should have received a copy of the GNU General Public License
14
// You should have received a copy of the GNU General Public License
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
15
// along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
Línea 16... Línea -...
16
 
-
 
17
 
16
 
18
/**
17
/**
19
 * Core file storage class definition.
18
 * Core file storage class definition.
20
 *
19
 *
21
 * @package   core_files
20
 * @package   core_files
22
 * @copyright 2008 Petr Skoda {@link http://skodak.org}
21
 * @copyright 2008 Petr Skoda {@link http://skodak.org}
23
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
22
 * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
Línea 24... Línea 23...
24
 */
23
 */
Línea -... Línea 24...
-
 
24
 
-
 
25
defined('MOODLE_INTERNAL') || die();
25
 
26
 
Línea 26... Línea 27...
26
defined('MOODLE_INTERNAL') || die();
27
use core_files\hook\before_file_created;
27
 
28
 
28
require_once("$CFG->libdir/filestorage/stored_file.php");
29
require_once("$CFG->libdir/filestorage/stored_file.php");
Línea 913... Línea 914...
913
     *      in the query like WHERE itemid $itemidstest. Must used named parameters,
914
     *      in the query like WHERE itemid $itemidstest. Must used named parameters,
914
     *      and may not used named parameters called contextid, component or filearea.
915
     *      and may not used named parameters called contextid, component or filearea.
915
     * @param array $params any query params used by $itemidstest.
916
     * @param array $params any query params used by $itemidstest.
916
     */
917
     */
917
    public function delete_area_files_select($contextid, $component,
918
    public function delete_area_files_select($contextid, $component,
918
            $filearea, $itemidstest, array $params = null) {
919
            $filearea, $itemidstest, ?array $params = null) {
919
        global $DB;
920
        global $DB;
Línea 920... Línea 921...
920
 
921
 
921
        $where = "contextid = :contextid
922
        $where = "contextid = :contextid
922
                AND component = :component
923
                AND component = :component
Línea 1073... Línea 1074...
1073
    protected function create_file($newrecord) {
1074
    protected function create_file($newrecord) {
1074
        global $DB;
1075
        global $DB;
1075
        $newrecord->id = $DB->insert_record('files', $newrecord);
1076
        $newrecord->id = $DB->insert_record('files', $newrecord);
Línea 1076... Línea 1077...
1076
 
1077
 
1077
        if ($newrecord->filename !== '.') {
-
 
1078
            // Callback for file created.
-
 
1079
            if ($pluginsfunction = get_plugins_with_function('after_file_created')) {
-
 
1080
                foreach ($pluginsfunction as $plugintype => $plugins) {
1078
        if ($newrecord->filename !== '.') {
1081
                    foreach ($plugins as $pluginfunction) {
-
 
1082
                        $pluginfunction($newrecord);
1079
            if (defined('PHPUNIT_TEST') && PHPUNIT_TEST) {
1083
                    }
-
 
1084
                }
1080
                return;
-
 
1081
            }
-
 
1082
 
-
 
1083
            // The $fileinstance is needed for the legacy callback.
-
 
1084
            $fileinstance = $this->get_file_instance($newrecord);
-
 
1085
            // Dispatch the new Hook implementation immediately after the legacy callback.
-
 
1086
            $hook = new \core_files\hook\after_file_created($fileinstance, $newrecord);
-
 
1087
            $hook->process_legacy_callbacks();
1085
            }
1088
            \core\di::get(\core\hook\manager::class)->dispatch($hook);
1086
        }
1089
        }
Línea 1087... Línea 1090...
1087
    }
1090
    }
1088
 
1091
 
Línea 1219... Línea 1222...
1219
     * @param string $url the URL to the file
1222
     * @param string $url the URL to the file
1220
     * @param array $options {@link download_file_content()} options
1223
     * @param array $options {@link download_file_content()} options
1221
     * @param bool $usetempfile use temporary file for download, may prevent out of memory problems
1224
     * @param bool $usetempfile use temporary file for download, may prevent out of memory problems
1222
     * @return stored_file
1225
     * @return stored_file
1223
     */
1226
     */
1224
    public function create_file_from_url($filerecord, $url, array $options = null, $usetempfile = false) {
1227
    public function create_file_from_url($filerecord, $url, ?array $options = null, $usetempfile = false) {
Línea 1225... Línea 1228...
1225
 
1228
 
1226
        $filerecord = (array)$filerecord;  // Do not modify the submitted record, this cast unlinks objects.
1229
        $filerecord = (array)$filerecord;  // Do not modify the submitted record, this cast unlinks objects.
Línea 1227... Línea 1230...
1227
        $filerecord = (object)$filerecord; // We support arrays too.
1230
        $filerecord = (object)$filerecord; // We support arrays too.
Línea 1484... Línea 1487...
1484
            $newrecord->mimetype = $filerecord->mimetype;
1487
            $newrecord->mimetype = $filerecord->mimetype;
1485
        }
1488
        }
Línea 1486... Línea 1489...
1486
 
1489
 
Línea -... Línea 1490...
-
 
1490
        $newrecord->pathnamehash = $this->get_pathname_hash($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename);
-
 
1491
 
-
 
1492
        if (!empty($filerecord->repositoryid)) {
-
 
1493
            $newrecord->referencefileid = $this->get_or_create_referencefileid($filerecord->repositoryid, $filerecord->reference);
1487
        $newrecord->pathnamehash = $this->get_pathname_hash($newrecord->contextid, $newrecord->component, $newrecord->filearea, $newrecord->itemid, $newrecord->filepath, $newrecord->filename);
1494
        }
1488
 
1495
 
1489
        try {
1496
        try {
1490
            $this->create_file($newrecord);
1497
            $this->create_file($newrecord);
1491
        } catch (dml_exception $e) {
1498
        } catch (dml_exception $e) {
Línea 1831... Línea 1838...
1831
     * @param string|null $contenthash sha1 hash of content if known (performance only)
1838
     * @param string|null $contenthash sha1 hash of content if known (performance only)
1832
     * @param stdClass|null $newrecord New file record
1839
     * @param stdClass|null $newrecord New file record
1833
     * @return array (contenthash, filesize, newfile)
1840
     * @return array (contenthash, filesize, newfile)
1834
     */
1841
     */
1835
    public function add_file_to_pool($pathname, $contenthash = null, $newrecord = null) {
1842
    public function add_file_to_pool($pathname, $contenthash = null, $newrecord = null) {
-
 
1843
        $hook = new before_file_created(
-
 
1844
            filerecord: $newrecord,
-
 
1845
            filepath: $pathname,
-
 
1846
        );
-
 
1847
 
-
 
1848
        $hook->process_legacy_callbacks();
1836
        $this->call_before_file_created_plugin_functions($newrecord, $pathname);
1849
        \core\di::get(\core\hook\manager::class)->dispatch($hook);
-
 
1850
 
-
 
1851
        if ($hook->has_changed()) {
-
 
1852
            $contenthash = null;
-
 
1853
            $pathname = $hook->get_filepath();
-
 
1854
        }
-
 
1855
 
1837
        return $this->filesystem->add_file_from_path($pathname, $contenthash);
1856
        return $this->filesystem->add_file_from_path($pathname, $contenthash);
1838
    }
1857
    }
Línea 1839... Línea 1858...
1839
 
1858
 
1840
    /**
1859
    /**
1841
     * Add string content to sha1 pool.
1860
     * Add string content to sha1 pool.
1842
     *
1861
     *
1843
     * @param string $content file content - binary string
1862
     * @param string $content file content - binary string
1844
     * @return array (contenthash, filesize, newfile)
1863
     * @return array (contenthash, filesize, newfile)
1845
     */
1864
     */
-
 
1865
    public function add_string_to_pool($content, $newrecord = null) {
1846
    public function add_string_to_pool($content, $newrecord = null) {
1866
        if ($content !== null) {
1847
        $this->call_before_file_created_plugin_functions($newrecord, null, $content);
1867
            // This is a directory and there is no record information.
-
 
1868
            $hook = new before_file_created(
-
 
1869
                filerecord: $newrecord,
1848
        return $this->filesystem->add_file_from_string($content);
1870
                filecontent: $content,
Línea 1849... Línea -...
1849
    }
-
 
1850
 
1871
            );
1851
    /**
-
 
1852
     * before_file_created hook.
1872
 
1853
     *
-
 
1854
     * @param stdClass|null $newrecord New file record.
-
 
1855
     * @param string|null $pathname Path to file.
1873
            $hook->process_legacy_callbacks();
1856
     * @param string|null $content File content.
-
 
1857
     */
-
 
1858
    protected function call_before_file_created_plugin_functions($newrecord, $pathname = null, $content = null) {
-
 
1859
        $pluginsfunction = get_plugins_with_function('before_file_created');
1874
            \core\di::get(\core\hook\manager::class)->dispatch($hook);
1860
        foreach ($pluginsfunction as $plugintype => $plugins) {
1875
 
1861
            foreach ($plugins as $pluginfunction) {
1876
            if ($hook->has_changed()) {
1862
                $pluginfunction($newrecord, ['pathname' => $pathname, 'content' => $content]);
1877
                $content = $hook->get_filecontent();
-
 
1878
            }
-
 
1879
        }
1863
            }
1880
 
Línea 1864... Línea 1881...
1864
        }
1881
        return $this->filesystem->add_file_from_string($content);
1865
    }
1882
    }
1866
 
1883