스키마 튜토리얼레슨 6: 검색, 교체 후 재저장
레슨 6: 검색, 교체 후 재저장
이 튜토리얼 레슨에서는 검색 및 교체를 수행하고 리소스를 DB에 다시 저장하는 콘텐츠 변환 예제를 소개합니다.
PHP Functions via Schema 확장 기능은 다음과 같은 "검색 및 교체" 필드를 제공합니다:
_strReplace: 문자열을 다른 문자열로 교체합니다_strReplaceMultiple: 문자열 목록을 다른 문자열 목록으로 교체합니다_strRegexReplace: 정규 표현식을 사용하여 교체할 문자열을 검색합니다_strRegexReplaceMultiple: 정규 표현식 목록을 사용하여 교체할 문자열을 검색합니다_strArrayReplace: 배열 내의 문자열을 다른 문자열로 교체합니다_strArrayReplaceMultiple: 배열 내의 문자열 목록을 다른 문자열 목록으로 교체합니다
문자열 검색 및 교체
이 GraphQL 쿼리는 게시물을 가져와서, 게시물의 콘텐츠와 제목에 있는 특정 문자열을 모두 다른 문자열로 교체한 후 게시물을 다시 저장합니다:
query GetPostData(
$postId: ID!
$replaceFrom: String!,
$replaceTo: String!
) {
post(by: { id: $postId }) {
title
adaptedPostTitle: _strReplace(
search: $replaceFrom
replaceWith: $replaceTo
in: $__title
)
@export(as: "adaptedPostTitle")
rawContent
adaptedRawContent: _strReplace(
search: $replaceFrom
replaceWith: $replaceTo
in: $__rawContent
)
@export(as: "adaptedRawContent")
}
}
mutation UpdatePost($postId: ID!)
@depends(on: "GetPostData")
{
updatePost(input: {
id: $postId,
title: $adaptedPostTitle,
contentAs: { html: $adaptedRawContent },
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
title
rawContent
}
}
}쿼리를 실행하려면 검색 및 교체할 문자열이 포함된 variables 딕셔너리를 제공합니다:
{
"postId": 1,
"replaceFrom": "Old string",
"replaceTo": "New string"
}여러 문자열 검색 및 교체
위와 동일한 쿼리이지만, _strReplaceMultiple을 사용하면 문자열 목록을 다른 문자열 목록으로 교체할 수 있습니다:
query GetPostData(
$postId: ID!
$replaceFrom: [String!]!,
$replaceTo: [String!]!
) {
post(by: { id: $postId }) {
title
adaptedPostTitle: _strReplaceMultiple(
search: $replaceFrom
replaceWith: $replaceTo
in: $__title
)
@export(as: "adaptedPostTitle")
rawContent
adaptedRawContent: _strReplaceMultiple(
search: $replaceFrom
replaceWith: $replaceTo
in: $__rawContent
)
@export(as: "adaptedRawContent")
}
}
mutation UpdatePost($postId: ID!)
@depends(on: "GetPostData")
{
updatePost(input: {
id: $postId,
title: $adaptedPostTitle,
contentAs: { html: $adaptedRawContent },
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
title
rawContent
}
}
}variables 딕셔너리에는 검색 및 교체할 문자열 목록을 지정합니다:
{
"postId": 1,
"replaceFrom": ["Old string 2", "Old string 2"],
"replaceTo": ["New string1", "New string 2"]
}누락된 링크 추가
이 GraphQL 쿼리는 정규 표현식을 이용한 검색 및 교체를 통해 게시물의 HTML 콘텐츠에 누락된 링크를 추가합니다:
query GetPostData($postId: ID!) {
post(by: { id: $postId }) {
id
rawContent
adaptedRawContent: _strRegexReplace(
searchRegex: "#\\s+((https?)://(\\S*?\\.\\S*?))([\\s)\\[\\]{},;\"\\':<]|\\.\\s|$)#i"
replaceWith: "<a href=\"$1\" target=\"_blank\">$3</a>$4"
in: $__rawContent
)
@export(as: "adaptedRawContent")
}
}
mutation UpdatePost($postId: ID!)
@depends(on: "GetPostData")
{
updatePost(input: {
id: $postId,
contentAs: { html: $adaptedRawContent },
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
title
rawContent
}
}
}다음과 같이 앵커 태그로 둘러싸이지 않은 모든 URL은:
<p>Visit my website: https://mysite.com.</p>...해당 <a> 태그가 추가되어(텍스트에서 도메인을 제거하고, 새 창에서 열리도록 target도 부여됩니다), 다음과 같이 변환됩니다:
<p>Visit my website: <a href="https://mysite.com" target="_blank">mysite.com</a>.</p>"\"문자는 정규 표현식 패턴 내에서"\\"로 이스케이프해야 합니다. 예를 들어,"/^https?:\/\//"는"/^https?:\\/\\//"로 작성합니다- PHP 함수
preg_replace문서에서는 교체 참조(예:$1) 및 PCRE 수정자 사용 방법을 설명합니다.
HTTP를 HTTPS로 교체
이 GraphQL 쿼리는 HTML 이미지 소스에 있는 모든 http URL을 https로 교체합니다:
query GetPostData($postId: ID!) {
post(by: {id: $postId}) {
id
rawContent
adaptedRawContent: _strRegexReplace(
searchRegex: "/<img(\\s+)?([^>]*?\\s+?)?src=([\"'])http:\\/\\/(.*?)/"
replaceWith: "<img$1$2src=$3https://$4$3"
in: $__rawContent
)
@export(as: "adaptedRawContent")
}
}
mutation UpdatePost($postId: ID!)
@depends(on: "GetPostData")
{
updatePost(input: {
id: $postId,
contentAs: { html: $adaptedRawContent },
}) {
status
errors {
__typename
...on ErrorPayload {
message
}
}
post {
id
title
rawContent
}
}
}