Ders 6: Ara, değiştir ve tekrar kaydet
Bu eğitim dersi, arama ve değiştirme işlemlerini içeren içerik uyarlamalarına ve kaynağı tekrar DB'ye kaydetmeye ilişkin örnekler sunmaktadır.
PHP Functions via Schema eklentisi aşağıdaki "arama ve değiştirme" alanlarını sağlar:
_strReplace: Bir dizeyi başka bir dizeyle değiştirir_strReplaceMultiple: Bir dize listesini başka bir dize listesiyle değiştirir_strRegexReplace: Değiştirilecek dizeyi düzenli ifade kullanarak arar_strRegexReplaceMultiple: Değiştirilecek dizeleri düzenli ifade listesi kullanarak arar_strArrayReplace: Bir dizide bir dizeyi başka bir dizeyle değiştirir_strArrayReplaceMultiple: Bir dizide bir dize listesini başka bir dize listesiyle değiştirir
Bir dizeyi ara ve değiştir
Bu GraphQL query'si bir gönderiyi getirir, gönderinin içeriğinde ve başlığında bir dizenin tüm geçtiği yerleri başka bir dizeyle değiştirir ve gönderiyi tekrar kaydeder:
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
}
}
}Query'yi çalıştırmak için, aranacak ve değiştirilecek dizeleri içeren variables sözlüğünü sağlarız:
{
"postId": 1,
"replaceFrom": "Old string",
"replaceTo": "New string"
}Birden fazla dizeyi ara ve değiştir
Bu, yukarıdakiyle aynı query'dir; ancak _strReplaceMultiple kullanarak bir dize listesini başka bir dize listesiyle değiştirebiliriz:
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 sözlüğü artık aranacak ve değiştirilecek bir dize listesi alır:
{
"postId": 1,
"replaceFrom": ["Old string 2", "Old string 2"],
"replaceTo": ["New string1", "New string 2"]
}Eksik bağlantıları ekleme
Bu GraphQL query'si, gönderinin HTML içeriğindeki eksik bağlantıları eklemek için regex arama ve değiştirme işlemi yapar:
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
}
}
}Bir anchor etiketiyle çevrilmemiş tüm URL'ler, örneğin:
<p>Visit my website: https://mysite.com.</p>...etraflarına karşılık gelen <a> etiketi eklenir (metinden alan adı çıkarılır ve yeni bir pencerede açmak için target eklenir), şu hale gelir:
<p>Visit my website: <a href="https://mysite.com" target="_blank">mysite.com</a>.</p>"\"karakteri, regex deseninin içinde"\\"olarak kaçış karakteriyle yazılmalıdır. Örneğin,"/^https?:\/\//"şu şekilde yazılır:"/^https?:\\/\\//"- PHP fonksiyonu
preg_replacebelgesi, değiştirme referanslarının (örn:$1) ve PRCE değiştiricilerinin nasıl kullanılacağını açıklar.
HTTP'yi HTTPS ile değiştirme
Bu GraphQL query'si, HTML görsel kaynaklarındaki tüm http URL'lerini https ile değiştirir:
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
}
}
}