Skip to content

Commit 8196217

Browse files
committed
feat: add query parameter support
1 parent 350ae03 commit 8196217

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ This plugin extends the YOURLS API by adding a `list` action with advanced capab
1414
- Sorting by keyword, URL, title, timestamp, IP address, or clicks.
1515
- Pagination using offset and limit (per page).
1616
- Custom selection of fields to be returned in the API response.
17+
- Searching by keyword.
1718
- Total count of links to assist with pagination.
1819

1920
## Features
2021

2122
- **Sorting**: Sort results by any valid field (`keyword`, `url`, `title`, `timestamp`, `ip`, `clicks`).
2223
- **Pagination**: Use the `offset` and `perpage` parameters to paginate results.
2324
- **Field Selection**: Select specific fields to return (e.g., `keyword`, `url`, `clicks`). By default, all fields are returned.
25+
- **Search**: Search for links by keyword.
2426
- **Total Count**: Returns the total number of links in the database, useful for pagination.
2527

2628
## Installation
@@ -52,6 +54,7 @@ http://<your-yourls-site>/yourls-api.php
5254
- `sortorder`: Sort order, either `ASC` (ascending) or `DESC` (descending). Default is `DESC`.
5355
- `offset`: Number of links to skip (used for pagination). Default is `0`.
5456
- `perpage`: Number of links to return per page. Default is `50`.
57+
- `query`: Search query to filter results by keyword.
5558
- `fields[]`: An array of fields to return (e.g., `fields[]=keyword&fields[]=url`). Default is all fields (`*`).
5659

5760
### Example Request
@@ -92,6 +95,7 @@ This example retrieves the first 10 results, sorted by clicks in ascending order
9295
| `sortorder`| string | Sort order, `ASC` for ascending, `DESC` for descending. | `DESC` |
9396
| `offset` | int | Offset for pagination, number of links to skip. | `0` |
9497
| `perpage` | int | Number of links to return per page. | `50` |
98+
| `query` | string | Search query to filter results by keyword. | |
9599
| `fields[]` | array | Fields to return in the response. Use array format: `fields[]=keyword`. | `*` (all) |
96100

97101
## License

plugin.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ function ti_list_function() {
2626
$offset = isset( $_REQUEST['offset'] ) && is_numeric( $_REQUEST['offset'] ) ? (int)$_REQUEST['offset'] : 0;
2727
$perpage = isset( $_REQUEST['perpage'] ) && is_numeric( $_REQUEST['perpage'] ) ? (int)$_REQUEST['perpage'] : 50;
2828
$fields = isset( $_REQUEST['fields'] ) && is_array( $_REQUEST['fields'] ) ? $_REQUEST['fields'] : [ '*' ];
29+
$query = isset( $_REQUEST['query'] ) ? $_REQUEST['query'] : '';
2930

3031
// Validate fields to ensure the query won't break if an invalid field is requested.
3132
$valid_fields = [ 'keyword', 'url', 'title', 'timestamp', 'ip', 'clicks' ];
@@ -34,7 +35,8 @@ function ti_list_function() {
3435

3536
// Get total number of links.
3637
$total = yourls_get_db()->fetchValue( "SELECT COUNT(*) FROM `$table`" );
37-
$query = "SELECT $selected_fields FROM `$table` ORDER BY `$sort_by` $sort_order LIMIT $offset, $perpage";
38+
$where = $query ? "WHERE `keyword` LIKE '%$query%'" : ''; // If you have millions of links, this query can be slow. You can replace LIKE with = if you want exact match.
39+
$query = "SELECT $selected_fields FROM `$table` $where ORDER BY `$sort_by` $sort_order LIMIT $offset, $perpage";
3840
$results = yourls_get_db()->fetchObjects( $query );
3941

4042
// Return the response with results and total count.

0 commit comments

Comments
 (0)