当前位置: 首页>>代码示例>>PHP>>正文


PHP stored_file::get_referencelastsync方法代码示例

本文整理汇总了PHP中stored_file::get_referencelastsync方法的典型用法代码示例。如果您正苦于以下问题:PHP stored_file::get_referencelastsync方法的具体用法?PHP stored_file::get_referencelastsync怎么用?PHP stored_file::get_referencelastsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在stored_file的用法示例。


在下文中一共展示了stored_file::get_referencelastsync方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。

示例1: sync_reference

 public function sync_reference(stored_file $file)
 {
     global $CFG;
     if ($file->get_referencelastsync() + DAYSECS > time()) {
         // Synchronise not more often than once a day.
         return false;
     }
     $ref = unserialize($file->get_reference());
     if (!isset($ref->url)) {
         // this is an old-style reference in DB. We need to fix it
         $ref = unserialize($this->fix_old_style_reference($file->get_reference()));
     }
     if (!isset($ref->url)) {
         return false;
     }
     $c = new curl();
     $url = $this->get_file_download_link($ref->url);
     if (file_extension_in_typegroup($ref->path, 'web_image')) {
         $saveas = $this->prepare_file('');
         try {
             $result = $c->download_one($url, array(), array('filepath' => $saveas, 'timeout' => $CFG->repositorysyncimagetimeout, 'followlocation' => true));
             $info = $c->get_info();
             if ($result === true && isset($info['http_code']) && $info['http_code'] == 200) {
                 $fs = get_file_storage();
                 list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($saveas);
                 $file->set_synchronized($contenthash, $filesize);
                 return true;
             }
         } catch (Exception $e) {
         }
     }
     $c->get($url, null, array('timeout' => $CFG->repositorysyncimagetimeout, 'followlocation' => true, 'nobody' => true));
     $info = $c->get_info();
     if (isset($info['http_code']) && $info['http_code'] == 200 && array_key_exists('download_content_length', $info) && $info['download_content_length'] >= 0) {
         $filesize = (int) $info['download_content_length'];
         $file->set_synchronized(null, $filesize);
         return true;
     }
     $file->set_missingsource();
     return true;
 }
开发者ID:krysnuvadga,项目名称:moodle,代码行数:41,代码来源:lib.php

