Extension:WordpressTeaser

From mediawiki.org
MediaWiki extensions manual
WordpressTeaser
Release status: unmaintained
Implementation Tag
Description This extension allows you to insert teaser (first paragraphs) from your Wordpress blog into a MediaWiki page.
Author(s) Claus van Beek (ClausVB~mediawikiwikitalk)
Latest version 0.9 (2009-11-20)
MediaWiki
License GPL (latest version)
Download #Code

Detailed description[edit]

This extension allows you to insert teaser (first paragraphs) from your Wordpress blog into a MediaWiki page. PHP5 and "mysqli" (see MySQL improved extension) are required. Work in progress ...

Installation[edit]

Afterwards continue with your MySQL settings.

MySQL and other settings[edit]

Your Wordpress installation should contain a file like

wp-config.php

with all your MySQL settings. Please copy your values to "wordpress.php".

// MySQL settings
define('WORDPRESS_HOST', 'localhost');
define('WORDPRESS_USER', 'mysql_user');
define('WORDPRESS_PASSWORD', 'mysql_password');
define('WORDPRESS_DATABASE', 'wordpress_mysql_database');
define('WORDPRESS_PREFIX', 'wordpress_');

Get help[edit]

First of all you can use

<wordpress>help</wordpress>

to get help. This will display all albums and all pictures, which are stored in your Wordpress database.

If you have further questions, please post them here (discussion).

Usage[edit]

To get help and general informations about your wordpress, please use

<wordpress>help</wordpress>

Work in progress ...

Code[edit]

I tried to comment my code. If you have any questions or need more comments on a special line, please let me know.

<?php

// MySQL settings
define('WORDPRESS_HOST', 'localhost');
define('WORDPRESS_USER', 'mysql_user');
define('WORDPRESS_PASSWORD', 'mysql_password');
define('WORDPRESS_DATABASE', 'wordpress_mysql_database');
define('WORDPRESS_PREFIX', 'wordpress_');

// Other settings
define('WORDPRESS_URI', 'http://your-domain.tld/wordpress/'); // trailing "/" is IMPORTANT
define('STRFTIME_LC_ALL', 'german');
define('STRFTIME_SETTINGS', '%A, %d. %B %Y (%H:%M:%S)'); // '%A, %B %d, %Y (%H:%M:%S)'


$wgExtensionFunctions[] = 'wordpress';

function wordpress()
{
  global $wgParser;
  $wgParser->setHook('wordpress', 'process_wordpress');
}

