定制 vielhuber/magicreplace 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

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

vielhuber/magicreplace

Composer 安装命令:

composer require vielhuber/magicreplace

包简介

A search/replace class with zero overhead.

README 文档

README

build status GitHub Tag Code Style License Last Commit PHP Version Support Packagist Downloads

✨ magicreplace ✨

magicreplace is a search replace tool for migrating databases.

Intro

The problem

When moving databases, usually the url environment also changes. If the URL is hardcoded in the database (like WordPress does), those URLs have to be changed. If you now do a search and replace on your entire database to change the URLs, you will corrupt data that has been serialized. Just try out

unserialize(str_replace('www.foo.tld', 'www.barrr.tld', serialize('url=www.foo.tld')));

and you will get an ugly error.

There already exist cool tools that solve this issue, for example...

How is magicreplace different from those tools?

  • Fast (~5sec runtime on 100mb database file with 300.000 rows)
  • Lightweight: Only <10kb in size
  • Works also on big files with small memory limit settings
  • File based: Does not need a database or a wp installation - works on plain (sql) files
  • Local usage: Does not need a remote server or a webservice
  • Multi replace: Does multiple replaces
  • Considers edge cases: Can handle objects and even references
  • Ignores classes that are not available at runtime
  • Can be used either with the command line or as a class
  • Acts carefully: If serialization fails, nothing is changed
  • Never changes data (out of bound ints are preserved, auto generated dates are not updated)
  • Does its work in junks to overcome php limits
  • Supports replacements in special base64 strings (e.g. in BeTheme)

Disclaimer

This does not release you from taking backups. Use this script at your own risk!

Command line

Requirements

Mac
brew install coreutils
Windows

Runs out of the box with WSL/WSL2/Cygwin.

Linux

Installation

wget https://raw.githubusercontent.com/vielhuber/magicreplace/main/src/magicreplace.php

Usage

php magicreplace.php input.sql output.sql search-1 replace-1 search-2 replace-2

Class

Installation

composer require vielhuber/magicreplace

Usage

<?php
require __DIR__ . '/vendor/autoload.php';
use vielhuber\magicreplace\magicreplace;
magicreplace::run('input.sql', 'output.sql', ['search-1' => 'replace-2', 'search-2' => 'replace-2']);

Recommended replace strategy

If you want for example to replace http://www.foo.tld with https://www.bar.tld, the safest method to do so is with the following replacements (in the given order):

  • http://www.foo.tld https://www.bar.tld
  • https://www.foo.tld https://www.bar.tld
  • http://foo.tld https://www.bar.tld
  • https://foo.tld https://www.bar.tld
  • www.foo.tld www.bar.tld
  • foo.tld bar.tld

Testing

Just place these 3 files in a (optionally nested) subfolder of tests/data:

  • input.sql: The desired input file
  • output.sql: The desired output file
  • settings.sql: Define your replacements

Example settings.sql file:

{
    "replace": {
        "http://www.foo.tld": "https://www.bar.tld",
        "https://www.foo.tld": "https://www.bar.tld"
    }
}

If a test fails, the expected output is stored in expected.sql.

You can even auto generate test cases (that compares magicreplace to Search-Replace-DB and only gives you the diff) if you omit input.sql and output.sql and define a mysql database to dump from locally. Example settings.sql file:

{
    "source": {
        "host": "localhost",
        "port": "3306",
        "database": "xxx",
        "username": "xxx",
        "password": "xxx",
    },
    "replace": {
        "http://www.foo.tld": "https://www.bar.tld",
        "https://www.foo.tld": "https://www.bar.tld"
    }
}

input.sql and output.sql then get generated automatically. After you rerun the tests, these generated files are used. If you want to generate them again, just delete them before running the test. You also can provide a whitelist.sql file that includes all lines from input.sql that should be ignored (e.g. where magicreplace acts differently from Search-Replace-DB).

vielhuber/magicreplace 适用场景与选型建议

vielhuber/magicreplace 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 230 次下载、GitHub Stars 达 1, 最近一次更新时间为 2017 年 03 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

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

围绕 vielhuber/magicreplace 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

  • 总下载量: 230
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 11
  • 依赖项目数: 2
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

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