Changeset 1980 for trunk


Ignore:
Timestamp:
Apr 26, 2007, 5:07:50 AM (18 years ago)
Author:
rvelices
Message:

put some code from the huge section_init to 2 functions

  • parse_section_url does the exact opposite of make_section_in_url
  • parse_well_known_params_url does the exact opposite of add_well_known_params_in_url

section_init is more readable and plugins can have their own views with the same url style
this is not a bug correction but I will merge it to branch 1_7 to take advantage for a plugin

Location:
trunk/include
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/include/functions_url.inc.php

    r1956 r1980  
    431431
    432432/**
     433 * the reverse of make_section_in_url
     434 * returns the 'section' (categories/tags/...) and the data associated with it
     435 *
     436 * Depending on section, other parameters are returned (category/tags/list/...)
     437 *
     438 * @param array of url tokens to parse
     439 * @param int the index in the array of url tokens; in/out
     440 * @return array
     441 */
     442function parse_section_url( $tokens, &$next_token)
     443{
     444  $page=array();
     445  if (0 === strpos(@$tokens[$next_token], 'categor'))
     446  {
     447    $page['section'] = 'categories';
     448    $next_token++;
     449
     450    if (isset($tokens[$next_token]) )
     451    {
     452      if (preg_match('/^(\d+)(?:-(.+))?$/', $tokens[$next_token], $matches))
     453      {
     454        if ( isset($matches[2]) )
     455          $page['hit_by']['cat_url_name'] = $matches[2];
     456        $page['category'] = $matches[1];
     457        $next_token++;
     458      }
     459      else
     460      {
     461        if ( strpos($tokens[$next_token], 'created-')!==0
     462            and strpos($tokens[$next_token], 'posted-')!==0
     463            and strpos($tokens[$next_token], 'start-')!==0
     464            and $tokens[$next_token] != 'flat')
     465        {// try a permalink
     466          $cat_id = get_cat_id_from_permalink($tokens[$next_token]);
     467          if ( !isset($cat_id) )
     468          {//try old permalink
     469            $cat_id = get_cat_id_from_old_permalink($tokens[$next_token], true);
     470          }
     471          if ( isset($cat_id) )
     472          {
     473            $page['category'] = $cat_id;
     474            $page['hit_by']['cat_permalink'] = $tokens[$next_token];
     475          }
     476          else
     477          {
     478            page_not_found('Permalink for album not found');
     479          }
     480          $next_token++;
     481        }
     482       }
     483    }
     484
     485    if (isset($page['category']))
     486    {
     487      $result = get_cat_info($page['category']);
     488      if (empty($result))
     489      {
     490        page_not_found('Requested category does not exist' );
     491      }
     492      $page['category']=$result;
     493    }
     494  }
     495  else if (0 === strpos(@$tokens[$next_token], 'tag'))
     496  {
     497    $page['section'] = 'tags';
     498    $page['tags'] = array();
     499
     500    $next_token++;
     501    $i = $next_token;
     502
     503    $requested_tag_ids = array();
     504    $requested_tag_url_names = array();
     505
     506    while (isset($tokens[$i]))
     507    {
     508      if ( preg_match('/^(created-|posted-|start-(\d)+)/', $tokens[$i]) )
     509        break;
     510
     511      if ( preg_match('/^(\d+)(?:-(.*))?/', $tokens[$i], $matches) )
     512      {
     513        array_push($requested_tag_ids, $matches[1]);
     514      }
     515      else
     516      {
     517        array_push($requested_tag_url_names, $tokens[$i]);
     518      }
     519      $i++;
     520    }
     521    $next_token = $i;
     522
     523    if ( empty($requested_tag_ids) && empty($requested_tag_url_names) )
     524    {
     525      bad_request('at least one tag required');
     526    }
     527
     528    $page['tags'] = find_tags($requested_tag_ids, $requested_tag_url_names);
     529    if ( empty($page['tags']) )
     530    {
     531      page_not_found('Requested tag does not exist', get_root_url().'tags.php' );
     532    }
     533  }
     534  else if (0 === strpos(@$tokens[$next_token], 'fav'))
     535  {
     536    $page['section'] = 'favorites';
     537    $next_token++;
     538  }
     539  else if ('most_visited' == @$tokens[$next_token])
     540  {
     541    $page['section'] = 'most_visited';
     542    $next_token++;
     543  }
     544  else if ('best_rated' == @$tokens[$next_token])
     545  {
     546    $page['section'] = 'best_rated';
     547    $next_token++;
     548  }
     549  else if ('recent_pics' == @$tokens[$next_token])
     550  {
     551    $page['section'] = 'recent_pics';
     552    $next_token++;
     553  }
     554  else if ('recent_cats' == @$tokens[$next_token])
     555  {
     556    $page['section'] = 'recent_cats';
     557    $next_token++;
     558  }
     559  else if ('search' == @$tokens[$next_token])
     560  {
     561    $page['section'] = 'search';
     562    $next_token++;
     563
     564    preg_match('/(\d+)/', @$tokens[$next_token], $matches);
     565    if (!isset($matches[1]))
     566    {
     567      bad_request('search identifier is missing');
     568    }
     569    $page['search'] = $matches[1];
     570    $next_token++;
     571  }
     572  else if ('list' == @$tokens[$next_token])
     573  {
     574    $page['section'] = 'list';
     575    $next_token++;
     576
     577    $page['list'] = array();
     578
     579    // No pictures
     580    if (empty($tokens[$next_token]))
     581    {
     582      // Add dummy element list
     583      array_push($page['list'], -1);
     584    }
     585    // With pictures list
     586    else
     587    {
     588      if (!preg_match('/^\d+(,\d+)*$/', $tokens[$next_token]))
     589      {
     590        bad_request('wrong format on list GET parameter');
     591      }
     592      foreach (explode(',', $tokens[$next_token]) as $image_id)
     593      {
     594        array_push($page['list'], $image_id);
     595      }
     596    }
     597    $next_token++;
     598  }
     599  return $page;
     600}
     601
     602/**
     603 * the reverse of add_well_known_params_in_url
     604 * parses start, flat and chronology from url tokens
     605*/
     606function parse_well_known_params_url($tokens, $i)
     607{
     608  $page = array();
     609  while (isset($tokens[$i]))
     610  {
     611    if (preg_match('/^start-(\d+)/', $tokens[$i], $matches))
     612    {
     613      $page['start'] = $matches[1];
     614    }
     615
     616    if ( 'flat' == $tokens[$i] )
     617    {
     618      // indicate a special list of images
     619      $page['flat'] = true;
     620    }
     621
     622    if (preg_match('/^(posted|created)/', $tokens[$i] ))
     623    {
     624      $chronology_tokens = explode('-', $tokens[$i] );
     625
     626      $page['chronology_field'] = $chronology_tokens[0];
     627
     628      array_shift($chronology_tokens);
     629      $page['chronology_style'] = $chronology_tokens[0];
     630
     631      array_shift($chronology_tokens);
     632      if ( count($chronology_tokens)>0 )
     633      {
     634        if ('list'==$chronology_tokens[0] or
     635            'calendar'==$chronology_tokens[0])
     636        {
     637          $page['chronology_view'] = $chronology_tokens[0];
     638          array_shift($chronology_tokens);
     639        }
     640        $page['chronology_date'] = $chronology_tokens;
     641      }
     642    }
     643    $i++;
     644  }
     645  return $page;
     646}
     647
     648/**
    433649 * Indicate to build url with full path
    434650 *
  • trunk/include/section_init.inc.php

    r1956 r1980  
    123123}
    124124
    125 if (0 === strpos(@$tokens[$next_token], 'categor'))
     125$page = array_merge( $page, parse_section_url( $tokens, $next_token) );
     126if ( !isset($page['section']) )
    126127{
    127128  $page['section'] = 'categories';
    128   $next_token++;
    129 
    130   if (isset($tokens[$next_token]) )
    131   {
    132     if (preg_match('/^(\d+)(?:-(.+))?$/', $tokens[$next_token], $matches))
    133     {
    134       if ( isset($matches[2]) )
    135         $page['hit_by']['cat_url_name'] = $matches[2];
    136       $page['category'] = $matches[1];
    137       $next_token++;
    138     }
    139     else
    140     {
    141       if ( strpos($tokens[$next_token], 'created-')!==0
    142           and strpos($tokens[$next_token], 'posted-')!==0
    143           and strpos($tokens[$next_token], 'start-')!==0
    144           and $tokens[$next_token] != 'flat')
    145       {// try a permalink
    146         $cat_id = get_cat_id_from_permalink($tokens[$next_token]);
    147         if ( !isset($cat_id) )
    148         {//try old permalink
    149           $cat_id = get_cat_id_from_old_permalink($tokens[$next_token], true);
    150         }
    151         if ( isset($cat_id) )
    152         {
    153           $page['category'] = $cat_id;
    154           $page['hit_by']['cat_permalink'] = $tokens[$next_token];
    155         }
    156         else
    157         {
    158           page_not_found('Permalink for album not found');
    159         }
    160         unset($cat_id);
    161         $next_token++;
    162       }
    163       elseif ( script_basename()=='picture' )
    164       { //access a picture only by id, file or id-file without given section
    165         $page['flat']=true;
    166       }
    167     }
    168   }
    169   elseif ( script_basename()=='picture' )
    170   { //access a picture only by id, file or id-file without given section
    171     $page['flat']=true;
    172   }
    173 }
    174 else if (0 === strpos(@$tokens[$next_token], 'tag'))
    175 {
    176   $page['section'] = 'tags';
    177   $page['tags'] = array();
    178 
    179   $next_token++;
    180   $i = $next_token;
    181 
    182   $requested_tag_ids = array();
    183   $requested_tag_url_names = array();
    184 
    185   while (isset($tokens[$i]))
    186   {
    187     if ( preg_match('/^(created-|posted-|start-(\d)+)/', $tokens[$i]) )
     129
     130  switch (script_basename())
     131  {
     132    case 'picture':
    188133      break;
    189 
    190     if ( preg_match('/^(\d+)(?:-(.*))?/', $tokens[$i], $matches) )
    191     {
    192       array_push($requested_tag_ids, $matches[1]);
    193     }
    194     else
    195     {
    196       array_push($requested_tag_url_names, $tokens[$i]);
    197     }
    198     $i++;
    199   }
    200   $next_token = $i;
    201 
    202   if ( empty($requested_tag_ids) && empty($requested_tag_url_names) )
    203   {
    204     bad_request('at least one tag required');
    205   }
    206 
    207   $page['tags'] = find_tags($requested_tag_ids, $requested_tag_url_names);
    208   if ( empty($page['tags']) )
    209   {
    210     page_not_found('Requested tag does not exist', get_root_url().'tags.php' );
    211   }
    212 }
    213 else if (0 === strpos(@$tokens[$next_token], 'fav'))
    214 {
    215   $page['section'] = 'favorites';
    216   $next_token++;
    217 }
    218 else if ('most_visited' == @$tokens[$next_token])
    219 {
    220   $page['section'] = 'most_visited';
    221   $next_token++;
    222 }
    223 else if ('best_rated' == @$tokens[$next_token])
    224 {
    225   $page['section'] = 'best_rated';
    226   $next_token++;
    227 }
    228 else if ('recent_pics' == @$tokens[$next_token])
    229 {
    230   $page['section'] = 'recent_pics';
    231   $next_token++;
    232 }
    233 else if ('recent_cats' == @$tokens[$next_token])
    234 {
    235   $page['section'] = 'recent_cats';
    236   $next_token++;
    237 }
    238 else if ('search' == @$tokens[$next_token])
    239 {
    240   $page['section'] = 'search';
    241   $next_token++;
    242 
    243   preg_match('/(\d+)/', @$tokens[$next_token], $matches);
    244   if (!isset($matches[1]))
    245   {
    246     bad_request('search identifier is missing');
    247   }
    248   $page['search'] = $matches[1];
    249   $next_token++;
    250 }
    251 else if ('list' == @$tokens[$next_token])
    252 {
    253   $page['section'] = 'list';
    254   $next_token++;
    255 
    256   $page['list'] = array();
    257 
    258   // No pictures
    259   if (empty($tokens[$next_token]))
    260   {
    261     // Add dummy element list
    262     array_push($page['list'], -1);
    263   }
    264   // With pictures list
    265   else
    266   {
    267     if (!preg_match('/^\d+(,\d+)*$/', $tokens[$next_token]))
    268     {
    269       bad_request('wrong format on list GET parameter');
    270     }
    271     foreach (explode(',', $tokens[$next_token]) as $image_id)
    272     {
    273       array_push($page['list'], $image_id);
    274     }
    275   }
    276   $next_token++;
    277 }
    278 else
    279 {
    280   $page['section'] = 'categories';
    281 
    282   switch (script_basename())
    283   {
    284     case 'picture':
    285     {
    286       //access a picture only by id, file or id-file without given section
    287       $page['flat'] = true;
    288       break;
    289     }
    290134    case 'index':
    291135    {
     
    314158}
    315159
    316 $i = $next_token;
    317 
    318 while (isset($tokens[$i]))
    319 {
    320   if (preg_match('/^start-(\d+)/', $tokens[$i], $matches))
    321   {
    322     $page['start'] = $matches[1];
    323   }
    324 
    325   if ('categories' == $page['section'] and
    326       'flat' == $tokens[$i] and
     160
     161$page = array_merge( $page, parse_well_known_params_url( $tokens, $next_token) );
     162
     163
     164if ( script_basename()=='picture' and 'categories'==$page['section'] and
    327165      !isset($page['chronology_field']) )
    328   {
    329     // indicate a special list of images
    330     $page['flat'] = true;
    331   }
    332 
    333   if (preg_match('/^(posted|created)/', $tokens[$i] ))
    334   {
    335     $chronology_tokens = explode('-', $tokens[$i] );
    336 
    337     $page['chronology_field'] = $chronology_tokens[0];
    338 
    339     array_shift($chronology_tokens);
    340     $page['chronology_style'] = $chronology_tokens[0];
    341 
    342     array_shift($chronology_tokens);
    343     if ( count($chronology_tokens)>0 )
    344     {
    345       if ('list'==$chronology_tokens[0] or
    346           'calendar'==$chronology_tokens[0])
    347       {
    348         $page['chronology_view'] = $chronology_tokens[0];
    349         array_shift($chronology_tokens);
    350       }
    351       $page['chronology_date'] = $chronology_tokens;
    352     }
    353     unset($page['flat']);
    354   }
    355 
    356   $i++;
     166{ //access a picture only by id, file or id-file without given section
     167  $page['flat']=true;
    357168}
    358169
     
    390201  if (isset($page['category']))
    391202  {
    392     $result = get_cat_info($page['category']);
    393     if (empty($result))
    394     {
    395       page_not_found('Requested category does not exist' );
    396     }
    397 
    398     $page = array_merge(
    399       $page,
    400       array(
    401         'comment'            => $result['comment'],
    402         'category'          => $result,
     203    $page = array_merge(
     204      $page,
     205      array(
     206        'comment'           => $page['category']['comment'],
    403207        'title'             =>
    404           get_cat_display_name($result['upper_names'], '', false),
     208          get_cat_display_name($page['category']['upper_names'], '', false),
    405209        )
    406210      );
Note: See TracChangeset for help on using the changeset viewer.