r64742 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r64741‎ | r64742 | r64743 >
Date:10:44, 8 April 2010
Author:svip
Status:deferred
Tags:
Comment:
Following up on r64739 and r64740; adding a global parameter to prevent creating too large lists.
Modified paths:
  • /trunk/extensions/NaturalLanguageList/NaturalLanguageList.php (modified) (history)

Diff [purge]

Index: trunk/extensions/NaturalLanguageList/NaturalLanguageList.php
@@ -54,6 +54,10 @@
5555
5656 $wgParserTestFiles[] = dirname( __FILE__ ) . "/nllParserTests.txt";
5757
 58+/* global variables */
 59+
 60+$wgNllMaxListLength = 500; # the maximum allowed length of a list.
 61+
5862 class NaturalLanguageList {
5963
6064 public static function onParserFirstCallInit( $parser ) {
@@ -174,6 +178,7 @@
175179 * @param $separator String [default:null] Input separator (e.g. ',')
176180 */
177181 private function readArgs( $separator=null ) {
 182+ global $wgNllMaxListLength;
178183 $items = array(); # array of args to include
179184
180185 # strip read items of duplicate elements if not permitted
@@ -204,6 +209,11 @@
205210 while ( count( $this->mParams ) > $this->mOptions['length'] )
206211 array_pop ( $this->mParams );
207212 }
 213+
 214+ # Remove anything over the allowed limit
 215+ while ( count( $this->mParams ) > $wgNllMaxListLength ) {
 216+ array_pop( $this->mParams );
 217+ }
208218 }
209219
210220 /**
@@ -213,6 +223,7 @@
214224 * @param $separator String [default:null] Input separator
215225 */
216226 private function readOptions ( $ignorefirst, $separator=null ) {
 227+ global $wgNllMaxListLength;
217228 $args = $this->mArgs;
218229
219230 # an array of items not options
@@ -266,6 +277,12 @@
267278 if ( $this->mOptions['itemoutput'] === null ) {
268279 $this->mOptions['itemoutput'] = wfMsgNoTrans( 'nll-itemoutput' );
269280 }
 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+ }
270287 }
271288
272289 /**
@@ -325,7 +342,7 @@
326343 $this->mOptions[$name] = self::parseNumeral( $value );
327344 break;
328345 case 'ignore':
329 - self::parseArrayItem( $this->mIgnores, $value, $separator );
 346+ self::parseArrayItem( $this->mIgnores, $value, $separator, false, true );
330347 break;
331348 case 'data':
332349 # just strip the parameter and make the $arg
@@ -377,14 +394,18 @@
378395 * @param $value Mixed The element to be verified.
379396 */
380397 private static function handle_interval ( &$array, $intervals, $value ) {
 398+ global $wgNllMaxListLength;
381399 if ( !$intervals )
382400 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 ) {
385403 if ( is_numeric($tmp[0])===false or is_numeric($tmp[1])===false or ($tmp[0] > $tmp[1]) )
386404 return false;
387 - for ( $i = $tmp[0]; $i <= $tmp[1]; $i++ )
 405+ for ( $i = $tmp[0]; $i <= $tmp[1]; $i++ ) {
388406 $array[] = $i;
 407+ if ( count( $array ) > $wgNllMaxListLength )
 408+ break;
 409+ }
389410 return true;
390411 } else {
391412 return false;
@@ -398,8 +419,14 @@
399420 * @param $value Mixed The element to be inserted
400421 * @param $separator String [default:null] Input separator
401422 * @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!
402425 */
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;
404431 # if no separator, just assume the value can be appended,
405432 # simple as that
406433 if ( $separator === null ) {
@@ -409,9 +436,12 @@
410437 # else, let's break the value up and append
411438 # each 'subvalue' to the array.
412439 $tmp = explode ( $separator, $value );
413 - foreach ( $tmp as $v )
 440+ foreach ( $tmp as $v ) {
414441 if ( ! self::handle_interval( $array, $intervals, $v ) )
415442 $array[] = $v;
 443+ if ( count( $array ) > $wgNllMaxListLength )
 444+ break;
 445+ }
416446 }
417447 }
418448

Follow-up revisions

RevisionCommit summaryAuthorDate
r64743Up on r64742, using array_slice instead of array_pop. :Psvip10:55, 8 April 2010
r64870Some changes to r64742 and r64867; changed the default cap limit to 1000 base...svip12:11, 10 April 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r64739Created 'intervals' and 'length' parameters/features.svip09:45, 8 April 2010
r64740Follow up to broken code of r64739, all bugs should be clear now.svip09:55, 8 April 2010

Status & tagging log