Legoktm has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/78942


Change subject: [WIP] Initial commit
......................................................................

[WIP] Initial commit

Change-Id: Ib613bef26891d974f0c472b78a22ebe6ee36f88c
---
A CentralLogEntry.php
A CentralLogFormatter.php
A CentralLogJob.php
A CentralLogging.i18n.php
A CentralLogging.php
A README
6 files changed, 251 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CentralLogging 
refs/changes/42/78942/1

diff --git a/CentralLogEntry.php b/CentralLogEntry.php
new file mode 100644
index 0000000..4f8103c
--- /dev/null
+++ b/CentralLogEntry.php
@@ -0,0 +1,66 @@
+<?php
+
+class CentralLogEntry extends ManualLogEntry {
+
+       /**
+        * @var bool
+        */
+       protected $shouldWePublish;
+
+       /**
+        * @var string
+        */
+       protected $publishTo;
+
+       /**
+        * Constructor function
+        *
+        * @param string $type
+        * @param string $subtype
+        */
+       function __construct( $type, $subtype ) {
+               parent::__construct( $type, $subtype );
+       }
+
+       /**
+        * Queues the log entry into the job queue. If the central wiki is
+        * the same as our current wiki, we will insert the log entry normally
+        * @param string $dbname Database name to insert into, will fallback on 
$wgCentralWiki if not set
+        * @param bool $publish Whether to call ManualLogEntry::publish 
afterwards
+        * @param string $to The $to parameter in ManualLogEntry::publish
+        * @return int
+        */
+       function queue( $dbname = null, $publish = true, $to = 'rcandudp' ) {
+               global $wgCentralWiki, $wgDBname;
+               if ( $dbname == null ) {
+                       $dbname = $wgCentralWiki;
+               }
+               // Make sure our dbname is stored in the log entry so we can 
use it when displaying
+               $this->parameters['dbname'] = $wgDBname;
+
+               if ( $wgDBname == $wgCentralWiki ) { // If we're on the central 
wiki, just log it normally
+                       $logid = parent::insert();
+                       if ( $publish ) {
+                               $this->publish( $logid, $to );
+                       }
+                       return $logid;
+               }
+
+               $this->shouldWePublish = $publish;
+               $this->publishTo = $to;
+               $this->setTimestamp( wfTimestampNow() ); // Job queue might be 
delayed so set the TS now
+               $params = array( 'data' => $this );
+               $job = new CentralLogJob( $this->getTarget(), $params );
+               JobQueueGroup::singleton( $dbname )->push( $job );
+               return 0; // Better than nothing?
+       }
+
+       function shouldWePublishEntry() {
+               return $this->shouldWePublish;
+       }
+
+       function publishEntryTo() {
+               return $this->publishTo;
+       }
+}
+
diff --git a/CentralLogFormatter.php b/CentralLogFormatter.php
new file mode 100644
index 0000000..bd1c31b
--- /dev/null
+++ b/CentralLogFormatter.php
@@ -0,0 +1,41 @@
+<?php
+
+class CentralLogFormatter extends LogFormatter {
+       /**
+        * Uses WikiMap to make a foreign link based on the dbname
+        * If the entry was local, use the normal method
+        * @param Title $title
+        * @param array $parameters
+        * @return String
+        */
+       protected function makePageLink( Title $title = null, $parameters = 
array() ) {
+               global $wgDBname;
+               $entry = $this->entry;
+               $params = $entry->getParameters();
+               $dbname = $params['dbname'];
+               if ( $wgDBname == $dbname ) { // Viewing on the same wiki it 
was inserted in
+                       return parent::makePageLink( $title, $parameters );
+               } else {
+                       return WikiMap::makeForeignLink( $dbname, 
$title->getPartialURL() );
+               }
+       }
+
+       /**
+        * Uses WikiMap to make a foreign link based on the dbname
+        * If the entry was local, use the normal method
+        * @param User $user
+        * @return String
+        */
+       function makeUserLink( User $user ) {
+               global $wgDBname;
+               $entry = $this->entry;
+               $params = $entry->getParameters();
+               $dbname = $params['dbname'];
+               if ( $wgDBname == $dbname ) { // Viewing on the same wiki it 
was inserted in
+                       return parent::makeUserLink( $user );
+               } else {
+                       return WikiMap::foreignUserLink( $dbname, 
$user->getName() );
+               }
+       }
+}
+
diff --git a/CentralLogJob.php b/CentralLogJob.php
new file mode 100644
index 0000000..41ae653
--- /dev/null
+++ b/CentralLogJob.php
@@ -0,0 +1,29 @@
+<?php
+
+class CentralLogJob extends Job {
+
+       /**
+        * @param Title $title
+        * @param array $params
+        * @param int $id
+        */
+       public function __construct( $title, $params, $id = 0 ) {
+               parent::__construct( 'centrallogJob', $title, $params, $id );
+       }
+
+       public function run() {
+               /**
+                * @var $entry CentralLogEntry
+                */
+               $entry = $this->params['data'];
+               $logId = $entry->insert();
+               if ( $entry->shouldWePublishEntry() ) {
+                       $entry->publish( $logId, $entry->publishEntryTo() );
+               }
+
+               return true;
+       }
+}
+
+
+
diff --git a/CentralLogging.i18n.php b/CentralLogging.i18n.php
new file mode 100644
index 0000000..0388ae0
--- /dev/null
+++ b/CentralLogging.i18n.php
@@ -0,0 +1,21 @@
+<?php
+
+/**
+ * Translations and stuff.
+ */
+
+$messages = array();
+
+/** English
+ * @author Kunal Mehta
+ */
+$messages['en'] = array(
+       'centrallogging-desc' => 'Allows extensions to create log entries on a 
central wiki',
+);
+
+/** Message documentation (Message documentation)
+ * @author Kunal Mehta
+ */
+$messages['qqq'] = array(
+       'centrallogging-desc' => 
'{{desc|name=CentralLogging|url=https://www.mediawiki.org/wiki/Extension:CentralLogging}}',
+);
diff --git a/CentralLogging.php b/CentralLogging.php
new file mode 100644
index 0000000..9de91a2
--- /dev/null
+++ b/CentralLogging.php
@@ -0,0 +1,34 @@
+<?php
+
+/**
+ * Extension to allow you to log to a central wiki rather than on that wiki.
+ * Implemented through the job queue
+ * See README for usage instructions
+ *
+ * @author Kunal Mehta
+ */
+
+/**
+ * Database name of the wiki to log to
+ * Individual extensions will be able to override this
+ * but it will fallback to this setting
+ */
+$wgCentralWiki = 'metawiki';
+
+
+$wgExtensionCredits['other'][] = array(
+       'path' => __FILE__,
+       'name' => 'CentralLogging',
+       'author' => 'Kunal Mehta',
+       'url' => 'https://www.mediawiki.org/wiki/Extension:CentralLogging',
+       'descriptionmsg' => 'centrallogging-desc',
+       'version' => '0.0.1',
+);
+
+$dir = dirname( __FILE__ );
+
+$wgAutoloadClasses['CentralLogEntry'] = $dir . '/CentralLogEntry.php';
+$wgAutoloadClasses['CentralLogFormatter'] = $dir . '/CentralLogFormatter.php';
+$wgAutoloadClasses['CentralLogJob'] = $dir . '/CentralLogJob.php';
+$wgJobClasses['centrallogJob'] = 'CentralLogJob';
+$wgExtensionMessagesFiles['CentralLogging'] = $dir . 
'/CentralLogging.i18n.php';
diff --git a/README b/README
new file mode 100644
index 0000000..2dd58af
--- /dev/null
+++ b/README
@@ -0,0 +1,60 @@
+= CentralLogging =
+
+CentralLogging is a MediaWiki extension that lets extensions send log entries 
to a central wiki.
+It provides no functionality by itself, rather is a framework for other 
functions to use.
+
+== Configuration ==
+
+$wgCentralWiki should be set to the database name of the central wiki to log 
on.
+Indivdual extensions will be able to override this if they choose to do so.
+The default value is:
+
+    $wgCentralWiki = 'metawiki';
+
+When setting up an extension to use CentralLogging, you need to set a few 
global variables:
+
+    $wgLogTypes[] = 'foo'; // You should do this anyways
+    $wgLogActionsHandlers['foo/*'] = 'CentralLogFormatter';
+
+
+== Example code ==
+
+Some example usage would look like:
+
+    $entry = new CentralLogEntry( 'foo', 'bar' );
+
+After this step, the next set of code is the same as ManualLogEntry.
+See https://www.mediawiki.org/wiki/Manual:Logging_to_Special:Log for more 
details
+
+    $entry->setTitle( $title );
+    $entry->setPerformer( $user );
+    $entry->setComment( 'comment' );
+
+Now we need to queue the message rather than insert it
+
+    $entry->queue( $dbname = null, $publish = true, $to = 'rcandudp' );
+
+All parameters are optional:
+* $dbname: if it is set to null, it defaults to $wgCentralWiki
+* $publish: whether we should call ManualLogEntry::publish afterwards. By 
default this is true.
+* $to: this will just be passed to ManualLogEntry::publish if it is called. 
This is 'rcandudp' by default.
+
+Another implementation might look like:
+
+    if ( class_exists( 'CentralLogEntry' ) ) {
+        $entry = new CentralLogEntry( 'foo', 'bar' );
+    } else {
+        $entry = new ManualLogEntry( 'foo', 'bar' );
+    }
+    $entry->setTitle( $title );
+    $entry->setPerformer( $user );
+    $entry->setComment( 'comment ');
+    if ( $entry instanceof CentralLogEntry ) {
+        $entry->queue();
+    } else {
+        $logId = $entry->insert();
+        $entry->publish( $logId );
+    }
+
+This will let your extension take advantage of central logging if you have 
this extension enabled,
+otherwise it falls back upon the local logging system.
\ No newline at end of file

-- 
To view, visit https://gerrit.wikimedia.org/r/78942
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ib613bef26891d974f0c472b78a22ebe6ee36f88c
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/CentralLogging
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to