MDL-37541 Wikimedia returns more information about images
authorMarina Glancy <marina@moodle.com>
Wed, 16 Jan 2013 04:19:15 +0000 (15:19 +1100)
committerMarina Glancy <marina@moodle.com>
Tue, 22 Jan 2013 02:41:38 +0000 (13:41 +1100)
repository/wikimedia/wikimedia.php

index d02b181..b70bfae 100644 (file)
@@ -142,6 +142,7 @@ class wikimedia {
      * @return array
      */
     public function search_images($keyword, $page = 0) {
+        global $OUTPUT;
         $files_array = array();
         $this->_param['action'] = 'query';
         $this->_param['generator'] = 'search';
@@ -150,7 +151,7 @@ class wikimedia {
         $this->_param['gsrlimit'] = WIKIMEDIA_THUMBS_PER_PAGE;
         $this->_param['gsroffset'] = $page * WIKIMEDIA_THUMBS_PER_PAGE;
         $this->_param['prop']   = 'imageinfo';
-        $this->_param['iiprop'] = 'url|dimensions|mime';
+        $this->_param['iiprop'] = 'url|dimensions|mime|timestamp|size|user';
         $this->_param['iiurlwidth'] = WIKIMEDIA_IMAGE_SIDE_LENGTH;
         $this->_param['iiurlheight'] = WIKIMEDIA_IMAGE_SIDE_LENGTH;
         //didn't work with POST
@@ -162,26 +163,45 @@ class wikimedia {
                 $file_type = $page['imageinfo'][0]['mime'];
                 $image_types = array('image/jpeg', 'image/png', 'image/gif', 'image/svg+xml');
                 if (in_array($file_type, $image_types)) {  //is image
-                    $thumbnail = $this->get_thumb_url($page['imageinfo'][0]['url'], $page['imageinfo'][0]['width'], $page['imageinfo'][0]['height']);
-                    $source = $page['imageinfo'][0]['thumburl'];        //upload scaled down image
                     $extension = pathinfo($title, PATHINFO_EXTENSION);
                     if (strcmp($extension, 'svg') == 0) {               //upload png version of svg-s
                         $title .= '.png';
                     }
-                } else {                                   //other file types
-                    $thumbnail = '';
-                    $source = $page['imageinfo'][0]['url'];
+                    if ($page['imageinfo'][0]['thumbwidth'] < $page['imageinfo'][0]['width']) {
+                        $attrs = array(
+                            //upload scaled down image
+                            'source' => $page['imageinfo'][0]['thumburl'],
+                            'image_width' => $page['imageinfo'][0]['thumbwidth'],
+                            'image_height' => $page['imageinfo'][0]['thumbheight']
+                        );
+                    } else {
+                        $attrs = array(
+                            //upload full size image
+                            'source' => $page['imageinfo'][0]['url'],
+                            'image_width' => $page['imageinfo'][0]['width'],
+                            'image_height' => $page['imageinfo'][0]['height'],
+                            'size' => $page['imageinfo'][0]['size']
+                        );
+                    }
+                    $attrs += array(
+                        'thumbnail' => $this->get_thumb_url($page['imageinfo'][0]['url'], $page['imageinfo'][0]['width'], $page['imageinfo'][0]['height'], 120),
+                        'icon' => $this->get_thumb_url($page['imageinfo'][0]['url'], $page['imageinfo'][0]['width'], $page['imageinfo'][0]['height'], 24),
+                        'author' => $page['imageinfo'][0]['user'],
+                        'datemodified' => strtotime($page['imageinfo'][0]['timestamp']),
+                        );
+                } else {  // other file types
+                    $attrs = array(
+                        'thumbnail' => $OUTPUT->pix_url(file_extension_icon(substr($title, 5), 120))->out(false),
+                        'source' => $page['imageinfo'][0]['url']);
                 }
                 $files_array[] = array(
                     'title'=>substr($title, 5),         //chop off 'File:'
-                    'thumbnail'=>$thumbnail,
                     'thumbnail_width'=>120,
                     'thumbnail_height'=>120,
-                    // plugin-dependent unique path to the file (id, url, path, etc.)
-                    'source'=>$source,
+                    'license' => 'cc-sa',
                     // the accessible url of the file
                     'url'=>$page['imageinfo'][0]['descriptionurl']
-                );
+                ) + $attrs;
             }
         }
         return $files_array;