YOURLS/YOURLS — pull request #1546 by claytondaley (2013-11-20 02:31:47+00:00)
Title: More explicit link creation logic

I started to dig into `yourls_add_new_link` since I anticipated either modify it or parsing it. The code was extremely clean, but it applied different logic depending on the situation:

- In some cases (i.e. duplicate links are disabled), it will return a pre-existing shorturl with a success (200) code even if the pre-existing keyword does not match the submitted keyword.
- In other cases, (i.e. duplicate links are enabled), the function fails without returning an existing keyword.
- I believe this will fail even if the exact same url-keyword combination already exists.

After some thought, there seemed to be two key dimensions that could be built in a generic function:
- Is the function required to create a new link or may it return an existing link?
- For example, a caller might use a shorturl to track clicks from a referrer and want to ensure that the stats start at zero the day it's created.
- Alternatively, the caller may be willing to accept an old shorturl if it meets the restrictions specified.
- Must the function use the keyword and/or title provided or may it obtain an alternate value (generate a new keyword or load the title from the target URL)?

With that in mind, I created two new function:
- `yourls_create_link` which is only able to create links (named accordingly) and strictly enforces url, keyword, and title
- `yourls_get_or_create_link` with additional parameters `$strict_create`, `$strict_keyword`, and `$strict_title`. I arbitrarily decided that it should (if given the option) get an existing link before creating a new link (and named the function accordingly).

To avoid duplicate code and clarify the logic used in each situation, I also refactored `yourls_add_new_link` to use the new code and heavily documented the change.

NOTE: This pull includes (and requires) the `yourls_get_longurl_keywords` pull request.