Skip to content

Commit

Permalink
Create own title instead of relying on YOURLS to fetch one.
Browse files Browse the repository at this point in the history
  • Loading branch information
vaughany committed Jul 25, 2020
1 parent 7d84883 commit 7c4db75
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 28 deletions.
51 changes: 27 additions & 24 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,12 @@
Plugin Name: Bulk Import and Shorten
Plugin URI: https://github.com/vaughany/yourls-bulk-import-and-shorten
Description: A YOURLS plugin allowing importing of URLs in bulk to be shortened or (optionally) with a custom short URL.
Version: 0.1
Release date: 2014-07-17
Version: 0.2
Release date: 2020-07-25
Author: Paul Vaughan
Author URI: http://github.com/vaughany/
*/

/**
* TODO:
* Write the plugin!
*/

/**
* https://github.com/YOURLS/YOURLS/wiki/Coding-Standards
* https://github.com/YOURLS/YOURLS/wiki#for-developpers
Expand All @@ -36,20 +31,20 @@ function vaughany_bias_add_page() {
}

function vaughany_bias_display_page() {
echo '<h2>Bulk Import and Shorten</h2>' . "\n";
echo '<p>Import links as long URLs and let YOURLS shorten them for you according to your settings.</p>' . "\n";
echo '<p>Upload a .csv file in the following format:</p>' . "\n";
echo '<ul><li>First column - required: a long URL</li><li>Second column - optional: a short URL of your choosing (otherwise one will be created by YOURLS according to your settings)</li></ul>' . "\n";
echo '<p>I don\'t know what will happen if two short links point to the same long link - this might or might not be allowed, according to your settings.</p>' . "\n";
//echo '<p></p>' . "\n";

echo '<h3>Import</h3>' . "\n";
echo '<form action="' . yourls_remove_query_arg( array( 'import', 'export', 'nonce', 'action' ) ) . '" method="post" accept-charset="utf-8" enctype="multipart/form-data">' . "\n";
//echo '<form action="" method="post" accept-charset="utf-8" enctype="multipart/form-data">' . "\n";
echo '<h2>Bulk Import and Shorten</h2>' . PHP_EOL;
echo '<p>Import links as long URLs and let YOURLS shorten them for you according to your settings.</p>' . PHP_EOL;
echo '<p>Upload a .csv file in the following format:</p>' . PHP_EOL;
echo '<ul><li>First column - required: a long URL</li><li>Second column - optional: a short URL of your choosing (otherwise one will be created by YOURLS according to your settings)</li></ul>' . PHP_EOL;
echo '<p>I don\'t know what will happen if two short links point to the same long link - this might or might not be allowed, according to your settings.</p>' . PHP_EOL;
//echo '<p></p>' . PHP_EOL;

echo '<h3>Import</h3>' . PHP_EOL;
echo '<form action="' . yourls_remove_query_arg( array( 'import', 'export', 'nonce', 'action' ) ) . '" method="post" accept-charset="utf-8" enctype="multipart/form-data">' . PHP_EOL;
//echo '<form action="" method="post" accept-charset="utf-8" enctype="multipart/form-data">' . PHP_EOL;
echo yourls_nonce_field( 'vaughany_bias_import', 'nonce', false, false );
echo '<input type="file" name="import" value="">' . "\n";
echo '<input type="submit" name="import" value="Upload">' . "\n";
echo '</form>' . "\n";
echo '<input type="file" name="import" value="">' . PHP_EOL;
echo '<input type="submit" name="import" value="Upload">' . PHP_EOL;
echo '</form>' . PHP_EOL;

}

Expand Down Expand Up @@ -97,21 +92,29 @@ function vaughany_bias_import_urls( $file ) {
// Get each line in turn as an array, comma-separated.
while ( $csv = fgetcsv( $fh, 1000, ',' ) ) {

$url = $keyword = $title = '';

$url = trim( $csv[0] );

// Trim out cruft and slashes.
$keyword = trim( str_replace( '/', '', $csv[1] ) );
$new_keyword = trim( str_replace( '/', '', $csv[1] ) );

// If the requested keyword is not free, use nothing.
if ( !yourls_keyword_is_free( $keyword ) ) {
$keyword = '';
if ( yourls_keyword_is_free( $new_keyword ) ) {
$keyword = $new_keyword;
}

// New in v0.2: Creating a title from the URL, so one is not fetched from the URL, slowing down the import.
$title = trim( str_replace(['http://', 'https://', '/'], '', $csv[0]) );

// Add a new link (passing the keyword) and get the result.
$result = yourls_add_new_link( trim( $csv[0] ), $keyword );
$result = yourls_add_new_link( $url, $keyword, $title );

if ( $result['status'] == 'success' ) {
$count++;
}
}

} else {
yourls_add_notice('File handle is bad.');
}
Expand Down
19 changes: 15 additions & 4 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ Plugin for [YOURLS](http://yourls.org) 1.7.x.

* Plugin URI: [github.com/vaughany/yourls-bulk-import-and-shorten](https://github.com/vaughany/yourls-bulk-import-and-shorten)
* Description: A YOURLS plugin allowing importing of URLs in bulk to be shortened or (optionally) with a custom short URL.
* Version: 0.1
* Release date: 2014-07-17
* Version: 0.2
* Release date: 2020-07-25
* Author: Paul Vaughan
* Author URI: [github.com/vaughany](http://github.com/vaughany/)

Expand Down Expand Up @@ -35,7 +35,7 @@ This plugin has no user-configurable options. If you know what you're doing you

This plugin expects you to upload a CSV file with at least one column and an optional second column. The first column should contain the long URL of the 'target', e.g. http://bbc.co.uk. The optional second column can contain a keyword you would like to associate with this URL, if you don't want YOURLS to generate one for you.

Note: In this repository is a file called `test.csv`, which you can use as an example.
Note: In this repository is a file called `test.csv`, which you can use as an example.


### No keywords
Expand Down Expand Up @@ -75,11 +75,21 @@ If you supply keywords in the second column of the CSV file, then when imported,
* If you want to use hyphens / dashes in your short URLs, you need to activate the 'Allow hyphens in short URLs' plugin which comes with YOURLS.

> `http://bbc.co.uk/,bbc-news` --> Without the plugin activated, will become 'bbcnews'. With the plugin activated, will import as-is.
> `http://bbc.co.uk/,bbc-news` --> Without the plugin activated, will become 'bbcnews'. With the plugin activated, will import as-is.
I have talked through some common plugins and the outcomes of various situations, but please check carefully the short URLs of bulk-imported long URLs to ensure everything is as you expect. Due to the way plugins can hook into YOURLS I cannot know what plugins are installed and how they may affect how a short URL is created.


## Troubleshooting

One user experienced timeout issues processing a CSV file containing ~60,000 rows. I've not investigated this issue thoroughly, but the 0.2 version contains a line that creates a title from the supplied URL instead of letting YOURLS CURLing the URL and extracting a title from the returned HTML. I believe this to be faster, but I have only done a little testing.

Some ideas, if processing a large CSV file:

* Change the `max_execution_time` setting in your `php.ini` file from it's default of 30 seconds.
* Change the `max_input_time` setting in your `php.ini` file from it's default of 60 seconds.


## License

Uses YOURLS' license, aka *"Do whatever the hell you want with it"*. I borrowed some code from others (including [GautamGupta](https://github.com/gautamgupta/yourls-Import-Export) who in turn borrowed from [John Godley](http://urbangiraffe.com/plugins/redirection/)) whose code had no licence so I can't claim this whole plugin as my own work, but the lion's share of it is.
Expand All @@ -96,6 +106,7 @@ I'm always keen to add new features, improve performance and squash bugs, so if

## History

* 2020-07-25, v0.2: From a bug report via email about it running slowly processing thousands of rows, I've attempted a 'fix' by creating a title from the URL and passing that to the YOURLS function that would otherwise attempt to fetch one from the URL's HTML.
* 2014-07-17, v0.1: Still a work in progress.

## Finally...
Expand Down

0 comments on commit 7c4db75

Please sign in to comment.