r113794 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r113793‎ | r113794 | r113795 >
Date:02:13, 14 March 2012
Author:yaron
Status:deferred
Tags:
Comment:
Patch from Ilmars Poikans to allow users to set number of digits for random number in auto-generated page names
Modified paths:
  • /trunk/extensions/SemanticForms/specials/SF_FormEdit.php (modified) (history)

Diff [purge]

Index: trunk/extensions/SemanticForms/specials/SF_FormEdit.php
@@ -81,8 +81,14 @@
8282 return $text;
8383 }
8484
85 - static function makeRandomNumber() {
86 - return rand() % 1000000;
 85+ static function makeRandomNumber( $numDigits, $hasPadding ) {
 86+ $maxValue = pow( 10, $numDigits ) - 1;
 87+ if ( $maxValue > getrandmax() ) {
 88+ $maxValue = getrandmax();
 89+ }
 90+ $value = rand( 0, $maxValue );
 91+ $format = '%' . ($hasPadding ? '0' : '') . $numDigits . 'd';
 92+ return trim( sprintf( $format, $value ) ); // trim needed, when $hasPadding == false
8793 }
8894
8995 static function printForm( &$form_name, &$target_name, $alt_forms = array(), $redirectOnError = false ) {
@@ -226,13 +232,17 @@
227233
228234 $title_number = "";
229235 $isRandom = false;
 236+ $randomNumHasPadding = false;
 237+ $randomNumDigits = 6;
230238
231239 if ( strpos( $target_name, '{num' ) !== false ) {
232240
233 - // random number
234 - if ( preg_match( '/{num;random}/', $target_name, $matches ) ) {
 241+ // Random number
 242+ if ( preg_match( '/{num;random(;(0)?([1-9][0-9]*))?}/', $target_name, $matches ) ) {
235243 $isRandom = true;
236 - $title_number = self::makeRandomNumber();
 244+ $randomNumHasPadding = array_key_exists( 2, $matches );
 245+ $randomNumDigits = ( array_key_exists( 3, $matches ) ? $matches[3] : $randomNumDigits );
 246+ $title_number = self::makeRandomNumber( $randomNumDigits, $randomNumHasPadding );
237247 } else {
238248 // get unique number start value
239249 // from target name; if it's not
@@ -257,7 +267,7 @@
258268 while ( $target_title->exists() ) {
259269
260270 if ( $isRandom ) {
261 - $title_number = self::makeRandomNumber();
 271+ $title_number = self::makeRandomNumber( $randomNumDigits, $randomNumHasPadding );
262272 }
263273 // if title number is blank,
264274 // change it to 2; otherwise,

Status & tagging log