r55785 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r55784‎ | r55785 | r55786 >
Date:20:17, 3 September 2009
Author:daniel
Status:deferred
Tags:
Comment:
introducing XMLRC extension; XMPP component is still incomplete.
Modified paths:
  • /trunk/extensions/XMLRC (added) (history)
  • /trunk/extensions/XMLRC/XMLRC.class.php (added) (history)
  • /trunk/extensions/XMLRC/XMLRC.i18n.php (added) (history)
  • /trunk/extensions/XMLRC/XMLRC.php (added) (history)
  • /trunk/extensions/XMLRC/XMLRC_File.class.php (added) (history)
  • /trunk/extensions/XMLRC/XMLRC_UDP.class.php (added) (history)
  • /trunk/extensions/XMLRC/XMLRC_XMPP.class.php (added) (history)

Diff [purge]

Index: trunk/extensions/XMLRC/XMLRC.class.php
@@ -0,0 +1,126 @@
 2+<?php
 3+if (!defined('MEDIAWIKI')) {
 4+ echo "XMLRC extension";
 5+ exit(1);
 6+}
 7+
 8+class XMLRC {
 9+ var $main;
 10+ var $query;
 11+ var $format;
 12+ var $transport;
 13+
 14+ public function __construct() {
 15+ $this->transportConfig = $GLOBALS['wgXMLRCTransport'];
 16+ $this->filter = @$GLOBALS['wgXMLRCFilter'];
 17+ }
 18+
 19+ public static function RecentChange_save( $rc ) {
 20+ $xmlrc = new XMLRC();
 21+ $xmlrc->processRecentChange( $rc );
 22+
 23+ return true; //continue processing normally
 24+ }
 25+
 26+ public function processRecentChange( $rc ) {
 27+ $xml = $this->formatRecentChange( $rc );
 28+ $this->sendRecentChangeXML( $xml );
 29+ }
 30+
 31+ private function getTransport() {
 32+ if ( $this->transport != NULL ) return $this->transport;
 33+
 34+ $class = $this->transportConfig['class'];
 35+ $this->transport = new $class( $this->transportConfig );
 36+
 37+ return $this->transport;
 38+ }
 39+
 40+ private function getMainModule() {
 41+ if ( $this->main != NULL ) return $this->main;
 42+
 43+ $req = new FauxRequest( array() );
 44+ $this->main = new ApiMain( $req );
 45+
 46+ return $this->main;
 47+ }
 48+
 49+ private function getFormatModule() {
 50+ if ( $this->format != NULL ) return $this->format;
 51+
 52+ $main = $this->getMainModule();
 53+ $this->format = new ApiFormatXml( $main, "xml" );
 54+
 55+ return $this->format;
 56+ }
 57+
 58+ private function getQueryModule() {
 59+ if ( $this->query != NULL ) return $this->query;
 60+
 61+ $main = $this->getMainModule();
 62+ $this->query = new ApiQueryRecentChanges( $main, "recentchanges" );
 63+
 64+ //TODO: configure!
 65+ $prop['comment'] = true;
 66+ $prop['user'] = true;
 67+ $prop['flags'] = true;
 68+ $prop['timestamp'] = true;
 69+ $prop['title'] = true;
 70+ $prop['ids'] = true;
 71+ $prop['sizes'] = true;
 72+ $prop['redirect'] = true;
 73+ $prop['patrolled'] = true;
 74+ $prop['loginfo'] = true;
 75+
 76+ $this->query->initProperties( $prop );
 77+
 78+ return $this->query;
 79+ }
 80+
 81+ public static function array2object($data)
 82+ {
 83+ $obj = new stdClass();
 84+
 85+ foreach ($data as $key => $value) {
 86+ $obj->$key = $value;
 87+ }
 88+
 89+ return $obj;
 90+ }
 91+
 92+ public function formatRecentChange( $rc ) {
 93+ $query = $this->getQueryModule();
 94+ $format = $this->getFormatModule();
 95+
 96+ $row = $rc->getAttributes();
 97+ $row = XMLRC::array2object( $row );
 98+
 99+ #wfDebug( "XMLRC: got attribute row: " . preg_replace('/\s+/', ' ', var_export($row, true)) . "\n" );
 100+
 101+ $info = $query->extractRowInfo( $row );
 102+
 103+ #wfDebug( "XMLRC: got info: " . preg_replace('/\s+/', ' ', var_export($info, true)) . "\n" );
 104+
 105+ $xml = $format->recXmlPrint( "rc", $info, "" );
 106+ $xml = trim( $xml );
 107+
 108+ return $xml;
 109+ }
 110+
 111+ public function sendRecentChangeXML( $xml ) {
 112+ if ( !is_string( $xml ) ) wfDebugDieBacktrace( "XMLRC: parameter xml must be a string\n" );
 113+ else wfDebug( "XMLRC: sending xml\n" );
 114+
 115+ $transport = $this->getTransport();
 116+ $transport->send( $xml );
 117+ }
 118+}
 119+
 120+abstract class XMLRC_Filter {
 121+ abstract function matches( $rc );
 122+}
 123+
 124+abstract class XMLRC_Transport {
 125+ abstract function send( $xml );
 126+}
 127+?>
