omatech/editora-dbinterface

该软件包最新版本(v1.4.0)没有提供许可证信息。

使用类似于Eloquent语法的闭包,以强大而神秘的方式从Editora提取或加载信息


README

使用类似于Eloquent的闭包函数从omatech Editora提取信息

首先创建一个新实例

// Create a new database connection
$connectionParams = array(
    'dbname' => 'xxx',
    'user' => 'xxx',
    'password' => 'xxx',
    'host' => 'xxx',
    'driver' => 'pdo_mysql',
		'charset' => 'utf8mb4'
);
$conn = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, new \Doctrine\DBAL\Configuration());

// Set-up the global params of the extraction (language,...) (See the "Global Params" section for more info)
$params = [
	'lang' => 'es'
	, 'debug' => false
	, 'metadata' => true
];
	
// Instantiate the extractor using the connection and params			
$e=new Extractor($conn, $params);

使用提取器提取信息

findInstanceByIdAllElements($inst_id, $params = null, $num = null, $level = 1, callable $callback = null)

  • inst_id * (要提取的实例的id)
  • params (特定提取参数,更多信息请参阅"提取参数"和"实例参数")
  • level 要提取的关联关系数量
  • callback (闭包函数)

findInstanceById($inst_id, $params, callable $callback = null);

  • inst_id * (要提取的实例的id)
  • params (特定提取参数,更多信息请参阅"提取参数"和"实例参数")
  • callback (闭包函数)

findInstancesInClass($class, $num=null, $params=null, callable $callback = null)

  • class * (类或标签的id)
  • num (要提取的实例数量,如果没有设置,则获取该类的所有实例。使用整数不使用分页,或使用"10/2"语法给出第11到20条记录)(有关详细信息,请参阅分页部分)
  • params (特定提取参数,更多信息请参阅"提取参数"和"实例参数")
  • callback (闭包函数)

提取参数

  • order = 按顺序类实例,按顺序标准,update_date|publishing_begins|inst_id|key_fields|order_date|order_string 默认为publishing_begins
  • order_direction = 排序子句的方向,desc|asc 默认为asc

findInstancesInList($inst_ids, $num=null, $class=null, $params = null, callable $callback = null)

  • inst_ids * (逗号分隔的实例id,要提取)
  • num (要提取的实例数量,如果没有设置,则获取该类的所有实例)。使用整数不使用分页,或使用"10/2"语法给出第11到20条记录)(有关详细信息,请参阅分页部分)
  • class (通过标签或id过滤特定类)
  • params (特定提取参数,更多信息请参阅"提取参数"和"实例参数")
  • callback (闭包函数)

注意:提取顺序由列表中ID的顺序强制

findInstancesBySearch($query, $num=null, $class=null, $params = null, callable $callback = null)

  • query * (搜索词)
  • num (要提取的实例数量,如果没有设置,则获取该类的所有实例)。使用整数不使用分页,或使用"10/2"语法给出第11到20条记录)(有关详细信息,请参阅分页部分)
  • class (通过标签或id过滤特定类)
  • params (特定提取参数,更多信息请参阅"提取参数"和"实例参数")
  • callback (闭包函数)

注意:提取顺序由搜索词(查询)的相关性强制

findRelatedInstances($inst_id, $relation, $num=null, $params = null, callable $callback = null)

  • inst_id * (起始搜索的父或子实例)
  • relation * (关系的标签或id)
  • num (要提取的实例数量,如果没有设置,则获取该类的所有实例)。使用整数不使用分页,或使用"10/2"语法给出第11到20条记录)(有关详细信息,请参阅分页部分)
  • params (特定提取参数,更多信息请参阅"提取参数"和"实例参数")
  • callback (闭包函数)

提取参数

  • direction (child|parent) 允许强制关系的方向,对于关系是类的父和子的情况,如果未设置,则尝试自动查找方向
  • alias (字符串) (默认关系的标签) 要提取的关系别名

findChildrenInstances($inst_id, $relation, $num=null, $params = null, callable $callback = null)

(与findRelatedInstances相同,但强制子方向)

