Jump to content

Extension talk:WikiToWordPress

Add topic
From mediawiki.org
Latest comment: 15 years ago by 83.145.207.200 in topic $SearchString

Description of changes for v0.5

[edit]

We had to change the whole system as some newly renamed variables in MediaWiki made it impossible to include the WordPress php files in LocalSettings.php. So now WikiToWordPress stays on the MediaWiki side and writes the posts to the shared Wordpress-mediawiki database using our own queries.

Here is the old version 0.1 which was valid before MediaWiki 1.13. The new version is recommended & less invasive on the WordPress side.

Installation

[edit]

1. Install Wordpress in any directory of your server (I use /wordpress/ from the main MediaWiki directory).

2. Wordpress and Mediawiki should share the same Database

2. Copy the code in a file called WikiToWordPress.php and place that file in YOURWORDPRESSDIRECTORY/wp-content/plugins/.

3. I did not make it user-friendly because it's experimental. You will need to make 4 changes to the code:

  • First on Line 25 change YOURWORDPRESSDIRECTORY to the main directory where you have installed Wordpress.
  • Second, on Line 33, you can change [[Category:News]] to whatever you want the program to scan for. This is the string that the program will look for in new MediaWiki Articles to determine if it needs to be sent to the Wordpress blog. I used the News category because I only wanted articles from the News category to be included in the collective blog, but you can use any string of text.
  • Third, on Line 34, beside the 'flags', there is the number 7. This is a flag that indicates to only publish newly created articles, not articles that are just being edited. If you want any edited article to be republished in Wordpress everytime it is edited in MediaWiki, then change this number to 8.

4. You need to install the plugin in Wordpress. For this, log in as administrator in Wordpress and at the top right corner in your dashboard, you will see the Plugins option. Click on it and you should see WikiToWordPress listed as deactivated. Activate it.

5. Include the extension in MediaWiki by changing and adding this line to your LocalSettings.php:

require_once('YOURWORDPRESSDIRECTORY/wp-content/plugins/WikiToWordPress.php');

6. WikiToWordPress is now installed, and it will publish in your Wordpress blog any newly created MediaWiki article that contains a given string. But I strongly encourage you to install WordPress Comments, and CreateBox and optionally DynamicPageList because WikiToWordPress was designed to be used with these extensions just like we did for the news system at EmuWiki.com. Sorry for crappy code, this is my first PHP program and I'm not a programmer.

Code

[edit]
<?php
/*
Plugin Name: WikiToWordPress extension
Plugin URI: http://www.emuwiki.com
Description: Scans new articles created in a wiki and adds them to a Word Press 'Collective blog'
Version: 0.1
Author: Jean-Francois Gariépy
Author URI: http://www.emuwiki.com
*/
//Those are the extensions credits that show up on the Wordpress plugin panel.

/**
 * Protect against register_globals vulnerabilities.
 * This line must be present before any global variable is referenced.
 */
// if (!defined('MEDIAWIKI')) die(); //Fails in Wordpress Environment.

//Extension credits that show up on Special:Version
$wgExtensionCredits['other'][] = array(
        'name' => 'WikiToWordPress',
        'version' => '0.1',
        'author' => 'Jean-Francois Gariépy',
        'url' => 'http://www.mediawiki.org/wiki/Extension:WikiToWordPress',
        'description' => 'Scans new articles created in a wiki and adds them to a WordPress blog.',
);

$WordpressConfig = 'YOURWORDPRESSDIRECTORY/wp-config.php';

$wgHooks['ArticleSaveComplete'][] = 'postAWikiNews';

require_once($WordpressConfig);

function postAWikiNews(&$article, &$user, &$text, &$summary, &$minoredit, &$watchthis, &$sectionanchor, &$flags, $revision) {

global $wgOut;

$SearchString = '[[Category:News]]';
$PassFlag = '7';

if (strpos($text, $SearchString)) {
if (strpos($flags, $PassFlag)) {

// create post object
class wm_mypost {
    var $post_title;
    var $post_content;
    var $post_status;
    var $post_author;    /* author user id (optional) */
    var $post_name;      /* slug (optional) */
    var $post_type;      /* 'page' or 'post' (optional, defaults to 'post') */
    var $comment_status; /* open or closed for commenting (optional) */
}

$wm_mypost = new wm_mypost();

$wgOut->parserOptions()->setEditSection( false );

$wm_mypost->post_title = $article->mTitle->getPrefixedText();
$wm_mypost->post_content = $wgOut->parse($text);
$wm_mypost->post_status = 'publish';
$wm_mypost->post_author = 1;
$wp_rewrite->feeds = 'no';

// Optional; uncomment as needed
// $wm_mypost->post_type = 'page';
// $wm_mypost->comment_status = 'closed';

wp_insert_post($wm_mypost);

return true;
}
}

return true;

}

$SearchString

[edit]

you can put a example for $SearchString? thanks

Apparently as can be seen in the code, any string which is included in the article text can be used as a trigger. It's not very elegant, i'm looking into perhaps hacking the code a bit --83.145.207.200 11:25, 12 November 2008 (UTC)Reply

error

[edit]

After installation there is an error:

Fatal error: Allowed memory size of 33554432 bytes exhausted (tried to allocate 4864 bytes) in /home/content/e/g/y/xxxxxx/html/includes/User.php on line 2340