Tpt has uploaded a new change for review.

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


Change subject: (bug 47071)  Allow use of the Lua API on a Wikibase repository
......................................................................

(bug 47071)  Allow use of the Lua API on a Wikibase repository

Implement a lua API for repositories with only one method
mw.wikibase.getEntityFromId

Change-Id: I689a188e2374ab09d81c60e7edb99646b9472c3a
---
M repo/Wikibase.classes.php
M repo/Wikibase.hooks.php
M repo/Wikibase.php
A repo/includes/WikibaseRepoLibrary.php
A repo/resources/mw.wikibase.lua
5 files changed, 151 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/21/62321/1

diff --git a/repo/Wikibase.classes.php b/repo/Wikibase.classes.php
index 20394f4..e0a5672 100644
--- a/repo/Wikibase.classes.php
+++ b/repo/Wikibase.classes.php
@@ -51,6 +51,7 @@
                'Wikibase\PropertyView' => 'includes/PropertyView.php',
                'Wikibase\Summary' => 'includes/Summary.php',
                'Wikibase\Repo\WikibaseRepo' => 'includes/WikibaseRepo.php',
+               'Scribunto_LuaWikibaseRepoLibrary' => 
'includes/WikibaseRepoLibrary.php',
 
                // includes/actions
                'Wikibase\HistoryEntityAction' => 
'includes/actions/HistoryEntityAction.php',
diff --git a/repo/Wikibase.hooks.php b/repo/Wikibase.hooks.php
index 16501ab..36a1fc3 100755
--- a/repo/Wikibase.hooks.php
+++ b/repo/Wikibase.hooks.php
@@ -1001,4 +1001,19 @@
 
                return false;
        }
+
+       /**
+        * External library for Scribunto
+        *
+        * @since 0.4
+        *
+        * @param $engine
+        * @param array $extraLibraries
+        * @return bool
+        */
+       public static function onScribuntoExternalLibraries ( $engine, array 
&$extraLibraries ) {
+               $extraLibraries['mw.wikibase'] = 
'Scribunto_LuaWikibaseRepoLibrary';
+
+               return true;
+       }
 }
diff --git a/repo/Wikibase.php b/repo/Wikibase.php
index b299d85..c962858 100644
--- a/repo/Wikibase.php
+++ b/repo/Wikibase.php
@@ -163,6 +163,7 @@
 $wgHooks['TitleGetRestrictionTypes'][]                         = 
'Wikibase\RepoHooks::onTitleGetRestrictionTypes';
 $wgHooks['AbuseFilter-contentToString'][]                      = 
'Wikibase\RepoHooks::onAbuseFilterContentToString';
 $wgHooks['SpecialPage_reorderPages'][]                         = 