findParentInstances($inst_id, $relation, $num=null, $params = null, callable $callback = null)

(与findRelatedInstances相同,但强制父方向)

实例参数

实例参数影响实例本身的提取行为

  • filter (字符串) (默认为all)
    • all - 提取实例的所有属性(即使其值为null)
    • detail - 仅提取具有class_attribute.detail='Y'的属性
    • resume - 仅提取具有class_attribute.detail='N'的属性
      • only-X - 仅包含属性_type=X的值(例如仅字符串类型的S)
      • except-Y - 排除属性_type=Y的值(例如避免通常较长的K属性)
    • 字段:fieldname1|fieldname2

附加调用

findClass($class)

  • class *(通过标签或id筛选特定类)

findRelation($relation)

  • relation *(发生冲突时,可以使用下面的调用)(关系的标签或id)

findParentRelation($relation, $inst_id)

  • relation * (关系的标签或id)
  • inst_id *(从该实例开始查找父关系)

findChildRelation($relation, $inst_id)

  • relation * (关系的标签或id)
  • inst_id *(从该实例开始查找子关系)

clearExtractionCache($extraction_cache_key);

全局参数

参数的格式是一个数组中的键值对,例如

$params = ['lang' => 'es', 'debug' => false, 'metadata' => true, 'show_inmediate_debug' => true];

全局参数可以是:

  • lang(2个字符的ISO语言代码或ALL)(默认ALL)('ALL'仅用于非语言依赖属性,'es'用于西班牙语和非语言依赖属性,'en'用于英语等)
  • debug(布尔值)(默认false)启用调试,您可以使用$e->debug_messages之后检查调试消息
  • show_inmediate_debug(布尔值)(默认false)如果启用调试,则直接输出调试消息并将其存储在debug_messages中
  • metadata(布尔值)(默认false)启用元数据输出,例如状态、发布开始时间、发布结束时间等信息
  • default_limit(整数)(默认10000)- 当调用中未设置限制时设置默认限制
  • cache_expiration(整数)(默认3600秒(1小时))缓存实例过期时间
  • avoid_cache(布尔值)(默认false)防止使用缓存系统
  • preview(布尔值)(默认false)将提取器设置为预览模式
  • preview_date(字符串mysql日期)(默认"NOW()")设置提取器的preview_date,仅在preview=true时才有意义,例如预览2020-01-01的提取
  • extract_values(布尔值)(默认true)提取实例的值(即使为null)
  • sql_select_instances(mysql选择字符串)(默认:select i.*, c.name class_name, c.tag class_tag, c.id class_id, i.key_fields nom_intern, i.update_date, ifnull(unix_timestamp(i.update_date),0) update_timestamp)
  • timings(布尔值)默认false显示每个提取的开始和结束以及总毫秒数,仅在metadata为true时才有意义
  • extraction_cache_key提取缓存键,例如'menu'或'footer'或'countries'
  • extraction_cache_expiration可选,设置提取缓存的过期时间,如果没有设置则使用cache_expiration
  • default_language_to_remove_from_url,设置从每个实例链接中删除的语言,例如如果default_language_to_remove_from_url='es',则通常由提取器生成的链接'/es/nice-url-in-spanish-instance'将被替换为'/nice-url-in-spanish-instance',仅当设置为两个字符的语言代码时才有效,例如'es'或'en'

使用可调用的参数合并提取

    $result = $extractor->findInstanceById($id, $params, function ($i) use ($extractor){
        $blocks = $extractor->findChildrenInstances($i, "blocks", null, null, function ($i) use ($extractor){
            $boxes = $extractor->findChildrenInstances($i, "boxes", null, null, null);
            return array_merge($boxes);
        });
        return array_merge($blocks);
    });

缓存如何工作?

默认情况下,所有从CMS提取的实例都缓存在memcache中,要避免这种行为,您必须设置avoid_cache参数。

实例本身在memcache中以以下键保存:DATABASE_NAME:dbinterface:LANGUAGE:INST_ID:FILTER

实例缓存默认过期时间为3600秒,但可以使用cache_expiration参数更改。

