Example #1
0
 /**
  * Runs the needed checks to see if the value is valid. A non
  * true value will be returned if it failed.
  *
  * @param mixed $value
  * @return bool|string
  */
 public function validate($value)
 {
     $valueLen = is_array($value) ? count($value) : zula_strlen($value);
     if ($valueLen < $this->min || $this->max && $valueLen > $this->max) {
         return sprintf(t('%%1$s must be between %1$s and %2$s characters long', I18n::_DTD), number_format($this->min), number_format($this->max));
     } else {
         return true;
     }
 }
Example #2
0
 /**
  * Helper function for emphasis
  *
  * @param string $text
  * @return string
  * @author MediaWiki Project
  */
 public function doQuotes($text)
 {
     $arr = preg_split("/(''+)/", $text, -1, PREG_SPLIT_DELIM_CAPTURE);
     if (count($arr) == 1) {
         return $text;
     } else {
         # First, do some preliminary work. This may shift some apostrophes from
         # being mark-up to being text. It also counts the number of occurrences
         # of bold and italics mark-ups.
         $i = 0;
         $numbold = 0;
         $numitalics = 0;
         foreach ($arr as $r) {
             if ($i % 2 == 1) {
                 # If there are ever four apostrophes, assume the first is supposed to
                 # be text, and the remaining three constitute mark-up for bold text.
                 if (zula_strlen($arr[$i]) == 4) {
                     $arr[$i - 1] .= "'";
                     $arr[$i] = "'''";
                 } else {
                     if (zula_strlen($arr[$i]) > 5) {
                         $arr[$i - 1] .= str_repeat("'", zula_strlen($arr[$i]) - 5);
                         $arr[$i] = "'''''";
                     }
                 }
                 # Count the number of occurrences of bold and italics mark-ups.
                 # We are not counting sequences of five apostrophes.
                 if (zula_strlen($arr[$i]) == 2) {
                     $numitalics++;
                 } else {
                     if (zula_strlen($arr[$i]) == 3) {
                         $numbold++;
                     } else {
                         if (zula_strlen($arr[$i]) == 5) {
                             $numitalics++;
                             $numbold++;
                         }
                     }
                 }
             }
             $i++;
         }
         # If there is an odd number of both bold and italics, it is likely
         # that one of the bold ones was meant to be an apostrophe followed
         # by italics. Which one we cannot know for certain, but it is more
         # likely to be one that has a single-letter word before it.
         if ($numbold % 2 == 1 && $numitalics % 2 == 1) {
             $i = 0;
             $firstsingleletterword = -1;
             $firstmultiletterword = -1;
             $firstspace = -1;
             foreach ($arr as $r) {
                 if ($i % 2 == 1 and zula_strlen($r) == 3) {
                     $x1 = zula_substr($arr[$i - 1], -1);
                     $x2 = zula_substr($arr[$i - 1], -2, 1);
                     if ($x1 == ' ') {
                         if ($firstspace == -1) {
                             $firstspace = $i;
                         }
                     } else {
                         if ($x2 == ' ') {
                             if ($firstsingleletterword == -1) {
                                 $firstsingleletterword = $i;
                             }
                         } else {
                             if ($firstmultiletterword == -1) {
                                 $firstmultiletterword = $i;
                             }
                         }
                     }
                 }
                 $i++;
             }
             # If there is a single-letter word, use it!
             if ($firstsingleletterword > -1) {
                 $arr[$firstsingleletterword] = "''";
                 $arr[$firstsingleletterword - 1] .= "'";
             } else {
                 if ($firstmultiletterword > -1) {
                     $arr[$firstmultiletterword] = "''";
                     $arr[$firstmultiletterword - 1] .= "'";
                 } else {
                     if ($firstspace > -1) {
                         $arr[$firstspace] = "''";
                         $arr[$firstspace - 1] .= "'";
                     }
                 }
             }
         }
         # Now let's actually convert our apostrophic mush to HTML!
         $output = '';
         $buffer = '';
         $state = '';
         $i = 0;
         foreach ($arr as $r) {
             if ($i % 2 == 0) {
                 if ($state == 'both') {
                     $buffer .= $r;
                 } else {
                     $output .= $r;
                 }
             } else {
                 if (zula_strlen($r) == 2) {
                     if ($state == 'i') {
                         $output .= '</em>';
                         $state = '';
                     } else {
                         if ($state == 'bi') {
                             $output .= '</em>';
                             $state = 'b';
                         } else {
                             if ($state == 'ib') {
                                 $output .= '</strong></em><strong>';
                                 $state = 'b';
                             } else {
                                 if ($state == 'both') {
                                     $output .= '<strong><em>' . $buffer . '</em>';
                                     $state = 'b';
                                 } else {
                                     $output .= '<em>';
                                     $state .= 'i';
                                 }
                             }
                         }
                     }
                 } else {
                     if (zula_strlen($r) == 3) {
                         if ($state == 'b') {
                             $output .= '</strong>';
                             $state = '';
                         } else {
                             if ($state == 'bi') {
                                 $output .= '</em></strong><em>';
                                 $state = 'i';
                             } else {
                                 if ($state == 'ib') {
                                     $output .= '</strong>';
                                     $state = 'i';
                                 } else {
                                     if ($state == 'both') {
                                         $output .= '<em><strong>' . $buffer . '</strong>';
                                         $state = 'i';
                                     } else {
                                         $output .= '<strong>';
                                         $state .= 'b';
                                     }
                                 }
                             }
                         }
                     } else {
                         if (zula_strlen($r) == 5) {
                             if ($state == 'b') {
                                 $output .= '</strong><em>';
                                 $state = 'i';
                             } else {
                                 if ($state == 'i') {
                                     $output .= '</em><strong>';
                                     $state = 'b';
                                 } else {
                                     if ($state == 'bi') {
                                         $output .= '</em></strong>';
                                         $state = '';
                                     } else {
                                         if ($state == 'ib') {
                                             $output .= '</strong></em>';
                                             $state = '';
                                         } else {
                                             if ($state == 'both') {
                                                 $output .= '<em><strong>' . $buffer . '</strong></em>';
                                                 $state = '';
                                             } else {
                                                 $buffer = '';
                                                 $state = 'both';
                                             }
                                         }
                                     }
                                 }
                             }
                         }
                     }
                 }
             }
             $i++;
         }
         # Now close all remaining tags.  Notice that the order is important.
         if ($state == 'b' || $state == 'ib') {
             $output .= '</strong>';
         }
         if ($state == 'i' || $state == 'bi' || $state == 'ib') {
             $output .= '</em>';
         }
         if ($state == 'bi') {
             $output .= '</strong>';
         }
         # There might be lonely ''''', so make sure we have a buffer
         if ($state == 'both' && $buffer) {
             $output .= '<strong><em>' . $buffer . '</em></strong>';
         }
         return $output;
     }
 }
Example #3
0
/**
 * Takes a string and creates a short zula_snippet/summary from it
 *
 * @param string $summary
 * @param int $charLimit
 * @param bool $ellipsis	Should ellipsis (...) be added to the end?
 * @return string
 */
function zula_snippet($summary, $charLimit = 400, $ellipsis = false)
{
    $summary = str_replace(array("\n", "\r"), ' ', $summary);
    $charLimit = abs($charLimit);
    if (!$charLimit || zula_strlen($summary) < $charLimit) {
        return $summary;
    } else {
        $str = trim(zula_substr($summary, 0, $charLimit));
        return $str . ($ellipsis ? '&#8230;' : '');
    }
}