Wikilink action table

Outputs a table of wikilinks with accompanying delete or move links; see in-depth instructions below.

Source code (hide)

Global files are not shown below.

Table of contents
  1. index.php

index.php

back to toc
<?php
/* globals, templates */
$locals = array (
    
'title'       => 'Wikilink action table',
    
'description' => 'Outputs a table of wikilinks with accompanying delete or move links; see in-depth <a href="#instructions">instructions below</a>.',
    
'files'       => array('index.php')
);
include(
'../globals/globals.php');

/************
** variables
************/
$input = array(
    
'text'   => stripslashes($_POST['text']),
    
'action' => ($_POST['action']) ? $_POST['action'] : 'delete',
    
'reason' => stripslashes($_POST['reason']),
    
'search' => stripslashes($_POST['search']),
    
'replace'=> stripslashes($_POST['replace']),
    
'url'    => ($_POST['url']) ? stripslashes($_POST['url']) : 'http://en.wikisource.org/w/index.php?title=',
    
'wikiML' => $_POST['wikiML']
);

/************
** parse wikilinks
************/
preg_match_all('/\[\[([^\]\|]*)[^\]]*\]\]/',$input['text'],$titles['input']);
$titles['input'] = $titles['input'][1];
$count count($titles['input']);

/************
** error-checking
************/
if($_REQUEST) {
    if(!
$count) {
        echo 
'<p class="fail">No wikilinks were found. Make sure that they are in the form [[title]].</p>';
    }
    else if(
$input['action']=='move' && empty($input['search'])) {
        echo 
'<p class="fail">No search pattern specified. Please fill in the form completely.</p>';
    }
    else {
        
/************
        ** generate namespace-adjusted titles
        ************/
        
$titles['main'] = array();
        
$titles['talk'] = array();
        
$i 0;
        
        foreach(
$titles['input'] as $title) {
            
$nsRegex '/^(user|meta|image|mediawiki|template|help|category)(?: talk)?:(.+)$/i';
            if(
preg_match($nsRegex,$title)) {
                
preg_match($nsRegex,$title,$matches);
                
$titles['main'][$i] = $matches[1] . ':' $matches[2];
                
$titles['talk'][$i] = $matches[1] . '_talk:' $matches[2];
            }
            else {
                
$titles['main'][$i] = $title;
                
$titles['talk'][$i] = 'talk:' $title;
            }
            
$i++;    
        }

        
/************
        ** move: generate new titles
        ************/
        
if($input['action']=='move') {
            
$titles['renamedmain'] = array();
            
$titles['renamedtalk'] = array();
            
$i=0;
            
            
/* regex */
            
if(preg_match('/^#[^#]*#(?:[^#]*#)?$/',$input['search'])) {
                for(
$i=0$i<$count$i++) {
                    
$titles['renamedmain'][$i] = preg_replace($input['search'],$input['replace'],$titles['main'][$i]);
                    
$titles['renamedtalk'][$i] = preg_replace($input['search'],$input['replace'],$titles['talk'][$i]);
                }
            }
            
/* string */
            
else {
                for(
$i=0$i<$count$i++) {
                    
$titles['renamedmain'][$i] = str_replace($input['search'],$input['replace'],$titles['main'][$i]);
                    
$titles['renamedtalk'][$i] = str_replace($input['search'],$input['replace'],$titles['talk'][$i]);
                }
            }
        }
    
        
/************
        ** generate URL format
        ************/
        
function buildUrl($namespace,$i) {
            global 
$input$titles;
            if(
$input['action']=='move') {
                return 
$input['url'] . 'Special:Movepage/' urlencode($titles[$namespace][$i]) . '&wpNewTitle=' urlencode($titles['renamed' $namespace][$i]) . '&wpReason=' urlencode($input['reason']);
            }
            else {
                return 
$input['url'] . urlencode($titles[$namespace][$i]) . '&action=delete&wpReason=' urlencode($input['reason']);
            }
        }
    
        
/************
        ** generate output
        ************/
        
echo '<h2>Generated table</h2>';
        
        if(
$input['wikiML']) {
            echo 
'<div><textarea rows="7" cols="100" readonly="readonly">{| class="prettytable"' "\n"
                
'! wikilink !! talk !! ' $input['action'];
            for(
$i=0$i<$count$i++) {
                if(
$input['action']=='move' && !$titles['renamedmain'][$i]) {
                    
// skip
                
}
                else {
                    echo 
"\n"
                        
'|-' "\n"
                        
'| [[' $titles['main'][$i] . ']]' "\n"
                        
'| [[' $titles['talk'][$i] . '|talk]]' "\n"
                        
'| <span class="plainlinks">[' buildUrl('main',$i) . ' main] [' buildUrl('talk',$i) . ' talk]</span>';
                }
            }
            echo 
"\n" '|}</textarea></div>';
        }
        else {
            
?>
            <table class="prettytable">
                <tr>
                    <th>wikilink</th>
                    <th>talk</th>
                </tr>
            <?php
            
for($i=0$i<$count$i++) {
                if(
$input['action']=='move' && !$titles['renamedmain'][$i]) {
                    
// skip
                
}
                else {
                    
?>
                    <tr>
                        <td><a href="<?php echo buildUrl('main',$i); ?>" title="<?php echo $input['action'] . ' ' $titles['main'][$i]; ?>"><?php echo $titles['main'][$i]; ?></a></td>
                        <td><a href="<?php echo buildUrl('talk',$i); ?>" title="<?php echo $input['action'] . ' ' $titles['talk'][$i]; ?>">talk</a></td>
                    </tr>
                    <?php
                
}
            }
            echo 
'</table>';
        }
    }
}

