Schema 教程
Schema 教程第6课:搜索、替换并重新保存

第6课:搜索、替换并重新保存

本教程课程提供了涉及搜索和替换,然后将资源重新保存到数据库的内容适配示例。

PHP Functions via Schema 扩展提供以下"搜索和替换"字段:

  • _strReplace:将一个字符串替换为另一个字符串
  • _strReplaceMultiple:将一组字符串替换为另一组字符串
  • _strRegexReplace:使用正则表达式搜索要替换的字符串
  • _strRegexReplaceMultiple:使用正则表达式列表搜索要替换的字符串
  • _strArrayReplace:将数组中的一个字符串替换为另一个字符串
  • _strArrayReplaceMultiple:将数组中的一组字符串替换为另一组字符串

搜索和替换字符串

此 GraphQL query 获取一篇文章,将文章内容和标题中某个字符串的所有出现替换为另一个字符串,然后重新保存文章:

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,需提供包含搜索和替换字符串的 variables 字典:

{
  "postId": 1,
  "replaceFrom": "Old string",
  "replaceTo": "New string"
}

搜索和替换多个字符串

与上面相同的 query,但使用 _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 query 使用正则表达式搜索和替换,为文章的 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>

将 HTTP 替换为 HTTPS

此 GraphQL query 将 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
    }
  }
}