Legoktm has submitted this change and it was merged.
Change subject: Initial commit
......................................................................
Initial commit
Change-Id: Ib613bef26891d974f0c472b78a22ebe6ee36f88c
---
A CentralLogEntry.php
A CentralLogFormatter.php
A CentralLogJob.php
A CentralLogging.hooks.php
A CentralLogging.i18n.php
A CentralLogging.php
A README.md
7 files changed, 312 insertions(+), 0 deletions(-)
Approvals:
Legoktm: Verified; Looks good to me, approved
diff --git a/CentralLogEntry.php b/CentralLogEntry.php
new file mode 100644
index 0000000..b5954a5
--- /dev/null
+++ b/CentralLogEntry.php
@@ -0,0 +1,74 @@
+<?php
+
+/**
+ * Our class to handle log entries
+ * Extends a normal log entry for
+ * all other functionality
+ *
+ * @author Kunal Mehta
+ */
+
+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..9aa5b5d
--- /dev/null
+++ b/CentralLogFormatter.php
@@ -0,0 +1,47 @@
+<?php
+
+/**
+ * Log formatter to account for foreign links
+ *
+ * @author Kunal Mehta
+ */
+
+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..9284b49
--- /dev/null
+++ b/CentralLogJob.php
@@ -0,0 +1,35 @@
+<?php
+
+/**
+ * Job class which submits jobs when run
+ *
+ * @author Kunal Mehta
+ */
+
+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.hooks.php b/CentralLogging.hooks.php
new file mode 100644
index 0000000..4c47a79
--- /dev/null
+++ b/CentralLogging.hooks.php
@@ -0,0 +1,30 @@
+<?php
+
+/**
+ * Hoooooooks!
+ *
+ * @author Kunal Mehta
+ */
+
+class CentralLoggingHooks {
+
+ /**
+ * Add number of queued entries to Special:Statistics
+ * @param $extraStats
+ * @return bool
+ */
+ public static function onSpecialStatsAddExtra( &$extraStats ) {
+ // from runJobs.php --group
+ $group = JobQueueGroup::singleton();
+ $queue = $group->get( 'centrallogJob' );
+ $pending = $queue->getSize();
+ $claimed = $queue->getAcquiredCount();
+ $abandoned = $queue->getAbandonedCount();
+ $active = ( $claimed - $abandoned );
+
+ $queued = $active + $pending;
+ $extraStats['centrallogging-queued-count'] = $queued;
+
+ return true;
+ }
+}
diff --git a/CentralLogging.i18n.php b/CentralLogging.i18n.php
new file mode 100644
index 0000000..21105f3
--- /dev/null
+++ b/CentralLogging.i18n.php
@@ -0,0 +1,23 @@
+<?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',
+ 'centrallogging-queued-count' => 'Queued log entries',
+);
+
+/** Message documentation (Message documentation)
+ * @author Kunal Mehta
+ */
+$messages['qqq'] = array(
+ 'centrallogging-desc' =>
'{{desc|name=CentralLogging|url=https://www.mediawiki.org/wiki/Extension:CentralLogging}}',
+ 'centrallogging-queued-count' => 'Text for row on
[[Special:Statistics]]',
+);
diff --git a/CentralLogging.php b/CentralLogging.php
new file mode 100644
index 0000000..9eeca30
--- /dev/null
+++ b/CentralLogging.php
@@ -0,0 +1,32 @@
+<?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 <[email protected]>
+ */
+
+/**
+ * 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' => '1.0',
+);
+
+$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.md b/README.md
new file mode 100644
index 0000000..4af2760
--- /dev/null
+++ b/README.md
@@ -0,0 +1,71 @@
+# 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:
+
+```php
+$wgCentralWiki = 'metawiki';
+```
+
+When setting up an extension to use CentralLogging, you need to set a few
global variables:
+
+```php
+$wgLogTypes[] = 'foo'; // You should do this anyways
+$wgLogActionsHandlers['foo/*'] = 'CentralLogFormatter';
+```
+
+## Example code
+
+Some example usage would look like:
+
+```php
+$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
+
+```php
+$entry->setTitle( $title );
+$entry->setPerformer( $user );
+$entry->setComment( 'comment' );
+```
+
+Now we need to queue the message rather than insert it
+
+```php
+$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:
+
+```php
+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: merged
Gerrit-Change-Id: Ib613bef26891d974f0c472b78a22ebe6ee36f88c
Gerrit-PatchSet: 2
Gerrit-Project: mediawiki/extensions/CentralLogging
Gerrit-Branch: master
Gerrit-Owner: Legoktm <[email protected]>
Gerrit-Reviewer: CSteipp <[email protected]>
Gerrit-Reviewer: Legoktm <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits