Skip to content

Commit

Permalink
Initial release
Browse files Browse the repository at this point in the history
  • Loading branch information
andreekeberg committed Sep 10, 2020
0 parents commit aec360e
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# OS files
.DS_Store
Thumbs.db

# Composer
vendor
composer.lock
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Changelog

All notable changes to this project will be documented in this file.

## [1.0.0] - 2020-09-10

Initial release

[1.0.0]: https://github.com/andreekeberg/wp-search-replace-cli/releases/tag/1.0.0
47 changes: 47 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Contributing to WP Search Replace CLI

This document contains basic guidelines to make contributing to this project as easy and transparent as possible, whether it's:

- Reporting a bug
- Discussing the current state of the code
- Submitting a fix
- Proposing new features
- Becoming a maintainer

## Pull requests are actively welcomed

1. Fork the repo and create your branch from `master`.
2. If you've added code that should be tested, add tests.
3. If you've changed APIs, update the documentation.
5. Make sure your code lints.
6. Issue your pull request.

## Any contributions you make will be under the MIT Software License

In short, when you submit code changes, your submissions are understood to be under the same [MIT License](http://choosealicense.com/licenses/mit/) that covers the project.

## Report bugs using [issues](https://github.com/andreekeberg/wp-search-replace-cli/issues)

All bugs are tracked using GitHub issues to track public bugs. Report a bug by [opening a new issue](https://github.com/andreekeberg/wp-search-replace-cli/issues/new); it's that easy!

## Write bug reports with detail and background

**Great bug reports** tend to have:

- A quick summary and/or background
- Steps to reproduce
- Be specific
- What you expected would happen
- What actually happens
- Notes (possibly including why you think this might be happening, or stuff you tried that didn't work)

## Use a consistent coding style

All code should follow the [PSR-12: Extended Coding Style](https://www.php-fig.org/psr/psr-12/)

* 4 spaces for indentation rather than tabs
* Newlines after opening curly brackets in classes and methods

## License

By contributing to WP Search Replace CLI, you agree that your contributions will be licensed under its MIT License.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2020 André Ekeberg

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
42 changes: 42 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# WP Search Replace CLI

[![Latest Stable Version](https://poser.pugx.org/andreekeberg/wp-search-replace-cli/v/stable)](https://packagist.org/packages/andreekeberg/wp-search-replace-cli) [![Total Downloads](https://poser.pugx.org/andreekeberg/wp-search-replace-cli/downloads)](https://packagist.org/packages/andreekeberg/wp-search-replace-cli) [![License](https://poser.pugx.org/andreekeberg/wp-search-replace-cli/license)](https://packagist.org/packages/andreekeberg/wp-search-replace-cli)

Command line tool to replace serialized strings in WordPress database dump files.

## Requirements

- PHP 4.0.0 or higher

## Installation

```
composer global require andreekeberg/wp-search-replace-cli
```

## Usage

```
wp-search-replace [options]
```

### Options

|Option|Description|Default|
|------|-----------|-------|
|**--input**, **-i**|Path to input file||
|**--search**, **-s**|The value being searched for||
|**--replace**, **-r**|Replacement string||
|**--output**, **-o**|Path to output file (optional)|`STDOUT`|

## Contributing

Read the [contribution guidelines](CONTRIBUTING.md).

## Changelog

Refer to the [changelog](CHANGELOG.md) for a full history of the project.

## License

WP Search Replace CLI is licensed under the [MIT license](LICENSE).
3 changes: 3 additions & 0 deletions bin/wp-search-replace
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/usr/bin/env php
<?php
include __DIR__ . '/wp-search-replace.php';
1 change: 1 addition & 0 deletions bin/wp-search-replace.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@php %~dp0\wp-search-replace.php %*
144 changes: 144 additions & 0 deletions bin/wp-search-replace.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

// Define script name
$script = basename($argv[0]);

// Define default options
$options = [
'input' => '',
'search' => '',
'replace' => '',
'output' => ''
];

// Define short options
$shortopts = 'i:';
$shortopts .= 's:';
$shortopts .= 'r:';
$shortopts .= 'o:';

// Define long options
$longopts = ['input:', 'search:', 'replace:', 'output:'];

// Define required options
$required = ['input', 'search', 'replace'];

// Get all options
$opts = getopt($shortopts, $longopts);

// Merge short and long options
foreach ($opts as $key => $value) {
switch ($key) {
case 'i':
$key = 'input';
break;
case 's':
$key = 'search';
break;
case 'r':
$key = 'replace';
break;
case 'o':
$key = 'output';
break;
}

$options[$key] = $value;
}

// Set whether we should show help message
$show_help = false;

// Validate required arguments
foreach ($options as $option => $value) {
if (in_array($option, $required) && empty($value)) {
$show_help = true;

break;
}
}

// Print help message if required any options are missing
if ($show_help) {
fwrite(
STDERR,
'Usage: ' . $script . ' [options]' . PHP_EOL . PHP_EOL .
'Options:' . PHP_EOL .
' --input, -i Path to input file' . PHP_EOL .
' --search, -s The value being searched for' . PHP_EOL .
' --replace, -r Replacement string' . PHP_EOL .
' --output, -o Path to output file (optional, defaults to the standard output stream)' . PHP_EOL
);

exit;
}

// Check if input file exists
if (!file_exists($options['input'])) {
// Print error and exit if the input file could not be found
fwrite(
STDERR,
'Error: Could not find input file ' . $options['input'] . PHP_EOL
);

exit;
}

// Try to open input file
$input = @file_get_contents($options['input']);

if ($input === false) {
// Print error and exit if input file can not be opened
fwrite(
STDERR,
'Error: Could not open input file ' . $options['input'] . PHP_EOL
);

exit;
}

// Perform replacements
$output = SerializedSearchReplace::replace(
$options['search'],
$options['replace'],
$input,
$count
);

if (!empty($options['output'])) {
// Try to create a file handle pointing to the specified output file
if (!$handle = @fopen($options['output'], 'w')) {
// Print error and exit if output file can not be opened or created
fwrite(
STDERR,
'Error: Cannot open or create file ' . $options['output'] . PHP_EOL
);

exit;
}

// Try to write to the specified output file
if (@fwrite($handle, $output) === false) {
// Print error and exit if output file can not be written to
fwrite(
STDERR,
'Error: Cannot write to file ' . $options['output'] . PHP_EOL
);

exit;
}
} else {
// Print replaced file data to the standard output stream
fwrite(STDOUT, $output);
}

// Print the number of replacements performed
fwrite(
STDERR,
'Replaced ' . $count .
' instances of "' . $options['search'] .
'" with "' . $options['replace'] .
'"' . PHP_EOL
);
44 changes: 44 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
{
"name": "andreekeberg/wp-search-replace-cli",
"type": "library",
"description": "Command line tool to replace serialized strings in WordPress database dump files",
"keywords": [
"search-replace",
"search",
"replace",
"serialize",
"unserialize",
"serialized",
"wp",
"wordpress",
"cli",
"tool",
"sql",
"mysql",
"database",
"export",
"import",
"dump",
"file"
],
"homepage": "https://github.com/andreekeberg/wp-search-replace-cli",
"readme": "README.md",
"license": "MIT",
"authors": [
{
"name": "André Ekeberg",
"email": "hello@andreekeberg.se",
"homepage": "https://andreekeberg.se"
}
],
"support": {
"issues": "https://github.com/andreekeberg/wp-search-replace-cli/issues"
},
"require": {
"php": ">=4.0.0",
"andreekeberg/serialized-search-replace": "^1.1"
},
"bin": [
"bin/wp-search-replace"
]
}

0 comments on commit aec360e

Please sign in to comment.