\ No newline at end of file
Index: trunk/extensions/XMLRC/XMLRC_XMPP.class.php
@@ -0,0 +1,69 @@
 2+<?php
 3+if (!defined('MEDIAWIKI')) {
 4+ echo "XMLRC extension";
 5+ exit(1);
 6+}
 7+
 8+class XMLRC_XMPP extends XMLRC_Transport {
 9+ function __construct( $config ) {
 10+
 11+ if (!isset($config['library_path'])) {
 12+ include( "XMPP.php" );
 13+ } else {
 14+ include("{$config['library_path']}/XMPP.php");
 15+ }
 16+
 17+
 18+ if (!isset($config['server']) || is_null($config['server'])) {
 19+ $config['server'] = $config['host'];
 20+ }
 21+
 22+ if (!isset($config['nickname']) || is_null($config['nickname'])) {
 23+ $config['nickname'] = $config['user'];
 24+ }
 25+
 26+ $this->conn = NULL;
 27+
 28+ $this->channel = $config['channel'];
 29+ $this->nickname = $config['nickname'];
 30+ $this->resource = $config['resource'];
 31+
 32+ $this->host = $config['host'];
 33+ $this->port = $config['port'];
 34+ $this->user = $config['user'];
 35+ $this->password = $config['password'];
 36+ $this->server = $config['server'];
 37+
 38+ $this->loglevel = -1;
 39+ }
 40+
 41+ public function connect() {
 42+ if ( $this->conn ) return;
 43+
 44+ $this->conn = new XMPP( $this->host, $this->port, $this->user, $this->password,
 45+ $this->resource, $this->server,
 46+ $this->loglevel <= LEVEL_VERBOSE && $this->loglevel >= 0, $this->loglevel );
 47+
 48+ $this->conn->connect();
 49+ $this->conn->processUntil( 'session_start' );
 50+
 51+ $conn->presence(NULL, "available", $this->channel . '/' . $this->nickname, "available" );
 52+ }
 53+
 54+ public function disconnect() {
 55+ if ( !$this->conn ) return;
 56+
 57+ $conn->presence(NULL, "unavailable", $this->channel . '/' . $this->nickname, "unavailable" );
 58+
 59+ $this->conn->disconnect();
 60+ $this->conn = NULL;
 61+ }
 62+
 63+ public function send( $xml ) {
 64+ $do_disconnect = !$this->conn;
 65+ $this->connect();
 66+ $conn->message( $this->channel, $xml, 'groupchat' ); //TODO: use send to send XML content directly
 67+ if ( $do_disconnect ) $this->disconnect();
 68+ }
 69+}
 70+?>
\ No newline at end of file
Index: trunk/extensions/XMLRC/XMLRC_UDP.class.php
@@ -0,0 +1,41 @@
 2+<?php
 3+if (!defined('MEDIAWIKI')) {
 4+ echo "XMLRC extension";
 5+ exit(1);
 6+}
 7+
 8+class XMLRC_UDP extends XMLRC_Transport {
 9+ function __construct( $config ) {
 10+ $this->conn = NULL;
 11+
 12+ $this->address = $config['address'];
 13+ $this->port = $config['port'];
 14+ }
 15+
 16+ public function connect() {
 17+ if ( $this->conn ) return;
 18+
 19+ $this->conn = socket_create( AF_INET, SOCK_DGRAM, SOL_UDP );
 20+ if ( !$this->handle ) wfDebug("XMLRC_UDP: failed to create UDP socket\n");
 21+ else wfDebug("XMLRC_UDP: created UDP socket\n");
 22+ }
 23+
 24+ public function disconnect() {
 25+ if ( !$this->conn ) return;
 26+
 27+ socket_close( $this->conn );
 28+ $this->conn = NULL;
 29+ wfDebug("XMLRC_UDP: closed UDP socket\n");
 30+ }
 31+
 32+ public function send( $xml ) {
 33+ $do_disconnect = !$this->conn;
 34+ $this->connect();
 35+
 36+ $ok = socket_sendto( $this->conn, $xml, strlen($xml), 0, $this->address, $this->port );
 37+ if ( !$ok ) wfDebug("XMLRC_UDP: failed to send UDP packet to {$this->address}:{$this->port}\n");
 38+
 39+ if ( $do_disconnect ) $this->disconnect();
 40+ }
 41+}
 42+?>
