MaxSem has uploaded a new change for review.

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


Change subject: Make PageImages return files themselves in NS_FILE
......................................................................

Make PageImages return files themselves in NS_FILE

Bug: 50252
Change-Id: Iec13da015d50fafde8c91a222c2a3a5d791412f8
---
M ApiQueryPageImages.php
M PageImages.php
2 files changed, 68 insertions(+), 23 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/PageImages 
refs/changes/25/72625/1

diff --git a/ApiQueryPageImages.php b/ApiQueryPageImages.php
index 6487bdb..db24a3b 100644
--- a/ApiQueryPageImages.php
+++ b/ApiQueryPageImages.php
@@ -19,17 +19,43 @@
                }
                $size = $params['thumbsize'];
                $limit = $params['limit'];
-
-               $this->addTables( 'page_props' );
-               $this->addFields( array( 'pp_page', 'pp_propname', 'pp_value' ) 
);
-               $this->addWhere( array( 'pp_page' => array_keys( $titles ), 
'pp_propname' => 'page_image' ) );
+               $continue = 0;
                if ( isset( $params['continue'] ) ) {
                        // is_numeric() accepts floats, so...
                        if ( intval( $params['continue'] ) == 
$params['continue'] ) {
-                               $this->addWhere( 'pp_page >= ' . intval( 
$params['continue'] ) );
+                               $continue = intval( $params['continue'] );
                        } else {
                                $this->dieUsage( 'Invalid continue param. You 
should pass the original value returned by the previous query' , '_badcontinue' 
);
                        }
+               }
+
+               // Filter out certain titles manually
+               $files = array();
+               $otherPages = array();
+               $requestedIds = array();
+               ksort( $titles );
+               $count = 0;
+               /** @var $title Title */
+               foreach( $titles as $id => $title ) {
+                       if ( $id < $continue ) {
+                               continue;
+                       }
+                       if ( $count++ > $limit + 1 ) {
+                               break;
+                       }
+                       if ( $title->inNamespace( NS_FILE ) ) {
+                               $files[] = $id;
+                       } else {
+                               $otherPages[] = $id;
+                       }
+                       $requestedIds[] = $id;
+               }
+
+               $this->addTables( 'page_props' );
+               $this->addFields( array( 'pp_page', 'pp_propname', 'pp_value' ) 
);
+               $this->addWhere( array( 'pp_page' => $otherPages, 'pp_propname' 
=> 'page_image' ) );
+               if ( $continue ) {
+                       $this->addWhere( 'pp_page >= ' . $continue );
                }
                $this->addOption( 'ORDER BY', 'pp_page' );
                $this->addOption( 'LIMIT', $limit + 1 );
@@ -47,34 +73,48 @@
                                echo 'break';
                                break;
                        }
-                       $vals = array();
-                       if ( isset( $prop['thumbnail'] ) ) {
-                               $file = wfFindFile( $row->pp_value );
-                               if ( $file ) {
-                                       $thumb = $file->transform( array( 
'width' => $size, 'height' => $size ) );
-                                       if ( $thumb ) {
-                                               $vals['thumbnail'] = array(
-                                                       'source' => 
wfExpandUrl( $thumb->getUrl(), PROTO_CURRENT ),
-                                                       'width' => 
$thumb->getWidth(),
-                                                       'height' => 
$thumb->getHeight(),
-                                               );
-                                       }
-                               }
-                       }
-                       if ( isset( $prop['name'] ) ) {
-                               $vals['pageimage'] = $row->pp_value;
-                       }
-                       $fit = $this->getResult()->addValue( array( 'query', 
'pages' ), $pageId, $vals );
+
+                       $fit = $this->getResult()->addValue( array( 'query', 
'pages' ),
+                               $pageId,
+                               $this->getInfo( $row->pp_value, $prop, $size )
+                       );
                        if ( !$fit ) {
                                $this->setContinueEnumParameter( 'continue', 
$pageId );
                                break;
                        }
                }
+               foreach( $files as $id ) {
+                       $fit = $this->getResult()->addValue( array( 'query', 
'pages' ),
+                               $pageId,
+                               $this->getInfo( $titles[$id], $prop, $size )
+                       );
+               }
                wfProfileOut( __METHOD__ . '-results' );
 
                wfProfileOut( __METHOD__ );
        }
 
+       private function getInfo( $imageName, $prop, $size ) {
+               $vals = array();
+               if ( isset( $prop['thumbnail'] ) ) {
+                       $file = wfFindFile( $imageName );
+                       if ( $file ) {
+                               $thumb = $file->transform( array( 'width' => 
$size, 'height' => $size ) );
+                               if ( $thumb ) {
+                                       $vals['thumbnail'] = array(
+                                               'source' => wfExpandUrl( 
$thumb->getUrl(), PROTO_CURRENT ),
+                                               'width' => $thumb->getWidth(),
+                                               'height' => $thumb->getHeight(),
+                                       );
+                               }
+                       }
+               }
+               if ( isset( $prop['name'] ) ) {
+                       $vals['pageimage'] = $imageName;
+               }
+               return $vals;
+       }
+
        public function getDescription() {
                return 'Returns information about images on the page such as 
thumbnail and presence of photos.';
        }
diff --git a/PageImages.php b/PageImages.php
index 2d1a96b..8dd57fd 100644
--- a/PageImages.php
+++ b/PageImages.php
@@ -68,3 +68,8 @@
  * Collect data only for these namespaces
  */
 $wgPageImagesNamespaces = array( NS_MAIN );
+
+/**
+ *
+ */
+$wgPageImagesExtensions = array( 'png', 'jpg', 'jpeg', 'gif', 'svg' );
\ No newline at end of file

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: Iec13da015d50fafde8c91a222c2a3a5d791412f8
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/PageImages
Gerrit-Branch: master
Gerrit-Owner: MaxSem <[email protected]>

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

Reply via email to