--- /dev/null
+<?php
+require_once('wikimedia.php');
+
+class repository_wikimedia extends repository {
+ public function __construct($repositoryid, $context = SITEID, $options = array()) {
+ parent::__construct($repositoryid, $context, $options);
+ $this->keyword = optional_param('wikimedia_keyword', '', PARAM_RAW);
+ }
+ public function get_listing($path = '') {
+ global $CFG;
+ $client = new wikimedia;
+ $result = $client->search_images($this->keyword);
+ $list = array();
+ $list['list'] = array();
+ foreach ($result as $title=>$url) {
+ $list['list'][] = array(
+ 'title'=>$title,
+ 'thumbnail'=>$CFG->pixpath.'/f/'.mimeinfo('icon', 'xx.jpg'),
+ // plugin-dependent unique path to the file (id, url, path, etc.)
+ 'source'=>$url,
+ // the accessible url of the file
+ 'url'=>$url
+ );
+ }
+ return $list;
+ }
+ // login
+ public function check_login() {
+ echo_fb($this->keyword);
+ return !empty($this->keyword);
+ }
+ // if check_login returns false,
+ // this function will be called to print a login form.
+ public function print_login() {
+ $keyword->label = get_string('keyword', 'repository_wikimedia').': ';
+ $keyword->id = 'input_text_keyword';
+ $keyword->type = 'text';
+ $keyword->name = 'wikimedia_keyword';
+ $keyword->value = '';
+
+ $form = array();
+ $form['login'] = array($keyword);
+ return $form;
+ }
+ //search
+ // if this plugin support global search, if this function return
+ // true, search function will be called when global searching working
+ public function global_search() {
+ return false;
+ }
+ public function search($text) {
+ $search_result = array();
+ $search_result['list'] = array();
+ return $search_result;
+ }
+ // when logout button on file picker is clicked, this function will be
+ // called.
+ public function logout() {
+ return true;
+ }
+ public static function get_type_option_names() {
+ return null;
+ }
+}
--- /dev/null
+<?php
+
+class wikimedia {
+ private $_conn = null;
+ private $_param = array();
+ public function __construct($url = '') {
+ if (empty($url)) {
+ $this->api = 'http://commons.wikimedia.org/w/api.php';
+ }
+ $this->_param['format'] = 'php';
+ $this->_param['redirects'] = true;
+ $this->_conn = new curl(array('cache'=>true, 'debug'=>false));
+ }
+ public function login($user, $pass) {
+ $this->_param['action'] = 'login';
+ $this->_param['lgname'] = $user;
+ $this->_param['lgpassword'] = $pass;
+ $content = $this->_conn->post($this->api, $this->_param);
+ $result = unserialize($content);
+ if (!empty($result['result']['sessionid'])) {
+ $this->userid = $result['result']['lguserid'];
+ $this->username = $result['result']['lgusername'];
+ $this->token = $result['result']['lgtoken'];
+ return true;
+ } else {
+ return false;
+ }
+ }
+ public function logout() {
+ $this->_param['action'] = 'logout';
+ $content = $this->_conn->post($this->api, $this->_param);
+ return;
+ }
+ public function get_image_url($titles) {
+ $image_urls = array();
+ $this->_param['action'] = 'query';
+ if (is_array($titles)) {
+ foreach ($titles as $title) {
+ $this->_param['titles'] .= ('|'.urldecode($title));
+ }
+ } else {
+ $this->_param['titles'] = urldecode($title);
+ }
+ $this->_param['prop'] = 'imageinfo';
+ $this->_param['iiprop'] = 'url';
+ $content = $this->_conn->post($this->api, $this->_param);
+ $result = unserialize($content);
+ foreach ($result['query']['pages'] as $page) {
+ if (!empty($page['imageinfo'][0]['url'])) {
+ $image_urls[] = $page['imageinfo'][0]['url'];
+ }
+ }
+ return $image_urls;
+ }
+ public function get_images_by_page($title) {
+ $image_urls = array();
+ $this->_param['action'] = 'query';
+ $this->_param['generator'] = 'images';
+ $this->_param['titles'] = urldecode($title);
+ $this->_param['prop'] = 'images|info|imageinfo';
+ $this->_param['iiprop'] = 'url';
+ $content = $this->_conn->post($this->api, $this->_param);
+ $result = unserialize($content);
+ if (!empty($result['query']['pages'])) {
+ foreach ($result['query']['pages'] as $page) {
+ $image_urls[$page['title']] = $page['imageinfo'][0]['url'];
+ }
+ }
+ return $image_urls;
+ }
+ public function search_images($title) {
+ $image_urls = array();
+ $this->_param['action'] = 'query';
+ $this->_param['generator'] = 'search';
+ $this->_param['gsrsearch'] = $title;
+ $this->_param['gsrnamespace'] = 6;
+ $this->_param['prop'] = 'imageinfo';
+ $this->_param['iiprop'] = 'url';
+ $content = $this->_conn->post($this->api, $this->_param);
+ $result = unserialize($content);
+ if (!empty($result['query']['pages'])) {
+ foreach ($result['query']['pages'] as $page) {
+ $image_urls[$page['title']] = $page['imageinfo'][0]['url'];
+ }
+ }
+ return $image_urls;
+ }
+}