Jump to content

User:Trublu/WP2Wiki

From Meta, a Wikimedia project coordination wiki
 
A proposal to move this page to MediaWiki.org was rejected.

Template:Extension WP2Wiki

An extension to show newest Wordpress-Posts in Mediawiki.

http://www.c-peper.de/wiki/WP2Wiki

Installation

[edit]
  1. Save Code as SpecialWP2Wiki.php in folder "extensions" in your mediawiki installation
  2. add
    include './extensions/SpecialWP2Wiki.php';
    in your Localsettings.php
  3. If your wordpress uses another name for tables, edit "$page" accordingly
  4. include {{Special:WP2Wiki}} wherever desired

Code

[edit]
 <?php
 
 if (!defined('MEDIAWIKI')) die();
 /**
 * A special page extension that show the newest posts from a wordpress-blog
 *
 * @package MediaWiki
 * @subpackage Extensions
 *
 * @link http://www.c-peper.de/wiki/WP2Wiki Documentation
 *
 * @author (Christian) Jason Peper <trublu@c-peper.de>
 * @copyright Copyright © 2006, (Christian) Jason Peper
 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 */
 
 $pars;
 $wgExtensionFunctions[] = 'wfSpecialWP2WikiS';
 $wgExtensionCredits['specialpage'][] = array(
    'name' => 'WP2Wiki',
    'author' => '(Christian) Jason Peper',
    'version' => '0.51',
    'description' => 'Wordpress to Mediawiki',
    'url' => 'http://www.c-peper.de/wiki/WP2Wiki'
 );
 
 function wfSpecialWP2WikiS() {
  global $IP, $wgMessageCache;
  $wgMessageCache->addMessage('wp2wiki', "WP2Wiki");
  $wgMessageCache->addMessage('wp2wikitemplate',"<small>$1</small> · '''[$2 $3]'''<br/>");
  $wgMessageCache->addMessage('wp2wikitemplateText', "$1");
 
  require_once "$IP/includes/SpecialPage.php";
 
 //  $wgSpecialPages['WP2Wiki'] = 'WP2Wiki';
 
  SpecialPage::addPage(new SpecialPage('WP2Wiki', '', true, 'wfSpecialWP2Wiki', 'default', true));
 }
 
 function wfSpecialWP2Wiki($par=NULL) {
  global $wgOut, $par_text, $par_count, $par_lines;
  $wgOut->setPageTitle("WP2Wiki");
  $pars = explode("/",$par);
  $par_count = $pars[0];
  $par_lines = $pars[2];
  if ($par_lines == "") {
    $par_lines = 1;
  }
  if ($par_count == "") {
    $par_count = 3;
  }
  if ($pars[1] == "text") {
    $par_text = True;
  }
  $dbr =& wfGetDB( DB_SLAVE );
  $page = $dbr->tableName( 'wp_posts' );
  $sql = 'SELECT post_title, post_date, post_content, guid FROM '
      . $page
      . ' order by post_date desc limit '
      . $par_count 
      . " ";
  // SELECT * FROM `wp_post2cat`,`wp_posts` WHERE `ID` = wp_post2cat.post_id AND wp_post2cat.category_id = 8 OR `ID` =      wp_post2cat.post_id AND wp_post2cat.category_id = 3 ORDER BY post_date DESC
  $res = $dbr->query( $sql );
  $out = "";
  while( $row = $dbr->fetchObject($res) )
  {
    $out .= makeListItem($row);
  }
  $wgOut->addWikiText($out);
 }
 
 function makeListItem( $row ) {
  global $par_text, $par_lines;
 
  // text parsing
  $text = $row->post_content;
  $text = preg_replace("/\n?<!--more-->.*/s", "",$text);
  $text = preg_replace("/<a.*href=\"(.*)\".*>(.*)<\/a>/U", "[$1 $2]", $text);
  $text = preg_replace("/((.*\. )).*$/sU","$2...",$text);
  $text = str_replace("\n","\n:",$text);
 
  // Date
  $parts = explode(" ",$row->post_date);
  $dates = explode("-", $parts[0]);
  $times = explode(":", $parts[1]);
  $date = $dates[2] . "." . $dates[1] . "." . $dates[0];
 
  $out = wfMsgForContent('wp2wikitemplate',$date,$row->guid,$row->post_title);
  if ($par_text == 1) {
    $out .= "\n:" . wfMsgForContent('wp2wikitemplateText',$text,$row->guid) . "<br/>\n";
  }
  return $out;
 }
 
 ?>