'Wikibase\RepoHooks::onSpecialPage_reorderPages';
+$wgHooks['ScribuntoExternalLibraries'][]                       = 
'Wikibase\RepoHooks::onScribuntoExternalLibraries';
 
 // Resource Loader Modules:
 $wgResourceModules = array_merge( $wgResourceModules, include( __DIR__ . 
"/resources/Resources.php" ) );
diff --git a/repo/includes/WikibaseRepoLibrary.php 
b/repo/includes/WikibaseRepoLibrary.php
new file mode 100644
index 0000000..cb6a51c
--- /dev/null
+++ b/repo/includes/WikibaseRepoLibrary.php
@@ -0,0 +1,94 @@
+<?php
+
+/**
+ * Registers and defines functions to access Wikibase through the Scribunto 
extension
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ *
+ * @since 0.4
+ *
+ * @licence GNU GPL v2+
+ * @author Thomas Pellissier Tanon
+ */
+
+use ValueParsers\ParseException;
+use Wikibase\Repo\WikibaseRepo;
+use Wikibase\EntityContentFactory;
+
+
+class Scribunto_LuaWikibaseRepoLibrary extends Scribunto_LuaLibraryBase {
+
+       /**
+        * Register mw.wikibase.lua library
+        *
+        * @since 0.4
+        */
+       public function register() {
+               $lib = array(
+                       'getEntity' => array( $this, 'getEntity' )
+               );
+               $this->getEngine()->registerInterface( dirname( __FILE__ ) . 
'/../resources/' . 'mw.wikibase.lua', $lib, array() );
+       }
+
+       /**
+        * Get entity from prefixed ID (e.g. "Q23") and return it as serialized 
array.
+        *
+        * @since 0.4
+        *
+        * @param string $prefixedEntityId
+        *
+        * @return array $entityArr
+        */
+       public function getEntity( $prefixedEntityId = null ) {
+               $this->checkType( 'getEntity', 1, $prefixedEntityId, 'string' );
+               $prefixedEntityId = trim( $prefixedEntityId );
+
+               $entityIdParser = 
WikibaseRepo::getDefaultInstance()->getEntityIdParser();
+
+               try {
+                       $entityId = $entityIdParser->parse( $prefixedEntityId );
+               }
+               catch ( ParseException $parseException ) {
+                       throw $this->getEngine()->newException( 
'wikibase-error-invalid-entity-id' );
+               }
+
+               $entityContent = EntityContentFactory::singleton()->getFromId( 
$entityId );
+
+               if ( $entityContent == null ) {
+                       return array( null );
+               }
+
+               $entityTitle = $entityContent->getTitle();
+
+               // Record in templatelinks, so edits cause the page to be 
refreshed
+               if ( $entityTitle !== false ) {
+                       $this->getParser()->getOutput()->addTemplate(
+                               $entityTitle, $entityTitle->getArticleID(), 
$entityTitle->getLatestRevID()
+                       );
+               }
+
+               $entityObject = $entityContent->getEntity();
+
+               $serializerFactory = new 
\Wikibase\Lib\Serializers\SerializerFactory();
+               $serializer = $serializerFactory->newSerializerForObject( 
$entityObject );
+
+               $opt = new 
\Wikibase\Lib\Serializers\EntitySerializationOptions();
+               $serializer->setOptions( $opt );
+
+               $entityArr = $serializer->getSerialized( $entityObject );
+               return array( $entityArr );
+       }
+}
diff --git a/repo/resources/mw.wikibase.lua b/repo/resources/mw.wikibase.lua
new file mode 100644
index 0000000..ef84afd
--- /dev/null
+++ b/repo/resources/mw.wikibase.lua
@@ -0,0 +1,40 @@
+--[[
+    Registers and defines functions to access Wikibase through the Scribunto 
extension
+    Provides Lua setupInterface
+
+    This program is free software; you can redistribute it and/or modify
+    it under the terms of the GNU General Public License as published by
+    the Free Software Foundation; either version 2 of the License, or
+    (at your option) any later version.
+
+    This program is distributed in the hope that it will be useful,
+    but WITHOUT ANY WARRANTY; without even the implied warranty of
+    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+    GNU General Public License for more details.
+
+    You should have received a copy of the GNU General Public License along
+    with this program; if not, write to the Free Software Foundation, Inc.,
+    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+    http://www.gnu.org/copyleft/gpl.html
+
+    @since 0.4
+
+    @licence GNU GPL v2+
+    @author Jens Ohlig < [email protected] >
+]]
+
+local wikibase = {}
+
+function wikibase.setupInterface()
+  local php = mw_interface
+  mw_interface = nil
+  wikibase.getEntityFromId = function ( id )
+    return php.getEntity( id )
+  end
+  mw = mw or {}
+  mw.wikibase = wikibase
+  package.loaded['mw.wikibase'] = wikibase
+  wikibase.setupInterface = nil
+end
+
+return wikibase

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I689a188e2374ab09d81c60e7edb99646b9472c3a
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Tpt <[email protected]>

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

Reply via email to