r80365 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r80364‎ | r80365 | r80366 >
Date:00:22, 15 January 2011
Author:husky
Status:ok
Tags:
Comment:
it works
Modified paths:
  • /trunk/tools/wp-photocommons/inc/class-http-request.php (added) (history)
  • /trunk/tools/wp-photocommons/inc/class-photocommons.php (modified) (history)
  • /trunk/tools/wp-photocommons/index.php (modified) (history)

Diff [purge]

Index: trunk/tools/wp-photocommons/inc/class-http-request.php
@@ -0,0 +1,180 @@
 2+<?php
 3+/**
 4+ * HttpRequest - A simple PHP class using cURL for Ajax-like HTTP requests
 5+ * by Hay Kranen <hay at bykr dot org>
 6+ * Released under the MIT / X11 license
 7+ * version 1.0
 8+ *
 9+ * Use:
 10+ * $r = new HttpRequest($method, $url, $data, $args)
 11+ * $method: "get" / "post"
 12+ * $url: duh :)
 13+ * $data: an array of values you want to send as query parameters or POST values
 14+ * $args: array of possible extra arguments for the request
 15+ *
 16+ * Then, check for errors with $r->getError() and response with $r->getResponse()
 17+ * eg with the public tweets from twitter
 18+ * $r = new HttpRequest("get", "http://twitter.com/statuses/public_timeline.json");
 19+ * if ($r->getError()) {
 20+ * echo "sorry, an error occured";
 21+ * } else {
 22+ * // parse json and show tweets
 23+ * $tweets = json_decode($r->getResponse());
 24+ * var_dump($tweets);
 25+ * }
 26+ *
 27+ * == License (MIT/X11) ==
 28+ *
 29+ * Permission is hereby granted, free of charge, to any person
 30+ * obtaining a copy of this software and associated documentation
 31+ * files (the "Software"), to deal in the Software without
 32+ * restriction, including without limitation the rights to use,
 33+ * copy, modify, merge, publish, distribute, sublicense, and/or sell
 34+ * copies of the Software, and to permit persons to whom the
 35+ * Software is furnished to do so, subject to the following
 36+ * conditions:
 37+ *
 38+ * The above copyright notice and this permission notice shall be
 39+ * included in all copies or substantial portions of the Software.
 40+ *
 41+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 42+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 43+ * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 44+ * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 45+ * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 46+ * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 47+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 48+ * OTHER DEALINGS IN THE SOFTWARE.
 49+ */
 50+class HttpRequest {
 51+ private $method, $url, $data = false;
 52+ private $error, $hasError = false, $response, $status;
 53+ private $requestInfo, $curlError, $headers = array();
 54+
 55+ // Default arguments
 56+ private $args = array(
 57+ "followRedirect" => true,
 58+ );
 59+
 60+ function __construct($method, $url, $data = false, $args = false) {
 61+ $method = strtolower($method);
 62+ if ($method == "post" || $method == "get") {
 63+ $this->method = $method;
 64+ } else {
 65+ $this->setError("Invalid method: $method");
 66+ return;
 67+ }
 68+
 69+ $this->url = $url;
 70+ $this->data = $data;
 71+
 72+ if (is_array($args)) {
 73+ // Add arguments to the already available default arguments
 74+ foreach($args as $key => $value) {
 75+ $this->args[$key] = $value;
 76+ }
 77+ }
 78+
 79+ $this->doRequest();
 80+ }
 81+
 82+ function hasError() {
 83+ return $this->hasError;
 84+ }
 85+
 86+ private function setError($msg) {
 87+ $this->error = $msg;
 88+ $this->hasError = true;
 89+ }
 90+
 91+ function getError() {
 92+ return $this->error;
 93+ }
 94+
 95+ function getStatus() {
 96+ return $this->status;
 97+ }
 98+
 99+ function getResponse() {
 100+ return $this->response;
 101+ }
 102+
 103+ function getRequestInfo() {
 104+ return $this->requestInfo;
 105+ }
 106+
 107+ function toString() {
 108+ var_dump($this);
 109+ }
 110+
 111+ private function doRequest() {
 112+ $this->doCurl();
 113+
 114+ if ($this->status != "200") {
 115+ $this->setError("Response error: " . $this->status . " (" . $this->curlError . ")");
 116+ }
 117+ }
 118+
 119+ private function doCurl() {
 120+ $c = curl_init();
 121+
 122+ // Maybe we want to rewrite the url for data arguments in GET requests
 123+ if ($this->method == "get" && $this->data) {
 124+ $this->url .= "?" . http_build_query($this->data);
 125+ }
 126+
 127+ // Default values
 128+ curl_setopt($c, CURLOPT_URL, $this->url);
 129+ curl_setopt($c, CURLOPT_RETURNTRANSFER ,true);
 130+ curl_setopt($c, CURLOPT_FOLLOWLOCATION, $this->args['followRedirect']);
 131+ curl_setopt($c, CURLOPT_HTTPHEADER, array('Expect:'));
 132+
 133+ //register a callback function which will process the headers
 134+ //this assumes your code is into a class method, and uses $this->readHeader as the callback //function
 135+ curl_setopt($c, CURLOPT_HEADERFUNCTION, array(&$this,'readHeader'));
 136+
 137+ // Authentication
 138+ if (isset($this->args['username']) && isset($this->args['password'])) {
 139+ curl_setopt($c, CURLOPT_USERPWD, $this->args['username'] . ':' . $this->args['password']);
 140+ }
 141+
 142+ // POST
 143+ if($this->method == "post") {
 144+ curl_setopt($c, CURLOPT_POST, true);
 145+ // Always escape HTTP data dammit!
 146+ curl_setopt($c, CURLOPT_POSTFIELDS, http_build_query($this->data));
 147+ }
 148+
 149+ // Many servers require this to output decent HTML
 150+ if (empty($this->args['useragent'])) {
 151+ curl_setopt($c, CURLOPT_USERAGENT, "Mozilla/5.0");
 152+ } else {
 153+ curl_setopt($c, CURLOPT_USERAGENT, $this->args['useragent']);
 154+ }
 155+
 156+ $this->response = curl_exec($c);
 157+ $this->status = curl_getinfo($c, CURLINFO_HTTP_CODE);
 158+ $this->curlError = curl_errno($c) . ": ". curl_error($c);
 159+ $this->requestInfo = curl_getinfo($c);
 160+ $this->headers = array_merge($this->requestInfo, $this->headers);
 161+ curl_close($c);
 162+ }
 163+
 164+ private function readHeader($ch, $header) {
 165+ $key = trim(substr($header, 0, strpos($header, ":")));
 166+ $val = trim(substr($header, strpos($header, ":") + 1));
 167+ if (!empty($key) && !empty($val)) {
 168+ $this->headers[$key] = $val;
 169+ }
 170+ return strlen($header);
 171+ }
 172+
 173+ function getHeaders($key = false) {
 174+ if ($key) {
 175+ return $this->headers[$key];
 176+ } else {
 177+ return $this->headers;
 178+ }
 179+ }
 180+}
 181+?>
