Skip to content

Commit

Permalink
Fixes for 3.0.6 (#147)
Browse files Browse the repository at this point in the history
* Remove unused class

* Don’t error with invalid locale

* Fix simple and advanced pagination

* Don’t issue a page change on first load

* Remove dev mode

* Bump packages

* Remove old help links

* Take into account skipped rows in advanced pagination

Advanced searching can return rows that don’t match. Although we ignore these from the returned results, the ‘next’ value has to take them into account

* Include current results + latest results when exporting

It was missing the already downloaded data

* Set 3.0.6 version
  • Loading branch information
johngodley authored Sep 5, 2022
1 parent 048ef31 commit 713df81
Show file tree
Hide file tree
Showing 21 changed files with 535 additions and 679 deletions.
4 changes: 1 addition & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,10 @@ Some parts of the UI are React and can be built with:

`yarn build`

To use in development mode then set `SEARCHREGEX_DEV_MODE` to true in PHP, and run:
To use in development mode run:

`yarn start`

This will start Webpack in hot-reload mode, and you can make changes to JS files and have them auto-loaded.

### Releasing

Finally, to produce a release copy:
Expand Down
3 changes: 1 addition & 2 deletions includes/api/class-route.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,7 @@
* @apiSuccess {Object[]} totals The totals for this search
* @apiSuccess {Integer} totals.current The current search offset
* @apiSuccess {Integer} totals.rows The total number of rows for the source, including non-matches
* @apiSuccess {Integer} totals.matched_rows The number of matched rows if known, or `-1` if a regular expression match and unknown
* @apiSuccess {Integer} totals.matched_phrases The number of matched phraes if known, or `-1` if a regular expression match and unknown
* @apiSuccess {Integer} totals.matched_rows The number of matched rows if known, or 0 if a regular expression match and unknown
* @apiSuccess {Object[]} progress The current search progress, and the previous and next set of results
* @apiSuccess {Integer} progress.current The current search offset
* @apiSuccess {Integer} progress.rows The number of rows contained within this result set
Expand Down
65 changes: 52 additions & 13 deletions includes/search/class-search.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,22 +88,13 @@ public function get_search_results( Action\Action $action, $offset, $per_page, $

// Calculate the prev/next pages of results
$previous = max( 0, $offset - $per_page );
$next = $totals->get_next_page( $offset + $per_page );

// We always go in $per_page groups, but we need to limit if we only need a few more to fill a result set
if ( $limit > 0 && $limit < count( $results ) ) {
$next = min( $offset + $limit, $next );
$results = array_slice( $results, 0, $limit );
}

if ( $next === $offset ) {
$next = false;
}

if ( $previous === $offset ) {
$previous = false;
}

list( $next, $results ) = $this->get_next_results( $totals, $offset, $per_page, $limit, $results );

return [
'results' => $action->should_save() ? [] : $results,
'totals' => $totals->to_json(),
Expand All @@ -116,6 +107,54 @@ public function get_search_results( Action\Action $action, $offset, $per_page, $
];
}

/**
* Get the results and next position. For advanced searches this may be a smaller set if we have a limit value
*
* @param Totals $totals Totals.
* @param int $offset Current page offset.
* @param int $per_page Per page limit.
* @param int $limit Max number of results.
* @param Array $results Result array.
* @return Array
*/
private function get_next_results( $totals, $offset, $per_page, $limit, $results ) {
$next = $totals->get_next_page( $offset + $per_page );

// We always go in $per_page groups, but we need to limit if we only need a few more to fill a result set
if ( $limit > 0 && $limit < count( $results ) ) {
$new_results = [];
$new_offset = $offset;

foreach ( $results as $result ) {
if ( $result ) {
$new_results[] = $result;
}

$new_offset++;

if ( count( $new_results ) === $limit ) {
break;
}
}

$results = $new_results;
$next = min( $new_offset, $next );
} else {
$results = array_filter( $results, function( $item ) {
return $item !== false;
} );
}

if ( $next === $offset ) {
$next = false;
}

return [
$next,
$results,
];
}

/**
* Get totals for source
*
Expand Down Expand Up @@ -213,9 +252,9 @@ public function convert_rows_to_results( array $source_results, Action\Action $a
return $saved;
}
}

$results[] = $result;
}

$results[] = $result;
}
}

Expand Down
1 change: 0 additions & 1 deletion includes/sql/select/class-select.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
use SearchRegex\Sql;

require_once __DIR__ . '/class-column.php';
require_once __DIR__ . '/class-sum.php';

/**
* SQL SELECT
Expand Down
74 changes: 0 additions & 74 deletions includes/sql/select/class-sum.php

This file was deleted.

18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "search-regex",
"version": "3.0.5",
"version": "3.0.6",
"description": "Adds search and replace functionality across posts, pages, comments, and meta-data, with full regular expression support",
"main": "search-regex.php",
"sideEffects": true,
Expand Down Expand Up @@ -38,8 +38,8 @@
},
"homepage": "https://github.com/johngodley/search-regex",
"devDependencies": {
"@types/jest": "^28.1.8",
"@types/react": "^18.0.17",
"@types/jest": "^29.0.0",
"@types/react": "^18.0.18",
"@types/react-dom": "^18.0.6",
"@types/react-highlight-words": "^0.16.4",
"@types/react-redux": "^7.1.24",
Expand All @@ -56,20 +56,20 @@
"gulp-po2json": "^1.0.0",
"gulp-zip": "^5.1.0",
"he": "^1.2.0",
"jest": "^29.0.0",
"jest-environment-jsdom": "^29.0.0",
"jest": "^29.0.1",
"jest-environment-jsdom": "^29.0.1",
"mocha": "^10.0.0",
"node-fetch": "2",
"path": "^0.12.7",
"prettier": "npm:[email protected]",
"release-it": "^15.4.0",
"release-it": "^15.4.1",
"request": "^2.88.2",
"through": "^2.3.8",
"through2": "^4.0.2",
"webpack-shell-plugin-next": "^2.2.2"
},
"dependencies": {
"@emotion/react": "^11.10.0",
"@emotion/react": "^11.10.4",
"@redux-devtools/extension": "^3.2.3",
"@wordpress/element": "^4.14.0",
"@wordpress/i18n": "^4.16.0",
Expand All @@ -92,9 +92,9 @@
"react-textarea-autosize": "^8.3.4",
"redux": "^4.2.0",
"redux-thunk": "^2.4.1",
"typescript": "^4.7.4",
"typescript": "^4.8.2",
"url": "^0.11.0",
"use-debounce": "^8.0.3"
"use-debounce": "^8.0.4"
},
"apidoc": {
"name": "Search Regex REST API",
Expand Down
7 changes: 6 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ Contributors: johnny5
Donate link: http://searchregex.com/donation/
Tags: search, replace, regex, regular expression, database, post, page
Requires at least: 5.6
Tested up to: 6.0.1
Tested up to: 6.0.2
Stable tag: trunk
Requires PHP: 5.6
License: GPLv3
Expand Down Expand Up @@ -113,6 +113,11 @@ Full documentation can be found on the [Search Regex](http://searchregex.com/) s

== Changelog ==

= 3.0.6 - September 5th 2022 =
* Fix incorrect pagination
* Fix incorrect page reload
* Fix export missing a page of data

= 3.0.5 - August 25th 2022 =
* Fix empty replacement string when saving a preset
* Fix preset being saved from search preset dropdown
Expand Down
3 changes: 1 addition & 2 deletions search-regex.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Search Regex
Plugin URI: https://searchregex.com/
Description: Adds search and replace functionality across posts, pages, comments, and meta-data, with full regular expression support
Version: 3.0.5
Version: 3.0.6
Author: John Godley
Text Domain: search-regex
Domain Path: /locale
Expand All @@ -21,7 +21,6 @@
*/

define( 'SEARCHREGEX_FILE', __FILE__ );
define( 'SEARCHREGEX_DEV_MODE', false );

// This file must support PHP < 5.6 so as not to crash
if ( version_compare( phpversion(), '5.6' ) < 0 ) {
Expand Down
7 changes: 7 additions & 0 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@ import { getInitialState } from './state/initial';
import Home from './page/home';
import { apiFetch } from '@wp-plugin-lib';

// Validate the locale works with the browser
try {
new Intl.NumberFormat( window.SearchRegexi10n.locale );
} catch {
window.SearchRegexi10n.locale = 'en-US';
}

// Set API nonce and root URL
apiFetch.resetMiddlewares();
apiFetch.use( apiFetch.createRootURLMiddleware( SearchRegexi10n?.api?.WP_API_root ?? '/wp-json/' ) );
Expand Down
4 changes: 2 additions & 2 deletions src/component/replace-progress/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ function Totals( { totals, current } ) {
function ReplaceProgress( props ) {
const { progress, totals, requestCount, onNext, status, onClear, isAdvanced, onError } = props;
const total = getTotal( isAdvanced, totals );
const { current = 0, next = 0 } = progress;
const { current = 0, next = 0, rows = 0 } = progress;
const percent = Math.min( 100, status === STATUS_IN_PROGRESS ? getPercent( next === false ? total : next, total ) : 100 );
const canLoad = progress.next !== false && status === STATUS_IN_PROGRESS;

Expand All @@ -68,7 +68,7 @@ function ReplaceProgress( props ) {
</div>

<div className="searchregex-replaceall__stats">
<Totals totals={ totals } current={ current } />
<Totals totals={ totals } current={ isAdvanced ? current : current + rows } />

{ status === STATUS_COMPLETE && (
<button type="button" className="button button-primary" onClick={ onClear }>
Expand Down
16 changes: 10 additions & 6 deletions src/page/home/page-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,23 +20,27 @@ function PageRouter( props ) {
setPage( page );
}

useEffect(() => {
useEffect( () => {
window.addEventListener( 'popstate', onPageChanged );

return () => {
window.removeEventListener( 'popstate', onPageChanged );
};
}, []);
}, [] );

useEffect(() => {
onPageChange();
useEffect( () => {
if ( previousPage.current === undefined ) {
previousPage.current = page;
return;
}

if ( previousPage.current && previousPage.current !== page) {
if ( previousPage.current && previousPage.current !== page ) {
onPageChange();
history.pushState( {}, '', getWordPressUrl( { sub: page }, { sub: 'search' }, '?page=search-regex.php' ) );
}

previousPage.current = page;
}, [ page ]);
}, [ page ] );

return children;
}
Expand Down
Loading

0 comments on commit 713df81

Please sign in to comment.