[MediaWiki-commits] [Gerrit] Initial commit of code - change (mediawiki...CommonMessages)

2014-06-15 Thread jenkins-bot (Code Review)
jenkins-bot has submitted this change and it was merged.

Change subject: Initial commit of code
..


Initial commit of code

Change-Id: I1dcafde55b9d84ddd0acd88c824433c569d58a66
---
A CommonMessages.body.php
A CommonMessages.php
A export/.gitignore
A export/README
A exportMessages.php
A i18n/en.json
A i18n/qqq.json
7 files changed, 168 insertions(+), 0 deletions(-)

Approvals:
  Legoktm: Looks good to me, approved
  Siebrand: Looks good to me, but someone else must approve
  jenkins-bot: Verified



diff --git a/CommonMessages.body.php b/CommonMessages.body.php
new file mode 100644
index 000..06d5ca6
--- /dev/null
+++ b/CommonMessages.body.php
@@ -0,0 +1,44 @@
+?php
+
+class CommonMessages {
+
+   /** @var array $keys */
+   protected $keys;
+
+   public static function singleton() {
+   static $self = null;
+   if ( !$self ) {
+   $self = new self;
+   }
+   return $self;
+   }
+
+   public function transformKey( $key ) {
+   global $wgCommonMessagesPrefix;
+   return $wgCommonMessagesPrefix . '-' . $key;
+   }
+
+   /**
+* @param string $key non-transformed key
+* @return bool
+*/
+   public function isKeyRegistered( $key ) {
+   if ( $this-keys === null ) {
+   // Load the JSON file.
+   wfSuppressWarnings();
+   $file = file_get_contents( __DIR__ . '/export/en.json' 
);
+   wfRestoreWarnings();
+   if ( !$file ) {
+   $this-keys = array();
+   return false;
+   }
+   $json = FormatJson::decode( $file, true );
+   if ( !$json ) {
+   $this-keys = array();
+   return false;
+   }
+   $this-keys = array_keys( $json );
+   }
+   return in_array( $this-transformKey( $key ), $this-keys );
+   }
+}
\ No newline at end of file
diff --git a/CommonMessages.php b/CommonMessages.php
new file mode 100644
index 000..af29d70
--- /dev/null
+++ b/CommonMessages.php
@@ -0,0 +1,60 @@
+?php
+
+/**
+ * CommonMessages extension
+ *
+ * Allows a wikifarm to have custom
+ * message overrides easilly
+ * Mainly designed for ShoutWiki's setup,
+ * but can be used for any farm.
+ *
+ * @requires MediaWiki 1.23
+ * @license WTFPL
+ * @author Kunal Mehta lego...@gmail.com
+ */
+
+if ( !defined( 'MEDIAWIKI' ) ) {
+   exit();
+}
+
+$wgExtensionCredits['other'][] = array(
+   'path' = __FILE__,
+   'name' = 'CommonMessages',
+   'author' = 'Kunal Mehta',
+   'descriptionmsg' = 'commonmessages-desc',
+   'version' = '0.0.1',
+);
+
+/**
+ * Prefix of messages when exporting.
+ * Must be configured.
+ */
+$wgCommonMessagesPrefix = '';
+
+/**
+ * Directory of where to export messages to
+ * and where to read from
+ * Must already exist
+ */
+$wgCommonMessagesExportDir = __DIR__ . '/export';
+
+$wgExtensionFunctions[] = function() {
+   global $wgCommonMessagesExportDir;
+   if ( file_exists( $wgCommonMessagesExportDir . '/en.json' ) ) {
+   // If an export has happened, load it
+   $wgMessagesDirs['CommonMessagesExport'] = 
$wgCommonMessagesExportDir;
+   }
+};
+
+
+$wgMessagesDirs['CommonMessages'] = __DIR__ . '/i18n';
+$wgAutoloadClasses['CommonMessages'] = __DIR__ . '/CommonMessages.body.php';
+
+$wgHooks['MessageCache::get'][] = function( $key ) {
+   $commons = CommonMessages::singleton();
+   if ( $commons-isKeyRegistered( $key ) ) {
+   $key = $commons-transformKey( $key );
+   }
+
+   return true;
+};
diff --git a/export/.gitignore b/export/.gitignore
new file mode 100644
index 000..a6c57f5
--- /dev/null
+++ b/export/.gitignore
@@ -0,0 +1 @@
+*.json
diff --git a/export/README b/export/README
new file mode 100644
index 000..f60e76e
--- /dev/null
+++ b/export/README
@@ -0,0 +1 @@
+This directory will be filled after using the exportMessages.php script.
diff --git a/exportMessages.php b/exportMessages.php
new file mode 100644
index 000..68d40d2
--- /dev/null
+++ b/exportMessages.php
@@ -0,0 +1,46 @@
+?php
+
+$IP = getenv( 'MW_INSTALL_PATH' );
+if ( $IP === false ) {
+   $IP = __DIR__ . '/../..';
+}
+
+// Require base maintenance class
+require_once( $IP/maintenance/Maintenance.php );
+
+class ExportMessages extends Maintenance {
+
+   public function execute() {
+   global $wgCommonMessagesExportDir;
+   $commons = CommonMessages::singleton();
+   $dbr = wfGetDB( DB_SLAVE );
+   // @todo Can we make this less memory intensive?
+   $rows = $dbr-select(
+   'page',
+  

[MediaWiki-commits] [Gerrit] Initial commit of code - change (mediawiki...CommonMessages)

2014-02-25 Thread Legoktm (Code Review)
Legoktm has uploaded a new change for review.

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

Change subject: Initial commit of code
..

Initial commit of code

Change-Id: I1dcafde55b9d84ddd0acd88c824433c569d58a66
---
A CommonMessages.body.php
A CommonMessages.i18n.php
A CommonMessages.php
A export/.gitignore
A export/README
A exportMessages.php
6 files changed, 158 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/CommonMessages 
refs/changes/90/115390/1

diff --git a/CommonMessages.body.php b/CommonMessages.body.php
new file mode 100644
index 000..06d5ca6
--- /dev/null
+++ b/CommonMessages.body.php
@@ -0,0 +1,44 @@
+?php
+
+class CommonMessages {
+
+   /** @var array $keys */
+   protected $keys;
+
+   public static function singleton() {
+   static $self = null;
+   if ( !$self ) {
+   $self = new self;
+   }
+   return $self;
+   }
+
+   public function transformKey( $key ) {
+   global $wgCommonMessagesPrefix;
+   return $wgCommonMessagesPrefix . '-' . $key;
+   }
+
+   /**
+* @param string $key non-transformed key
+* @return bool
+*/
+   public function isKeyRegistered( $key ) {
+   if ( $this-keys === null ) {
+   // Load the JSON file.
+   wfSuppressWarnings();
+   $file = file_get_contents( __DIR__ . '/export/en.json' 
);
+   wfRestoreWarnings();
+   if ( !$file ) {
+   $this-keys = array();
+   return false;
+   }
+   $json = FormatJson::decode( $file, true );
+   if ( !$json ) {
+   $this-keys = array();
+   return false;
+   }
+   $this-keys = array_keys( $json );
+   }
+   return in_array( $this-transformKey( $key ), $this-keys );
+   }
+}
\ No newline at end of file
diff --git a/CommonMessages.i18n.php b/CommonMessages.i18n.php
new file mode 100644
index 000..9733a41
--- /dev/null
+++ b/CommonMessages.i18n.php
@@ -0,0 +1,18 @@
+?php
+
+$messages = array();
+
+/**
+ * English
+ * @author Kunal Mehta
+ */
+$messages['en'] = array(
+   'commonmessages-desc' = 'Allows a wikifarm to override messages from 
another wiki',
+);
+
+/** Message documentation (Message documentation)
+ * @author Kunal Mehta
+ */
+$messages['qqq'] = array(
+   'commonmessages-desc' = 
'{{desc|name=CommonMessages|url=https://www.mediawiki.org/wiki/Extension:CommonMessages}}',
+);
diff --git a/CommonMessages.php b/CommonMessages.php
new file mode 100644
index 000..3439423
--- /dev/null
+++ b/CommonMessages.php
@@ -0,0 +1,49 @@
+?php
+
+/**
+ * CommonMessages extension
+ *
+ * Allows a wikifarm to have custom
+ * message overrides easilly
+ * Mainly designed for ShoutWiki's setup,
+ * but can be used for any farm.
+ *
+ * @requires MediaWiki 1.23
+ * @license WTFPL
+ * @author Kunal Mehta lego...@gmail.com
+ */
+
+if ( !defined( 'MEDIAWIKI' ) ) {
+   exit;
+}
+
+$wgExtensionCredits['other'][] = array(
+   'path' = __FILE__,
+   'name' = 'CommonMessages',
+   'author' = 'Kunal Mehta',
+   'descriptionmsg' = 'commonmessages-desc',
+   'version' = '0.0.1',
+);
+
+/**
+ * Prefix of messages when exporting.
+ * Must be configured.
+ */
+$wgCommonMessagesPrefix = '';
+
+if ( file_exists( __DIR__ . '/export/en.json' ) ) {
+   // If an export has happened, load it
+   $wgMessagesDirs['MessageCommons'] = __DIR__ . '/export';
+}
+
+$wgExtensionMessagesFiles['CommonMessages'] =  __DIR__ . 
'/CommonMessages.i18n.php';
+$wgAutoloadClasses['CommonMessages'] = __DIR__ . '/CommonMessages.body.php';
+
+$wgHooks['MessageCache::get'][] = function( $key ) {
+   $commons = CommonMessages::singleton();
+   if ( $commons-isKeyRegistered( $key ) ) {
+   $key = $commons-transformKey( $key );
+   }
+
+   return true;
+};
diff --git a/export/.gitignore b/export/.gitignore
new file mode 100644
index 000..a6c57f5
--- /dev/null
+++ b/export/.gitignore
@@ -0,0 +1 @@
+*.json
diff --git a/export/README b/export/README
new file mode 100644
index 000..f60e76e
--- /dev/null
+++ b/export/README
@@ -0,0 +1 @@
+This directory will be filled after using the exportMessages.php script.
diff --git a/exportMessages.php b/exportMessages.php
new file mode 100644
index 000..8b8c233
--- /dev/null
+++ b/exportMessages.php
@@ -0,0 +1,45 @@
+?php
+
+$IP = getenv( 'MW_INSTALL_PATH' );
+if ( $IP === false ) {
+   $IP = __DIR__ . '/../..';
+}
+
+// Require base maintenance class
+require_once( $IP/maintenance/Maintenance.php );
+
+class ExportMessages extends Maintenance {
+
+