Joomla Platform  13.1
Documentation des API du framework Joomla Platform
 Tout Classes Espaces de nommage Fichiers Fonctions Variables Pages
opensearch.php
Aller à la documentation de ce fichier.
1 <?php
2 /**
3  * @package Joomla.Platform
4  * @subpackage Document
5  *
6  * @copyright Copyright (C) 2005 - 2013 Open Source Matters, Inc. All rights reserved.
7  * @license GNU General Public License version 2 or later; see LICENSE.txt
8  */
9 
10 defined('JPATH_PLATFORM') or die;
11 
12 /**
13  * OpenSearch class, provides an easy interface to display an OpenSearch document
14  *
15  * @package Joomla.Platform
16  * @subpackage Document
17  * @see http://www.opensearch.org/
18  * @since 11.1
19  */
21 {
22  /**
23  * ShortName element
24  *
25  * required
26  *
27  * @var string
28  * @since 11.1
29  */
30  private $_shortName = "";
31 
32  /**
33  * Images collection
34  *
35  * optional
36  *
37  * @var object
38  * @since 11.1
39  */
40  private $_images = array();
41 
42  /**
43  * The url collection
44  *
45  * @var array
46  * @since 11.1
47  */
48  private $_urls = array();
49 
50  /**
51  * Class constructor
52  *
53  * @param array $options Associative array of options
54  *
55  * @since 11.1
56  */
57  public function __construct($options = array())
58  {
59  parent::__construct($options);
60 
61  // Set document type
62  $this->_type = 'opensearch';
63 
64  // Set mime type
65  $this->_mime = 'application/opensearchdescription+xml';
66 
67  // Add the URL for self updating
68  $update = new JOpenSearchUrl;
69  $update->type = 'application/opensearchdescription+xml';
70  $update->rel = 'self';
71  $update->template = JRoute::_(JUri::getInstance());
72  $this->addUrl($update);
73 
74  // Add the favicon as the default image
75  // Try to find a favicon by checking the template and root folder
76  $app = JFactory::getApplication();
77  $dirs = array(JPATH_THEMES . '/' . $app->getTemplate(), JPATH_BASE);
78 
79  foreach ($dirs as $dir)
80  {
81  if (file_exists($dir . '/favicon.ico'))
82  {
83 
84  $path = str_replace(JPATH_BASE . '/', '', $dir);
85  $path = str_replace('\\', '/', $path);
86 
87  $favicon = new JOpenSearchImage;
88  $favicon->data = JUri::base() . $path . '/favicon.ico';
89  $favicon->height = '16';
90  $favicon->width = '16';
91  $favicon->type = 'image/vnd.microsoft.icon';
92 
93  $this->addImage($favicon);
94 
95  break;
96  }
97  }
98  }
99 
100  /**
101  * Render the document
102  *
103  * @param boolean $cache If true, cache the output
104  * @param array $params Associative array of attributes
105  *
106  * @return The rendered data
107  *
108  * @since 11.1
109  */
110  public function render($cache = false, $params = array())
111  {
112  $xml = new DOMDocument('1.0', 'utf-8');
113  if (defined('JDEBUG') && JDEBUG)
114  {
115  $xml->formatOutput = true;
116  }
117 
118  // The OpenSearch Namespace
119  $osns = 'http://a9.com/-/spec/opensearch/1.1/';
120 
121  // Create the root element
122  $elOs = $xml->createElementNS($osns, 'OpenSearchDescription');
123 
124  $elShortName = $xml->createElementNS($osns, 'ShortName');
125  $elShortName->appendChild($xml->createTextNode(htmlspecialchars($this->_shortName)));
126  $elOs->appendChild($elShortName);
127 
128  $elDescription = $xml->createElementNS($osns, 'Description');
129  $elDescription->appendChild($xml->createTextNode(htmlspecialchars($this->description)));
130  $elOs->appendChild($elDescription);
131 
132  // Always set the accepted input encoding to UTF-8
133  $elInputEncoding = $xml->createElementNS($osns, 'InputEncoding');
134  $elInputEncoding->appendChild($xml->createTextNode('UTF-8'));
135  $elOs->appendChild($elInputEncoding);
136 
137  foreach ($this->_images as $image)
138  {
139  $elImage = $xml->createElementNS($osns, 'Image');
140  $elImage->setAttribute('type', $image->type);
141  $elImage->setAttribute('width', $image->width);
142  $elImage->setAttribute('height', $image->height);
143  $elImage->appendChild($xml->createTextNode(htmlspecialchars($image->data)));
144  $elOs->appendChild($elImage);
145  }
146 
147  foreach ($this->_urls as $url)
148  {
149  $elUrl = $xml->createElementNS($osns, 'Url');
150  $elUrl->setAttribute('type', $url->type);
151 
152  // Results is the default value so we don't need to add it
153  if ($url->rel != 'results')
154  {
155  $elUrl->setAttribute('rel', $url->rel);
156  }
157  $elUrl->setAttribute('template', $url->template);
158  $elOs->appendChild($elUrl);
159  }
160 
161  $xml->appendChild($elOs);
162  parent::render();
163  return $xml->saveXML();
164  }
165 
166  /**
167  * Sets the short name
168  *
169  * @param string $name The name.
170  *
171  * @return JDocumentOpensearch instance of $this to allow chaining
172  *
173  * @since 11.1
174  */
175  public function setShortName($name)
176  {
177  $this->_shortName = $name;
178 
179  return $this;
180  }
181 
182  /**
183  * Adds an URL to the OpenSearch description.
184  *
185  * @param JOpenSearchUrl $url The url to add to the description.
186  *
187  * @return JDocumentOpensearch instance of $this to allow chaining
188  *
189  * @since 11.1
190  */
191  public function addUrl(JOpenSearchUrl $url)
192  {
193  $this->_urls[] = $url;
194 
195  return $this;
196  }
197 
198  /**
199  * Adds an image to the OpenSearch description.
200  *
201  * @param JOpenSearchImage $image The image to add to the description.
202  *
203  * @return JDocumentOpensearch instance of $this to allow chaining
204  *
205  * @since 11.1
206  */
207  public function addImage(JOpenSearchImage $image)
208  {
209  $this->_images[] = $image;
210 
211  return $this;
212  }
213 }
214 
215 /**
216  * JOpenSearchUrl is an internal class that stores the search URLs for the OpenSearch description
217  *
218  * @package Joomla.Platform
219  * @subpackage Document
220  * @since 11.1
221  */
223 {
224  /**
225  * Type item element
226  *
227  * required
228  *
229  * @var string
230  * @since 11.1
231  */
232  public $type = 'text/html';
233 
234  /**
235  * Rel item element
236  *
237  * required
238  *
239  * @var string
240  * @since 11.1
241  */
242  public $rel = 'results';
243 
244  /**
245  * Template item element. Has to contain the {searchTerms} parameter to work.
246  *
247  * required
248  *
249  * @var string
250  * @since 11.1
251  */
252  public $template;
253 }
254 
255 /**
256  * JOpenSearchImage is an internal class that stores Images for the OpenSearch Description
257  *
258  * @package Joomla.Platform
259  * @subpackage Document
260  * @since 11.1
261  */
263 {
264  /**
265  * The images MIME type
266  *
267  * required
268  *
269  * @var string
270  * @since 11.1
271  */
272  public $type = "";
273 
274  /**
275  * URL of the image or the image as base64 encoded value
276  *
277  * required
278  *
279  * @var string
280  * @since 11.1
281  */
282  public $data = "";
283 
284  /**
285  * The image's width
286  *
287  * required
288  *
289  * @var string
290  * @since 11.1
291  */
292  public $width;
293 
294  /**
295  * The image's height
296  *
297  * required
298  *
299  * @var string
300  * @since 11.1
301  */
302  public $height;
303 }