示例2: sync_reference

    /**
     * Performs synchronisation of an external file if the previous one has expired.
     *
     * This function must be implemented for external repositories supporting
     * FILE_REFERENCE, it is called for existing aliases when their filesize,
     * contenthash or timemodified are requested. It is not called for internal
     * repositories (see {@link repository::has_moodle_files()}), references to
     * internal files are updated immediately when source is modified.
     *
     * Referenced files may optionally keep their content in Moodle filepool (for
     * thumbnail generation or to be able to serve cached copy). In this
     * case both contenthash and filesize need to be synchronized. Otherwise repositories
     * should use contenthash of empty file and correct filesize in bytes.
     *
     * Note that this function may be run for EACH file that needs to be synchronised at the
     * moment. If anything is being downloaded or requested from external sources there
     * should be a small timeout. The synchronisation is performed to update the size of
     * the file and/or to update image and re-generated image preview. There is nothing
     * fatal if syncronisation fails but it is fatal if syncronisation takes too long
     * and hangs the script generating a page.
     *
     * Note: If you wish to call $file->get_filesize(), $file->get_contenthash() or
     * $file->get_timemodified() make sure that recursion does not happen.
     *
     * Called from {@link stored_file::sync_external_file()}
     *
     * @uses stored_file::set_missingsource()
     * @uses stored_file::set_synchronized()
     * @param stored_file $file
     * @return bool false when file does not need synchronisation, true if it was synchronised
     */
    public function sync_reference(stored_file $file) {
        if ($file->get_repository_id() != $this->id) {
            // This should not really happen because the function can be called from stored_file only.
            return false;
        }

        if ($this->has_moodle_files()) {
            // References to local files need to be synchronised only once.
            // Later they will be synchronised automatically when the source is changed.
            if ($file->get_referencelastsync()) {
                return false;
            }
            $fs = get_file_storage();
            $params = file_storage::unpack_reference($file->get_reference(), true);
            if (!is_array($params) || !($storedfile = $fs->get_file($params['contextid'],
                    $params['component'], $params['filearea'], $params['itemid'], $params['filepath'],
                    $params['filename']))) {
                $file->set_missingsource();
            } else {
                $file->set_synchronized($storedfile->get_contenthash(), $storedfile->get_filesize());
            }
            return true;
        }

        // Backward compatibility (Moodle 2.3-2.5) implementation that calls
        // methods repository::get_reference_file_lifetime(), repository::sync_individual_file()
        // and repository::get_file_by_reference(). These methods are removed from the
        // base repository class but may still be implemented by the child classes.

        // THIS IS NOT A GOOD EXAMPLE of implementation. For good examples see the overwriting methods.

        if (!method_exists($this, 'get_file_by_reference')) {
            // Function get_file_by_reference() is not implemented. No synchronisation.
            return false;
        }

        // Check if the previous sync result is still valid.
        if (method_exists($this, 'get_reference_file_lifetime')) {
            $lifetime = $this->get_reference_file_lifetime($file->get_reference());
        } else {
            // Default value that was hardcoded in Moodle 2.3 - 2.5.
            $lifetime =  60 * 60 * 24;
        }
        if (($lastsynced = $file->get_referencelastsync()) && $lastsynced + $lifetime >= time()) {
            return false;
        }

        $cache = cache::make('core', 'repositories');
        if (($lastsyncresult = $cache->get('sync:'.$file->get_referencefileid())) !== false) {
            if ($lastsyncresult === true) {
                // We are in the process of synchronizing this reference.
                // Avoid recursion when calling $file->get_filesize() and $file->get_contenthash().
                return false;
            } else {
                // We have synchronised the same reference inside this request already.
                // It looks like the object $file was created before the synchronisation and contains old data.
                if (!empty($lastsyncresult['missing'])) {
                    $file->set_missingsource();
                } else {
                    $cache->set('sync:'.$file->get_referencefileid(), true);
                    if ($file->get_contenthash() != $lastsyncresult['contenthash'] ||
                            $file->get_filesize() != $lastsyncresult['filesize']) {
                        $file->set_synchronized($lastsyncresult['contenthash'], $lastsyncresult['filesize']);
                    }
                    $cache->set('sync:'.$file->get_referencefileid(), $lastsyncresult);
                }
                return true;
            }
        }
//.........这里部分代码省略.........
开发者ID:educacionbe,项目名称:cursos,代码行数:101,代码来源:lib.php

示例3: sync_reference

 /**
  * Performs synchronisation of an external file if the previous one has expired.
  *
  * This function must be implemented for external repositories supporting
  * FILE_REFERENCE, it is called for existing aliases when their filesize,
  * contenthash or timemodified are requested. It is not called for internal
  * repositories (see {@link repository::has_moodle_files()}), references to
  * internal files are updated immediately when source is modified.
  *
  * Referenced files may optionally keep their content in Moodle filepool (for
  * thumbnail generation or to be able to serve cached copy). In this
  * case both contenthash and filesize need to be synchronized. Otherwise repositories
  * should use contenthash of empty file and correct filesize in bytes.
  *
  * Note that this function may be run for EACH file that needs to be synchronised at the
  * moment. If anything is being downloaded or requested from external sources there
  * should be a small timeout. The synchronisation is performed to update the size of
  * the file and/or to update image and re-generated image preview. There is nothing
  * fatal if syncronisation fails but it is fatal if syncronisation takes too long
  * and hangs the script generating a page.
  *
  * Note: If you wish to call $file->get_filesize(), $file->get_contenthash() or
  * $file->get_timemodified() make sure that recursion does not happen.
  *
  * Called from {@link stored_file::sync_external_file()}
  *
  * @uses stored_file::set_missingsource()
  * @uses stored_file::set_synchronized()
  * @param stored_file $file
  * @return bool false when file does not need synchronisation, true if it was synchronised
  */
 public function sync_reference(stored_file $file)
 {
     if ($file->get_repository_id() != $this->id) {
         // This should not really happen because the function can be called from stored_file only.
         return false;
     }
     if ($this->has_moodle_files()) {
         // References to local files need to be synchronised only once.
         // Later they will be synchronised automatically when the source is changed.
         if ($file->get_referencelastsync()) {
             return false;
         }
         $fs = get_file_storage();
         $params = file_storage::unpack_reference($file->get_reference(), true);
         if (!is_array($params) || !($storedfile = $fs->get_file($params['contextid'], $params['component'], $params['filearea'], $params['itemid'], $params['filepath'], $params['filename']))) {
             $file->set_missingsource();
         } else {
             $file->set_synchronized($storedfile->get_contenthash(), $storedfile->get_filesize(), 0, $storedfile->get_timemodified());
         }
         return true;
     }
     return false;
 }
开发者ID:isuruAb,项目名称:moodle,代码行数:54,代码来源:lib.php

示例4: import_external_file_contents

 /**
  * Downloads the file from external repository and saves it in moodle filepool.
  * This function is different from {@link repository::sync_external_file()} because it has
  * bigger request timeout and always downloads the content.
  *
  * This function is invoked when we try to unlink the file from the source and convert
  * a reference into a true copy.
  *
  * @throws exception when file could not be imported
  *
  * @param stored_file $file
  * @param int $maxbytes throw an exception if file size is bigger than $maxbytes (0 means no limit)
  */
 public function import_external_file_contents(stored_file $file, $maxbytes = 0)
 {
     if (!$file->is_external_file()) {
         // nothing to import if the file is not a reference
         return;
     } else {
         if ($file->get_repository_id() != $this->id) {
             // error
             debugging('Repository instance id does not match');
             return;
         } else {
             if ($this->has_moodle_files()) {
                 // files that are references to local files are already in moodle filepool
                 // just validate the size
                 if ($maxbytes > 0 && $file->get_filesize() > $maxbytes) {
                     throw new file_exception('maxbytes');
                 }
                 return;
             } else {
                 if ($maxbytes > 0 && $file->get_filesize() > $maxbytes) {
                     // note that stored_file::get_filesize() also calls synchronisation
                     throw new file_exception('maxbytes');
                 }
                 $fs = get_file_storage();
                 $contentexists = $fs->content_exists($file->get_contenthash());
                 if ($contentexists && $file->get_filesize() && $file->get_contenthash() === sha1('')) {
                     // even when 'file_storage::content_exists()' returns true this may be an empty
                     // content for the file that was not actually downloaded
                     $contentexists = false;
                 }
                 $now = time();
                 if ($file->get_referencelastsync() + $file->get_referencelifetime() >= $now && !$file->get_status() && $contentexists) {
                     // we already have the content in moodle filepool and it was synchronised recently.
                     // Repositories may overwrite it if they want to force synchronisation anyway!
                     return;
                 } else {
                     // attempt to get a file
                     try {
                         $fileinfo = $this->get_file($file->get_reference());
                         if (isset($fileinfo['path'])) {
                             list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($fileinfo['path']);
                             // set this file and other similar aliases synchronised
                             $lifetime = $this->get_reference_file_lifetime($file->get_reference());
                             $file->set_synchronized($contenthash, $filesize, 0, $lifetime);
                         } else {
                             throw new moodle_exception('errorwhiledownload', 'repository', '', '');
                         }
                     } catch (Exception $e) {
                         if ($contentexists) {
                             // better something than nothing. We have a copy of file. It's sync time
                             // has expired but it is still very likely that it is the last version
                         } else {
                             throw $e;
                         }
                     }
                 }
             }
         }
     }
 }
开发者ID:saurabh947,项目名称:MoodleLearning,代码行数:73,代码来源:lib.php

示例5: sync_reference

 /**
  * Synchronize the references.
  *
  * @param stored_file $file Stored file.
  * @return boolean
  */
 public function sync_reference(stored_file $file)
 {
     if ($file->get_referencelastsync() + DAYSECS > time()) {
         // Synchronise not more often than once a day.
         return false;
     }
     $c = new curl();
     $reference = unserialize(self::convert_to_valid_reference($file->get_reference()));
     $url = $reference->downloadurl;
     if (file_extension_in_typegroup($file->get_filename(), 'web_image')) {
         $path = $this->prepare_file('');
         $result = $c->download_one($url, null, array('filepath' => $path, 'timeout' => $CFG->repositorysyncimagetimeout));
         $info = $c->get_info();
         if ($result === true && isset($info['http_code']) && $info['http_code'] == 200) {
             $fs = get_file_storage();
             list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($path);
             $file->set_synchronized($contenthash, $filesize);
             return true;
         }
     }
     $c->get($url, null, array('timeout' => $CFG->repositorysyncimagetimeout, 'followlocation' => true, 'nobody' => true));
     $info = $c->get_info();
     if (isset($info['http_code']) && $info['http_code'] == 200 && array_key_exists('download_content_length', $info) && $info['download_content_length'] >= 0) {
         $filesize = (int) $info['download_content_length'];
         $file->set_synchronized(null, $filesize);
         return true;
     }
     $file->set_missingsource();
     return true;
 }
开发者ID:covex-nn,项目名称:moodle,代码行数:36,代码来源:lib.php

示例6: sync_reference

 public function sync_reference(stored_file $file)
 {
     if ($file->get_referencelastsync() + 60 > time()) {
         // Does not cost us much to synchronise within our own filesystem, check every 1 minute.
         return false;
     }
     static $issyncing = false;
     if ($issyncing) {
         // Avoid infinite recursion when calling $file->get_filesize() and get_contenthash().
         return false;
     }
     $filepath = $this->get_rootpath() . ltrim($file->get_reference(), '/');
     if ($this->is_in_repository($file->get_reference()) && file_exists($filepath) && is_readable($filepath)) {
         $fs = get_file_storage();
         $issyncing = true;
         if (file_extension_in_typegroup($filepath, 'web_image')) {
             $contenthash = sha1_file($filepath);
             if ($file->get_contenthash() == $contenthash) {
                 // File did not change since the last synchronisation.
                 $filesize = filesize($filepath);
             } else {
                 // Copy file into moodle filepool (used to generate an image thumbnail).
                 list($contenthash, $filesize, $newfile) = $fs->add_file_to_pool($filepath);
             }
         } else {
             // Update only file size so file will NOT be copied into moodle filepool.
             $emptyfile = $contenthash = sha1('');
             $currentcontenthash = $file->get_contenthash();
             if ($currentcontenthash !== $emptyfile && $currentcontenthash === sha1_file($filepath)) {
                 // File content was synchronised and has not changed since then, leave it.
                 $contenthash = null;
             }
             $filesize = filesize($filepath);
         }
         $issyncing = false;
         $file->set_synchronized($contenthash, $filesize);
     } else {
         $file->set_missingsource();
     }
     return true;
 }
开发者ID:Hirenvaghasiya,项目名称:moodle,代码行数:41,代码来源:lib.php

示例7: sync_reference

 /**
  * Performs synchronisation of an external file if the previous one has expired.
  *
  * This function must be implemented for external repositories supporting
  * FILE_REFERENCE, it is called for existing aliases when their filesize,
  * contenthash or timemodified are requested. It is not called for internal
  * repositories (see {@link repository::has_moodle_files()}), references to
  * internal files are updated immediately when source is modified.
  *
  * Referenced files may optionally keep their content in Moodle filepool (for
  * thumbnail generation or to be able to serve cached copy). In this
  * case both contenthash and filesize need to be synchronized. Otherwise repositories
  * should use contenthash of empty file and correct filesize in bytes.
  *
  * Note that this function may be run for EACH file that needs to be synchronised at the
  * moment. If anything is being downloaded or requested from external sources there
  * should be a small timeout. The synchronisation is performed to update the size of
  * the file and/or to update image and re-generated image preview. There is nothing
  * fatal if syncronisation fails but it is fatal if syncronisation takes too long
  * and hangs the script generating a page.
  *
  * Note: If you wish to call $file->get_filesize(), $file->get_contenthash() or
  * $file->get_timemodified() make sure that recursion does not happen.
  *
  * Called from {@link stored_file::sync_external_file()}
  *
  * @inheritDocs
  */
 public function sync_reference(stored_file $file)
 {
     global $CFG;
     if ($file->get_referencelastsync() + DAYSECS > time()) {
         // Only synchronise once per day.
         return false;
     }
     $reference = $this->unpack_reference($file->get_reference());
     if (!isset($reference->url)) {
         // The URL to sync with is missing.
         return false;
     }
     $c = new curl();
     $url = $this->get_file_download_link($reference->url);
     if (file_extension_in_typegroup($reference->path, 'web_image')) {
         $saveas = $this->prepare_file('');
         try {
             $result = $c->download_one($url, [], ['filepath' => $saveas, 'timeout' => $CFG->repositorysyncimagetimeout, 'followlocation' => true]);
             $info = $c->get_info();
             if ($result === true && isset($info['http_code']) && $info['http_code'] == 200) {
                 $fs = get_file_storage();
                 list($contenthash, $filesize, ) = $fs->add_file_to_pool($saveas);
                 $file->set_synchronized($contenthash, $filesize);
                 return true;
             }
         } catch (Exception $e) {
             // IF the download_one fails, we will attempt to download
             // again with get() anyway.
         }
     }
     $c->get($url, null, array('timeout' => $CFG->repositorysyncimagetimeout, 'followlocation' => true, 'nobody' => true));
     $info = $c->get_info();
     if (isset($info['http_code']) && $info['http_code'] == 200 && array_key_exists('download_content_length', $info) && $info['download_content_length'] >= 0) {
         $filesize = (int) $info['download_content_length'];
         $file->set_synchronized(null, $filesize);
         return true;
     }
     $file->set_missingsource();
     return true;
 }
开发者ID:dg711,项目名称:moodle,代码行数:68,代码来源:lib.php


注:本文中的stored_file::get_referencelastsync方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。