Hi Anders,
Here’s my final code, including :
– A log function to allow debugging, plus my detailed debug lines,
– Use of variables to make S&R code more readable, debug-friendly and customizable,
– Customization of the 1st search’s $strSearch value according to my configuration.
/**
* Send to debug.log if WP_DEBUG is defined and true
*
* @since 2.2.0
*
* @param string $function Function name
* @param int $line Line number
* @param mixed $msg Message to output
* @param mixed $debug Variable to print_r
*/
public static function log_if_debug( $function, $line, $msg, $debug = null ) {
if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
error_log( "In $function, $line:" . print_r( $msg, true ) . ( $debug ? print_r( $debug, true ) : '' ) );
}
}
//Rewrite links and image tags
function rewrite ( $text, $mode='images' ) {
//Log parameters when WP_DEBUG is set to True
self::log_if_debug( __METHOD__, __LINE__, '==== BEGIN DEBUG ====' );
self::log_if_debug( __METHOD__, __LINE__, 'wikidir : '.$this->wikidir );
self::log_if_debug( __METHOD__, __LINE__, 'wikiurl : '.$this->wikiurl );
self::log_if_debug( __METHOD__, __LINE__, '======== Text before Replacements ========' );
self::log_if_debug( __METHOD__, __LINE__, $text );
//Rewrite links to other wiki pages
$strSearch = '/'.$this->wikidir.'/index.php?title=';
$strReplace = '?wikipage=';
self::log_if_debug( __METHOD__, __LINE__, '======== 1st str_replace() ========' );
self::log_if_debug( __METHOD__, __LINE__, 'Search for : '.$strSearch );
self::log_if_debug( __METHOD__, __LINE__, 'Replace with : '.$strReplace );
$text = str_replace( $strSearch, $strReplace, $text );
self::log_if_debug( __METHOD__, __LINE__, 'Text after Replacement : ' );
self::log_if_debug( __METHOD__, __LINE__, $text );
//Rewrite other relative links
$strSearch = '/'.$this->wikidir;
$strReplace = $this->wikiurl;
self::log_if_debug( __METHOD__, __LINE__, '======== 2nd str_replace() ========' );
self::log_if_debug( __METHOD__, __LINE__, 'Search for : '.$strSearch );
self::log_if_debug( __METHOD__, __LINE__, 'Replace with : '.$strReplace );
$text = str_replace( $strSearch, $strReplace, $text );
self::log_if_debug( __METHOD__, __LINE__, 'Text after Replacement : ' );
self::log_if_debug( __METHOD__, __LINE__, $text );
//Rewrite image
if( $mode == 'images' ) {
self::log_if_debug( __METHOD__, __LINE__, '======== Call to $this->rewrite_images() ========' );
$text = $this->rewrite_images( $text );
self::log_if_debug( __METHOD__, __LINE__, 'text after Replacement : ' );
self::log_if_debug( __METHOD__, __LINE__, $text );
} else {
self::log_if_debug( __METHOD__, __LINE__, '======== No Image Replacement ========' );
}
self::log_if_debug( __METHOD__, __LINE__, '==== END DEBUG ====' );
return $text;
}
//Load the page from the wiki and rewrite links
function load_page( $page ) {
$url=$this->wikiurl.'/api.php?action=parse&format=json&disableeditsection&redirects&page='.urlencode($page);
curl_setopt($this->ch,CURLOPT_URL,$url);
$json_raw=curl_exec($this->ch);
$json=json_decode($json_raw,true);
$json['parse']['text']['*']=$this->rewrite($json['parse']['text']['*']);
return $json;
}