\ No newline at end of file
Property changes on: trunk/tools/wp-photocommons/inc/class-http-request.php
___________________________________________________________________
Added: svn:eol-style
1182 + native
Index: trunk/tools/wp-photocommons/inc/class-photocommons.php
@@ -2,6 +2,7 @@
33 class Photocommons {
44 // TODO: ugly
55 const PLUGIN_PATH = "/wp-content/plugins/wp-photocommons/";
 6+ const RESIZE_URL = "http://commons.wikimedia.org/w/api.php?action=query&titles=%s&prop=imageinfo&iiprop=url&iiurlwidth=%s&format=php";
67
78 function __construct() {
89 if (is_admin()) {
@@ -12,9 +13,40 @@
1314 }
1415
1516 public function addShortcode($args) {
16 - return print_r($args, false);
 17+ $filename = $args['file'];
 18+ $size = $args['size'];
 19+ $d = $this->doApiThumbResizeRequest($filename, $size);
 20+
 21+ return sprintf(
 22+ '<a href="%s" title="%s">' .
 23+ '<img src="%s" title="%s" alt="%s" width="%s" height="%s" />' .
 24+ '</a>',
 25+ $d['url'], $d['title'], $d['src'], $d['title'], $d['title'],
 26+ $d['width'], $d['height']
 27+ );
1728 }
1829
 30+ private function doApiThumbResizeRequest($filename, $size) {
 31+ ini_set('user_agent', 'Photocommons/1.0');
 32+ $url = $this->getResizeUrl($filename, $size);
 33+ $result = unserialize(file_get_contents($url));
 34+ $data = array_pop($result['query']['pages']);
 35+
 36+ $image = $data['imageinfo'][0];
 37+
 38+ return array(
 39+ "url" => $image['descriptionurl'],
 40+ "src" => $image['thumburl'],
 41+ "width" => $image['thumbwidth'],
 42+ "height" => $image['thumbheight'],
 43+ "title" => $data['title']
 44+ );
 45+ }
 46+
 47+ private function getResizeUrl($file, $size) {
 48+ return sprintf(self::RESIZE_URL, rawurlencode($file), rawurlencode($size));
 49+ }
 50+
1951 private function initAdmin() {
2052 wp_enqueue_script('jquery');
2153 wp_enqueue_script('jquery-ui-core');
@@ -40,6 +72,6 @@
4173 }
4274
4375 private function initFrontend() {
44 - add_shortcode("shortcode", array($this, "addShortcode"));
 76+ add_shortcode("photocommons", array($this, "addShortcode"));
4577 }
4678 }
\ No newline at end of file
Index: trunk/tools/wp-photocommons/index.php
@@ -12,5 +12,6 @@
1313 Author URI: http://
1414 */
1515
 16+require 'inc/class-http-request.php';
1617 require 'inc/class-photocommons.php';
1718 new PhotoCommons();
\ No newline at end of file

Status & tagging log