更新:上传时在 SilverStripe 中更新清单缓存版本和日期

UPDATE: Updating manifest cache version and date in SilverStripe on upload

我正在尝试将新图像文件动态附加到 SilverStripe 站点的缓存清单文件中。我有一个函数的开始,该函数当前保存着我需要缓存的所有当前文件:

class HomePage_Controller extends ContentController {

private static $allowed_actions = array (
    "UpdateManifest"
);

public function UpdateManifest() {

        $static = <<<EOT
CACHE MANIFEST
#$ManifestDate $ManifestVersion
#all javascript/jquery files currently added

#css files currently added

#image files currently added
EOT;

        $static = $static . "\n" . "/test.html";
        $this->response->addHeader("Content-type", "text/cache-manifest");
        return $static;
    }
}

我想将在 SilverStripe 管理中上传的新图像文件附加到 $static 变量。不过,我需要以某种方式构建图像标签,因为图像是从 Youtube 缩略图中提取的。我有一个名为 VideoLinkIDs 的管理模型,它有一个名为 VideoID 的字段,该字段采用 Youtube 视频 ID。我需要以某种方式找到一种方法来使用这种图像标签设置构建图像列表:

 <img class='vidThumb' src='http://img.youtube.com/vi/".VideoID."/0.jpg />

我(最终)设法在这方面取得了一些进展,这就是我所拥有的:我创建了一个 CacheManifest class 来保存当前日期和版本号变量(这些将用于更新上面 UpdateManifest 函数中 $static 变量内的版本和日期。

<?php

class CacheManifest extends DataObject {

    private static $db = array(
        'CurrentDate' => 'Date',
        'VersionNumber' => 'int'
    );

    private static $summary_fields = array(
        'ID' => 'ID',
        'CurrentDate' => 'Current Date',
        'VersionNumber' => 'Version Number'
    );

    public function updateCache(){
        $currentDate = SS_Datetime::now();
        $versionNumber = 1;

        $this->CurrentDate = $currentDate;
        $this->VersionNumber = //increment the value currently stored in the database

        $this->write();
    }
}

然后在 class 文件中为 VideoLinkID 的 onAfterWrite() 方法调用此 updateCache 函数,以便每次在管理中保存新视频 ID 时都会更新数据库:

 public function onAfterWrite(){
    $updateGallery = parent::getCMSFields();

    //get the entry from the CacheManifest database
    CacheManifest::get()->(get VersionNumber and CurrentDate from database?)
    return $updateGallery;
}

我想用 updateCache 做的是每次添加新视频 ID 时将 VersionNumber 递增 1(使用 SilverStripe 语法,我对如何做这部分有点困惑)。我不想每次上传视频时都向 CacheManifest table 添加新条目——我只想继续更新第一个条目。

编辑:CMS 和 youtube 视频 ID 之间的关系是这样的:页面模板类型有一个名为 "Video IDs" 的选项卡,内容管理员可以在其中粘贴任意数量的 youtube 视频 ID。这些 id 然后用于抓取 youtube 视频预览缩略图。这些缩略图显示在图库弹出模式中,供用户查看并单击以查看完整长度的视频。

此网站将需要一个缓存清单文件,以确保至少用户在查看图库时仍能看到 YouTube 预览缩略图。鉴于维护缓存清单的标准方法是手动更新它,这并不理想,因为开发人员需要不断更新站点的缓存清单,因为内容管理器会在 youtube 视频 ID 中添加(不知道如何很多 youtube 视频 ID 都会一直存在)

在这种情况下,动态缓存清单文件会非常有用,它会随着添加到 CMS 页面模板中的每个视频 ID 更新日期和版本号。因此存在 CacheManifest 数据对象 class。 class 在 updateCache() 函数中存储当前日期和一个整数。应该以某种方式在 VideoLinkID class.

的 onAfterWrite() 中调用此函数

None 这是我的想法,它是由另一个开发人员制定的,这似乎是我必须接受的。老实说,我宁愿找到一个更好的方法,但我还没有找到任何被认可的方法,而且我认识的其他人也没有。这一切都是全新的,绝对不是常态。

这里是您如何实现类似功能的示例。

存储视频哈希的视频对象

class GalleryVideo extends DataObject {
    private static $db = array(
        'Name' => 'Varchar(255)',
        'VideoHash'  => 'Varchar(16)'
    );

    public function getThumbLink() {
        return 'http://img.youtube.com/vi/' . $this->VideoHash . '/0.jpg';
    }
}

使用缓存清单的模板,/themes/simples/templates/GalleryPage.ss

<html manifest="gallery-appcache">
...
</html>

缓存清单页面

class GalleryCacheManifest extends SiteTree {
    private static $db = array(
        'Version' => 'Int',
    );

    private static $has_many = array(
        'Videos' => 'GalleryVideo',
    );

    public functon getCMSFields() {
        $fields = parent::getCMSFields();
        $fields->addFieldToTab('Root.Main', NumericField::create('Version'));
        $fields->addFieldToTab('Root.Main', TextField::create('Content', 'Cached files')->setRows(20));
        $fields->addFieldToTab('Root.Videos', GridField::create('Videos'));
        return $fields;
    }

    // Override actions to Save & Publish always.
    public function getCMSActions() {
        $actions = parent::getCMSActions();
        $actions->removeByName('action_save');
        return $actions;
    }

    public functon requireDefaultRecord() {
        if (0 < Versioned::get_by_stage('GalleryCacheManifest', 'Stage')->count()) {
            return;
        }

        $page = new GalleryCacheManifest();
        $page->Title = 'gallery-appcache';
        // URL in the http request is split into segments, and extension does not count, so gallery.appcache is wrong
        $page->URLSegment = 'gallery-appcache';
        $page->ShowInMenus = false;
        $page->ShowInSearch = false;
        $page->AuthorID = 1; // Default Admin
        $page->write();

        $page->publish( 'Stage', 'Live' );
        $page->flushCache();
        DB::alteration_message( 'GalleryCacheManifest created', 'created' );

    }

    public functon canCreate($member = null) {
        return 0 === Versioned::get_by_stage('GalleryCacheManifest', 'Stage')->count();
    }

    public function canDelete($member = null) {
        return false;
    }
}

class GalleryCacheManifest_Controller extends ContentController {
    public functon index(SS_HTTPRequest $request) {
        $this->getResponse()->addHeader( 'Content-Type', 'text/cache-manifest' );
    }
}

模板文件:themes/simple/templates/GalleryCacheManifest.ss:

CACHE MANIFEST
# $LastEdited.Format('Y-m-d'):$Version

CACHE:
$Content
<% if $Videos.Count %>
<% loop $Videos %>
$ThumbLink
<% end_loop %>
<% end_if %>

NETWORK:
*