Guest User

Untitled

a guest
Jul 15th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. <?php
  2. /**
  3. * @package Habari
  4. *
  5. */
  6.  
  7. /**
  8. * Habari Tags Class
  9. *
  10. */
  11. class Tags extends ArrayObject
  12. {
  13. protected $get_param_cache; // Stores info about the last set of data fetched that was not a single value
  14.  
  15. /**
  16. * Returns a tag or tags based on supplied parameters.
  17. * @todo This class should cache query results!
  18. *
  19. * @param array $paramarray An associated array of parameters, or a querystring
  20. * @return array An array of Tag objects, or a single Tag object, depending on request
  21. **/
  22. public static function get( $paramarray = array() )
  23. {
  24. $params = array();
  25. $fns = array( 'get_results', 'get_row', 'get_value' );
  26. $select = '';
  27. // what to select -- by default, everything
  28. foreach ( Tag::default_fields() as $field => $value ) {
  29. $select .= ( '' == $select )
  30. ? "{tags}.$field"
  31. : ", {tags}.$field";
  32. }
  33. // defaults
  34. $orderby = 'id ASC';
  35. $nolimit = TRUE;
  36.  
  37. // Called with no parameters. Assume we want all tags
  38. if( 0 == count( $paramarray ) ) {
  39. $paramarray['orderby'] = 'tag_text ASC';
  40.  
  41. }
  42.  
  43. // Put incoming parameters into the local scope
  44. $paramarray = Utils::get_params( $paramarray );
  45.  
  46. // Transact on possible multiple sets of where information that is to be OR'ed
  47. if ( isset( $paramarray['where'] ) && is_array( $paramarray['where'] ) ) {
  48. $wheresets = $paramarray['where'];
  49. }
  50. else {
  51. $wheresets = array( array() );
  52. }
  53.  
  54. $wheres = array();
  55. $join = '';
  56. if ( isset( $paramarray['where'] ) && is_string( $paramarray['where'] ) ) {
  57. $wheres[] = $paramarray['where'];
  58. }
  59. else {
  60. foreach( $wheresets as $paramset ) {
  61. // safety mechanism to prevent empty queries
  62. $where = array();
  63. $paramset = array_merge((array) $paramarray, (array) $paramset);
  64.  
  65. $default_fields = Tag::default_fields();
  66. foreach ( Tag::default_fields() as $field => $scrap ) {
  67. if ( !isset( $paramset[$field] ) ) {
  68. continue;
  69. }
  70. switch ( $field ) {
  71. case 'id':
  72. if ( !is_numeric( $paramset[$field] ) ) {
  73. continue;
  74. }
  75. default:
  76. $where[] = "{$field}= ?";
  77. $params[] = $paramset[$field];
  78. }
  79. }
  80.  
  81. if(count($where) > 0) {
  82. $wheres[] = ' (' . implode( ' AND ', $where ) . ') ';
  83. }
  84. }
  85. }
  86.  
  87. // Get any full-query parameters
  88. $possible = array( 'fetch_fn', 'count', 'nolimit', 'limit', 'offset' );
  89. foreach ( $possible as $varname ) {
  90. if ( isset( $paramarray[$varname] ) ) {
  91. $$varname = $paramarray[$varname];
  92. }
  93. }
  94.  
  95. if ( isset( $fetch_fn ) ) {
  96. if ( ! in_array( $fetch_fn, $fns ) ) {
  97. $fetch_fn = $fns[0];
  98. }
  99. }
  100. else {
  101. $fetch_fn = $fns[0];
  102. }
  103.  
  104. // is a count being request?
  105. if ( isset( $count ) ) {
  106. $select = "COUNT($count)";
  107. $fetch_fn = 'get_value';
  108. $orderby = '';
  109. }
  110. if ( isset( $limit ) ) {
  111. $limit = " LIMIT $limit";
  112. if ( isset( $offset ) ) {
  113. $limit .= " OFFSET $offset";
  114. }
  115. }
  116. if ( isset( $nolimit ) ) {
  117. $limit = '';
  118. }
  119.  
  120. $query = '
  121. SELECT ' . $select
  122. . ' FROM {tags} '
  123. . $join;
  124.  
  125. if ( count( $wheres ) > 0 ) {
  126. $query .= ' WHERE ' . implode( " \nOR\n ", $wheres );
  127. }
  128. $query .= ( ($orderby == '') ? '' : ' ORDER BY ' . $orderby ) . $limit;
  129. //Utils::debug($paramarray, $fetch_fn, $query, $params);
  130.  
  131. DB::set_fetch_mode(PDO::FETCH_CLASS);
  132. DB::set_fetch_class('Tag');
  133. $results = DB::$fetch_fn( $query, $params, 'Tag' );
  134.  
  135. if ( 'get_results' != $fetch_fn ) {
  136. // return the results
  137. return $results;
  138. }
  139. elseif ( is_array( $results ) ) {
  140. $c = __CLASS__;
  141. $return_value = new $c( $results );
  142. $return_value->get_param_cache = $paramarray;
  143. return $return_value;
  144. }
  145. }
  146.  
  147. /**
  148. * Return a tag based on an id, tag text or slug
  149. *
  150. * @return QueryRecord A tag QueryRecord
  151. **/
  152. public static function get_one($tag)
  153. {
  154. $params = array();
  155. if( is_numeric( $tag ) ) {
  156. $params['id'] = $tag;
  157. }
  158. else {
  159. $params['tag_slug'] = Utils::slugify( $tag );
  160. }
  161. $params['limit'] = 1;
  162. $params['fetch_fn'] = 'get_row';
  163. return Tags::get( $params );
  164. }
  165.  
  166. /**
  167. * Deletes a tag
  168. *
  169. * @param Tag tag The tag to be deleted
  170. **/
  171. public static function delete($tag)
  172. {
  173. $tag->delete();
  174. }
  175.  
  176. /**
  177. * TODO: be more careful
  178. * INSERT INTO {tag2post} / SELECT $master_tag->ID,post_ID FROM {tag2post} WHERE tag_id = $tag->id" and then "DELETE FROM {tag2post} WHERE tag_id = $tag->id"
  179. * Renames tags.
  180. * If the master tag exists, the tags will be merged with it.
  181. * If not, it will be created first.
  182. *
  183. * @param Array tags The tag text, slugs or ids to be renamed
  184. * @param mixed master The Tag to which they should be renamed, or the slug, text or id of it
  185. **/
  186. public static function rename($master, $tags)
  187. {
  188. $tags = Utils::single_array( $tags );
  189. $tag_names = array();
  190. $post_ids = array();
  191.  
  192. // get array of existing tags first to make sure we don't conflict with a new master tag
  193. foreach ( $tags as $tag ) {
  194.  
  195. $posts = array();
  196. // $post_ids = array();
  197. $tag = Tags::get_one( $tag );
  198.  
  199. // get all the post ID's tagged with this tag
  200. $posts = DB::get_results( 'SELECT post_id FROM {tag2post} WHERE tag_id = ?', array( $tag->id ) );
  201.  
  202. if ( count( $posts ) > 0 ) {
  203.  
  204. // build a list of all the post_id's we need for the new tag
  205. foreach ( $posts as $post ) {
  206. $post_ids[] = $post->post_id;
  207. }
  208. $tag_names[] = $tag->tag;
  209. }
  210.  
  211. Tags::delete( $tag );
  212. }
  213.  
  214. // get the master tag
  215. $master_tag = Tags::get_one($master);
  216.  
  217. if ( !isset($master_tag->slug) ) {
  218. // it didn't exist, so we assume it's tag text and create it
  219. $master_tag = Tag::create(array('tag_slug' => Utils::slugify($master), 'tag_text' => $master));
  220.  
  221. $master_ids = array();
  222. }
  223. else {
  224. // get the posts the tag is already on so we don't duplicate them
  225. $master_posts = DB::get_results( 'SELECT post_id FROM {tag2post} WHERE tag_id = ?', array( $master_tag->id ) );
  226.  
  227. $master_ids = array();
  228.  
  229. foreach ( $master_posts as $master_post ) {
  230. $master_ids[] = $master_post->post_id;
  231. }
  232.  
  233. }
  234.  
  235. if ( count( $post_ids ) > 0 ) {
  236.  
  237. // only try and add the master tag to posts it's not already on
  238. $post_ids = array_diff( $post_ids, $master_ids );
  239.  
  240. // link the master tag to each distinct post we removed tags from
  241. foreach ( $post_ids as $post_id ) {
  242.  
  243. DB::query( 'INSERT INTO {tag2post} ( tag_id, post_id ) VALUES ( ?, ? )', array( $master_tag->id, $post_id ) );
  244.  
  245. }
  246.  
  247. }
  248. EventLog::log(sprintf(
  249. _n('Tag %s has been renamed to %s.',
  250. 'Tags %s have been renamed to %s.',
  251. count($tags)
  252. ), implode($tag_names, ', '), $master ), 'info', 'tag', 'habari'
  253. );
  254.  
  255. }
  256.  
  257. /**
  258. * Returns the number of times the most used tag is used.
  259. *
  260. * @return int The number of times the most used tag is used.
  261. **/
  262. public static function max_count()
  263. {
  264. return DB::get_value( 'SELECT count( t2.post_id ) AS max FROM {tags} t, {tag2post} t2 WHERE t2.tag_id = t.id GROUP BY t.id ORDER BY max DESC LIMIT 1' );
  265. }
  266.  
  267. /**
  268. * Returns the count of times a tag is used.
  269. *
  270. * @param mixed The tag to count usage.
  271. * @return int The number of times a tag is used.
  272. **/
  273. public static function post_count($tag)
  274. {
  275. $params = array();
  276. $params['fetch_fn'] = 'get_row';
  277. if ( is_int( $tag ) ) {
  278. $params['id'] = $tag;
  279. }
  280. else if ( is_string( $tag ) ) {
  281. $params['tag_slug'] = Utils::slugify( $tag );
  282. }
  283. $tag = Tags::get( $params );
  284. return $tag->count;
  285. }
  286.  
  287. public static function get_by_text($tag)
  288. {
  289. return Tags::get( array( 'tag_text' => $tag, 'fetch_fn' => 'get_row', 'limit' => 1 ) );
  290. }
  291.  
  292. public static function get_by_slug($tag)
  293. {
  294. return Tags::get( array( 'tag_slug' => $tag, 'fetch_fn' => 'get_row', 'limit' => 1 ) );
  295. }
  296.  
  297. /**
  298. * Returns a Tag object based on a supplied ID
  299. *
  300. * @param tag_id The ID of the tag to retrieve
  301. * @return A Tag object
  302. */
  303. public static function get_by_id( $tag )
  304. {
  305. return Tags::get( array( 'id' => $tag, 'fetch_fn' => 'get_row', 'limit' => 1 ) );
  306. }
  307. }
  308. ?>
Add Comment
Please, Sign In to add comment