function process_wordpress($input, $args, $parser)
{
  $db = new mysqli(WORDPRESS_HOST, WORDPRESS_USER, WORDPRESS_PASSWORD, WORDPRESS_DATABASE);

  // If MySQL cannot be connected, do nothing and exit function
  if (mysqli_connect_errno())
  {
    return '<h2>MySQL settings are wrong</h2> <p><strong>Error: "' . mysqli_connect_error() . '"</strong>. Please open</p> <pre>' .  __FILE__ . '</pre> <p>and check WORDPRESS_HOST, WORDPRESS_USER, WORDPRESS_PASSWORD, WORDPRESS_DATABASE and WORDPRESS_PREFIX, please.</p>';
  }

  // SET all encodings from an to RDBMS to UTF-8
  $db->query('SET character_set_results = utf8');
  $db->query('SET character_set_client = utf8');

  setlocale(LC_ALL, STRFTIME_LC_ALL);

  /**
   * help, e.g. <wordpress>help</wordpress>
  **/
  if ($input == 'help')
  {
    // Select basic stats
    $select_stats = 'SELECT COUNT(*) AS how_many_posts, SUM(comment_count) AS sum_of_all_comments FROM ' . WORDPRESS_PREFIX . 'posts WHERE post_status = "publish"';
    $result_stats = $db->query($select_stats)
      or die('ERROR MySQL: ' . $db->error . '<pre>' . $select_stats . '</pre>');
    $row_stats = $result_stats->fetch_assoc();

    // Select date of last post and last comment
    $select_last_date = 'SELECT MAX(post_date) AS last_post_date, MAX(comment_date) AS last_comment_date FROM ' . WORDPRESS_PREFIX . 'posts, ' . WORDPRESS_PREFIX . 'comments WHERE post_status = "publish" AND comment_approved = 1';
    $result_last_date = $db->query($select_last_date)
      or die('ERROR MySQL: ' . $db->error . '<pre>' . $select_last_date . '</pre>');
    $row_last_date = $result_last_date->fetch_assoc();

    // Select wordpress settings
    $select_settings = 'SELECT option_value FROM ' . WORDPRESS_PREFIX . 'options
      WHERE option_name = "home" OR option_name = "permalink_structure"';
    $result_settings = $db->query($select_settings)
      or die('ERROR MySQL: ' . $db->error . '<pre>' . $select_settings . '</pre>');
    $row_settings = $result_settings->fetch_assoc();
    $wordpress_home = $row_settings['option_value'] . '/';
    $row_settings = $result_settings->fetch_assoc();
    $wordpress_permalink_structure = $row_settings['option_value'];

    // Introduction and help text
    $output = '<h2>&lt;wordpress&gt;help&lt;/wordpress&gt;</h2> <p>This help will try to teach a few basics about this extension. WordPress stores data in a MySQL database. To access this data you will have to provide a few things like user and password. Take a look at</p> <pre>' . __FILE__ . '</pre> <p>and change it to your needs.</p> <h2>Your WordPress statistics and settings</h2>';

    $output .= '<ul>' . "\n";
    $output .= '  <li><strong>Sum of all posts</strong>: ' . $row_stats['how_many_posts'] . '</li>' . "\n";
    $output .= '  <li><strong>Sum of all comments</strong>: ' . $row_stats['sum_of_all_comments'] . '</li>' . "\n";
    $output .= '  <li><strong>Last post</strong>: '
      . strftime(STRFTIME_SETTINGS, strtotime($row_last_date['last_post_date']))
      . '</li>' . "\n";
    $output .= '  <li><strong>Last comment</strong>: '
      . strftime(STRFTIME_SETTINGS, strtotime($row_last_date['last_comment_date']))
      . '</li>' . "\n";
      $output .= '  <li><strong>Settings "home"</strong>: ' . $wordpress_home . '</li>' . "\n";
    $output .= '  <li><strong>Settings "permalink_structure"</strong>: ' . $wordpress_permalink_structure . '</li>' . "\n";
    $output .= '</ul>' . "\n";

    if (WORDPRESS_URI == $wordpress_home)
    {
      $check_uri_text = 'Constat &quot;WORDPRESS_URI&quot; is equal to &quot;$wordpress_home&quot;';
      $color = 'green';
    }
    else
    {
      $check_uri_text = 'Constat &quot;WORDPRESS_URI&quot; is NOT equal to &quot;$wordpress_home&quot;';
      $color = 'red';
    }

    $output .= '<p style="color: ' . $color . '">' . $check_uri_text . '</p>' . "\n";

    return $output;
  }
  else
  {
    // Select date of last post and last comment
    $select_posts = 'SELECT post_name, post_title, LEFT(post_content, 500) AS content, post_date, comment_count
      FROM ' . WORDPRESS_PREFIX . 'posts WHERE post_status = "publish" and ID != 2
      ORDER BY post_date DESC';
    $result_posts = $db->query($select_posts)
      or die('ERROR MySQL: ' . $db->error . '<pre>' . $select_posts . '</pre>');

    while ($row_posts = $result_posts->fetch_assoc())
    {
      $comments = ($row_posts['comment_count'] == 0)
        ? 'Kommentare: 0'
        : '<a href="' . WORDPRESS_URI . $row_posts['post_name'] . '.htm#comments" target="_blank">Kommentare: ' . $row_posts['comment_count'] . '</a>';

      $date_and_time = strftime(STRFTIME_SETTINGS, strtotime($row_posts['post_date']));

      $output .= '<fieldset>' . "\n";
      $output .= '<legend>' . $row_posts['post_title'] . '</legend>' . "\n";
      $output .= $row_posts['content'] . "\n";
      $output .= '<p style="font-size: 90%;">erstellt am ' . $date_and_time . ' &nbsp;&middot;&nbsp; <a href="' . WORDPRESS_URI . $row_posts['post_name'] . '.htm" target="_blank">Zum Blog</a> &nbsp;&middot;&nbsp; ' . $comments . '</p>' . "\n";
      $output .= '</fieldset>' . "\n";
    }

    return $output;
  }

  $db->close();
}

?>

Screenshots[edit]

Work in progress ...