Skip to content

Commit

Permalink
Merge pull request #4 from joshp23/full-release
Browse files Browse the repository at this point in the history
Full release
  • Loading branch information
joshp23 authored Jun 24, 2017
2 parents 11cda98 + d34f253 commit 0a01984
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 32 deletions.
26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,27 @@
A simple plugin to refetch poorly defined titles in YOURLS

### The Problem
Sometimes YOURLS does not fetch the title of a url that is being processed, this happens frequently when using the api with certain applications (looking at you, [Drupal: Shorten URLs](https://www.drupal.org/project/shorten)). When this occures YOURLS will use the url itself as the title in the databse, which is less than desireable.
Sometimes YOURLS does not fetch the title of a url that is being processed, this happens frequently when using the api with certain 3rd party applications (which need attention from their developers). When this occures YOURLS uses the url itself as the title in the databse, which is less than desireable.

### The solution
This plugin will check and refetch the title of any url if it begins with either `http://` or `https://` when visiting the url's stats page. Alternatively, there is an option to batch process the entire datbase at once from the admin menu.

### The catch
This doesn't fix the problem. It would be more beneficial to address the actual issue and have proper titles returned 100% of the time, or at least catch the fails when they happen.
This plugin adds a new `key->value` pair to `action=shorturl`, and if it is present will check the title of the new link and attempt to repair it if it is malformed.

### Installation
Drop this repo into the `YOURLS/user/plugins/` directory and enable it in the admin interface.
Clone this repo into the `YOURLS/user/plugins/` directory and enable it in the admin interface.

### Usage
1. Send the `refetch=true` along with a typical API shorturl call. Ex:
```
https://eg.com/yourls-api.php?refetch=true&action=shorturl&url=https://some.really.long.url.com/it_never/ends/index.php
```
2. This plugin also creates a new action:
- `action=refetch` which will trigger a refetch check according to one of the following required conditions:
- `target=title` which will check a single link and requires`shorturl=EX`. The short url can be a keyword or a full short url.
- `target=all` which will run a check on the entire database and requires a valid log in (eg. signature or user/pass)
3. There is also a simple admin interface page where a database refetch check can be performed. A simple button in the admin section to update a single link is on the way.

#### The catch
While this doesn't cause the developers of third party applications to give attetnion to their code, it does make integration more flexible and provides an immediate working solution on YOURLS's end, akin to the [api-concurrence-fix](https://bitbucket.org/laceous/yourls-concurrency-fix) plugin.

#### Note:
This module was initially developed to assist with integration of the [Drupal: Shorten URLs](https://www.drupal.org/project/shorten) module. An issue regarding this can be found [here](https://www.drupal.org/node/2889342).
213 changes: 187 additions & 26 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Plugin Name: Title Refetch
Plugin URI: https://github.com/joshp23/YOURLS-title-refetch
Description: Refetch poorly defined titles
Version: 0.2.0
Version: 1.0.0
Author: Josh Panter
Author URI: https://unfettered.net
*/
Expand Down Expand Up @@ -49,29 +49,9 @@ function title_refetch_do_page() {
</div>
HTML;

}

// Title Refetch on Stats Page Load (if sharebox is enabled)
yourls_add_filter( 'share_box_data', 'title_refetch_share' );
function title_refetch_share( $data ) {

if( $data['title'] !== '' ) {

if( in_array( yourls_get_protocol( $data['title'] ), array( 'http://', 'https://' ) ) ) {

$data['title'] = yourls_get_remote_title( $data['longurl'] );

$keyword = str_replace( YOURLS_SITE . '/' , '', $data['shorturl'] );
yourls_edit_link_title( $keyword, $data['title'] );

echo '<h3 style="color:green;">New Title: ' . $data['title'] . '</h3>';
}
}
return $data;
}

// Mass Title Refetch Via Admin Page
// TODO consider maybe a button in the admin page?
// Mass Title Refetch
function title_refetch_batch_do() {
global $ydb;
$table = defined( 'YOURLS_DB_PREFIX' ) ? YOURLS_DB_PREFIX . 'url' : 'url';
Expand All @@ -89,9 +69,190 @@ function title_refetch_batch_do() {
}
}
}
if( $i > 0 ) {
echo '<p style="color:green;">Total URL title updates: ' . $i . '</p>';
if( yourls_is_API() ) {
return $i;
} else {
echo '<p style="color:green;">No URL title updates needed at this time.</p>';

if( $i > 0 ) {
echo '<p style="color:green;">Total URL title updates: ' . $i . '</p>';
} else {
echo '<p style="color:green;">No URL title updates needed at this time.</p>';
}
}
}

// API addition to action=shorturl - this is the basic fix
yourls_add_action( 'post_add_new_link', 'title_refetch_api_add' );
function title_refetch_api_add( $data ) {

if( yourls_is_API() ) {

if ( isset( $_REQUEST['refetch'] ) && ( $_REQUEST['refetch'] == 'true') ) {

if( in_array( yourls_get_protocol( $data[2] ), array( 'http://', 'https://' ) ) ) {

$data[2] = yourls_get_remote_title( $data[0] );

yourls_edit_link_title( $data[1], $data[2] );
}
}
}
}
// API-Updates
yourls_add_filter( 'api_action_refetch', 'title_refetch_api' );
function title_refetch_api() {

// We need a target for the refetch
if( !isset( $_REQUEST['target'] ) ) {
return array(
'statusCode' => 400,
'simple' => "Need a 'target' parameter",
'message' => 'error: missing param',
);
}

// That target must be precise
if( !in_array( $_REQUEST['target'], array( 'title', 'all' ) ) ) {
return array(
'statusCode' => 400,
'simple' => "Key: 'target' must match Value: 'title', or 'all'.",
'message' => 'error: missing param',
);
}

// Refetch Single Title
if( $_REQUEST['target'] == 'title' ) {

// We need a short url to work with
if( !isset( $_REQUEST['shorturl'] ) ) {
return array(
'statusCode' => 400,
'simple' => "Need a 'shorturl' parameter",
'message' => 'error: missing param',
);
}

$shorturl = $_REQUEST['shorturl'];
$keyword = str_replace( YOURLS_SITE . '/' , '', $shorturl ); // accept either 'http://ozh.in/abc' or 'abc'

$keyword = yourls_sanitize_string( $keyword );
$url = yourls_get_keyword_longurl( $keyword );
$title = yourls_get_keyword_title( $keyword );

$do = title_refetch_do( $url, $keyword, $title );

if( $do ) {
switch ($do) {
case 1:
$code = 200;
$simple = "Title refetched: unchanged.";
$msg = 'success: refetched';
break;
case 2:
$code = 200;
$simple = "Title refetched: updated.";
$msg = 'success: refetched';
break;
case 3:
$code = 200;
$simple = "No refetch required.";
$msg = 'success: no refetch';
break;

default:
$code = 200;
$simple = "Title refetched.";
$msg = 'success: refetched';
}

return array(
'statusCode' => $code,
'simple' => $simple,
'message' => $msg,
);
} else {
return array(
'statusCode' => 500,
'simple' => 'Error: could not refetch title, not sure why :-/',
'message' => 'error: unknown error',
);
}
}

// Refetch Entire DB
if( $_REQUEST['target'] == 'all' ) {

$auth = yourls_is_valid_user();
if( $auth !== true ) {
$format = ( isset($_REQUEST['format']) ? $_REQUEST['format'] : 'xml' );
$callback = ( isset($_REQUEST['callback']) ? $_REQUEST['callback'] : '' );
yourls_api_output( $format, array(
'simple' => $auth,
'message' => $auth,
'errorCode' => 403,
'callback' => $callback,
) );
}

$do = title_refetch_batch_do();

if( $do ) {

if( $do == 0 ) {
$code = 200;
$simple = "No refetching required";
$msg = 'success: nothing refetched';
}

elseif( $do > 0 ) {
$code = 200;
$simple = $do . " titles refetched.";
$msg = 'success: refetched';
}

else {
$code = 200;
$simple = "Titles checked and refetched.";
$msg = 'success: refetched';
}

return array(
'statusCode' => $code,
'simple' => $simple,
'message' => $msg,
);

} else {
return array(
'statusCode' => 500,
'simple' => 'Error: could not refetch database titles, not sure why :-/',
'message' => 'error: unknown error',
);
}
}
}

// Title Refetch for API-Updates
function title_refetch_do( $url, $keyword, $title ) {

if( in_array( yourls_get_protocol( $title ), array( 'http://', 'https://' ) ) ) {

$title_refetch = yourls_get_remote_title( $url );

if( $title == $title_refetch ) {

$data = 1;

} else {

yourls_edit_link_title( $keyword, $title_refetch );
$data = 2;
}

} else {

$data = 3;
}

return $data;
}

0 comments on commit 0a01984

Please sign in to comment.