Index: trunk/extensions/NaturalLanguageList/NaturalLanguageList.php |
— | — | @@ -54,6 +54,10 @@ |
55 | 55 | |
56 | 56 | $wgParserTestFiles[] = dirname( __FILE__ ) . "/nllParserTests.txt"; |
57 | 57 | |
| 58 | +/* global variables */ |
| 59 | + |
| 60 | +$wgNllMaxListLength = 500; # the maximum allowed length of a list. |
| 61 | + |
58 | 62 | class NaturalLanguageList { |
59 | 63 | |
60 | 64 | public static function onParserFirstCallInit( $parser ) { |
— | — | @@ -174,6 +178,7 @@ |
175 | 179 | * @param $separator String [default:null] Input separator (e.g. ',') |
176 | 180 | */ |
177 | 181 | private function readArgs( $separator=null ) { |
| 182 | + global $wgNllMaxListLength; |
178 | 183 | $items = array(); # array of args to include |
179 | 184 | |
180 | 185 | # strip read items of duplicate elements if not permitted |
— | — | @@ -204,6 +209,11 @@ |
205 | 210 | while ( count( $this->mParams ) > $this->mOptions['length'] ) |
206 | 211 | array_pop ( $this->mParams ); |
207 | 212 | } |
| 213 | + |
| 214 | + # Remove anything over the allowed limit |
| 215 | + while ( count( $this->mParams ) > $wgNllMaxListLength ) { |
| 216 | + array_pop( $this->mParams ); |
| 217 | + } |
208 | 218 | } |
209 | 219 | |
210 | 220 | /** |
— | — | @@ -213,6 +223,7 @@ |
214 | 224 | * @param $separator String [default:null] Input separator |
215 | 225 | */ |
216 | 226 | private function readOptions ( $ignorefirst, $separator=null ) { |
| 227 | + global $wgNllMaxListLength; |
217 | 228 | $args = $this->mArgs; |
218 | 229 | |
219 | 230 | # an array of items not options |
— | — | @@ -266,6 +277,12 @@ |
267 | 278 | if ( $this->mOptions['itemoutput'] === null ) { |
268 | 279 | $this->mOptions['itemoutput'] = wfMsgNoTrans( 'nll-itemoutput' ); |
269 | 280 | } |
| 281 | + |
| 282 | + # don't permit a length larger than the allowed |
| 283 | + if ( $this->mOptions['length'] != -1 |
| 284 | + && $this->mOptions['length'] > $wgNllMaxListLength ) { |
| 285 | + $this->mOptions['length'] = -1; |
| 286 | + } |
270 | 287 | } |
271 | 288 | |
272 | 289 | /** |
— | — | @@ -325,7 +342,7 @@ |
326 | 343 | $this->mOptions[$name] = self::parseNumeral( $value ); |
327 | 344 | break; |
328 | 345 | case 'ignore': |
329 | | - self::parseArrayItem( $this->mIgnores, $value, $separator ); |
| 346 | + self::parseArrayItem( $this->mIgnores, $value, $separator, false, true ); |
330 | 347 | break; |
331 | 348 | case 'data': |
332 | 349 | # just strip the parameter and make the $arg |
— | — | @@ -377,14 +394,18 @@ |
378 | 395 | * @param $value Mixed The element to be verified. |
379 | 396 | */ |
380 | 397 | private static function handle_interval ( &$array, $intervals, $value ) { |
| 398 | + global $wgNllMaxListLength; |
381 | 399 | if ( !$intervals ) |
382 | 400 | return false; |
383 | | - if ( preg_match("@[0-9]+\.\.[1-9][0-9]*@is", $value ) ) { |
384 | | - $tmp = explode ( "|", preg_replace("@([0-9]+)\.\.([1-9][0-9]*)@is", "$1|$2", $value) ); |
| 401 | + $tmp = explode ( "..", $value ); |
| 402 | + if ( count( $tmp ) == 2 ) { |
385 | 403 | if ( is_numeric($tmp[0])===false or is_numeric($tmp[1])===false or ($tmp[0] > $tmp[1]) ) |
386 | 404 | return false; |
387 | | - for ( $i = $tmp[0]; $i <= $tmp[1]; $i++ ) |
| 405 | + for ( $i = $tmp[0]; $i <= $tmp[1]; $i++ ) { |
388 | 406 | $array[] = $i; |
| 407 | + if ( count( $array ) > $wgNllMaxListLength ) |
| 408 | + break; |
| 409 | + } |
389 | 410 | return true; |
390 | 411 | } else { |
391 | 412 | return false; |
— | — | @@ -398,8 +419,14 @@ |
399 | 420 | * @param $value Mixed The element to be inserted |
400 | 421 | * @param $separator String [default:null] Input separator |
401 | 422 | * @param $intervals Boolean [default:false] Whether intervals are allowed |
| 423 | + * @param $ignorelength Boolean [default:false] Whether to ignore the limit |
| 424 | + * on the length. Be careful! |
402 | 425 | */ |
403 | | - private static function parseArrayItem( &$array, $value, $separator=null, $intervals = false ) { |
| 426 | + private static function parseArrayItem( &$array, $value, $separator=null, $intervals = false, $ignorelength = false ) { |
| 427 | + global $wgNllMaxListLength; |
| 428 | + # if the maximum length has been reached; don't bother. |
| 429 | + if ( count( $array ) > $wgNllMaxListLength && !$ignorelength ) |
| 430 | + return; |
404 | 431 | # if no separator, just assume the value can be appended, |
405 | 432 | # simple as that |
406 | 433 | if ( $separator === null ) { |
— | — | @@ -409,9 +436,12 @@ |
410 | 437 | # else, let's break the value up and append |
411 | 438 | # each 'subvalue' to the array. |
412 | 439 | $tmp = explode ( $separator, $value ); |
413 | | - foreach ( $tmp as $v ) |
| 440 | + foreach ( $tmp as $v ) { |
414 | 441 | if ( ! self::handle_interval( $array, $intervals, $v ) ) |
415 | 442 | $array[] = $v; |
| 443 | + if ( count( $array ) > $wgNllMaxListLength ) |
| 444 | + break; |
| 445 | + } |
416 | 446 | } |
417 | 447 | } |
418 | 448 | |