如果CMS中缓存的实例被修改,缓存将自动过期,update_date列是实例缓存过期的时间参考

如果您需要在提取级别使用缓存,可以使用内置的提取缓存系统。

提取直到提取_cache_expiration秒后才会被缓存,因此您需要负责清除缓存或设置足够短的时间,以避免从CMS获取过时内容。

提取在memcache中以以下键保存:DATABASE_NAME::extractor_cache:EXTRACTION_CACHE_KEY:LANGUAGE

使用函数$e->clearExtractionCache(extraction_cache_key)清除特定键(与您使用的extraction_cache_key相同)

分页

在提取器的核心调用中,您可以在 $num 参数中使用分页语法,例如

  • 使用 10/3 提取每页 10 个元素的第三页实例
  • 使用 50/1 提取每页 50 个元素的第一页

如何获取使用分页后的更多信息?

使用 getPaginator 调用获取有关分页结果的全部相关信息。

getPaginator ($prefix='', $postfix='')

  • prefix 是生成分页结果中 URL 的前缀
  • postfix 是生成分页结果中 URL 的后缀

例如

$paginator=$extractor->getPaginator('/ca/news/', '?utm_source=xxx');

这将生成以下输出(假设最后分页选项是 10/3,即每页 10 个元素的第三页,总记录数为 51)

$paginator=[
'lastPage'=>false
,'firstPage'=>false
,'hasMorePages'=>true
,'nextPage'=>4
,'previousPage'=>2
,'onFirstPage'=>false
,'currentPage'=>3
'elements'=>[
	1=>['url'=>'/ca/news/1?utm_source=xxx'
		, 'isFirst'=>true
		, 'isLast'=>false
		, 'isCurrent=>false
	]
	2=>['url'=>'/ca/news/2?utm_source=xxx'
		, 'isFirst'=>false
		, 'isLast'=>false
		, 'isCurrent=>false
	]
	3=>['url'=>'/ca/news/3?utm_source=xxx'
		, 'isFirst'=>false
		, 'isLast'=>false
		, 'isCurrent=>true
	]
	4=>['url'=>'/ca/news/4?utm_source=xxx'
		, 'isFirst'=>false
		, 'isLast'=>false
		, 'isCurrent=>false
	]
	5=>['url'=>'/ca/news/5?utm_source=xxx'
		, 'isFirst'=>false
		, 'isLast'=>false
		, 'isCurrent=>false
	]
	6=>['url'=>'/ca/news/6?utm_source=xxx'
		, 'isFirst'=>false
		, 'isLast'=>true
		, 'isCurrent=>false
	]
]
];

从给定的提取中获取最后更新的时间戳

$extractor->last_updated_timestamp; 返回所有提取元素的最新更新 UNIX 时间戳

测试提取器

  1. 如果不存在,则创建一个具有 utf8_mb4 和 collate utf8_mb4_general_ci 的数据库,例如在本例中假设 editora_test 在 localhost 上,我们将假设用户为 root 且没有密码
  2. 进入命令文件夹

cd Commands

  1. 生成 editora 结构(请记住更改您的数据库连接参数)

php generate-editora.php --from=file --inputformat=array --inputfile=../data/sample_editora_array.php --to=db4 --dbtohost=localhost --dbtouser=root --dbtopass= --dbtoname=editora_test

该命令将输出 2 个用户和随机密码,例如

新用户:omatech,密码 eJjZQU&5 新用户:test,密码 6r4!QBPB

  1. 用假内容填充 editora

php fake-content.php --to=db4 --dbhost=localhost --dbuser=root --dbpass= --dbname=editora_test --num_instances=6

该命令将输出类似以下的内容

创建内容:126 个实例,959 个属性,以及 126 个关系实例,批量 ID=1533716151 创建

注意:如果您以后想删除这些假数据,可以使用

php remove-content.php --to=db4 --dbhost=localhost --dbuser=root --dbpass= --dbname=editora_test --batch_id=1533716151

  1. 进入项目的根文件夹

cd ..

  1. 运行测试以查看一切是否正常!

在 Windows 中

phpunit ./tests/Omatech/Editora/Extractor/ExtractorTest

