Changeset 4444


Ignore:
Timestamp:
Dec 7, 2009, 11:28:40 PM (14 years ago)
Author:
plg
Message:

feature 1312 added: pwg.images.setInfo can replace multiple values propreties
(tags/categories). The primary condition is to provide "tag_ids" and/or
"categories" input parameters, ie pwg.images.set won't create emptiness. You
can reduce the set of tags/categories but not remove all tags/categories.

bug 1277 fixed: with a stronger algorithm for adding/replacing categories, we
now avoid to recreate an existing image_category association.

When a remote client calls pwg.images.setInfo, Piwigo returns an error 500 if:

  1. the "categories" parameter is malformed (no numeric id inside)
  2. one of the input categories does not exist at database level.
Location:
branches/2.0
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/2.0/include/ws_functions.inc.php

    r4424 r4444  
    17311731    ws_add_image_category_relations(
    17321732      $params['image_id'],
    1733       $params['categories']
     1733      $params['categories'],
     1734      $params['replace_mode']
    17341735      );
    17351736  }
     
    17391740  {
    17401741    include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
    1741     add_tags(
    1742       explode(',', $params['tag_ids']),
    1743       array($params['image_id'])
    1744       );
     1742
     1743    $tag_ids = explode(',', $params['tag_ids']);
     1744
     1745    if ($params['replace_mode'])
     1746    {
     1747      set_tags(
     1748        $tag_ids,
     1749        $params['image_id']
     1750        );
     1751    }
     1752    else
     1753    {
     1754      add_tags(
     1755        $tag_ids,
     1756        array($params['image_id'])
     1757        );
     1758    }
    17451759  }
    17461760
     
    17481762}
    17491763
    1750 function ws_add_image_category_relations($image_id, $categories_string)
     1764function ws_add_image_category_relations($image_id, $categories_string, $replace_mode=false)
    17511765{
    17521766  // let's add links between the image and the categories
     
    17661780    @list($cat_id, $rank) = explode(',', $token);
    17671781
     1782    if (!preg_match('/^\d+$/', $cat_id))
     1783    {
     1784      continue;
     1785    }
     1786
    17681787    array_push($cat_ids, $cat_id);
    17691788
     
    17821801  $cat_ids = array_unique($cat_ids);
    17831802
    1784   if (count($cat_ids) > 0)
    1785   {
    1786     if ($search_current_ranks)
     1803  if (count($cat_ids) == 0)
     1804  {
     1805    new PwgError(
     1806      500,
     1807      '[ws_add_image_category_relations] there is no category defined in "'.$categories_string.'"'
     1808      );
     1809    exit();
     1810  }
     1811 
     1812  $query = '
     1813SELECT
     1814    id
     1815  FROM '.CATEGORIES_TABLE.'
     1816  WHERE id IN ('.implode(',', $cat_ids).')
     1817;';
     1818  $db_cat_ids = array_from_query($query, 'id');
     1819
     1820  $unknown_cat_ids = array_diff($cat_ids, $db_cat_ids);
     1821  if (count($unknown_cat_ids) != 0)
     1822  {
     1823    new PwgError(
     1824      500,
     1825      '[ws_add_image_category_relations] the following categories are unknown: '.implode(', ', $unknown_cat_ids)
     1826      );
     1827    exit();
     1828  }
     1829 
     1830  $to_update_cat_ids = array();
     1831   
     1832  // in case of replace mode, we first check the existing associations
     1833  $query = '
     1834SELECT
     1835    category_id
     1836  FROM '.IMAGE_CATEGORY_TABLE.'
     1837  WHERE image_id = '.$image_id.'
     1838;';
     1839  $existing_cat_ids = array_from_query($query, 'category_id');
     1840
     1841  if ($replace_mode)
     1842  {
     1843    $to_remove_cat_ids = array_diff($existing_cat_ids, $cat_ids);
     1844    if (count($to_remove_cat_ids) > 0)
    17871845    {
    17881846      $query = '
     1847DELETE
     1848  FROM '.IMAGE_CATEGORY_TABLE.'
     1849  WHERE image_id = '.$image_id.'
     1850    AND category_id IN ('.implode(', ', $to_remove_cat_ids).')
     1851;';
     1852      pwg_query($query);
     1853      update_category($to_remove_cat_ids);
     1854    }
     1855  }
     1856 
     1857  $new_cat_ids = array_diff($cat_ids, $existing_cat_ids);
     1858  if (count($new_cat_ids) == 0)
     1859  {
     1860    return true;
     1861  }
     1862   
     1863  if ($search_current_ranks)
     1864  {
     1865    $query = '
    17891866SELECT
    17901867    category_id,
     
    17921869  FROM '.IMAGE_CATEGORY_TABLE.'
    17931870  WHERE rank IS NOT NULL
    1794     AND category_id IN ('.implode(',', $cat_ids).')
     1871    AND category_id IN ('.implode(',', $new_cat_ids).')
    17951872  GROUP BY category_id
    17961873;';
    1797       $current_rank_of = simple_hash_from_query(
    1798         $query,
    1799         'category_id',
    1800         'max_rank'
    1801         );
    1802 
    1803       foreach ($cat_ids as $cat_id)
     1874    $current_rank_of = simple_hash_from_query(
     1875      $query,
     1876      'category_id',
     1877      'max_rank'
     1878      );
     1879
     1880    foreach ($new_cat_ids as $cat_id)
     1881    {
     1882      if (!isset($current_rank_of[$cat_id]))
    18041883      {
    1805         if (!isset($current_rank_of[$cat_id]))
    1806         {
    1807           $current_rank_of[$cat_id] = 0;
    1808         }
    1809 
    1810         if ('auto' == $rank_on_category[$cat_id])
    1811         {
    1812           $rank_on_category[$cat_id] = $current_rank_of[$cat_id] + 1;
    1813         }
     1884        $current_rank_of[$cat_id] = 0;
    18141885      }
    1815     }
    1816 
    1817     $inserts = array();
    1818 
    1819     foreach ($cat_ids as $cat_id)
    1820     {
    1821       array_push(
    1822         $inserts,
    1823         array(
    1824           'image_id' => $image_id,
    1825           'category_id' => $cat_id,
    1826           'rank' => $rank_on_category[$cat_id],
    1827           )
    1828         );
    1829     }
    1830 
    1831     include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
    1832     mass_inserts(
    1833       IMAGE_CATEGORY_TABLE,
    1834       array_keys($inserts[0]),
    1835       $inserts
    1836       );
    1837 
    1838     update_category($cat_ids);
    1839   }
     1886     
     1887      if ('auto' == $rank_on_category[$cat_id])
     1888      {
     1889        $rank_on_category[$cat_id] = $current_rank_of[$cat_id] + 1;
     1890      }
     1891    }
     1892  }
     1893 
     1894  $inserts = array();
     1895 
     1896  foreach ($new_cat_ids as $cat_id)
     1897  {
     1898    array_push(
     1899      $inserts,
     1900      array(
     1901        'image_id' => $image_id,
     1902        'category_id' => $cat_id,
     1903        'rank' => $rank_on_category[$cat_id],
     1904        )
     1905      );
     1906  }
     1907 
     1908  include_once(PHPWG_ROOT_PATH.'admin/include/functions.php');
     1909  mass_inserts(
     1910    IMAGE_CATEGORY_TABLE,
     1911    array_keys($inserts[0]),
     1912    $inserts
     1913    );
     1914 
     1915  update_category($new_cat_ids);
    18401916}
    18411917
  • branches/2.0/ws.php

    r4345 r4444  
    290290        'maxValue' => $conf['available_permission_levels']
    291291        ),
     292      'replace_mode' => array('default' => false),
    292293      ),
    293294    'POST method only. Admin only
Note: See TracChangeset for help on using the changeset viewer.