\ No newline at end of file
Index: trunk/extensions/XMLRC/XMLRC.i18n.php
@@ -0,0 +1,11 @@
 2+<?php
 3+$messages['qqq'] = array(
 4+ 'xmlrc-desc' => 'Description of the XMLRC extension, to be shown on Special:Version.',
 5+);
 6+$messages['en'] = array(
 7+ 'xmlrc-desc' => 'Sends notifications about changes as XML, via UDP, Jabber or other means.',
 8+);
 9+$messages['de'] = array(
 10+ 'xmlrc-desc' => 'Verschickt benachrichtigungen über Änderungen als XML, über UDP, Jabber oder andere Protokolle.',
 11+);
 12+?>
\ No newline at end of file
Index: trunk/extensions/XMLRC/XMLRC.php
@@ -0,0 +1,59 @@
 2+<?php
 3+
 4+# Not a valid entry point, skip unless MEDIAWIKI is defined
 5+if (!defined('MEDIAWIKI')) {
 6+ echo "XMLRC extension";
 7+ exit(1);
 8+}
 9+
 10+$wgExtensionCredits['other'][] = array(
 11+ 'path' => __FILE__,
 12+ 'author' => array( 'Daniel Kinzler' ),
 13+ 'name' => 'XMLRC',
 14+ 'url' => 'http://www.mediawiki.org/wiki/Extension:XMLRC',
 15+ 'description' => 'Sends notifications about changes as XML, via UDP, Jabber or other means',
 16+ 'descriptionmsg'=> 'xmlrc-desc',
 17+);
 18+
 19+# Internationalisation file
 20+$dir = dirname(__FILE__) . '/';
 21+$wgExtensionMessagesFiles['XMLRC'] = $dir . 'XMLRC.i18n.php';
 22+#$wgExtensionAliasesFiles['XMLRC'] = $dir . 'XMLRC.alias.php';
 23+
 24+$wgXMLRCTransport = array(
 25+ 'class' => 'XMLRC_File',
 26+ 'file' => '/tmp/rc.xml',
 27+);
 28+
 29+/*
 30+$wgXMLRCTransport = array(
 31+ 'class' => 'XMLRC_XMPP',
 32+ 'channel' => 'recentchanges',
 33+ 'nickname' => $wgSitename,
 34+ 'host' => 'localhost',
 35+ 'port' => 5347,
 36+ 'user' => 'mediawiki',
 37+ 'server' => 'localhost',
 38+ 'resource' => 'recentchanges',
 39+ 'password' => 'yourpassword',
 40+ 'include_path' => './xmpphp',
 41+);
 42+*/
 43+
 44+/*
 45+$wgXMLRCTransport = array(
 46+ 'class' => 'XMLRC_UDP',
 47+ 'address' => 'localhost',
 48+ 'port' => 12345,
 49+);
 50+*/
 51+
 52+$wgAutoloadClasses[ 'XMLRC' ] = "$dir/XMLRC.class.php";
 53+$wgAutoloadClasses[ 'XMLRC_Transport' ] = "$dir/XMLRC.class.php";
 54+$wgAutoloadClasses[ 'XMLRC_XMPP' ] = "$dir/XMLRC_XMPP.class.php";
 55+$wgAutoloadClasses[ 'XMLRC_UDP' ] = "$dir/XMLRC_UDP.class.php";
 56+$wgAutoloadClasses[ 'XMLRC_File' ] = "$dir/XMLRC_File.class.php";
 57+
 58+$wgHooks['RecentChange_save'][] = 'XMLRC::RecentChange_save';
 59+
 60+?>
\ No newline at end of file
Index: trunk/extensions/XMLRC/XMLRC_File.class.php
@@ -0,0 +1,42 @@
 2+<?php
 3+if (!defined('MEDIAWIKI')) {
 4+ echo "XMLRC extension";
 5+ exit(1);
 6+}
 7+
 8+class XMLRC_File extends XMLRC_Transport {
 9+ function __construct( $config ) {
 10+ $this->handle = NULL;
 11+
 12+ $this->file = $config['file'];
 13+ }
 14+
 15+ public function open() {
 16+ if ( $this->handle ) return;
 17+
 18+ $this->handle = fopen( $this->file, 'a' );
 19+ if ( !$this->handle ) wfDebug("XMLRC_File: failed to open {$this->file}\n");
 20+ else wfDebug("XMLRC_File: opened {$this->file}\n");
 21+ }
 22+
 23+ public function close() {
 24+ if ( !$this->handle ) return;
 25+
 26+ fclose( $this->handle );
 27+ $this->handle = NULL;
 28+
 29+ wfDebug("XMLRC_File: closed {$this->file}\n");
 30+ }
 31+
 32+ public function send( $xml ) {
 33+ $do_close = !$this->handle;
 34+ $this->open();
 35+
 36+ $ok = fwrite( $this->handle, $xml . "\n" );
 37+ if ( $ok ) $ok = fflush( $this->handle );
 38+ if ( !$ok ) wfDebug("XMLRC_File: failed to write to {$this->file}\n");
 39+
 40+ if ( $do_close ) $this->close();
 41+ }
 42+}
 43+?>
\ No newline at end of file

Status & tagging log