Ignore:
Timestamp:
May 18, 2013, 5:58:36 AM (11 years ago)
Author:
rvelices
Message:

bug 2900: Improve web service output

File:
1 edited

Legend:

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

    r22302 r22729  
    193193}
    194194
    195 function ws_getMissingDerivatives($params, &$service)
     195function ws_std_get_category_xml_attributes()
     196{
     197  return array(
     198    'id', 'url', 'nb_images', 'total_nb_images', 'nb_categories', 'date_last', 'max_date_last',
     199    );
     200}
     201
     202function ws_std_get_tag_xml_attributes()
     203{
     204  return array(
     205    'id', 'name', 'url_name', 'counter', 'url', 'page_url',
     206    );
     207}
     208
     209function ws_getMissingDerivatives($params, $service)
    196210{
    197211  if (!is_admin())
     
    292306 * returns PWG version (web service method)
    293307 */
    294 function ws_getVersion($params, &$service)
     308function ws_getVersion($params, $service)
    295309{
    296310  global $conf;
     
    304318 * returns general informations (web service method)
    305319 */
    306 function ws_getInfos($params, &$service)
     320function ws_getInfos($params, $service)
    307321{
    308322  if (!is_admin())
     
    368382}
    369383
    370 function ws_caddie_add($params, &$service)
     384function ws_caddie_add($params, $service)
    371385{
    372386  if (!is_admin())
     
    401415 * returns images per category (web service method)
    402416 */
    403 function ws_categories_getImages($params, &$service)
     417function ws_categories_getImages($params, $service)
    404418{
    405419  global $user, $conf;
     
    470484    $order_by = empty($order_by) ? $conf['order_by'] : 'ORDER BY '.$order_by;
    471485
     486    $params['per_page'] = (int)$params['per_page'];
     487    $params['page'] = (int)$params['page'];
     488
    472489    $query = '
    473490SELECT i.*, GROUP_CONCAT(category_id) AS cat_ids
     
    478495GROUP BY i.id
    479496'.$order_by.'
    480 LIMIT '.(int)$params['per_page'].' OFFSET '.(int)($params['per_page']*$params['page']);
     497LIMIT '.$params['per_page'].' OFFSET '.($params['per_page']*$params['page']);
    481498
    482499    $result = pwg_query($query);
     
    512529                  )
    513530                );
    514         array_push( $image_cats,  array(
    515               WS_XML_ATTRIBUTES => array (
    516                   'id' => (int)$cat_id,
    517                   'url' => $url,
    518                   'page_url' => $page_url,
    519                 )
    520             )
    521           );
     531        $image_cats[] = array (
     532                'id' => (int)$cat_id,
     533                'url' => $url,
     534                'page_url' => $page_url,
     535            );
    522536      }
    523537
     
    525539            $image_cats,'category', array('id','url','page_url')
    526540          );
    527       array_push($images, $image);
    528     }
    529   }
    530 
    531   return array( 'images' =>
    532     array (
    533       WS_XML_ATTRIBUTES =>
     541      $images[] = $image;
     542    }
     543  }
     544
     545  return array (
     546      'paging' => new PwgNamedStruct(
    534547        array(
    535548            'page' => $params['page'],
    536549            'per_page' => $params['per_page'],
    537550            'count' => count($images)
    538           ),
    539        WS_XML_CONTENT => new PwgNamedArray($images, 'image',
    540           ws_std_get_image_xml_attributes() )
    541       )
     551          ) ),
     552       'images' => new PwgNamedArray($images, 'image', ws_std_get_image_xml_attributes() )
    542553    );
    543554}
     
    558569    if (!isset($node['id_uppercat']))
    559570    {
    560       $tree[$key] = &$node;
     571      $tree[] = &$node;
    561572    }
    562573    else
     
    564575      if (!isset($categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories']))
    565576      {
    566         $categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories'] = array();
     577        $categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories'] = new PwgNamedArray(array(), 'category', ws_std_get_category_xml_attributes());
    567578      }
    568579
    569       $categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories'][$key] = &$node;
     580      $categories[ $key_of_cat[ $node['id_uppercat'] ] ]['sub_categories']->_content[] = &$node;
    570581    }
    571582  }
     
    577588 * returns a list of categories (web service method)
    578589 */
    579 function ws_categories_getList($params, &$service)
     590function ws_categories_getList($params, $service)
    580591{
    581592  global $user,$conf;
    582 
    583   if ($params['tree_output'])
    584   {
    585     if (!isset($_GET['format']) or !in_array($_GET['format'], array('php', 'json')))
    586     {
    587       // the algorithm used to build a tree from a flat list of categories
    588       // keeps original array keys, which is not compatible with
    589       // PwgNamedArray.
    590       //
    591       // PwgNamedArray is useful to define which data is an attribute and
    592       // which is an element in the XML output. The "hierarchy" output is
    593       // only compatible with json/php output.
    594 
    595       return new PwgError(405, "The tree_output option is only compatible with json/php output formats");
    596     }
    597   }
    598593
    599594  $where = array('1=1');
     
    609604      $where[] = 'id_uppercat IS NULL';
    610605  }
    611   else if ($params['cat_id']>0)
     606  elseif ($params['cat_id']>0)
    612607  {
    613608    $where[] = 'uppercats '.DB_REGEX_OPERATOR.' \'(^|,)'.
     
    705700      $image_id = $row['user_representative_picture_id'];
    706701    }
    707     else if (!empty($row['representative_picture_id']))
     702    elseif (!empty($row['representative_picture_id']))
    708703    { // if a representative picture is set, it has priority
    709704      $image_id = $row['representative_picture_id'];
    710705    }
    711     else if ($conf['allow_random_representative'])
     706    elseif ($conf['allow_random_representative'])
    712707    {
    713708      // searching a random representant among elements in sub-categories
     
    747742      if ($conf['representative_cache_on_subcats'] and $row['user_representative_picture_id'] != $image_id)
    748743      {
    749         $user_representative_updates_for[ $user['id'].'#'.$row['id'] ] = $image_id;
     744        $user_representative_updates_for[ $row['id'] ] = $image_id;
    750745      }
    751746
    752747      $row['representative_picture_id'] = $image_id;
    753       array_push($image_ids, $image_id);
    754       array_push($categories, $row);
     748      $image_ids[] = $image_id;
     749      $categories[] = $row;
    755750    }
    756751    unset($image_id);
     
    758753
    759754
    760     array_push($cats, $row);
     755    $cats[] = $row;
    761756  }
    762757  usort($cats, 'global_rank_compare');
     
    804799            if ($conf['representative_cache_on_level'])
    805800            {
    806               $user_representative_updates_for[ $user['id'].'#'.$category['id'] ] = $image_id;
     801              $user_representative_updates_for[ $category['id'] ] = $image_id;
    807802            }
    808803
     
    836831    $updates = array();
    837832
    838     foreach ($user_representative_updates_for as $user_cat => $image_id)
    839     {
    840       list($user_id, $cat_id) = explode('#', $user_cat);
    841 
     833    foreach ($user_representative_updates_for as $cat_id => $image_id)
     834    {
    842835      array_push(
    843836        $updates,
    844837        array(
    845           'user_id' => $user_id,
     838          'user_id' => $user['id'],
    846839          'cat_id' => $cat_id,
    847840          'user_representative_picture_id' => $image_id,
     
    879872  if ($params['tree_output'])
    880873  {
    881     return categories_flatlist_to_tree($cats);
    882   }
    883   else
    884   {
    885     return array(
    886       'categories' => new PwgNamedArray(
    887         $cats,
    888         'category',
    889         array(
    890           'id',
    891           'url',
    892           'nb_images',
    893           'total_nb_images',
    894           'nb_categories',
    895           'date_last',
    896           'max_date_last',
    897           )
    898         )
    899       );
    900   }
     874    $cats = categories_flatlist_to_tree($cats);
     875  }
     876
     877  return array(
     878    'categories' => new PwgNamedArray($cats, 'category', ws_std_get_category_xml_attributes())
     879    );
    901880}
    902881
     
    908887 * account.
    909888 */
    910 function ws_categories_getAdminList($params, &$service)
     889function ws_categories_getAdminList($params, $service)
    911890{
    912891  if (!is_admin())
     
    954933        )
    955934      );
    956     array_push($cats, $row);
     935    $cats[] = $row;
    957936  }
    958937
     
    976955 * returns detailed information for an element (web service method)
    977956 */
    978 function ws_images_addComment($params, &$service)
     957function ws_images_addComment($params, $service)
    979958{
    980959  if (!$service->isPost())
     
    10241003          'validation' => $comment_action=='validate',
    10251004        );
    1026       return new PwgNamedStruct(
    1027           'comment',
    1028           $ret,
    1029           null, array()
    1030         );
     1005      return array( 'comment' =>  new PwgNamedStruct($ret) );
    10311006    default:
    10321007      return new PwgError(500, "Unknown comment action ".$comment_action );
     
    10371012 * returns detailed information for an element (web service method)
    10381013 */
    1039 function ws_images_getInfo($params, &$service)
     1014function ws_images_getInfo($params, $service)
    10401015{
    10411016  global $user, $conf;
     
    11721147    {
    11731148      $row['id']=(int)$row['id'];
    1174       array_push($related_comments, $row);
     1149      $related_comments[] = $row;
    11751150    }
    11761151  }
     
    12021177  $ret['rates'] = array( WS_XML_ATTRIBUTES => $rating );
    12031178  $ret['categories'] = new PwgNamedArray($related_categories, 'category', array('id','url', 'page_url') );
    1204   $ret['tags'] = new PwgNamedArray($related_tags, 'tag', array('id','url_name','url','name','page_url') );
     1179  $ret['tags'] = new PwgNamedArray($related_tags, 'tag', ws_std_get_tag_xml_attributes() );
    12051180  if ( isset($comment_post_data) )
    12061181  {
    12071182    $ret['comment_post'] = array( WS_XML_ATTRIBUTES => $comment_post_data );
    12081183  }
    1209   $ret['comments'] = array(
    1210      WS_XML_ATTRIBUTES =>
    1211         array(
    1212           'page' => $params['comments_page'],
    1213           'per_page' => $params['comments_per_page'],
    1214           'count' => count($related_comments),
    1215           'nb_comments' => $nb_comments,
    1216         ),
    1217      WS_XML_CONTENT => new PwgNamedArray($related_comments, 'comment', array('id','date') )
    1218       );
    1219 
    1220   return new PwgNamedStruct('image',$ret, null, array('name','comment') );
     1184  $ret['comments_paging'] = new PwgNamedStruct( array(
     1185        'page' => $params['comments_page'],
     1186        'per_page' => $params['comments_per_page'],
     1187        'count' => count($related_comments),
     1188        'total_count' => $nb_comments,
     1189      ) );
     1190
     1191  $ret['comments'] = new PwgNamedArray($related_comments, 'comment', array('id','date') );
     1192
     1193  if ($service->_responseFormat != 'rest')
     1194    return $ret; // for backward compatibility only
     1195  else
     1196    return array( 'image' => new PwgNamedStruct($ret, null, array('name','comment') ) );
    12211197}
    12221198
     
    12251201 * rates the image_id in the parameter
    12261202 */
    1227 function ws_images_Rate($params, &$service)
     1203function ws_images_Rate($params, $service)
    12281204{
    12291205  $image_id = (int)$params['image_id'];
     
    12591235 * returns a list of elements corresponding to a query search
    12601236 */
    1261 function ws_images_search($params, &$service)
     1237function ws_images_search($params, $service)
    12621238{
    12631239  global $page;
     
    12801256      implode(' AND ', $where_clauses)
    12811257    );
     1258
     1259  $params['per_page'] = (int)$params['per_page'];
     1260  $params['page'] = (int)$params['page'];
    12821261
    12831262  $image_ids = array_slice(
     
    13161295  }
    13171296
    1318 
    1319   return array( 'images' =>
    1320     array (
    1321       WS_XML_ATTRIBUTES =>
    1322         array(
    1323             'page' => $params['page'],
    1324             'per_page' => $params['per_page'],
    1325             'count' => count($images)
    1326           ),
    1327        WS_XML_CONTENT => new PwgNamedArray($images, 'image',
    1328           ws_std_get_image_xml_attributes() )
    1329       )
     1297  return array (
     1298    'paging' => new PwgNamedStruct(
     1299      array(
     1300        'page' => $params['page'],
     1301        'per_page' => $params['per_page'],
     1302        'count' => count($images),
     1303        'total_count' => count($search_result['items']),
     1304        ) ),
     1305     'images' => new PwgNamedArray($images, 'image',
     1306        ws_std_get_image_xml_attributes() )
    13301307    );
    13311308}
    13321309
    1333 function ws_images_setPrivacyLevel($params, &$service)
     1310function ws_images_setPrivacyLevel($params, $service)
    13341311{
    13351312  if (!is_admin())
     
    13661343}
    13671344
    1368 function ws_images_setRank($params, &$service)
     1345function ws_images_setRank($params, $service)
    13691346{
    13701347  if (!is_admin())
     
    14781455}
    14791456
    1480 function ws_images_add_chunk($params, &$service)
     1457function ws_images_add_chunk($params, $service)
    14811458{
    14821459  global $conf;
     
    16371614}
    16381615
    1639 function ws_images_addFile($params, &$service)
     1616function ws_images_addFile($params, $service)
    16401617{
    16411618  ws_logfile(__FUNCTION__.', input :  '.var_export($params, true));
     
    17321709}
    17331710
    1734 function ws_images_add($params, &$service)
     1711function ws_images_add($params, $service)
    17351712{
    17361713  global $conf, $user;
     
    18901867}
    18911868
    1892 function ws_images_addSimple($params, &$service)
     1869function ws_images_addSimple($params, $service)
    18931870{
    18941871  global $conf;
     
    20272004}
    20282005
    2029 function ws_rates_delete($params, &$service)
     2006function ws_rates_delete($params, $service)
    20302007{
    20312008  global $conf;
     
    20692046 * perform a login (web service method)
    20702047 */
    2071 function ws_session_login($params, &$service)
     2048function ws_session_login($params, $service)
    20722049{
    20732050  global $conf;
     
    20882065 * performs a logout (web service method)
    20892066 */
    2090 function ws_session_logout($params, &$service)
     2067function ws_session_logout($params, $service)
    20912068{
    20922069  if (!is_a_guest())
     
    20972074}
    20982075
    2099 function ws_session_getStatus($params, &$service)
     2076function ws_session_getStatus($params, $service)
    21002077{
    21012078  global $user;
     
    21192096 * returns a list of tags (web service method)
    21202097 */
    2121 function ws_tags_getList($params, &$service)
     2098function ws_tags_getList($params, $service)
    21222099{
    21232100  $tags = get_available_tags();
     
    21412118      );
    21422119  }
    2143   return array('tags' => new PwgNamedArray($tags, 'tag', array('id','url_name','url', 'name', 'counter' )) );
     2120  return array('tags' => new PwgNamedArray($tags, 'tag', ws_std_get_tag_xml_attributes()) );
    21442121}
    21452122
     
    21512128 * account.
    21522129 */
    2153 function ws_tags_getAdminList($params, &$service)
     2130function ws_tags_getAdminList($params, $service)
    21542131{
    21552132  if (!is_admin())
     
    21632140      $tags,
    21642141      'tag',
    2165       array(
    2166         'name',
    2167         'id',
    2168         'url_name',
    2169         )
     2142      ws_std_get_tag_xml_attributes()
    21702143      )
    21712144    );
     
    21752148 * returns a list of images for tags (web service method)
    21762149 */
    2177 function ws_tags_getImages($params, &$service)
     2150function ws_tags_getImages($params, $service)
    21782151{
    21792152  global $conf;
     
    22032176    ws_std_image_sql_order($params) );
    22042177
    2205 
    2206   $image_ids = array_slice($image_ids, (int)($params['per_page']*$params['page']), (int)$params['per_page'] );
     2178  $count_set = count($image_ids);
     2179  $params['per_page'] = (int)$params['per_page'];
     2180  $params['page'] = (int)$params['page'];
     2181  $image_ids = array_slice($image_ids, $params['per_page']*$params['page'], $params['per_page'] );
    22072182
    22082183  $image_tag_map = array();
     
    22182193    {
    22192194      $row['image_id'] = (int)$row['image_id'];
    2220       array_push( $image_ids, $row['image_id'] );
     2195      $image_ids[] = $row['image_id'];
    22212196      $image_tag_map[ $row['image_id'] ] = explode(',', $row['tag_ids']);
    22222197    }
     
    22652240                )
    22662241              );
    2267         array_push($image_tags, array(
     2242        $image_tags[] = array(
    22682243                'id' => (int)$tag_id,
    22692244                'url' => $url,
    22702245                'page_url' => $page_url,
    2271               )
    2272             );
     2246              );
    22732247      }
    2274       $image['tags'] = new PwgNamedArray($image_tags, 'tag',
    2275               array('id','url_name','url','page_url')
    2276             );
    2277       array_push($images, $image);
     2248      $image['tags'] = new PwgNamedArray($image_tags, 'tag', ws_std_get_tag_xml_attributes() );
     2249      $images[] = $image;
    22782250    }
    22792251    usort($images, 'rank_compare');
     
    22812253  }
    22822254
    2283   return array( 'images' =>
    2284     array (
    2285       WS_XML_ATTRIBUTES =>
     2255  return array(
     2256      'paging' => new PwgNamedStruct(
    22862257        array(
    2287             'page' => $params['page'],
    2288             'per_page' => $params['per_page'],
    2289             'count' => count($images)
    2290           ),
    2291        WS_XML_CONTENT => new PwgNamedArray($images, 'image',
     2258          'page' => $params['page'],
     2259          'per_page' => $params['per_page'],
     2260          'count' => count($images),
     2261          'total_count' => $count_set,
     2262          ) ),
     2263       'images' => new PwgNamedArray($images, 'image',
    22922264          ws_std_get_image_xml_attributes() )
    2293       )
    22942265    );
    22952266}
    22962267
    2297 function ws_categories_add($params, &$service)
     2268function ws_categories_add($params, $service)
    22982269{
    22992270  if (!is_admin())
     
    23422313}
    23432314
    2344 function ws_tags_add($params, &$service)
     2315function ws_tags_add($params, $service)
    23452316{
    23462317  if (!is_admin())
     
    23612332}
    23622333
    2363 function ws_images_exist($params, &$service)
     2334function ws_images_exist($params, $service)
    23642335{
    23652336  ws_logfile(__FUNCTION__.' '.var_export($params, true));
     
    24412412}
    24422413
    2443 function ws_images_checkFiles($params, &$service)
     2414function ws_images_checkFiles($params, $service)
    24442415{
    24452416  ws_logfile(__FUNCTION__.', input :  '.var_export($params, true));
     
    25142485}
    25152486
    2516 function ws_images_setInfo($params, &$service)
     2487function ws_images_setInfo($params, $service)
    25172488{
    25182489  global $conf;
     
    26612632}
    26622633
    2663 function ws_images_delete($params, &$service)
     2634function ws_images_delete($params, $service)
    26642635{
    26652636  global $conf;
     
    28522823}
    28532824
    2854 function ws_categories_setInfo($params, &$service)
     2825function ws_categories_setInfo($params, $service)
    28552826{
    28562827  global $conf;
     
    29102881}
    29112882
    2912 function ws_categories_setRepresentative($params, &$service)
     2883function ws_categories_setRepresentative($params, $service)
    29132884{
    29142885  global $conf;
     
    29822953}
    29832954
    2984 function ws_categories_delete($params, &$service)
     2955function ws_categories_delete($params, $service)
    29852956{
    29862957  global $conf;
     
    30503021}
    30513022
    3052 function ws_categories_move($params, &$service)
     3023function ws_categories_move($params, $service)
    30533024{
    30543025  global $conf, $page;
     
    31863157}
    31873158
    3188 function ws_images_checkUpload($params, &$service)
     3159function ws_images_checkUpload($params, $service)
    31893160{
    31903161  global $conf;
     
    32073178}
    32083179
    3209 function ws_plugins_getList($params, &$service)
     3180function ws_plugins_getList($params, $service)
    32103181{
    32113182  global $conf;
     
    32323203    }
    32333204
    3234     array_push(
    3235       $plugin_list,
     3205    $plugin_list[] =
    32363206      array(
    32373207        'id' => $plugin_id,
     
    32403210        'state' => $state,
    32413211        'description' => $fs_plugin['description'],
    3242         )
    3243       );
     3212        );
    32443213  }
    32453214
     
    32813250}
    32823251
    3283 function ws_themes_performAction($params, &$service)
     3252function ws_themes_performAction($params, $service)
    32843253{
    32853254  global $template;
     
    33143283}
    33153284
    3316 function ws_extensions_update($params, &$service)
     3285function ws_extensions_update($params, $service)
    33173286{
    33183287  if (!is_webmaster())
     
    34043373}
    34053374
    3406 function ws_extensions_ignoreupdate($params, &$service)
     3375function ws_extensions_ignoreupdate($params, $service)
    34073376{
    34083377  global $conf;
     
    34583427}
    34593428
    3460 function ws_extensions_checkupdates($params, &$service)
     3429function ws_extensions_checkupdates($params, $service)
    34613430{
    34623431  global $conf;
Note: See TracChangeset for help on using the changeset viewer.