在 Linux 中

vendor/bin/phpunit ./tests/Omatech/Editora/Extractor/ExtractorTest

Editora-Loader

比较方法

relationInstanceExist($rel_id, $parent_inst_id, $child_inst_id)

existsInstanceWithExternalID($class_id, $external_id)

ExistingInstanceIsDifferent($inst_id, $nom_intern, $values, $status = 'O', &$difference, &$attr_difference)

	// -1 instance not exist
	// -2 status is different
	// -3 nom_intern is different
	// -4 some value is different
	// -5 some value not exists in current instance
	// 0 same!

插入/更新方法

insertRelationInstance($rel_id, $parent_inst_id, $child_inst_id, $external_id = null, $batch_id = null)

updateInstance($inst_id, $nom_intern, $values, $status = 'O', $publishing_begins = null, $publishing_ends = null)

insertInstanceWithExternalID($class_id, $nom_intern, $external_id, $batch_id, $values, $status = 'O', $publishing_begins = null, $publishing_ends = null, $creation_date = 'now()', $update_date = 'now()') {

insertInstance($class_id, $nom_intern, $values, $status = 'O', $publishing_begins = null, $publishing_ends = null)

insertInstanceForcingID($inst_id, $class_id, $nom_intern, $values, $status = 'O', $publishing_begins = null, $publishing_ends = null)

updateUrlNice($nice_url, $inst_id, $language)

insertUrlNice($nice_url, $inst_id, $language)

删除方法

deleteInstance ($inst_id)

  • inst_id * (要删除的实例)

deleteInstancesWithExternalID ($external_id, $class_id)

deleteRelationInstancesWithExternalID($external_id, $rel_id)

delete_instance($inst_id)

deleteInstancesInBatch($batch_id)

deleteRelationInstancesInBatch($batch_id)

deleteRelationInstance($id)

命令

Editora 结构

generate-editora.php

从数组或 json 文件生成 editora 结构

reverse-engineer-editora.php

提取 editora 结构并生成兼容的生成器文件

compare-editoras.php

比较两个本地数据库中的editora结构,获取它们之间的差异报告

modernize.php

将editora数据库现代化,包括最新的数据库结构更改

regenerate-passwords.php

使用包含大写字母、小写字母、符号和数字的字符串重置数据库中的所有密码

内容操作

fake-content.php

用假内容填充editora CMS

export-content.php

将所有editora内容导出到文件或输入(json或序列化数组)

import-content.php

从文件或输入(json或序列化数组)导入所有editora内容

remove-content.php

删除所有带有指定batch_id的内容或删除previous_data标志存在时除了全局和主页之外的所有内容

翻译

export-translation.php

将editora数据库中一种语言下的字符串导出到Excel文件或输出

import-translation.php

将Excel、json文件或输入中的字符串导入到editora数据库

实验性

data-transfer.php

使用data_transfer.php提取editora4信息并将其转换为editora5格式

translate.php

使用Google翻译API自动翻译内容

migrator.php

使用migrator.php提取editora4信息并将其转换为editora5格式,直接传输到db5数据库或生成3种不同的文件格式(完整、最小、生成器)

export-allinstances-mongo

将所有实例导出到输出或json格式的文件中。

属性类型

  • A: WYSIWYG文本区域
  • B: 短字符串,用于对实例进行排序
  • C: 代码文本区域
  • D: 日期
  • E: 日期,用于对实例进行排序
  • F: 文件
  • G: Flash文件
  • H: 网格图像
  • I: 图像
  • K: CKEDITOR文本区域
  • L: 查找
  • M: 地理位置(Google地图)
  • N: 数字
  • O: 颜色选择器
  • R: 关系
  • S: 短字符串
  • T: HTML文本区域
  • U: URL
  • W: APP类型
  • X: XML
  • Y: 视频(YouTube或Vimeo)
  • Z: 美观的URL

类图

作者

  • Agusti Pons
  • Christian Bohollo
  • Javier Mogollon
  • Hector Arnau
  • Alvaro Aguilar
  • Cesc Delgado

许可证

此项目受MIT许可证许可