FastBacklinks starter

Goal:

The script was created to make the FastBacklinks recipe for PmWiki easier to use. It pre-generates all data required by the script, so that you don\'t need to manually touch every Wiki page after installing the recipe. Follow the instructions below to use it.

Usage:

This script should be executed after you install the FastBacklinks recipe in your PmWiki installation. It prepares all required backlinks data, based on existing Wiki data in a specified directory.

The script should be best put in your main PmWiki directory - in the same directory where you have the pmwiki.php file.

The script should be run for each directory you have Wiki data, that is in a standard, non-WikiFarm installation, you should run it two times, with the Source Directory option set to, in specified order:

  1. wikilib.d
  2. wiki.d

Two steps with those directories already set have been prepared below for your convenience. If you have your data in other directories, you must change the Source Directory options in those steps accordingly.

Note:

'; TimeoutExtend(); echo '

Step 1:




Step 2:



'; exit; } echo( 'FastBacklinks generation

FastBacklinks generation started, please wait...

'); ## Trying to extend the PHP's script execution timeout TimeoutExtend(); flush(); ob_flush(); $src = rtrim( $_REQUEST['src'], '/' ); $dest = rtrim( $_REQUEST['dest'], '/' ); mkdirp($dest); $dh = opendir( $src ); if( $dh === false ) Abort( "Error opening directory \'$src\'." ); $targets_str = 'targets='; $targets_len = strlen( $targets_str ); ## Start measuring time. $starttime = microtime(); while (($wikifname = readdir($dh)) !== false) { $wikifile = $src . '/' . $wikifname; # echo( trim($wikifile) . ' = ' ); $wikifile = @file( $wikifile ); /* #### Version 1 - substr() foreach( $wikifile as $line ) { if( substr( $line, 0, $targets_len ) == $targets_str ) { echo( $line ); break; } } */ #### /* #### Version 2 - strpos() foreach( $wikifile as $line ) { if( strpos( $line, 'targets=' ) === 0 ) { echo( $line ); break; } } */ #### /* #### Version 3 - preg_match() + implode() $wikifile = implode( '', $wikifile ); $matches = array(); if( preg_match( '/\ntargets=.*?\n/', $wikifile, $matches ) ) { echo( $matches[0] ); } */ #### #### Version 4 - strpos() + implode() $wikifile = implode( '', $wikifile ); $start = strpos( $wikifile, 'targets=' ); if( $start !== false ) { $start += $targets_len; $end = strpos( $wikifile, "\n", $start ); if( $end !== false ) { #echo( substr( $wikifile, $start, $end-$start ) ); $targets = substr( $wikifile, $start, $end-$start ); } } #### $targets = trim($targets); if( !$targets ) continue; $targets = explode( ',', $targets ); ## Adding backlink info to each target. foreach( $targets as $t ) { $fh = @fopen( $dest . '/' . $t, 'a' ); if( $fh === false ) { Warn( "Error opening file '$dest/$t' for appending!" ); break; } fwrite( $fh, $wikifname . "\n" ); fclose( $fh ); fixperms( $dest . '/' . $t ); } # echo( '
' ); } ## Stop measuring time, show the result. $stoptime = microtime(); $starttime = array_sum( explode( ' ', $starttime ) ); $stoptime = array_sum( explode( ' ', $stoptime ) ); closedir($dh); echo '

FastBacklinks generation ended

'; echo '

Please, press the "Back" button and proceed to next step, if any.

'; echo '
Backlinks generated in ' . sprintf("%01.5f",$stoptime-$starttime) . ' sec
'; echo "

Source directory was: $src, Destination directory was: $dest.

"; function Warn( $t ) { echo "Warning: $t - Backlinks generation may not be completed!
"; } ## Thanks to haganfox at gmail.com for the idea. Hope it helps. function TimeoutExtend() { $timeout = ini_get('max_execution_time'); echo( '

PHP script execution timeout defaults here to: ' . $timeout . ' sec. ' ); if( ini_get('safe_mode') ) { echo 'Unfortunately, safe_mode is set, so we cannot extend it, ' . 'but don\'t worry, there may not be any problem.'; } else { $timeout = max( $timeout, 10*60 ); echo 'New timeout will now be set to: ' . $timeout . ' sec (' . $timeout/60 . ' min).'; ini_set( 'max_execution_time', $timeout ); } echo '

'; } ############################################### ## Required PmWiki functions extract ## (from: pmwiki.php, by Patrick Michaud) ## mkdirp creates a directory and its parents as needed, and sets ## permissions accordingly. function mkdirp($dir) { global $ScriptUrl; if (file_exists($dir)) return; if (!file_exists(dirname($dir))) mkdirp(dirname($dir)); if (mkdir($dir, 0777)) { fixperms($dir); if (@touch("$dir/xxx")) { unlink("$dir/xxx"); return; } rmdir($dir); } $parent = realpath(dirname($dir)); $perms = decoct(fileperms($parent) & 03777); $msg = "PmWiki needs to have a writable $dir/ directory before it can continue. You can create the directory manually by executing the following commands on your server:
    mkdir $parent/$dir\n    chmod 777 $parent/$dir
Then, reload this page."; $safemode = ini_get('safe_mode'); if (!$safemode) $msg .= "

Or, for a slightly more secure installation, try executing
    chmod 2777 $parent
on your server and following this link. Afterwards you can restore the permissions to their current setting by executing
    chmod $perms $parent
."; Abort($msg); } ## fixperms attempts to correct permissions on a file or directory ## so that both PmWiki and the account (current dir) owner can manipulate it function fixperms($fname, $add = 0) { clearstatcache(); if (!file_exists($fname)) Abort('no such file'); $bp = 0; if (fileowner($fname)!=fileowner('.')) $bp = (is_dir($fname)) ? 007 : 006; if (filegroup($fname)==filegroup('.')) $bp <<= 3; $bp |= $add; if ($bp && (fileperms($fname) & $bp) != $bp) @chmod($fname,fileperms($fname)|$bp); } function Abort($msg) { # exit pmwiki with an abort message echo "

FastBacklinks-starter script can't process your request

$msg

We are sorry for any inconvenience.

"; exit; }