wp-cli/search-replace-command 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

wp-cli/search-replace-command

Composer 安装命令:

composer require wp-cli/search-replace-command

包简介

Searches/replaces strings in the database.

README 文档

README

Searches/replaces strings in the database.

Testing Code Coverage

Quick links: Using | Installing | Contributing | Support

Using

wp search-replace [<old>] [<new>] [<table>...] [--old=<value>] [--new=<value>] [--dry-run] [--network] [--all-tables-with-prefix] [--all-tables] [--export[=<file>]] [--export_insert_size=<rows>] [--skip-tables=<tables>] [--skip-columns=<columns>] [--include-columns=<columns>] [--precise] [--recurse-objects] [--verbose] [--regex] [--regex-flags=<regex-flags>] [--regex-delimiter=<regex-delimiter>] [--regex-limit=<regex-limit>] [--format=<format>] [--report] [--report-changed-only] [--log[=<file>]] [--before_context=<num>] [--after_context=<num>]

Searches through all rows in a selection of tables and replaces appearances of the first string with the second string.

By default, the command uses tables registered to the $wpdb object. On multisite, this will just be the tables for the current site unless --network is specified.

Search/replace intelligently handles PHP serialized data, and does not change primary key values.

Tables without a primary key are skipped. To check whether a table has a primary key, run wp db query 'DESCRIBE <table>' and look for 'PRI' in the Key column.

OPTIONS

[<old>]
	A string to search for within the database.

[<new>]
	Replace instances of the first string with this new string.

[<table>...]
	List of database tables to restrict the replacement to. Wildcards are
	supported, e.g. `'wp_*options'` or `'wp_post*'`.

[--old=<value>]
	An alternative way to specify the search string. Use this when the
	search string starts with '--' (e.g., --old='--some-text').

[--new=<value>]
	An alternative way to specify the replacement string. Use this when the
	replacement string starts with '--' (e.g., --new='--other-text') or is
	empty.

[--dry-run]
	Run the entire search/replace operation and show report, but don't save
	changes to the database.

[--network]
	Search/replace through all the tables registered to $wpdb in a
	multisite install.

[--all-tables-with-prefix]
	Enable replacement on any tables that match the table prefix even if
	not registered on $wpdb.

[--all-tables]
	Enable replacement on ALL tables in the database, regardless of the
	prefix, and even if not registered on $wpdb. Overrides --network
	and --all-tables-with-prefix.

[--export[=<file>]]
	Write transformed data as SQL file instead of saving replacements to
	the database. If <file> is not supplied, will output to STDOUT.

[--export_insert_size=<rows>]
	Define number of rows in single INSERT statement when doing SQL export.
	You might want to change this depending on your database configuration
	(e.g. if you need to do fewer queries). Default: 50

[--skip-tables=<tables>]
	Do not perform the replacement on specific tables. Use commas to
	specify multiple tables. Wildcards are supported, e.g. `'wp_*options'` or `'wp_post*'`.

[--skip-columns=<columns>]
	Do not perform the replacement on specific columns. Use commas to
	specify multiple columns. Table-qualified column names ("table.column")
	are supported to apply the skip to a specific table only.

[--include-columns=<columns>]
	Perform the replacement on specific columns. Use commas to
	specify multiple columns. Table-qualified column names ("table.column")
	are supported to apply the inclusion to a specific table only.

[--precise]
	Force the use of PHP (instead of SQL) for all columns. By default, the command
	uses fast SQL queries, but automatically switches to PHP for columns containing
	serialized data. Use this flag to ensure PHP processes all columns, which is
	slower but handles complex serialized data structures more reliably.

[--recurse-objects]
	Enable recursing into objects to replace strings. Defaults to true;
	pass --no-recurse-objects to disable.

[--verbose]
	Prints rows to the console as they're updated.

[--regex]
	Runs the search using a regular expression (without delimiters).
	Warning: search-replace will take about 15-20x longer when using --regex.

[--regex-flags=<regex-flags>]
	Pass PCRE modifiers to regex search-replace (e.g. 'i' for case-insensitivity).

[--regex-delimiter=<regex-delimiter>]
	The delimiter to use for the regex. It must be escaped if it appears in the search string. The default value is the result of `chr(1)`.

[--regex-limit=<regex-limit>]
	The maximum possible replacements for the regex per row (or per unserialized data bit per row). Defaults to -1 (no limit).

