~ubuntu-branches/ubuntu/maverick/mediawiki/maverick

« back to all changes in this revision

Viewing changes to includes/api/ApiFormatBase.php

  • Committer: Bazaar Package Importer
  • Author(s): Romain Beauxis
  • Date: 2009-06-19 01:38:50 UTC
  • mfrom: (16.1.2 sid)
  • Revision ID: james.westby@ubuntu.com-20090619013850-dsn4lrxvs90ab4rx
Tags: 1:1.15.0-1
* New upstream release. 
* Upstream added support for OASIS documents.
Closes: #530328
* Refreshed quilt patches
* Bumped standards versions to 3.8.2
* Bumped compat to 7
* Pointed to GPL-2 in debian/copyright
* Added php5-sqlite to possible DB backend dependencies.
Closes: #501569
* Proofread README.Debian, upgrade is documented there.
Closes: #520121

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
        private $mIsHtml, $mFormat, $mUnescapeAmps, $mHelp, $mCleared;
39
39
 
40
40
        /**
41
 
        * Create a new instance of the formatter.
42
 
        * If the format name ends with 'fm', wrap its output in the proper HTML.
43
 
        */
 
41
         * Constructor
 
42
         * If $format ends with 'fm', pretty-print the output in HTML.
 
43
         * @param $main ApiMain
 
44
         * @param $format string Format name
 
45
         */
44
46
        public function __construct($main, $format) {
45
47
                parent :: __construct($main, $format);
46
48
 
61
63
        public abstract function getMimeType();
62
64
 
63
65
        /**
64
 
         * If formatter outputs data results as is, the results must first be sanitized.
65
 
         * An XML formatter on the other hand uses special tags, such as "_element" for special handling,
66
 
         * and thus needs to override this function to return true.
 
66
         * Whether this formatter needs raw data such as _element tags
 
67
         * @return bool
67
68
         */
68
69
        public function getNeedsRawData() {
69
70
                return false;
71
72
 
72
73
        /**
73
74
         * Get the internal format name
 
75
         * @return string
74
76
         */
75
77
        public function getFormat() {
76
78
                return $this->mFormat;
77
79
        }
78
80
 
79
81
        /**
80
 
         * Specify whether or not ampersands should be escaped to '&' when rendering. This
81
 
         * should only be set to true for the help message when rendered in the default (xmlfm)
82
 
         * format. This is a temporary special-case fix that should be removed once the help
83
 
         * has been reworked to use a fully html interface.
 
82
         * Specify whether or not sequences like " should be unescaped
 
83
         * to " . This should only be set to true for the help message
 
84
         * when rendered in the default (xmlfm) format. This is a temporary
 
85
         * special-case fix that should be removed once the help has been
 
86
         * reworked to use a fully HTML interface.
84
87
         *
85
 
         * @param boolean Whether or not ampersands should be escaped.
 
88
         * @param $b bool Whether or not ampersands should be escaped.
86
89
         */
87
90
        public function setUnescapeAmps ( $b ) {
88
91
                $this->mUnescapeAmps = $b;
89
92
        }
90
93
 
91
94
        /**
92
 
         * Returns true when an HTML filtering printer should be used.
 
95
         * Returns true when the HTML pretty-printer should be used.
93
96
         * The default implementation assumes that formats ending with 'fm'
94
97
         * should be formatted in HTML.
 
98
         * @return bool
95
99
         */
96
100
        public function getIsHtml() {
97
101
                return $this->mIsHtml;
98
102
        }
99
103
 
100
104
        /**
101
 
         * Initialize the printer function and prepares the output headers, etc.
 
105
         * Initialize the printer function and prepare the output headers, etc.
102
106
         * This method must be the first outputing method during execution.
103
107
         * A help screen's header is printed for the HTML-based output
 
108
         * @param $isError bool Whether an error message is printed
104
109
         */
105
110
        function initPrinter($isError) {
106
111
                $isHtml = $this->getIsHtml();
167
172
        }
168
173
 
169
174
        /**
170
 
         * The main format printing function. Call it to output the result string to the user.
171
 
         * This function will automatically output HTML when format name ends in 'fm'.
 
175
         * The main format printing function. Call it to output the result
 
176
         * string to the user. This function will automatically output HTML
 
177
         * when format name ends in 'fm'.
 
178
         * @param $text string
172
179
         */
173
180
        public function printText($text) {
174
181
                if ($this->getIsHtml())
188
195
        }
189
196
 
190
197
        /**
191
 
        * Says pretty-printer that it should use *bold* and $italics$ formatting
192
 
        */
 
198
         * Sets whether the pretty-printer should format *bold* and $italics$
 
199
         * @param $help bool
 
200
         */
193
201
        public function setHelp( $help = true ) {
194
202
                $this->mHelp = true;
195
203
        }
196
204
 
197
205
        /**
198
 
        * Prety-print various elements in HTML format, such as xml tags and URLs.
199
 
        * This method also replaces any '<' with &lt;
 
206
        * Prety-print various elements in HTML format, such as xml tags and
 
207
        * URLs. This method also escapes characters like <
 
208
        * @param $text string
 
209
        * @return string
200
210
        */
201
211
        protected function formatHTML($text) {
202
212
                global $wgUrlProtocols;
209
219
                // identify URLs
210
220
                $protos = implode("|", $wgUrlProtocols);
211
221
                # This regex hacks around bug 13218 (&quot; included in the URL)
212
 
                $text = preg_replace("#(($protos).*?)(&quot;)?([ \\'\"()<\n])#", '<a href="\\1">\\1</a>\\3\\4', $text);
 
222
                $text = preg_replace("#(($protos).*?)(&quot;)?([ \\'\"<>\n]|&lt;|&gt;|&quot;)#", '<a href="\\1">\\1</a>\\3\\4', $text);
213
223
                // identify requests to api.php
214
224
                $text = preg_replace("#api\\.php\\?[^ \\()<\n\t]+#", '<a href="\\0">\\0</a>', $text);
215
225
                if( $this->mHelp ) {
216
226
                        // make strings inside * bold
217
 
                        $text = ereg_replace("\\*[^<>\n]+\\*", '<b>\\0</b>', $text);
 
227
                        $text = preg_replace("#\\*[^<>\n]+\\*#", '<b>\\0</b>', $text);
218
228
                        // make strings inside $ italic
219
 
                        $text = ereg_replace("\\$[^<>\n]+\\$", '<b><i>\\0</i></b>', $text);
 
229
                        $text = preg_replace("#\\$[^<>\n]+\\$#", '<b><i>\\0</i></b>', $text);
220
230
                }
221
231
 
222
232
                /* Temporary fix for bad links in help messages. As a special case,
229
239
                return $text;
230
240
        }
231
241
 
232
 
        /**
233
 
         * Returns usage examples for this format.
234
 
         */
235
242
        protected function getExamples() {
236
243
                return 'api.php?action=query&meta=siteinfo&siprop=namespaces&format=' . $this->getModuleName();
237
244
        }
241
248
        }
242
249
 
243
250
        public static function getBaseVersion() {
244
 
                return __CLASS__ . ': $Id: ApiFormatBase.php 43470 2008-11-14 00:30:34Z tstarling $';
 
251
                return __CLASS__ . ': $Id: ApiFormatBase.php 48521 2009-03-18 19:25:29Z ialex $';
245
252
        }
246
253
}
247
254
 
256
263
        }
