User:Cpiral/relink.pl

This is listed at  (starting 17 Dec 2015) after being used dozens of times to cleanup redlinks posted at category:wikipedia red link cleanup.

Purpose
Purposes:
 * Give a count the total number of links.
 * Give a count of each unique link.
 * Modify what is linked.
 * Cleanup redlinks at category:wikipedia red link cleanup.
 * Cleanup wp:overlinking.
 * Add links to underrepresented pages to promote them more widely.

Usage
Given some wikitext it can list all the links. This list becomes your links-configuration file. You edit it to remove links. To add links, you type up a list the links you want to add and make that your links-configuration file. Then you rerun the script against the wikitext to produce the desired linkage for that wikitext.

See the output of relink -h for usage and instructions. You'll need perl 5 and its getopts module from CPAN.

Use redirection or piping to specify output source files. You name your own input, output, and configuration files.

Use command-line options to keep, remove, or add the links in your links configuration filename.
 * -l source_filename to list, or to create your links configuration file.
 * -k links_configfile to keep
 * -r links_configfile to remove
 * -a links_configfile to add

So to modify the way a file is linked, you can
 * add links from a list you wrote (a links-configuration file).
 * remove links listed in an auto-generated links-configuration file you edited.
 * keep links listed in an auto-generated links-configuration file you edited.

Save the output of relink -l to generate the links configfile. All the links are listed, and they're in the order they were found. Then, while viewing both the rendered page and the links configuration file, you use the rendered page to decide where to jump to in the configuration file to do the removal of links. The editing is only the removal of one or more lines. What remains may be what's kept or whats removed from the linkage.

For example to cleanup redlinks, first gague which is greater, the redlinks or the blue links. If most of the links are blue, remove redlinks and use relink -k. If most of the links are red, remove blue links and use relink -r.

Examples
What is outside the link does not count for uniqueness. $ cat wikitext link label 3linked 4labelling

$ relink -c wikitext 2 link 2 link|label 4 total wikilinks

$ relink -l wikitext link link|label 2 unique wikilinks

Remove or keep
$ cat wikitext title label title3ed  4labelling

$ relink -l wikitext > links 4 unique wikilinks

$ cat links title title|label title3 title4|label

Editing the file we called links here, and removing two lines...

$ cat links title3 title4|label

Here's two opposite uses of the remaining two lines, for the sake of example.

$ relink -r links < wikitext title label title3ed  4labelling 2 links removed

$ relink -k links < wikitext title label title3ed  4labelling 2 links removed

To save output, use redirection $ relink -r links processed_file

You can use the processed file to act as new wikitext to do more linkage configuration before uploading the final processed_file to the edit box.

Add
$ cat wikitext title label label label title title

$ cat promote title title | label

$ relink -a promote < wikitext title label label label title title 2 links added.

$ relink -ma promote < wikitext title label label label title title 4 links added.