• Resolved mutosfr

    (@mutosfr)


    Hi Anders, hi all,

    I’m currently trying to set up a WP site for the Sci-Fi universe I’m writing with friends. We already have a MW encyclopedia of this universe, so it’s important to be able to reference it inside WP pages.

    I’m under WP 5.0.2 in French with theme Illdy from colorlib.

    The link I provide is on a test install, it just displays a MW page within a WP page. Text is in French, but I guess you just don’t care…

    The only thing that doesn’t work is link rewriting :
    – If I’ve understood well, clicking a link within the wiki data should replace current wiki data with that of the page clicked.
    – Actually, clicking a link within the wiki data replaces the current WP page with the MW page.

    Thanks if you can fix that or indicate me where to search for making a fix myself. Not sure though I’ll have the skill and/or time to fix, it’s the first time I really dive into WP, let alone extensions. But if I can, I have a GitHub account, so I could even contribute the fix ^-^

    Keep up the good work, and Happy New Year 2019 !

    Benoît ‘Mutos’ Robin

    The page I need help with: [log in to see the link]

Viewing 7 replies - 1 through 7 (of 7 total)
  • Thread Starter mutosfr

    (@mutosfr)

    Hi Anders,

    I’ve found a clue, but I still don’t understand.

    The following Links :

    http://hoshikaze.net/hk-wiki/index.php?title=Sshaad#Physique
    http://hoshikaze.net/hk-wiki/index.php?title=Daneb

    get rewritten to :

    http://test.hoshikaze.net/wordpress-5.0.2-fr_FR/presentation-de-lunivers/especes/#Physique
    http://hoshikaze.net/hk-wiki//index.php?title=Daneb

    The first one gets correctly rewritten, the second one is NOT rewritten…

    It sure has something to do with the rewrite() function, and more precisely with the str_replace() parameters in line 88 of wikiembedder.php, but for now I don’t know how to print variables to a kind of debug log… I’ll look at that…

    Thread Starter mutosfr

    (@mutosfr)

    Hi Anders,

    I’ve modified the rewrite() function as follows to issue debug logs :

    	/**
    	 * 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__, 'wikidir : '.$this->wikidir );
    		self::log_if_debug( __METHOD__, __LINE__, 'wikiurl : '.$this->wikiurl );
    		self::log_if_debug( __METHOD__, __LINE__, 'text before           : \n'.$text );
    		//Rewrite links to other wiki pages
    		$text = str_replace( '/'.$this->wikidir.'/index.php/', '?wikipage=',$text );
    		self::log_if_debug( __METHOD__, __LINE__, 'text after 1          : \n'.$text );
    		//Rewrite other relative links
    		$text = str_replace( '/'.$this->wikidir,$this->wikiurl, $text );
    		self::log_if_debug( __METHOD__, __LINE__, 'text after 2          : \n'.$text );
    		//Rewrite image
    		if( $mode == 'images' ) {
    			$text = $this->rewrite_images( $text );
    			self::log_if_debug( __METHOD__, __LINE__, 'text after images : \n'.$text );
    		}
    		return $text;
    	}
    
    
    Thread Starter mutosfr

    (@mutosfr)

    Hello Anders,

    In fact, the ToC links were not rewritten because they were in the form href=”#Marker”, so that’s normal.

    On the links that don’t get rewritten, I’m still in the dark…

    Thread Starter mutosfr

    (@mutosfr)

    Hi Anders,

    Should have seen that in the first place : my MW URLs are not in the same form as yours. It could be, that I use a very old MW, or a parameter (I’ll have to upgrade it anyway, but for now I’ll work around the issue temporarily).

    Basically, your format is

    index.php/PageName

    when mine is

    index.php?title=PageName

    Now the URLs get rewritten, but they are not recognized by the page.

    I get a rewritten URL in the form

    http://test.hoshikaze.net/wordpress-5.0.2-fr_FR/presentation-de-lunivers/especes/?wikipage=PageName

    Thread Starter mutosfr

    (@mutosfr)

    Hi Anders,

    In fact, it actually worked, but it seems a combination of the Illdy theme and the WP_DEBUG prevented the page from displaying. With 2013 theme, I got the WP_DEBUG error messages, then the wiki data, and when I disable WP_DEBUG, all is well.

    Thread Starter mutosfr

    (@mutosfr)

    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;
    	}
    
    
    Thread Starter mutosfr

    (@mutosfr)

    OK, solved for me with the above changes !

Viewing 7 replies - 1 through 7 (of 7 total)
  • The topic ‘Link rewrite under WP 5.0.2 with theme Illdy’ is closed to new replies.