https://www.mediawiki.org/wiki/Special:Code/MediaWiki/113794

Revision: 113794
Author:   yaron
Date:     2012-03-14 02:13:22 +0000 (Wed, 14 Mar 2012)
Log Message:
-----------
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: trunk/extensions/SemanticForms/specials/SF_FormEdit.php
===================================================================
--- trunk/extensions/SemanticForms/specials/SF_FormEdit.php     2012-03-14 
01:58:01 UTC (rev 113793)
+++ trunk/extensions/SemanticForms/specials/SF_FormEdit.php     2012-03-14 
02:13:22 UTC (rev 113794)
@@ -81,8 +81,14 @@
                return $text;
        }
 
-       static function makeRandomNumber() {
-               return rand() % 1000000;
+       static function makeRandomNumber( $numDigits, $hasPadding ) {
+               $maxValue = pow( 10, $numDigits ) - 1;
+               if ( $maxValue > getrandmax() ) {
+                       $maxValue = getrandmax();
+               }
+               $value = rand( 0, $maxValue );
+               $format = '%' . ($hasPadding ? '0' : '') . $numDigits . 'd';
+               return trim( sprintf( $format, $value ) ); // trim needed, when 
$hasPadding == false
        }
 
        static function printForm( &$form_name, &$target_name, $alt_forms = 
array(), $redirectOnError = false ) {
@@ -226,13 +232,17 @@
 
                                        $title_number = "";
                                        $isRandom = false;
+                                       $randomNumHasPadding = false;
+                                       $randomNumDigits = 6;
 
                                        if ( strpos( $target_name, '{num' ) !== 
false ) {
 
-                                               // random number
-                                               if ( preg_match( 
'/{num;random}/', $target_name, $matches ) ) {
+                                               // Random number
+                                               if ( preg_match( 
'/{num;random(;(0)?([1-9][0-9]*))?}/', $target_name, $matches ) ) {
                                                        $isRandom = true;
-                                                       $title_number = 
self::makeRandomNumber();
+                                                       $randomNumHasPadding = 
array_key_exists( 2, $matches );
+                                                       $randomNumDigits = ( 
array_key_exists( 3, $matches ) ? $matches[3] : $randomNumDigits );
+                                                       $title_number = 
self::makeRandomNumber( $randomNumDigits, $randomNumHasPadding );
                                                } else {
                                                        // get unique number 
start value
                                                        // from target name; if 
it's not
@@ -257,7 +267,7 @@
                                                while ( $target_title->exists() 
) {
 
                                                        if ( $isRandom ) {
-                                                               $title_number = 
self::makeRandomNumber();
+                                                               $title_number = 
self::makeRandomNumber( $randomNumDigits, $randomNumHasPadding );
                                                        }
                                                        // if title number is 
blank,
                                                        // change it to 2; 
otherwise,


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to