Index: trunk/phase3/includes/api/ApiInstantCommons.php |
— | — | @@ -0,0 +1,191 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * @author suuch (mediawiki @ suuch . com) |
| 5 | + * In the public domain. At least in Ghana. |
| 6 | + */ |
| 7 | + |
| 8 | + |
| 9 | +if (!defined('MEDIAWIKI')) { |
| 10 | + // Eclipse helper - will be ignored in production |
| 11 | + require_once ('ApiBase.php'); |
| 12 | +} |
| 13 | + |
| 14 | +class ApiInstantCommons extends ApiBase { |
| 15 | + var $arrOutput = array(); |
| 16 | + var $resParser; |
| 17 | + var $strXmlData; |
| 18 | + |
| 19 | + public function __construct($main, $action) { |
| 20 | + parent :: __construct($main, $action); |
| 21 | + } |
| 22 | + |
| 23 | + /** |
| 24 | + * InstantCommons execution happens in the following steps: |
| 25 | + */ |
| 26 | + public function execute() { |
| 27 | + $media = $maint = $meta = null; |
| 28 | + extract($this->extractRequestParams()); |
| 29 | + $data = array(); |
| 30 | + $data = $this->fetchImage($media); |
| 31 | + if($data!=NULL){ |
| 32 | + $this->getResult()->addValue('instantcommons', 'image', $data); |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + |
| 37 | + /** |
| 38 | + * Override the parent to generate help messages for all available query modules. |
| 39 | + */ |
| 40 | + public function makeHelpMsg() { |
| 41 | + |
| 42 | + // Use parent to make default message for the query module |
| 43 | + $msg = parent :: makeHelpMsg(); |
| 44 | + |
| 45 | + // Make sure the internal object is empty |
| 46 | + // (just in case a sub-module decides to optimize during instantiation) |
| 47 | + $this->mPageSet = null; |
| 48 | + |
| 49 | + $astriks = str_repeat('--- ', 8); |
| 50 | + $msg .= "\n$astriks InstantCommons: Prop $astriks\n\n"; |
| 51 | + $msg .= "\n See http://meta.wikimedia.org/wiki/InstantCommons\n\n"; |
| 52 | + return $msg; |
| 53 | + } |
| 54 | + |
| 55 | + private function makeHelpMsgHelper($moduleList, $paramName) { |
| 56 | + |
| 57 | + $moduleDscriptions = array (); |
| 58 | + |
| 59 | + foreach ($moduleList as $moduleName => $moduleClass) { |
| 60 | + $msg = "* $paramName=$moduleName *"; |
| 61 | + $module = new $moduleClass ($this, $moduleName, null); |
| 62 | + $msg2 = $module->makeHelpMsg(); |
| 63 | + if ($msg2 !== false) |
| 64 | + $msg .= $msg2; |
| 65 | + if ($module instanceof ApiInstantCommonsGeneratorBase) |
| 66 | + $msg .= "Generator:\n This module may be used as a generator\n"; |
| 67 | + $moduleDscriptions[] = $msg; |
| 68 | + } |
| 69 | + |
| 70 | + return implode("\n", $moduleDscriptions); |
| 71 | + } |
| 72 | + |
| 73 | + protected function getAllowedParams() { |
| 74 | + return array ( |
| 75 | + 'media' => null, |
| 76 | + 'maint' => null, |
| 77 | + 'meta' => null, |
| 78 | + ); |
| 79 | + } |
| 80 | + protected function getParamDescription() { |
| 81 | + return array ( |
| 82 | + 'media' => 'Get properties for the media', |
| 83 | + 'maint' => 'Which maintenance actions to perform', |
| 84 | + 'meta' => 'Which meta data to get about this site', |
| 85 | + ); |
| 86 | + } |
| 87 | + |
| 88 | + protected function getDescription() { |
| 89 | + return array ( |
| 90 | + 'InstantCommons API InstantCommons is an API feature of MediaWiki to ' . |
| 91 | + 'allow the usage of any uploaded media file from the Wikimedia Commons ' . |
| 92 | + 'in any MediaWiki installation world-wide. InstantCommons-enabled wikis ' . |
| 93 | + 'cache Commons content so that it is only downloaded once, and subsequent ' . |
| 94 | + 'pageviews load the locally existing copy.' |
| 95 | + ); |
| 96 | + } |
| 97 | + |
| 98 | + protected function getExamples() { |
| 99 | + return array ( |
| 100 | + 'api.php?action=instantcommons&media=Image:MusekeBannerl.jpg', |
| 101 | + 'api.php?action=instantcommons&media=Image:MusekeBannerl.jpg&maint=update', //performs update on this media |
| 102 | + 'api.php?action=instantcommons&media=Image:MusekeBannerl.jpg&maint=delete', //performs delete on this media |
| 103 | + 'api.php?action=instantcommons&maint=update', //TODO: performs update on all commons media |
| 104 | + 'api.php?action=instantcommons&maint=delete', //TODO: performs delete on all commons imedia |
| 105 | + 'api.php?action=instantcommons&maint=both', //TODO: performs update/delete on all commons media |
| 106 | + 'api.php?action=instantcommons&maint=pending', //TODO: return a GD temp image |
| 107 | + ); |
| 108 | + } |
| 109 | + |
| 110 | + public function getVersion() { |
| 111 | + $psModule = new ApiPageSet($this); |
| 112 | + $vers = array (); |
| 113 | + $vers[] = __CLASS__ . ': $Id: ApiInstantCommons.php 17074 2006-10-27 05:27:43Z suuch $'; |
| 114 | + $vers[] = $psModule->getVersion(); |
| 115 | + return $vers; |
| 116 | + } |
| 117 | + |
| 118 | + /** |
| 119 | + * Fetch the media from the commons server in the background. |
| 120 | + * Save it as a local media (but noting its source in the appropriate media table) |
| 121 | + * @fileName is a fully qualified mediawiki object name (e.g. Image:sing.png) |
| 122 | + * @return an associative array containing file properties in property=>value pairs |
| 123 | + */ |
| 124 | + public function fetchImage($fileName){ |
| 125 | + global $wgScriptPath; |
| 126 | + $nt = Title::newFromText( $fileName ); |
| 127 | + if(is_object($nt)){ |
| 128 | + $image = new Image ($nt); |
| 129 | + if($image->exists()){ |
| 130 | + $image->url = substr(strstr($image->repo->url, $wgScriptPath), strlen($wgScriptPath)).'/'.$image->repo->getHashPath($image->name).$image->name; |
| 131 | + $image->metadata = addslashes($image->metadata); |
| 132 | + $ari=(array)$image; |
| 133 | + //unset non-string elements |
| 134 | + foreach($ari as $property=>$value){ |
| 135 | + if(is_object($value)){ |
| 136 | + unset($ari[$property]); |
| 137 | + } |
| 138 | + } |
| 139 | + return $ari; |
| 140 | + }else{ |
| 141 | + return array('error'=>1, 'description'=>'File not found'); //file not found |
| 142 | + } |
| 143 | + } |
| 144 | + else |
| 145 | + { |
| 146 | + return array('error'=>2, 'description'=>'Not a valid title'); //not a valid title |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + function parse($strInputXML) { |
| 153 | + $this->resParser = xml_parser_create (); |
| 154 | + xml_set_object($this->resParser,$this); |
| 155 | + xml_set_element_handler($this->resParser, "tagOpen", "tagClosed"); |
| 156 | + |
| 157 | + xml_set_character_data_handler($this->resParser, "tagData"); |
| 158 | + |
| 159 | + $this->strXmlData = xml_parse($this->resParser,$strInputXML ); |
| 160 | + if(!$this->strXmlData) { |
| 161 | + die(sprintf("XML error: %s at line %d", |
| 162 | + xml_error_string(xml_get_error_code($this->resParser)), |
| 163 | + xml_get_current_line_number($this->resParser))); |
| 164 | + } |
| 165 | + |
| 166 | + xml_parser_free($this->resParser); |
| 167 | + |
| 168 | + return $this->arrOutput; |
| 169 | + } |
| 170 | + function tagOpen($parser, $name, $attrs) { |
| 171 | + $tag=array("name"=>$name,"attrs"=>$attrs); |
| 172 | + array_push($this->arrOutput,$tag); |
| 173 | + } |
| 174 | + |
| 175 | + function tagData($parser, $tagData) { |
| 176 | + if(trim($tagData)) { |
| 177 | + if(isset($this->arrOutput[count($this->arrOutput)-1]['tagData'])) { |
| 178 | + $this->arrOutput[count($this->arrOutput)-1]['tagData'] .= $tagData; |
| 179 | + } |
| 180 | + else { |
| 181 | + $this->arrOutput[count($this->arrOutput)-1]['tagData'] = $tagData; |
| 182 | + } |
| 183 | + } |
| 184 | + } |
| 185 | + |
| 186 | + function tagClosed($parser, $name) { |
| 187 | + $this->arrOutput[count($this->arrOutput)-2]['children'][] = $this->arrOutput[count($this->arrOutput)-1]; |
| 188 | + array_pop($this->arrOutput); |
| 189 | + } |
| 190 | + |
| 191 | +} |
| 192 | +?> |
Property changes on: trunk/phase3/includes/api/ApiInstantCommons.php |
___________________________________________________________________ |
Added: svn:executable |
1 | 193 | + * |