Brian Wolff has uploaded a new change for review.

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


Change subject: (DO NOT MERGE; needs I49b7d8a05) Add an lua interface to 
getting file metadata
......................................................................

(DO NOT MERGE; needs I49b7d8a05) Add an lua interface to getting file metadata

This allows people to make fancy templates using the EXIF/XMP/IPTC metadata
from files.

Note, this didn't add any hooks to the formatting code, I figure that
if someone wants to manipulate it with lua, they're looking for the
raw data. Anyhow, see my comments on the bug.

p.s. First attempt touching anything lua related, so let me know
if I did something horribly horribly wrong.

Bug: 41498
Change-Id: Ie749b30703b1994e80986478fc5d3fce8f3d25a6
---
M engines/LuaCommon/TitleLibrary.php
M engines/LuaCommon/lualib/mw.title.lua
2 files changed, 64 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Scribunto 
refs/changes/88/67588/1

diff --git a/engines/LuaCommon/TitleLibrary.php 
b/engines/LuaCommon/TitleLibrary.php
index 5125920..0399fc7 100644
--- a/engines/LuaCommon/TitleLibrary.php
+++ b/engines/LuaCommon/TitleLibrary.php
@@ -15,6 +15,7 @@
                        'getUrl' => array( $this, 'getUrl' ),
                        'getContent' => array( $this, 'getContent' ),
                        'fileExists' => array( $this, 'fileExists' ),
+                       'getFileMetadata' => array( $this, 'getFileMetadata' ),
                );
                $this->getEngine()->registerInterface( 'mw.title.lua', $lib, 
array(
                        'thisTitle' => $this->returnTitleToLua( 
$this->getTitle() ),
@@ -254,4 +255,57 @@
                );
                return array( (bool)$file->exists() );
        }
+
+       function getFileMetadata( $text ) {
+               $this->checkType( 'getFileMetadata', 1, $text, 'string' );
+               $title = Title::newFromText( $text );
+               if ( !$title ) {
+                       return array( false );
+               }
+               $ns = $title->getNamespace();
+               if ( $ns !== NS_FILE && $ns !== NS_MEDIA ) {
+                       return array( false );
+               }
+
+               $this->incrementExpensiveFunctionCount();
+               $file = wfFindFile( $title );
+               if ( !$file ) {
+                       return array( false );
+               }
+               $this->getParser()->getOutput()->addImage(
+                       $file->getName(), $file->getTimestamp(), 
$file->getSha1()
+               );
+
+               $metadata = $file->getStandardMetaArray();
+               if ( $metadata === false ) {
+                       return array( false );
+               }
+               return $this->normalizeMetadataForLua( $metadata );
+       }
+
+       /**
+        * Change indicies to be 1 based, and make the array more
+        * uniformly consistent.
+        */
+       private function normalizeMetadataForLua( $metadata ) {
+               foreach( $metadata as &$entry ) {
+                       // Don't let anything be a simple value,
+                       // let everything be an array with potentially only
+                       // 1 element, so it is more consistent to the user.
+                       if ( is_string( $entry ) ) {
+                               $entry = array( $entry );
+                       }
+
+                       if ( isset( $entry[0] ) ) {
+                               for ( $i = 1, $cur = $entry[0]; isset( 
$entry[$i] ); $i++ ) {
+                                       $next = $entry[$i]; 
+                                       $entry[$i] = $cur;
+                                       $cur = $next;
+                               }
+                               $entry[$i] = $cur;
+                               unset( $entry[0] );
+                       }
+               }
+               return array( $metadata );
+       }
 }
diff --git a/engines/LuaCommon/lualib/mw.title.lua 
b/engines/LuaCommon/lualib/mw.title.lua
index 03184ef..456df99 100644
--- a/engines/LuaCommon/lualib/mw.title.lua
+++ b/engines/LuaCommon/lualib/mw.title.lua
@@ -154,6 +154,16 @@
                return content
        end
 
+       function data:getFileMetadata()
+               checkSelf( self, 'getFileMetadata' )
+               local metadata = php.getFileMetadata( self.fullText )
+               data.getFileMetadata = function ( self )
+                       checkSelf( self, 'getFileMetadata' )
+                       return metadata
+               end
+               return metadata
+       end
+
        -- Known fields, both those defined above and any dynamically handled in
        -- __index, mapped to whether they are read-only.
        local readOnlyFields = {

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie749b30703b1994e80986478fc5d3fce8f3d25a6
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Scribunto
Gerrit-Branch: master
Gerrit-Owner: Brian Wolff <[email protected]>

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

Reply via email to