Skip to content

Commit

Permalink
MDL-37541 Wikimedia allows to resize found images
Browse files Browse the repository at this point in the history
  • Loading branch information
marinaglancy committed Jan 22, 2013
1 parent df8b1e7 commit 1d37910
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 14 deletions.
4 changes: 3 additions & 1 deletion repository/wikimedia/lang/en/repository_wikimedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

$string['keyword'] = 'Full text';
$string['keyword'] = 'Search for';
$string['pluginname'] = 'Wikimedia';
$string['wikimedia:view'] = 'View wikimedia repository';
$string['configplugin'] = 'Wikimedia repository type configuration';
$string['maxwidth'] = 'Max image width (px)';
$string['maxheight'] = 'Max image height (px)';
58 changes: 55 additions & 3 deletions repository/wikimedia/lib.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,14 +55,53 @@ public function __construct($repositoryid, $context = SYSCONTEXTID, $options = a
$SESSION->{$sess_keyword} = $this->keyword;
}
}

/**
* Returns maximum width for images
*
* Takes the maximum width for images eithre from search form or from
* user preferences, updates user preferences if needed
*
* @return int
*/
public function get_maxwidth() {
$param = optional_param('wikimedia_maxwidth', 0, PARAM_INT);
$pref = get_user_preferences('repository_wikimedia_maxwidth', WIKIMEDIA_IMAGE_SIDE_LENGTH);
if ($param > 0 && $param != $pref) {
$pref = $param;
set_user_preference('repository_wikimedia_maxwidth', $pref);
}
return $pref;
}

/**
* Returns maximum height for images
*
* Takes the maximum height for images eithre from search form or from
* user preferences, updates user preferences if needed
*
* @return int
*/
public function get_maxheight() {
$param = optional_param('wikimedia_maxheight', 0, PARAM_INT);
$pref = get_user_preferences('repository_wikimedia_maxheight', WIKIMEDIA_IMAGE_SIDE_LENGTH);
if ($param > 0 && $param != $pref) {
$pref = $param;
set_user_preference('repository_wikimedia_maxheight', $pref);
}
return $pref;
}

public function get_listing($path = '', $page = '') {
$client = new wikimedia;
$list = array();
$list['page'] = (int)$page;
if ($list['page'] < 1) {
$list['page'] = 1;
}
$list['list'] = $client->search_images($this->keyword, $list['page'] - 1);
$list['list'] = $client->search_images($this->keyword, $list['page'] - 1,
array('iiurlwidth' => $this->get_maxwidth(),
'iiurlheight' => $this->get_maxheight()));
$list['nologin'] = true;
$list['norefresh'] = true;
$list['nosearch'] = true;
Expand All @@ -88,13 +127,26 @@ public function print_login() {
$keyword->type = 'text';
$keyword->name = 'wikimedia_keyword';
$keyword->value = '';
$maxwidth = array(
'label' => get_string('maxwidth', 'repository_wikimedia').': ',
'type' => 'text',
'name' => 'wikimedia_maxwidth',
'value' => get_user_preferences('repository_wikimedia_maxwidth', WIKIMEDIA_IMAGE_SIDE_LENGTH),
);
$maxheight = array(
'label' => get_string('maxheight', 'repository_wikimedia').': ',
'type' => 'text',
'name' => 'wikimedia_maxheight',
'value' => get_user_preferences('repository_wikimedia_maxheight', WIKIMEDIA_IMAGE_SIDE_LENGTH),
);
if ($this->options['ajax']) {
$form = array();
$form['login'] = array($keyword);
$form['login'] = array($keyword, (object)$maxwidth, (object)$maxheight);
$form['nologin'] = true;
$form['norefresh'] = true;
$form['nosearch'] = true;
$form['allowcaching'] = true; // indicates that login form can be cached in filepicker.js
$form['allowcaching'] = false; // indicates that login form can NOT
// be cached in filepicker.js (maxwidth and maxheight are dynamic)
return $form;
} else {
echo <<<EOD
Expand Down
30 changes: 20 additions & 10 deletions repository/wikimedia/wikimedia.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
define('WIKIMEDIA_THUMBS_PER_PAGE', 24);
define('WIKIMEDIA_FILE_NS', 6);
define('WIKIMEDIA_IMAGE_SIDE_LENGTH', 1024);
define('WIKIMEDIA_THUMB_SIZE', 120);

class wikimedia {
private $_conn = null;
Expand Down Expand Up @@ -135,13 +136,16 @@ public function get_thumb_url($image_url, $orig_width, $orig_height, $thumb_widt
return $thumb_url;
}
}

/**
* Search for images and return photos array.
*
* @param string $keyword
* @param int $page
* @param array $params additional query params
* @return array
*/
public function search_images($keyword, $page = 0) {
public function search_images($keyword, $page = 0, $params = array()) {
global $OUTPUT;
$files_array = array();
$this->_param['action'] = 'query';
Expand All @@ -152,8 +156,9 @@ public function search_images($keyword, $page = 0) {
$this->_param['gsroffset'] = $page * WIKIMEDIA_THUMBS_PER_PAGE;
$this->_param['prop'] = 'imageinfo';
$this->_param['iiprop'] = 'url|dimensions|mime|timestamp|size|user';
$this->_param['iiurlwidth'] = WIKIMEDIA_IMAGE_SIDE_LENGTH;
$this->_param['iiurlheight'] = WIKIMEDIA_IMAGE_SIDE_LENGTH;
$this->_param += $params;
$this->_param += array('iiurlwidth' => WIKIMEDIA_IMAGE_SIDE_LENGTH,
'iiurlheight' => WIKIMEDIA_IMAGE_SIDE_LENGTH);
//didn't work with POST
$content = $this->_conn->get($this->api, $this->_param);
$result = unserialize($content);
Expand All @@ -174,6 +179,12 @@ public function search_images($keyword, $page = 0) {
'image_width' => $page['imageinfo'][0]['thumbwidth'],
'image_height' => $page['imageinfo'][0]['thumbheight']
);
if ($attrs['image_width'] <= WIKIMEDIA_THUMB_SIZE && $attrs['image_height'] <= WIKIMEDIA_THUMB_SIZE) {
$attrs['realthumbnail'] = $attrs['source'];
}
if ($attrs['image_width'] <= 24 && $attrs['image_height'] <= 24) {
$attrs['realicon'] = $attrs['source'];
}
} else {
$attrs = array(
//upload full size image
Expand All @@ -184,20 +195,19 @@ public function search_images($keyword, $page = 0) {
);
}
$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),
'realthumbnail' => $this->get_thumb_url($page['imageinfo'][0]['url'], $page['imageinfo'][0]['width'], $page['imageinfo'][0]['height'], WIKIMEDIA_THUMB_SIZE),
'realicon' => $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']);
$attrs = array('source' => $page['imageinfo'][0]['url']);
}
$files_array[] = array(
'title'=>substr($title, 5), //chop off 'File:'
'thumbnail_width'=>120,
'thumbnail_height'=>120,
'thumbnail' => $OUTPUT->pix_url(file_extension_icon(substr($title, 5), WIKIMEDIA_THUMB_SIZE))->out(false),
'thumbnail_width' => WIKIMEDIA_THUMB_SIZE,
'thumbnail_height' => WIKIMEDIA_THUMB_SIZE,
'license' => 'cc-sa',
// the accessible url of the file
'url'=>$page['imageinfo'][0]['descriptionurl']
Expand Down

0 comments on commit 1d37910

Please sign in to comment.