[ Index ] |
PHP Cross Reference of moodle-2.8 |
[Summary view] [Print] [Text view]
1 <?php 2 3 // This file is part of Moodle - http://moodle.org/ 4 // 5 // Moodle is free software: you can redistribute it and/or modify 6 // it under the terms of the GNU General Public License as published by 7 // the Free Software Foundation, either version 3 of the License, or 8 // (at your option) any later version. 9 // 10 // Moodle is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 17 18 /** 19 * wikimedia class 20 * class for communication with Wikimedia Commons API 21 * 22 * @author Dongsheng Cai <[email protected]>, Raul Kern <[email protected]> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU Public License 24 */ 25 26 define('WIKIMEDIA_THUMBS_PER_PAGE', 24); 27 define('WIKIMEDIA_FILE_NS', 6); 28 define('WIKIMEDIA_IMAGE_SIDE_LENGTH', 1024); 29 define('WIKIMEDIA_THUMB_SIZE', 120); 30 31 class wikimedia { 32 private $_conn = null; 33 private $_param = array(); 34 35 public function __construct($url = '') { 36 if (empty($url)) { 37 $this->api = 'http://commons.wikimedia.org/w/api.php'; 38 } else { 39 $this->api = $url; 40 } 41 $this->_param['format'] = 'php'; 42 $this->_param['redirects'] = true; 43 $this->_conn = new curl(array('cache'=>true, 'debug'=>false)); 44 } 45 public function login($user, $pass) { 46 $this->_param['action'] = 'login'; 47 $this->_param['lgname'] = $user; 48 $this->_param['lgpassword'] = $pass; 49 $content = $this->_conn->post($this->api, $this->_param); 50 $result = unserialize($content); 51 if (!empty($result['result']['sessionid'])) { 52 $this->userid = $result['result']['lguserid']; 53 $this->username = $result['result']['lgusername']; 54 $this->token = $result['result']['lgtoken']; 55 return true; 56 } else { 57 return false; 58 } 59 } 60 public function logout() { 61 $this->_param['action'] = 'logout'; 62 $content = $this->_conn->post($this->api, $this->_param); 63 return; 64 } 65 public function get_image_url($titles) { 66 $image_urls = array(); 67 $this->_param['action'] = 'query'; 68 if (is_array($titles)) { 69 foreach ($titles as $title) { 70 $this->_param['titles'] .= ('|'.urldecode($title)); 71 } 72 } else { 73 $this->_param['titles'] = urldecode($title); 74 } 75 $this->_param['prop'] = 'imageinfo'; 76 $this->_param['iiprop'] = 'url'; 77 $content = $this->_conn->post($this->api, $this->_param); 78 $result = unserialize($content); 79 foreach ($result['query']['pages'] as $page) { 80 if (!empty($page['imageinfo'][0]['url'])) { 81 $image_urls[] = $page['imageinfo'][0]['url']; 82 } 83 } 84 return $image_urls; 85 } 86 public function get_images_by_page($title) { 87 $image_urls = array(); 88 $this->_param['action'] = 'query'; 89 $this->_param['generator'] = 'images'; 90 $this->_param['titles'] = urldecode($title); 91 $this->_param['prop'] = 'images|info|imageinfo'; 92 $this->_param['iiprop'] = 'url'; 93 $content = $this->_conn->post($this->api, $this->_param); 94 $result = unserialize($content); 95 if (!empty($result['query']['pages'])) { 96 foreach ($result['query']['pages'] as $page) { 97 $image_urls[$page['title']] = $page['imageinfo'][0]['url']; 98 } 99 } 100 return $image_urls; 101 } 102 /** 103 * Generate thumbnail URL from image URL. 104 * 105 * @param string $image_url 106 * @param int $orig_width 107 * @param int $orig_height 108 * @param int $thumb_width 109 * @global object OUTPUT 110 * @return string 111 */ 112 public function get_thumb_url($image_url, $orig_width, $orig_height, $thumb_width=75) { 113 global $OUTPUT; 114 115 if ($orig_width <= $thumb_width AND $orig_height <= $thumb_width) { 116 return $image_url; 117 } else { 118 $thumb_url = ''; 119 $commons_main_dir = 'http://upload.wikimedia.org/wikipedia/commons/'; 120 if ($image_url) { 121 $short_path = str_replace($commons_main_dir, '', $image_url); 122 $extension = strtolower(pathinfo($short_path, PATHINFO_EXTENSION)); 123 if (strcmp($extension, 'gif') == 0) { //no thumb for gifs 124 return $OUTPUT->pix_url(file_extension_icon('.gif', $thumb_width))->out(false); 125 } 126 $dir_parts = explode('/', $short_path); 127 $file_name = end($dir_parts); 128 if ($orig_height > $orig_width) { 129 $thumb_width = round($thumb_width * $orig_width/$orig_height); 130 } 131 $thumb_url = $commons_main_dir . 'thumb/' . implode('/', $dir_parts) . '/'. $thumb_width .'px-' . $file_name; 132 if (strcmp($extension, 'svg') == 0) { //png thumb for svg-s 133 $thumb_url .= '.png'; 134 } 135 } 136 return $thumb_url; 137 } 138 } 139 140 /** 141 * Search for images and return photos array. 142 * 143 * @param string $keyword 144 * @param int $page 145 * @param array $params additional query params 146 * @return array 147 */ 148 public function search_images($keyword, $page = 0, $params = array()) { 149 global $OUTPUT; 150 $files_array = array(); 151 $this->_param['action'] = 'query'; 152 $this->_param['generator'] = 'search'; 153 $this->_param['gsrsearch'] = $keyword; 154 $this->_param['gsrnamespace'] = WIKIMEDIA_FILE_NS; 155 $this->_param['gsrlimit'] = WIKIMEDIA_THUMBS_PER_PAGE; 156 $this->_param['gsroffset'] = $page * WIKIMEDIA_THUMBS_PER_PAGE; 157 $this->_param['prop'] = 'imageinfo'; 158 $this->_param['iiprop'] = 'url|dimensions|mime|timestamp|size|user'; 159 $this->_param += $params; 160 $this->_param += array('iiurlwidth' => WIKIMEDIA_IMAGE_SIDE_LENGTH, 161 'iiurlheight' => WIKIMEDIA_IMAGE_SIDE_LENGTH); 162 //didn't work with POST 163 $content = $this->_conn->get($this->api, $this->_param); 164 $result = unserialize($content); 165 if (!empty($result['query']['pages'])) { 166 foreach ($result['query']['pages'] as $page) { 167 $title = $page['title']; 168 $file_type = $page['imageinfo'][0]['mime']; 169 $image_types = array('image/jpeg', 'image/png', 'image/gif', 'image/svg+xml'); 170 if (in_array($file_type, $image_types)) { //is image 171 $extension = pathinfo($title, PATHINFO_EXTENSION); 172 if (strcmp($extension, 'svg') == 0) { //upload png version of svg-s 173 $title .= '.png'; 174 } 175 if ($page['imageinfo'][0]['thumbwidth'] < $page['imageinfo'][0]['width']) { 176 $attrs = array( 177 //upload scaled down image 178 'source' => $page['imageinfo'][0]['thumburl'], 179 'image_width' => $page['imageinfo'][0]['thumbwidth'], 180 'image_height' => $page['imageinfo'][0]['thumbheight'] 181 ); 182 if ($attrs['image_width'] <= WIKIMEDIA_THUMB_SIZE && $attrs['image_height'] <= WIKIMEDIA_THUMB_SIZE) { 183 $attrs['realthumbnail'] = $attrs['source']; 184 } 185 if ($attrs['image_width'] <= 24 && $attrs['image_height'] <= 24) { 186 $attrs['realicon'] = $attrs['source']; 187 } 188 } else { 189 $attrs = array( 190 //upload full size image 191 'source' => $page['imageinfo'][0]['url'], 192 'image_width' => $page['imageinfo'][0]['width'], 193 'image_height' => $page['imageinfo'][0]['height'], 194 'size' => $page['imageinfo'][0]['size'] 195 ); 196 } 197 $attrs += array( 198 'realthumbnail' => $this->get_thumb_url($page['imageinfo'][0]['url'], $page['imageinfo'][0]['width'], $page['imageinfo'][0]['height'], WIKIMEDIA_THUMB_SIZE), 199 'realicon' => $this->get_thumb_url($page['imageinfo'][0]['url'], $page['imageinfo'][0]['width'], $page['imageinfo'][0]['height'], 24), 200 'author' => $page['imageinfo'][0]['user'], 201 'datemodified' => strtotime($page['imageinfo'][0]['timestamp']), 202 ); 203 } else { // other file types 204 $attrs = array('source' => $page['imageinfo'][0]['url']); 205 } 206 $files_array[] = array( 207 'title'=>substr($title, 5), //chop off 'File:' 208 'thumbnail' => $OUTPUT->pix_url(file_extension_icon(substr($title, 5), WIKIMEDIA_THUMB_SIZE))->out(false), 209 'thumbnail_width' => WIKIMEDIA_THUMB_SIZE, 210 'thumbnail_height' => WIKIMEDIA_THUMB_SIZE, 211 'license' => 'cc-sa', 212 // the accessible url of the file 213 'url'=>$page['imageinfo'][0]['descriptionurl'] 214 ) + $attrs; 215 } 216 } 217 return $files_array; 218 } 219 220 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 20:29:05 2014 | Cross-referenced by PHPXref 0.7.1 |