Replace Action

See Also DRAFT
  • action works, but userinterface needs attention...
DRAFT - NOT Included in any Wikka version yet
Last edited by OnegWR:
Replaces old-style internal links with new pipe-split links.
Fri, 20 May 2016 07:38 UTC [diff]

What

Use

Examples

Parameters
nametyperequired?defaultdescription
aaastringoptionalempty...


Features

Installation

Code
<?php
/**
 * Find and replace strings in (all) pages.
 *
 * With some tools you can shoot yourself in the foot,
 * aim wrong with this action, and both your legs are gone...
 * Extremely powerfull, equaly dangerous !
 *
 * DRAFT - fully working, but userinterface needs attention
 *
 * For syntax: see the php-manual for preg_match and preg_replace
 *
 * UNDO is possible via the normal page revisions / history
 *    as all saved changes have the same note (with a timestamp)
 *    it is easy to undo a replacement via e.g. phpMyAdmin - tool
 *
 * @package        Action
 * @subpackage     AdminModules
 * @name           Replace
 *
 * @author         {@link http://wikkawiki.org/OnegWR OnegWR}
 * @license        http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
 * @copyright      Copyright (c) 2005, OnegWR
 * @version        0.1
 *
 * @input          string replace_page - preg_match/grep on pagename/tag
 *                 to limit the number of pages to change (30sec timeout workaround)
 *                 default: / .* / - all pages
 * @input          string replace_find - pattern to find
 *                 default: // - match any
 * @input          string replace_by
 *                 default: EMPTY - delete found pattern
 * @input          boolean replace_save - confirm write to db
 *                 on = save the changes
 *                 default: off - changes not saved
 *
 * @output         (list of changed pages)
 *
 *
 * @todo           - Benchmark: what can be done in 30sec
 *                 - i18n
 *                 - GET requests (only POST now)
 *                 - count changes (+preview ?)
 *
 * @bugs           - if you start two replace jobs in the same sec
 *                    no individual 'undo' can be done
 */


$replace = array();

if ( $this->IsAdmin() )
{
    if ( isset( $_REQUEST['submit'] ) && $_REQUEST['submit']=='Replace' )
    {
        $replace['page'] = ($_REQUEST['replace_page']<>'') ? $_REQUEST['replace_page'] : '/.*/';
        $replace['find'] = ($_REQUEST['replace_find']<>'') ? $_REQUEST['replace_find'] : '//';
        $replace['by'] = $_REQUEST['replace_by'];
        $replace['save'] = (isset( $_REQUEST['replace_save'] ) && $_REQUEST['replace_save']=='on') ? TRUE : FALSE;
       
        $replace_arr = $this->LoadAllPages();
        foreach ( $replace_arr as $key => $value )
        {
            if ( preg_match( $replace['page'], $value['tag'] ) )
            {
                if ( !preg_match( $replace['find'], $value['body'] ) )
                {
                    unset( $replace_arr[$key] );    // hasn't got string to find in body
                }

            }
            else
            {
                unset( $replace_arr[$key] );    // Not right page/tag name
            }
        }

        // OnegWR: at this point only related pages are left...
        if ( $replace_arr == array() )
        {
            print "\nNo pages found matching both requirements - nothing to replace...<br />\n";
        }
        else
        {
            $replace['undo'] = "SnR". date('YmdHis');
            foreach ( $replace_arr as $key => $value )
            {
                if ( $replace['save'] )
                {
                    $replace_arr[$key]['body'] = preg_replace( $replace['find'], $replace['by'], $value['body'] );
                    $this->SavePage( $value['tag'], $replace_arr[$key]['body'], $replace['undo'] );

                    print $this->Link( $value['tag'], "", "", 0 ) .
                        " (". $this->Link( $value['tag'], "revisions", $timeformatted, 0, 1, "View recent revisions list for ".$value['tag']).")" .
                        " [". $this->Link( $value['tag'], "history", "history", 0, 1, "View edit history of ".$value['tag'])."]" .
                        "<br />\n";
                    //print_r( $replace_arr[$key] );
                }
                else
                {
                    // test only... nothing is saved...
                    print $this->Link( $value['tag'], "", "", 0 ) ."<br />\n";
                }
            }
            if ( $replace['save'] )
            {
                print "\n<br /><br />Undo trigger in note: ". $replace['undo'] ."<br />\n\n";
            }
        }
    }


    // print form
    print $this->FormOpen("","","post");
    print <<< EOT
        <fieldset>
        <legend>Search and replace strings in (all) pages</legend>
        {$replace_error}
        <label for="replace_page">Page grep: </label><input type="text" name="replace_page" value="{$replace['page']}" size="10" maxlength="50" title="Limit the number of pages"><br />
        <label for="replace_find">Find string: </label><input type="text" name="replace_find" value="{$replace['find']}" size="10" maxlength="50"><br />
        <label for="replace_by">Replace with: </label><input type="text" name="replace_by" value="{$replace['by']}" size="10" maxlength="50"><br />
        <label for="replace_save">Save changes? </label><input type="checkbox" name="replace_save" size="10" maxlength="50"><br />
        <label for="replace_submit"> </label><input type="submit" name="submit" value="Replace">
        </fieldset>
EOT;
    print $this->FormClose();

}
else
{
    // no error if not admin...
}
?>

Demo

To Do


CategoryUserContributions
There are no comments on this page.
Valid XHTML :: Valid CSS: :: Powered by WikkaWiki