Index: trunk/extensions/OpenSearchXml/ApiOpenSearchXml.php |
— | — | @@ -41,7 +41,7 @@ |
42 | 42 | } |
43 | 43 | return $printer; |
44 | 44 | } |
45 | | - |
| 45 | + |
46 | 46 | protected function validateFormat() { |
47 | 47 | $params = $this->extractRequestParams(); |
48 | 48 | $format = $params['format']; |
— | — | @@ -63,19 +63,19 @@ |
64 | 64 | // Pass back to the JSON defaults |
65 | 65 | return parent::execute(); |
66 | 66 | } |
67 | | - |
| 67 | + |
68 | 68 | $params = $this->extractRequestParams(); |
69 | 69 | $search = $params['search']; |
70 | 70 | $limit = $params['limit']; |
71 | 71 | $namespaces = $params['namespace']; |
72 | | - |
| 72 | + |
73 | 73 | // Open search results may be stored for a very long time |
74 | 74 | $this->getMain()->setCacheMaxAge(1200); |
75 | 75 | |
76 | 76 | $srchres = PrefixSearch::titleSearch( $search, $limit, $namespaces ); |
77 | 77 | |
78 | 78 | $items = array_filter( array_map( array( $this, 'formatItem' ), $srchres ) ); |
79 | | - |
| 79 | + |
80 | 80 | $result = $this->getResult(); |
81 | 81 | $result->addValue( null, 'version', '2.0' ); |
82 | 82 | $result->addValue( null, 'xmlns', 'http://opensearch.org/searchsuggest2' ); |
— | — | @@ -83,13 +83,13 @@ |
84 | 84 | $result->setIndexedTagName( $items, 'Item' ); |
85 | 85 | $result->addValue( null, 'Section', $items ); |
86 | 86 | } |
87 | | - |
| 87 | + |
88 | 88 | public function getAllowedParams() { |
89 | 89 | $params = parent::getAllowedParams(); |
90 | 90 | $params['format'] = null; |
91 | 91 | return $params; |
92 | 92 | } |
93 | | - |
| 93 | + |
94 | 94 | protected function formatItem( $name ) { |
95 | 95 | $title = Title::newFromText( $name ); |
96 | 96 | if( $title ) { |
— | — | @@ -97,7 +97,7 @@ |
98 | 98 | if( $this->_seen( $title ) ) { |
99 | 99 | return false; |
100 | 100 | } |
101 | | - |
| 101 | + |
102 | 102 | list( $extract, $badge ) = $this->getExtract( $title ); |
103 | 103 | $image = $this->getBadge( $title, $badge ); |
104 | 104 | |
— | — | @@ -118,7 +118,7 @@ |
119 | 119 | } |
120 | 120 | return $item; |
121 | 121 | } |
122 | | - |
| 122 | + |
123 | 123 | protected function _checkRedirect( $title ) { |
124 | 124 | $art = new Article( $title ); |
125 | 125 | $target = $art->getRedirectTarget(); |
— | — | @@ -150,12 +150,12 @@ |
151 | 151 | */ |
152 | 152 | function _stripMarkup( $text ) { |
153 | 153 | $text = substr( $text, 0, 4096 ); // don't bother with long text... |
154 | | - |
| 154 | + |
155 | 155 | $text = str_replace( "'''", "", $text ); |
156 | 156 | $text = str_replace( "''", "", $text ); |
157 | | - |
| 157 | + |
158 | 158 | $text = preg_replace( '#__[a-z0-9_]+__#i', '', $text ); // magic words |
159 | | - |
| 159 | + |
160 | 160 | $cleanChar = "[^|\[\]]"; |
161 | 161 | $subLink = "\[\[$cleanChar*(?:\|$cleanChar*)*\]\]"; |
162 | 162 | $pipeContents = "(?:$cleanChar|$subLink)*"; |
— | — | @@ -175,7 +175,7 @@ |
176 | 176 | $text = Sanitizer::decodeCharReferences( $text ); |
177 | 177 | return trim( $text ); |
178 | 178 | } |
179 | | - |
| 179 | + |
180 | 180 | function _stripLink( $matches ) { |
181 | 181 | $target = trim( $matches[1] ); |
182 | 182 | if( isset( $matches[2] ) ) { |
— | — | @@ -183,7 +183,7 @@ |
184 | 184 | } else { |
185 | 185 | $text = $target; |
186 | 186 | } |
187 | | - |
| 187 | + |
188 | 188 | $title = Title::newFromText( $target ); |
189 | 189 | if( $title ) { |
190 | 190 | $ns = $title->getNamespace(); |
— | — | @@ -196,7 +196,7 @@ |
197 | 197 | return $matches[0]; |
198 | 198 | } |
199 | 199 | } |
200 | | - |
| 200 | + |
201 | 201 | /** |
202 | 202 | * Extract the first two sentences, if detectable, from the text. |
203 | 203 | * @param string $text |
— | — | @@ -210,7 +210,7 @@ |
211 | 211 | '.', '!', '?', // double-width roman forms |
212 | 212 | '。', // half-width ideographic full stop |
213 | 213 | ); |
214 | | - |
| 214 | + |
215 | 215 | $endgroup = implode( '|', $endchars ); |
216 | 216 | $end = "(?:$endgroup)"; |
217 | 217 | $sentence = ".*?$end+"; |
— | — | @@ -224,7 +224,7 @@ |
225 | 225 | return trim( $lines[0] ); |
226 | 226 | } |
227 | 227 | } |
228 | | - |
| 228 | + |
229 | 229 | /** |
230 | 230 | * Grab the first thing that looks like an image link from the body text. |
231 | 231 | * This will exclude any templates, including infoboxes... |
— | — | @@ -239,13 +239,13 @@ |
240 | 240 | return false; |
241 | 241 | } |
242 | 242 | } |
243 | | - |
| 243 | + |
244 | 244 | function _validateBadge( $arg ) { |
245 | 245 | // Some templates want an entire [[Image:Foo.jpg|250px]] |
246 | 246 | if( substr( $arg, 0, 2 ) == '[[' ) { |
247 | 247 | return $this->_extractBadge( $arg ); |
248 | 248 | } |
249 | | - |
| 249 | + |
250 | 250 | // Others will take Image:Foo.jpg or Foo.jpg |
251 | 251 | $title = Title::newFromText( $arg, NS_IMAGE ); |
252 | 252 | if( $title && $title->getNamespace() == NS_IMAGE ) { |
— | — | @@ -253,12 +253,12 @@ |
254 | 254 | } |
255 | 255 | return false; |
256 | 256 | } |
257 | | - |
| 257 | + |
258 | 258 | protected function getExtract( $title, $chars=50 ) { |
259 | 259 | $rev = Revision::newFromTitle( $title ); |
260 | 260 | if( $rev ) { |
261 | 261 | $text = substr( $rev->getText(), 0, 16384 ); |
262 | | - |
| 262 | + |
263 | 263 | // Ok, first note this is a TERRIBLE HACK. :D |
264 | 264 | // |
265 | 265 | // First, we use the system preprocessor to break down the text |
— | — | @@ -268,14 +268,14 @@ |
269 | 269 | $wgParser->clearState(); |
270 | 270 | $frame = $wgParser->getPreprocessor()->newFrame(); |
271 | 271 | $dom = $wgParser->preprocessToDom( $text ); |
272 | | - |
| 272 | + |
273 | 273 | $imageArgs = array( |
274 | 274 | 'image', |
275 | 275 | 'image_skyline', |
276 | 276 | 'img', |
277 | 277 | 'Img', |
278 | 278 | ); |
279 | | - |
| 279 | + |
280 | 280 | // Now, we strip out everything that's not text. |
281 | 281 | // This works with both DOM and Hash parsers, but feels fragile. |
282 | 282 | $node = $dom->getFirstChild(); |
— | — | @@ -306,7 +306,7 @@ |
307 | 307 | } |
308 | 308 | $node = $node->getNextSibling(); |
309 | 309 | } |
310 | | - |
| 310 | + |
311 | 311 | if( !$badge ) { |
312 | 312 | // Look for the first image in the body text if there wasn't |
313 | 313 | // one in an infobox. |
— | — | @@ -317,7 +317,7 @@ |
318 | 318 | // We'll use our shitty hand parser to strip most of those from |
319 | 319 | // the beginning of the text. |
320 | 320 | $stripped = $this->_stripMarkup( $out ); |
321 | | - |
| 321 | + |
322 | 322 | // And now, we'll grab just the first sentence as text, and |
323 | 323 | // also try to rip out a badge image. |
324 | 324 | return array( |