Moodle  2.2.1
http://www.collinsharper.com
C:/xampp/htdocs/moodle/repository/wikimedia/wikimedia.php
Go to the documentation of this file.
00001 <?php
00002 
00003 // This file is part of Moodle - http://moodle.org/
00004 //
00005 // Moodle is free software: you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License as published by
00007 // the Free Software Foundation, either version 3 of the License, or
00008 // (at your option) any later version.
00009 //
00010 // Moodle is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 // GNU General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Moodle.  If not, see <http://www.gnu.org/licenses/>.
00017 
00026 define('WIKIMEDIA_THUMBS_PER_PAGE', 24);
00027 define('WIKIMEDIA_FILE_NS', 6);
00028 define('WIKIMEDIA_IMAGE_SIDE_LENGTH', 1024);
00029 
00030 class wikimedia {
00031     private $_conn  = null;
00032     private $_param = array();
00033 
00034     public function __construct($url = '') {
00035         if (empty($url)) {
00036             $this->api = 'http://commons.wikimedia.org/w/api.php';
00037         } else {
00038             $this->api = $url;
00039         }
00040         $this->_param['format'] = 'php';
00041         $this->_param['redirects'] = true;
00042         $this->_conn = new curl(array('cache'=>true, 'debug'=>false));
00043     }
00044     public function login($user, $pass) {
00045         $this->_param['action']   = 'login';
00046         $this->_param['lgname']   = $user;
00047         $this->_param['lgpassword'] = $pass;
00048         $content = $this->_conn->post($this->api, $this->_param);
00049         $result = unserialize($content);
00050         if (!empty($result['result']['sessionid'])) {
00051             $this->userid = $result['result']['lguserid'];
00052             $this->username = $result['result']['lgusername'];
00053             $this->token = $result['result']['lgtoken'];
00054             return true;
00055         } else {
00056             return false;
00057         }
00058     }
00059     public function logout() {
00060         $this->_param['action']   = 'logout';
00061         $content = $this->_conn->post($this->api, $this->_param);
00062         return;
00063     }
00064     public function get_image_url($titles) {
00065         $image_urls = array();
00066         $this->_param['action'] = 'query';
00067         if (is_array($titles)) {
00068             foreach ($titles as $title) {
00069                 $this->_param['titles'] .= ('|'.urldecode($title));
00070             }
00071         } else {
00072             $this->_param['titles'] = urldecode($title);
00073         }
00074         $this->_param['prop']   = 'imageinfo';
00075         $this->_param['iiprop'] = 'url';
00076         $content = $this->_conn->post($this->api, $this->_param);
00077         $result = unserialize($content);
00078         foreach ($result['query']['pages'] as $page) {
00079             if (!empty($page['imageinfo'][0]['url'])) {
00080                 $image_urls[] = $page['imageinfo'][0]['url'];
00081             }
00082         }
00083         return $image_urls;
00084     }
00085     public function get_images_by_page($title) {
00086         $image_urls = array();
00087         $this->_param['action'] = 'query';
00088         $this->_param['generator'] = 'images';
00089         $this->_param['titles'] = urldecode($title);
00090         $this->_param['prop']   = 'images|info|imageinfo';
00091         $this->_param['iiprop'] = 'url';
00092         $content = $this->_conn->post($this->api, $this->_param);
00093         $result = unserialize($content);
00094         if (!empty($result['query']['pages'])) {
00095             foreach ($result['query']['pages'] as $page) {
00096                 $image_urls[$page['title']] = $page['imageinfo'][0]['url'];
00097             }
00098         }
00099         return $image_urls;
00100     }
00111     public function get_thumb_url($image_url, $orig_width, $orig_height, $thumb_width=75) {
00112         global $OUTPUT;
00113 
00114         if ($orig_width <= $thumb_width AND $orig_height <= $thumb_width) {
00115             return $image_url;
00116         } else {
00117             $thumb_url = '';
00118             $commons_main_dir = 'http://upload.wikimedia.org/wikipedia/commons/';
00119             if ($image_url) {
00120                 $short_path = str_replace($commons_main_dir, '', $image_url);
00121                 $extension = pathinfo($short_path, PATHINFO_EXTENSION);
00122                 if (strcmp($extension, 'gif') == 0) {  //no thumb for gifs
00123                     return $OUTPUT->pix_url(file_extension_icon('xx.jpg', 32));
00124                 }
00125                 $dir_parts = explode('/', $short_path);
00126                 $file_name = end($dir_parts);
00127                 if ($orig_height > $orig_width) {
00128                     $thumb_width = round($thumb_width * $orig_width/$orig_height);
00129                 }
00130                 $thumb_url = $commons_main_dir . 'thumb/' . implode('/', $dir_parts) . '/'. $thumb_width .'px-' . $file_name;
00131                 if (strcmp($extension, 'svg') == 0) {  //png thumb for svg-s
00132                     $thumb_url .= '.png';
00133                 }
00134             }
00135             return $thumb_url;
00136         }
00137     }
00144     public function search_images($keyword) {
00145         $files_array = array();
00146         $this->_param['action'] = 'query';
00147         $this->_param['generator'] = 'search';
00148         $this->_param['gsrsearch'] = $keyword;
00149         $this->_param['gsrnamespace'] = WIKIMEDIA_FILE_NS;
00150         $this->_param['gsrlimit'] = WIKIMEDIA_THUMBS_PER_PAGE;
00151         $this->_param['prop']   = 'imageinfo';
00152         $this->_param['iiprop'] = 'url|dimensions|mime';
00153         $this->_param['iiurlwidth'] = WIKIMEDIA_IMAGE_SIDE_LENGTH;
00154         $this->_param['iiurlheight'] = WIKIMEDIA_IMAGE_SIDE_LENGTH;
00155         //didn't work with POST
00156         $content = $this->_conn->get($this->api, $this->_param);
00157         $result = unserialize($content);
00158         if (!empty($result['query']['pages'])) {
00159             foreach ($result['query']['pages'] as $page) {
00160                 $title = $page['title'];
00161                 $file_type = $page['imageinfo'][0]['mime'];
00162                 $image_types = array('image/jpeg', 'image/png', 'image/gif', 'image/svg+xml');
00163                 if (in_array($file_type, $image_types)) {  //is image
00164                     $thumbnail = $this->get_thumb_url($page['imageinfo'][0]['url'], $page['imageinfo'][0]['width'], $page['imageinfo'][0]['height']);
00165                     $source = $page['imageinfo'][0]['thumburl'];        //upload scaled down image
00166                     $extension = pathinfo($title, PATHINFO_EXTENSION);
00167                     if (strcmp($extension, 'svg') == 0) {               //upload png version of svg-s
00168                         $title .= '.png';
00169                     }
00170                 } else {                                   //other file types
00171                     $thumbnail = '';
00172                     $source = $page['imageinfo'][0]['url'];
00173                 }
00174                 $files_array[] = array(
00175                     'title'=>substr($title, 5),         //chop off 'File:'
00176                     'thumbnail'=>$thumbnail,
00177                     'thumbnail_width'=>120,
00178                     'thumbnail_height'=>120,
00179                     // plugin-dependent unique path to the file (id, url, path, etc.)
00180                     'source'=>$source,
00181                     // the accessible url of the file
00182                     'url'=>$page['imageinfo'][0]['descriptionurl']
00183                 );
00184             }
00185         }
00186         return $files_array;
00187     }
00188 
00189 }
 All Data Structures Namespaces Files Functions Variables Enumerations