[--format=<format>]
	Render output in a particular format.
	---
	default: table
	options:
	  - table
	  - count
	---

[--report]
	Produce report. Defaults to true.

[--report-changed-only]
	Report changed fields only. Defaults to false, unless logging, when it defaults to true.

[--log[=<file>]]
	Log the items changed. If <file> is not supplied or is "-", will output to STDOUT.
	Warning: causes a significant slow down, similar or worse to enabling --precise or --regex.

[--before_context=<num>]
	For logging, number of characters to display before the old match and the new replacement. Default 40. Ignored if not logging.

[--after_context=<num>]
	For logging, number of characters to display after the old match and the new replacement. Default 40. Ignored if not logging.

EXAMPLES

# Search and replace but skip one column
$ wp search-replace 'http://example.test' 'http://example.com' --skip-columns=guid

# Run search/replace operation but dont save in database
$ wp search-replace 'foo' 'bar' wp_posts wp_postmeta wp_terms --dry-run

# Run case-insensitive regex search/replace operation (slow)
$ wp search-replace '\[foo id="([0-9]+)"' '[bar id="\1"' --regex --regex-flags='i'

# Turn your production multisite database into a local dev database
$ wp search-replace --url=example.com example.com example.test 'wp_*options' wp_blogs wp_site --network

# Search/replace to a SQL file without transforming the database
$ wp search-replace foo bar --export=database.sql

# Search/replace string containing hyphens
$ wp search-replace --old='--old-string' --new='new-string'

# Remove a string by replacing it with an empty value
$ wp search-replace --old='remove-me' --new=''

# Use precise mode for complex serialized data
$ wp search-replace 'oldurl.com' 'newurl.com' --precise

# Bash script: Search/replace production to development url (multisite compatible)
#!/bin/bash
if $(wp --url=http://example.com core is-installed --network); then
    wp search-replace --url=http://example.com 'http://example.com' 'http://example.test' --recurse-objects --network --skip-columns=guid --skip-tables=wp_users
else
    wp search-replace 'http://example.com' 'http://example.test' --recurse-objects --skip-columns=guid --skip-tables=wp_users
fi

Installing

This package is included with WP-CLI itself, no additional installation necessary.

To install the latest version of this package over what's included in WP-CLI, run:

wp package install git@github.com:wp-cli/search-replace-command.git

Contributing

We appreciate you taking the initiative to contribute to this project.

Contributing isn’t limited to just code. We encourage you to contribute in the way that best fits your abilities, by writing tutorials, giving a demo at your local meetup, helping other users with their support questions, or revising our documentation.

For a more thorough introduction, check out WP-CLI's guide to contributing. This package follows those policy and guidelines.

Reporting a bug

Think you’ve found a bug? We’d love for you to help us get it fixed.

Before you create a new issue, you should search existing issues to see if there’s an existing resolution to it, or if it’s already been fixed in a newer version.

Once you’ve done a bit of searching and discovered there isn’t an open or fixed issue for your bug, please create a new issue. Include as much detail as you can, and clear steps to reproduce if possible. For more guidance, review our bug report documentation.

Creating a pull request

Want to contribute a new feature? Please first open a new issue to discuss whether the feature is a good fit for the project.

Once you've decided to commit the time to seeing your pull request through, please follow our guidelines for creating a pull request to make sure it's a pleasant experience. See "Setting up" for details specific to working on this package locally.

License

This project is licensed under the MIT License. See the LICENSE file for details.

Support

GitHub issues aren't for general support questions, but there are other venues you can try: https://wp-cli.org/#support

This README.md is generated dynamically from the project's codebase using wp scaffold package-readme (doc). To suggest changes, please submit a pull request against the corresponding part of the codebase.

wp-cli/search-replace-command 适用场景与选型建议

wp-cli/search-replace-command 是一款 基于 Gherkin 开发的 Composer 扩展包,目前已累计 8.11M 次下载、GitHub Stars 达 59, 最近一次更新时间为 2017 年 03 月 13 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 wp-cli/search-replace-command 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 wp-cli/search-replace-command 我们能提供哪些服务?
定制开发 / 二次开发

基于 wp-cli/search-replace-command 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 8.11M
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 59
  • 点击次数: 16
  • 依赖项目数: 8
  • 推荐数: 0

GitHub 信息

  • Stars: 59
  • Watchers: 9
  • Forks: 47
  • 开发语言: Gherkin

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-03-13