Overview
Comment: | + Meilleure gestion des retours entre boucles parent et enfant + Génération de code plus propre |
---|---|
Downloads: | Tarball | ZIP archive | SQL archive |
Timelines: | family | ancestors | descendants | both | trunk |
Files: | files | file ages | folders |
SHA1: |
dd192f09197ff57f94ca003614792c98 |
User & Date: | bohwaz on 2012-05-24 03:58:38 |
Other Links: | manifest | tags |
Context
2012-05-24
| ||
04:01 | Fix [c3c7240afe] check-in: 55444d7f88 user: bohwaz tags: trunk | |
03:58 | + Meilleure gestion des retours entre boucles parent et enfant + Génération de code plus propre check-in: dd192f0919 user: bohwaz tags: trunk | |
03:03 | Menues corrections pour wiki et squelettes check-in: 2bd0af25f0 user: bohwaz tags: trunk | |
Changes
Modified include/class.squelette.php from [6e350f7daf] to [161a3a525a].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | <?php require_once GARRADIN_ROOT . '/include/libs/miniskel/class.miniskel.php'; require_once GARRADIN_ROOT . '/include/lib.squelette.filtres.php'; class Squelette extends miniSkel { private $parent = null; private $_vars = array(); private function _registerDefaultModifiers() { foreach (Squelette_Filtres::$filtres_php as $func=>$name) { if (is_string($func)) | > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 | <?php require_once GARRADIN_ROOT . '/include/libs/miniskel/class.miniskel.php'; require_once GARRADIN_ROOT . '/include/lib.squelette.filtres.php'; class Squelette_Snippet { const TEXT = 0; const PHP = 1; const GUESS = 2; const OBJ = 3; protected $_content = array(); protected function _getType($type, $value) { if ($type == self::GUESS) { if ($value instanceof Squelette_Snippet) return self::OBJ; else return self::TEXT; } return $type; } public function __construct($type = self::TEXT, $value = '') { $type = $this->_getType($type, $value); if ($type == self::OBJ) { $this->_content = $value->get(); } else { $this->_content[] = (string) (int) $type . $value; } unset($value); } public function prepend($type = self::TEXT, $value, $pos = false) { $type = $this->_getType($type, $value); if ($type == self::OBJ) { if ($pos) { array_splice($this->_content, $pos, 0, $value->get()); } else { $this->_content = array_merge($value->get(), $this->_content); } } else { $value = (string) (int) $type . $value; if ($pos) { array_splice($this->_content, $pos, 0, $value); } else { array_unshift($this->_content, $value); } } unset($value); } public function append($type = self::TEXT, $value, $pos = false) { $type = $this->_getType($type, $value); if ($type == self::OBJ) { if ($pos) { array_splice($this->_content, $pos + 1, 0, $value->get()); } else { $this->_content = array_merge($this->_content, $value->get()); } } else { $value = (string) (int) $type . $value; if ($pos) { array_splice($this->_content, $pos + 1, 0, $value); } else { array_push($this->_content, $value); } } unset($value); } public function output($in_php = false) { $out = ''; $php = $in_php ?: false; foreach ($this->_content as $line) { if ($line[0] == self::PHP && !$php) { $php = true; $out .= '<?php '; } elseif ($line[0] == self::TEXT && $php) { $php = false; $out .= ' ?>'; } $out .= substr($line, 1); if ($line[0] == self::PHP) { $out .= "\n"; } } if ($php && !$in_php) { $out .= ' ?>'; } $this->_content = array(); return $out; } public function __toString() { return $this->output(false); } public function get() { return $this->_content; } public function replace($key, $type = self::TEXT, $value) { $type = $this->_getType($type, $value); if ($type == self::OBJ) { array_splice($this->_content, $key, 1, $value->get()); } else { $this->_content[$key] = (string) (int) $type . $value; } unset($value); } } class Squelette extends miniSkel { private $parent = null; private $current = null; private $_vars = array(); private function _registerDefaultModifiers() { foreach (Squelette_Filtres::$filtres_php as $func=>$name) { if (is_string($func)) |
︙ | ︙ | |||
53 54 55 56 57 58 59 | throw new miniSkelMarkupException("Le tag INCLURE demande à préciser le fichier à inclure."); $file = key($args); if (empty($file) || !preg_match('!^[\w\d_-]+(?:\.[\w\d_-]+)*$!', $file)) throw new miniSkelMarkupException("INCLURE: le nom de fichier ne peut contenir que des caractères alphanumériques."); | | | > | | < | < > | < < | > | | > > | | > > > | > | > > > > > > > | | > > | > | > > > > | > > > | > | | > | > | | | > < > > > > | | 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 | throw new miniSkelMarkupException("Le tag INCLURE demande à préciser le fichier à inclure."); $file = key($args); if (empty($file) || !preg_match('!^[\w\d_-]+(?:\.[\w\d_-]+)*$!', $file)) throw new miniSkelMarkupException("INCLURE: le nom de fichier ne peut contenir que des caractères alphanumériques."); return new Squelette_Snippet(1, '$this->fetch("'.$file.'", false);'); } protected function processVariable($name, $value, $applyDefault, $modifiers, $pre, $post, $context) { if ($context == self::CONTEXT_IN_ARG) { $out = new Squelette_Snippet(1, '$this->getVariable(\''.$name.'\')'); if ($pre) { $out->prepend(2, $pre); } if ($post) { $out->append(2, $post); } return $out; } $out = new Squelette_Snippet(1, '$value = $this->getVariable(\''.$name.'\');'); // We process modifiers foreach ($modifiers as &$modifier) { if (!isset($this->modifiers[$modifier['name']])) { throw new miniSkelMarkupException('Filtre '.$modifier['name'].' inconnu !'); } $out->append(1, '$value = call_user_func_array('.var_export($this->modifiers[$modifier['name']], true).', array($value, '); foreach ($modifier['arguments'] as $arg) { if ($arg == 'debut_liste') { $out->append(1, '$this->getVariable(\'debut_liste\')'); } elseif ($arg instanceOf Squelette_Snippet) { $out->append(3, $arg); } else { //if (preg_match('!getVariable!', $arg)) throw new Exception("lol"); $out->append(1, '"'.str_replace('"', '\\"', $arg).'"'); } $out->append(1, ', '); } $out->append(1, '));'); if (in_array($modifier['name'], Squelette_Filtres::$desactiver_defaut)) { $applyDefault = false; } } if ($applyDefault) { $out->append(1, 'if (is_string($value) && trim($value)) $value = htmlspecialchars($value, ENT_QUOTES, \'UTF-8\', false);'); } $out->append(1, 'if ($value === true || trim($value) !== \'\'):'); // Getting pre-content if ($pre) { $out->append(2, $pre); } $out->append(1, 'echo is_bool($value) ? "" : $value;'); // Getting post-content if ($post) { $out->append(2, $post); } $out->append(1, 'endif;'); return $out; } protected function processLoop($loopName, $loopType, $loopCriterias, $loopContent, $preContent, $postContent, $altContent) { if ($loopType != 'articles' && $loopType != 'rubriques' && $loopType != 'pages') { throw new miniSkelMarkupException("Le type de boucle '".$loopType."' est inconnu."); } $loopStart = ''; $query = $where = $order = ''; $limit = $begin = 0; $query = 'SELECT w.*, strftime(\\\'%s\\\', w.date_creation) AS date_creation, strftime(\\\'%s\\\', w.date_modification) AS date_modification'; if (trim($loopContent)) { $query .= ', r.contenu AS texte FROM wiki_pages AS w LEFT JOIN wiki_revisions AS r ON (w.id = r.id_page AND w.revision = r.revision) '; } else { $query .= '\'\' AS texte '; } $where = 'WHERE w.droit_lecture = -1 '; if ($loopType == 'articles') { $where .= 'AND (SELECT COUNT(id) FROM wiki_pages WHERE parent = w.id) = 0 '; } elseif ($loopType == 'rubriques') { $where .= 'AND (SELECT COUNT(id) FROM wiki_pages WHERE parent = w.id) > 0 '; } $allowed_fields = array('id', 'uri', 'titre', 'date', 'date_creation', 'date_modification', 'parent', 'rubrique', 'revision', 'points', 'recherche', 'texte'); $search = $search_rank = false; foreach ($loopCriterias as $criteria) { if (isset($criteria['field'])) { if (!in_array($criteria['field'], $allowed_fields)) |
︙ | ︙ | |||
204 205 206 207 208 209 210 | { $query = 'SELECT w.*, r.contenu AS texte, rank(matchinfo(wiki_recherche), 0, 1.0, 1.0) AS points FROM wiki_pages AS w INNER JOIN wiki_recherche AS r ON (w.id = r.id) '; $where .= ' AND wiki_recherche MATCH \\\'\'.$db->escapeString($this->getVariable(\''.$criteria['field'].'\')).\'\\\''; $search = true; } else { | > > > > > | | 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 | { $query = 'SELECT w.*, r.contenu AS texte, rank(matchinfo(wiki_recherche), 0, 1.0, 1.0) AS points FROM wiki_pages AS w INNER JOIN wiki_recherche AS r ON (w.id = r.id) '; $where .= ' AND wiki_recherche MATCH \\\'\'.$db->escapeString($this->getVariable(\''.$criteria['field'].'\')).\'\\\''; $search = true; } else { if ($criteria['field'] == 'parent') $field = 'id'; else $field = $criteria['field']; $where .= ' AND '.$criteria['field'].' = \\\'\'.$db->escapeString($this->getVariable(\''.$field.'\')).\'\\\''; } break; } default: break; } } |
︙ | ︙ | |||
234 235 236 237 238 239 240 | if ($limit) { $query .= ' LIMIT '.(is_numeric($begin) ? (int) $begin : '\'.$this->variables[\'debut_liste\'].\'').','.(int)$limit; } $hash = sha1(uniqid(mt_rand(), true)); | | | | | | | < | < | | | | | < | < | < | | | | | | > > | > > > | > | | | | | 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 | if ($limit) { $query .= ' LIMIT '.(is_numeric($begin) ? (int) $begin : '\'.$this->variables[\'debut_liste\'].\'').','.(int)$limit; } $hash = sha1(uniqid(mt_rand(), true)); $out = new Squelette_Snippet(); $out->append(1, '$this->parent =& $this->_vars[$parent_hash]; '); if ($search) { $out->append(1, 'if (trim($this->getVariable(\'recherche\'))) { '); } $out->append(1, '$result_'.$hash.' = $db->query(\''.$query.'\'); '); $out->append(1, '$nb_rows = $db->countRows($result_'.$hash.'); '); if ($search) { $out->append(1, '} else { $result_'.$hash.' = false; $nb_rows = 0; }'); } $out->append(1, '$this->_vars[\''.$hash.'\'] = array(\'_self_hash\' => \''.$hash.'\', \'_parent_hash\' => $parent_hash, \'total_boucle\' => $nb_rows, \'compteur_boucle\' => 0);'); $out->append(1, '$this->current =& $this->_vars[\''.$hash.'\']; '); $out->append(1, '$parent_hash = "'.$hash.'"; '); $out->append(1, 'if ($nb_rows > 0):'); if ($preContent) { $out->append(2, $this->parse($preContent, $loopName, self::PRE_CONTENT)); } $out->append(1, 'while ($row = $result_'.$hash.'->fetchArray(SQLITE3_ASSOC)): '); $out->append(1, '$this->_vars[\''.$hash.'\'][\'compteur_boucle\'] += 1; '); $out->append(1, $loopStart); $out->append(1, '$this->_vars[\''.$hash.'\'] = array_merge($this->_vars[\''.$hash.'\'], $row); '); $out->append(2, $this->parseVariables($loopContent)); $out->append(1, 'endwhile;'); // we put the post-content after the loop content if ($postContent) { $out->append(2, $this->parse($postContent, $loopName, self::POST_CONTENT)); } if ($altContent) { $out->append(1, 'else:'); $out->append(2, $this->parse($altContent, $loopName, self::ALT_CONTENT)); } $out->append(1, 'endif; '); $out->append(1, '$parent_hash = $this->_vars[\''.$hash.'\'][\'_parent_hash\']; '); $out->append(1, 'unset($result_'.$hash.', $nb_rows, $this->_vars[\''.$hash.'\']); '); $out->append(1, '$this->current =& $this->_vars[$parent_hash]; '); $out->append(1, '$parent_hash = $this->current[\'_parent_hash\']; '); $out->append(1, '$this->parent =& $parent_hash ? $this->_vars[$_parent_hash] : null;'); return $out; } public function fetch($template, $no_display = false) { $this->currentTemplate = $template; $path = file_exists(GARRADIN_ROOT . '/squelettes/' . $template) ? GARRADIN_ROOT . '/squelettes/' . $template : GARRADIN_ROOT . '/squelettes-dist/' . $template; $tpl_id = basename(dirname($path)) . '/' . $template; if (!self::compile_check($tpl_id, $path)) { if (!file_exists($path)) { throw new miniSkelMarkupException('Le squelette "'.$tpl_id.'" n\'existe pas.'); } $content = file_get_contents($path); $content = strtr($content, array('<?php' => '<?php', '<?' => '<?php echo \'<?\'; ?>')); $out = new Squelette_Snippet(2, $this->parse($content)); $out->prepend(1, '/* '.$tpl_id.' */ '. '$db = Garradin_DB::getInstance(); '. 'if ($this->parent && !isset($parent_hash)) $parent_hash = $this->parent[\'_self_hash\']; '. // For included files 'elseif (!$this->parent) $parent_hash = false;'); if (!$no_display) { self::compile_store($tpl_id, $out); } } if (!$no_display) { require self::compile_get_path($tpl_id); } |
︙ | ︙ | |||
365 366 367 368 369 370 371 | } elseif (preg_match('!^/admin/!', $uri)) { throw new UserException('Cette page n\'existe pas.'); } else { | < > > > > > > > > > > | 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 | } elseif (preg_match('!^/admin/!', $uri)) { throw new UserException('Cette page n\'existe pas.'); } else { $_GET['uri'] = $_REQUEST['uri'] = substr($uri, 1); if (preg_match('!^[\w\d_-]+$!i', $_GET['uri']) && file_exists(GARRADIN_ROOT . '/squelettes/' . strtolower($_GET['uri']) . '.html')) { $skel = strtolower($_GET['uri']) . '.html'; } else { $skel = 'article.html'; } } $this->display($skel); } static private function compile_get_path($path) { |
︙ | ︙ | |||
420 421 422 423 424 425 426 | unlink($path); return true; } protected function getVariable($var) { | > > > > > > > > | > > > > > > | 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 | unlink($path); return true; } protected function getVariable($var) { if (isset($this->current[$var])) { return $this->current[$var]; } elseif (isset($this->parent[$var])) { return $this->parent[$var]; } elseif (isset($this->variables[$var])) { return $this->variables[$var]; } elseif (isset($_REQUEST[$var])) { return $_REQUEST[$var]; } else { return null; } } static public function getSource($template) { if (!preg_match('!^[\w\d_-]+(?:\.[\w\d_-]+)*$!', $template)) return false; |
︙ | ︙ |