/* debug */
gDebug(get_defined_vars());

/* input form */
?>
<h2>Input</h2>
<form action="index.php" method="post">
    <label for="text">List of wikilinks:</label><br />
    <textarea
        id="text"
        name="text" rows="7" cols="100"><?php echo htmlspecialchars($input['text']); ?></textarea><br />
    <input
        type="text"
        id="reason"
        name="reason"
        rows="2"
        cols="100"
        value="<?php echo $input['reason']; ?>"
        style="width:20em;"
    /> <label for="reason">reason</label><br />
    <input
        type="text"
        id="url"
        name="url"
        value="<?php echo htmlspecialchars($input['url']); ?>"
        style="width:20em;"
    /> <label for="url">base URL</label><br /><br />
    <table>
        <tr>
            <th colspan="2">Type of action</th>
        </tr>    
        <tr valign="top">
            <td>
                <input
                    type="radio"
                    id="action_delete"
                    name="action"
                    value="delete"
                    <?php gCheckRadio($input['action'],'delete'); ?>
                /> <label for="action_delete">delete links</label>
            </td>
            <td>
                <input
                    type="radio"
                    id="action_move"
                    name="action"
                    value="move"
                    <?php gCheckRadio($input['action'],'move'); ?>
                /> <label for="action_move">move links</label><br />
                <input
                    type="text"
                    id="search"
                    name="search"
                    value="<?php echo $input['search']; ?>"
                /> <label for="search">match pattern</label><br />
                <input
                    type="text"
                    id="replace"
                    name="replace"
                    value="<?php echo $input['replace']; ?>"
                /> <label for="search">replace with</label><br />
            </td>
        </tr>
    </table>
    <fieldset>
        <legend>Extended options</legend>
        <input
            type="checkbox"
            id="wikiML"
            name="wikiML"
            <?php gCheckBox($input['wikiML']); ?>
        />
        <label for="wikiML">Output wiki code (instead of showing table on this page)</label><br />
        <?php gDebugOption(); ?>
    
        <input type="submit" value="create table" /><br />
        <input type="reset" value="reset form" />
    </fieldset>
</form>

<h2 id="instructions">Instructions</h2>
<ol>
    <li>List of wikilinks: any text containing wikiML links in the format [[page name]]. All non-link text will be ignored.</li>
    <li>Reason (optional): the reason to use in the wiki form.</li>
    <li>Base Url: the wiki's action URL, in the format with "?title=".</li>
    <li>Type of action: Select the action you want to do (delete or move pages).</li>
    <li>moving pages: Enter search and replace patterns. For example, you can rename pages from <b>"Title: Subpage"</b> to <b>"Title/Subpage"</b> by entering <b>"Title: "</b> and <b>"Title/"</b>. You can use regex with the syntax #pattern# or #pattern#modifiers# (including all the # symbols; other syntax will be treated as a string).</li>
</ol>


<?php
/* globals, templates */
makeFooter();
?>

No wikilinks were found. Make sure that they are in the form [[title]].

Input






Type of action



Extended options


Instructions

  1. List of wikilinks: any text containing wikiML links in the format [[page name]]. All non-link text will be ignored.
  2. Reason (optional): the reason to use in the wiki form.
  3. Base Url: the wiki's action URL, in the format with "?title=".
  4. Type of action: Select the action you want to do (delete or move pages).
  5. moving pages: Enter search and replace patterns. For example, you can rename pages from "Title: Subpage" to "Title/Subpage" by entering "Title: " and "Title/". You can use regex with the syntax #pattern# or #pattern#modifiers# (including all the # symbols; other syntax will be treated as a string).

This tool is written and copyright by Jesse Plamondon-Willard (Pathoschild). You may freely use, distribute, and modify this script in any way and for any purpose, so long as you cite the above name as original author.