257
264
 
258
265
        /**
259
 
         * Call this method to initialize output data. See self::execute()
 
266
         * Call this method to initialize output data. See execute()
 
267
         * @param $result ApiResult
 
268
         * @param $feed object an instance of one of the $wgFeedClasses classes
 
269
         * @param $feedItems array of FeedItem objects
260
270
         */
261
271
        public static function setResult($result, $feed, $feedItems) {
262
272
                // Store output in the Result data.
263
273
                // This way we can check during execution if any error has occured
264
 
                $data = & $result->getData();
265
 
                $data['_feed'] = $feed;
266
 
                $data['_feeditems'] = $feedItems;
 
274
                // Disable size checking for this because we can't continue
 
275
                // cleanly; size checking would cause more problems than it'd
 
276
                // solve
 
277
                $result->disableSizeCheck();
 
278
                $result->addValue(null, '_feed', $feed);
 
279
                $result->addValue(null, '_feeditems', $feedItems);
 
280
                $result->enableSizeCheck();
267
281
        }
268
282
 
269
283
        /**
282
296
 
283
297
        /**
284
298
         * This class expects the result data to be in a custom format set by self::setResult()
285
 
         * $result['_feed']              - an instance of one of the $wgFeedClasses classes
286
 
         * $result['_feeditems'] - an array of FeedItem instances
 
299
         * $result['_feed']             - an instance of one of the $wgFeedClasses classes
 
300
         * $result['_feeditems']        - an array of FeedItem instances
287
301
         */
288
302
        public function execute() {
289
303
                $data = $this->getResultData();
302
316
        }
303
317
 
304
318
        public function getVersion() {
305
 
                return __CLASS__ . ': $Id: ApiFormatBase.php 43470 2008-11-14 00:30:34Z tstarling $';
 
319
                return __CLASS__ . ': $Id: ApiFormatBase.php 48521 2009-03-18 19:25:29Z ialex $';
306
320
        }
307
 
}
 
321
}
 
 
b'\\ No newline at end of file'