Hoo man has uploaded a new change for review.

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

Change subject: WIP Prototype: Make infoboxes created with Capiunto machine 
readable
......................................................................

WIP Prototype: Make infoboxes created with Capiunto machine readable

Introduce a action=query prop=capiuntodata api module for retrieving
the data stored in Infoboxes created with Capiunto as json structure.

TODO/ To discuss:
* Use one page prop per Infobox?
* Return one json string per Infobox in the api instead of one
  large list? Continuation? Limits?
* Handle sub infoboxes in a nicer way (don't just pull the in as
  html, but extend the data structure).
* Support data structures other than json?
* Review the data structure
* …

Change-Id: Ia176fb6736d6dc78980928efe9ed3dd444b7780b
---
M Capiunto.php
M includes/LuaLibrary.php
A includes/api/CapiuntoData.php
M includes/lua/Infobox.lua
4 files changed, 121 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Capiunto 
refs/changes/65/213765/1

diff --git a/Capiunto.php b/Capiunto.php
index bd8e0b8..49656f2 100644
--- a/Capiunto.php
+++ b/Capiunto.php
@@ -26,6 +26,7 @@
 $wgAutoloadClasses['Capiunto\Hooks']           = __DIR__ . 
'/includes/Hooks.php';
 $wgAutoloadClasses['Capiunto\LuaLibrary']                                      
= __DIR__ . '/includes/LuaLibrary.php';
 $wgAutoloadClasses['Capiunto\Test\InfoboxModuleTest']          = __DIR__ . 
'/tests/phpunit/includes/lua/InfoboxTest.php';
+$wgAutoloadClasses['Capiunto\Api\CapiuntoData']                = __DIR__ . 
'/includes/api/CapiuntoData.php';
 
 $wgHooks['UnitTestsList'][]                            = 
'\Capiunto\Hooks::registerUnitTests';
 $wgHooks['ScribuntoExternalLibraries'][]       = 
'\Capiunto\Hooks::registerScribuntoLibraries';
@@ -39,3 +40,5 @@
 $wgResourceModules['capiunto.infobox.main'] = array(
        'styles' => 'infobox.css',
 ) + $commonModuleInfo;
+
+$wgAPIPropModules['capiuntodata'] = 'Capiunto\Api\CapiuntoData';
\ No newline at end of file
diff --git a/includes/LuaLibrary.php b/includes/LuaLibrary.php
index 0447335..17ac7bd 100644
--- a/includes/LuaLibrary.php
+++ b/includes/LuaLibrary.php
@@ -22,6 +22,7 @@
                return $this->getEngine()->registerInterface(
                        __DIR__ . '/lua/Infobox.lua',
                        array(
+                               'storeInfobox' => array( $this, 'storeInfobox' 
),
                                'addResourceLoaderModules' => array( $this, 
'addResourceLoaderModules' )
                        ),
                        array()
@@ -34,4 +35,25 @@
        public function addResourceLoaderModules() {
                $this->getParser()->getOutput()->addModuleStyles( 
'capiunto.infobox.main' );
        }
+
+       /**
+        * Add the data from a given Infobox to the page props so that the API 
module etc.
+        * can access it.
+        *
+        * @param array $data
+        */
+       public function storeInfobox( array $data ) {
+               $key = 'mw-capiunto-infoboxes';
+               $parserOutput = $this->getParser()->getOutput();
+
+               // XXX: Decoding and encoding json... who cares about 
performance anyway, right?
+               // XXX: Shall we use on page prop per infobox, instead of one 
for all maybe?
+               $infoboxes = $parserOutput->getProperty( $key );
+               $infoboxes = $infoboxes !== null ? json_decode( $infoboxes ) : 
array();
+
+               $infoboxes[] = $data;
+               $json = json_encode( $infoboxes );
+
+               $parserOutput->setProperty( $key, $json );
+       }
 }
diff --git a/includes/api/CapiuntoData.php b/includes/api/CapiuntoData.php
new file mode 100644
index 0000000..5d888ae
--- /dev/null
+++ b/includes/api/CapiuntoData.php
@@ -0,0 +1,95 @@
+<?php
+
+namespace Capiunto\Api;
+
+use ApiBase;
+use ApiQuery;
+use ApiQueryBase;
+
+/**
+ * Provides access to the Capiunto infoboxes on pages.
+ *
+ * @since 0.5
+ *
+ * @licence GNU GPL v2+
+ * @author Marius Hoch < [email protected] >
+ */
+class CapiuntoData extends ApiQueryBase {
+
+       /**
+        * @param ApiQuery $query
+        * @param string $moduleName
+        */
+       public function __construct(
+               ApiQuery $query,
+               $moduleName
+       ) {
+               parent::__construct( $query, $moduleName, 'cpif' );
+       }
+
+       public function execute() {
+               # Only operate on existing pages
+               $titles = $this->getPageSet()->getGoodTitles();
+               if ( !count( $titles ) ) {
+                       # Nothing to do
+                       return;
+               }
+
+               // NOTE: continuation relies on $titles being sorted by page ID.
+               ksort( $titles );
+
+               $this->processTitles( $titles );
+       }
+
+       /**
+        * @param array $titles
+        */
+       private function processTitles( array $titles ) {
+               $dbr = wfGetDB( DB_SLAVE );
+
+               $res = $dbr->select(
+                       'page_props',
+                       array( 'pp_page', 'pp_value' ),
+                       array( 'pp_page' => array_keys( $titles ), 
'pp_propname' => 'mw-capiunto-infoboxes' ),
+                       __METHOD__
+               );
+
+               foreach( $res as $row ) {
+                       $this->getResult()->addValue(
+                               array( 'query', 'pages', $row->pp_page ),
+                               'infobox',
+                               $row->pp_value
+                       );
+               }
+
+               // XXX: Not sure this is correct... do we need to care for some 
limit?
+               end( $titles );
+               $this->setContinueEnumParameter( 'continue', key( $titles ) );
+       }
+
+       /**
+        * @see ApiBase::getAllowedParams
+        */
+       protected function getAllowedParams() {
+               return array(
+                       'continue' => array(
+                               ApiBase::PARAM_HELP_MSG => 
'api-help-param-continue',
+                               ApiBase::PARAM_TYPE => 'integer',
+                       ),
+               );
+       }
+
+       /**
+        * @see ApiBase::getExamplesMessages
+        */
+       protected function getExamplesMessages() {
+               return array(
+                       // @TODO
+               );
+       }
+
+       public function getHelpUrls() {
+               // @TODO
+       }
+
+}
diff --git a/includes/lua/Infobox.lua b/includes/lua/Infobox.lua
index b51519a..353a9cf 100644
--- a/includes/lua/Infobox.lua
+++ b/includes/lua/Infobox.lua
@@ -41,6 +41,7 @@
        local html = mw.html.create( '' )
        local args = t.args
        local render = require( 'CapiuntoInfoboxRender' )
+       php.storeInfobox( args )
 
        html = render.renderWrapper( html, args )
 

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ia176fb6736d6dc78980928efe9ed3dd444b7780b
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Capiunto
Gerrit-Branch: master
Gerrit-Owner: Hoo man <[email protected]>

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

Reply via email to