ruby-wordpress 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 38f5112cc2886be6398ae4573f806025b0b8b6c9
4
+ data.tar.gz: 3e4dbf0b9de205b21e7f33407d01b62985e02675
5
+ SHA512:
6
+ metadata.gz: 9f379fd8208d157b17811e2192a9101ebd54584177ddbbb517e8a4b8aaf4d88be76e2611ae95c11468997accaab6ed0b04f95dd779148a5d5ffd78e4d7b44ca3
7
+ data.tar.gz: 8d095d6e1475059603430adebc374e8162946a85c0e835960a90c426d1ddbc6576c7aab288383c0b21d13ee5ee08e5348e018613b3eaf4404d787803aeccc62e
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
18
+ .DS_*
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby-wordpress.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Keitaroh Kobayashi
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # Ruby/WordPress
2
+
3
+ Access your WordPress database with Ruby.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ruby-wordpress', :require => 'wordpress'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ruby-wordpress
18
+
19
+ ## Usage
20
+
21
+ require 'wordpress'
22
+ wp = WordPress.new options
23
+
24
+ ## Initialization options
25
+
26
+ A symbol hash.
27
+
28
+ See the [mysql2 connection options](https://github.com/brianmario/mysql2#connection-options).
29
+
30
+ Additional options:
31
+
32
+ * `:wordpress_prefix` (default: 'wp_')
33
+
34
+ ## Changelog
35
+
36
+ ### 0.0.1
37
+
38
+ * Initial public release
39
+ * Basic SQL functions (WordPress::Base)
40
+ * `wp_options` accessor
41
+
42
+ ## Contributing
43
+
44
+ 1. Fork it
45
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
46
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
47
+ 4. Push to the branch (`git push origin my-new-feature`)
48
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/wordpress.rb ADDED
@@ -0,0 +1,31 @@
1
+ require "wordpress/version"
2
+
3
+ require 'mysql2'
4
+ require 'php_serialize'
5
+ require 'cgi'
6
+
7
+ class WordPress
8
+ def initialize(options)
9
+ # Symbolize keys
10
+ options = options.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
11
+
12
+ options[:wordpress_prefix] ||= 'wp_'
13
+ @tbl = {
14
+ terms: options[:wordpress_prefix] + 'terms',
15
+ termtax: options[:wordpress_prefix] + 'term_taxonomy',
16
+ termrel: options[:wordpress_prefix] + 'term_relationships',
17
+ posts: options[:wordpress_prefix] + 'posts',
18
+ postmeta: options[:wordpress_prefix] + 'postmeta',
19
+ options: options[:wordpress_prefix] + 'options',
20
+ prefix: options[:wordpress_prefix]
21
+ }
22
+
23
+ @conn = Mysql2::Client.new options
24
+ @conn.query_options.merge!(symbolize_keys: true)
25
+
26
+ # The WordPress options table
27
+ @options = WP::Options.new @conn, @tbl
28
+ end
29
+
30
+ attr_reader :options
31
+ end
@@ -0,0 +1,107 @@
1
+ # Encoding: UTF-8
2
+
3
+ require 'unicode_utils/nfc'
4
+
5
+ class WordPress::Base
6
+ def initialize(connection, wp_tables)
7
+ @tbl = wp_tables
8
+ @conn = connection
9
+ end
10
+
11
+ def insert(table, content)
12
+ return nil if content.keys.length == 0
13
+
14
+ fields = content.keys.map { |e| "`#{@conn.escape e.to_s}`" }
15
+ values = content.keys.map { |e| "'#{@conn.escape content[e].to_s}'" }
16
+
17
+ @conn.query("INSERT INTO `#{@conn.escape table}` (#{fields.join ', '}) VALUES (#{values.join ', '})")
18
+ @conn.last_id
19
+ end
20
+
21
+ def update(table, where, content)
22
+ return nil if content.keys.length == 0
23
+
24
+ fields = content.keys.map { |e| "`#{@conn.escape e.to_s}`" }
25
+ result = @conn.query("SELECT #{fields.join ', '} FROM `#{@conn.escape table}` WHERE #{where}")
26
+ return false if result.count == 0
27
+
28
+ if content.respond_to?(:diff)
29
+ row = result.to_a[0]
30
+ diff = content.diff row
31
+
32
+ # Already up-to-date
33
+ return true if diff.count == 0
34
+ else
35
+ diff = content
36
+ end
37
+
38
+ # Let's update the difference
39
+ statements = diff.keys.map { |e| "`#{@conn.escape e.to_s}`='#{@conn.escape content[e].to_s}'" }
40
+ @conn.query("UPDATE `#{@conn.escape table}` SET #{statements.join ', ' } WHERE #{where}")
41
+ true
42
+ end
43
+
44
+ def update_or_insert(table, where, content)
45
+ return nil if content.keys.length == 0
46
+
47
+ unless update table, where, content
48
+ insert table, content
49
+ end
50
+ end
51
+
52
+ def get_post_meta(id, meta_key)
53
+ (@conn.query("SELECT `meta_value` FROM `#{@tbl[:postmeta]}` WHERE `post_id`='#{id.to_i}' AND `meta_key`='#{@conn.escape meta_key.to_s}'").first || {})[:meta_value]
54
+ end
55
+
56
+ def set_post_meta(id, meta_key, meta_value)
57
+ update_or_insert $tbl[:postmeta], "`post_id`='#{id.to_i}' AND `meta_key`='#{@conn.escape meta_key.to_s}'", {
58
+ post_id: id,
59
+ meta_key: meta_key.to_s,
60
+ meta_value: meta_value.to_s
61
+ }
62
+ end
63
+
64
+ def get_the_terms(id, taxonomy)
65
+ terms = @conn.query("SELECT `#{@tbl[:termtax]}`.`taxonomy`, `#{@tbl[:terms]}`.`name` FROM `#{@tbl[:posts]}`, `#{@tbl[:termtax]}`, `#{@tbl[:termrel]}`, `#{@tbl[:terms]}` WHERE `#{@tbl[:posts]}`.`ID` = '#{id.to_i}' AND `#{@tbl[:termrel]}`.`object_id` = `#{@tbl[:posts]}`.`ID` AND `#{@tbl[:termrel]}`.`term_taxonomy_id` = `#{@tbl[:termtax]}`.`term_taxonomy_id` AND `#{@tbl[:termtax]}`.`taxonomy` = '#{@conn.escape taxonomy}' AND `#{@tbl[:terms]}`.`term_id` = `#{@tbl[:termtax]}`.`term_id`")
66
+ terms.map { |e| e[:name] }
67
+ end
68
+
69
+ def set_post_terms(post_id, terms, taxonomy, append=false)
70
+ terms.map! { |e| UnicodeUtils.nfc(e) }
71
+ terms_esc = terms.map { |e| "'#{@conn.escape e.to_s}'" }
72
+ terms_slugs = terms.map { |e| "'#{@conn.escape(CGI::escape e.to_s)}'"}
73
+ raise ArgumentError, 'Terms must be an array with more than zero elements' unless terms_esc.count > 0
74
+ # Cache post terms and term IDs
75
+ termtax_rel = Hash[@conn.query("SELECT `#{@tbl[:termtax]}`.`term_taxonomy_id`, `#{@tbl[:terms]}`.`name` FROM `#{@tbl[:terms]}`, `#{@tbl[:termtax]}` WHERE (`#{@tbl[:terms]}`.`name` IN (#{ terms_esc.join ', ' }) OR `#{@tbl[:terms]}`.`slug` IN (#{ terms_slugs.join ', ' })) AND `#{@tbl[:terms]}`.`term_id` = `#{@tbl[:termtax]}`.`term_id` AND `#{@tbl[:termtax]}`.`taxonomy` = '#{@conn.escape taxonomy}' GROUP BY `#{@tbl[:terms]}`.`name`").map { |e| [e[:name], e[:term_taxonomy_id]] }]
76
+
77
+ (terms - termtax_rel.keys).each do |x|
78
+ # These are terms that do not exist yet
79
+ term_id = (@conn.query("SELECT `term_id` FROM `#{@tbl[:terms]}` WHERE `name`='#{@conn.escape x}' AND `slug`='#{@conn.escape(CGI::escape x)}' LIMIT 1").first || {})[:term_id]
80
+ unless term_id
81
+ @conn.query("INSERT INTO `#{@tbl[:terms]}` (`name`, `slug`, `term_group`) VALUES ('#{@conn.escape x}', '#{@conn.escape(CGI::escape x)}', '0')")
82
+ term_id = @conn.last_id
83
+ end
84
+ termtax_id = (@conn.query("SELECT `term_taxonomy_id` FROM `#{@tbl[:termtax]}` WHERE `term_id`='#{term_id.to_i}' AND `taxonomy`='#{@conn.escape taxonomy}' LIMIT 1").first || {})[:term_taxonomy_id]
85
+ unless termtax_id
86
+ @conn.query("INSERT INTO `#{@tbl[:termtax]}` (`term_id`, `taxonomy`, `parent`, `count`) VALUES ('#{term_id.to_i}', '#{@conn.escape taxonomy}', '0', '0')")
87
+ termtax_id = @conn.last_id
88
+ end
89
+ termtax_rel[x] = termtax_id
90
+ end
91
+
92
+ termtax_to_add = terms
93
+
94
+ if !append
95
+ # Delete all associations first
96
+ @conn.query("DELETE `#{@tbl[:termrel]}` FROM `#{@tbl[:termrel]}` JOIN `#{@tbl[:termtax]}` ON `#{@tbl[:termrel]}`.`term_taxonomy_id`=`#{@tbl[:termtax]}`.`term_taxonomy_id` WHERE `#{@tbl[:termtax]}`.`taxonomy`='#{@conn.escape taxonomy}' AND `#{@tbl[:termrel]}`.`object_id` = '#{post_id.to_i}'")
97
+ else
98
+ currently_associated = @conn.query("SELECT `#{@tbl[:termrel]}`.`term_taxonomy_id` FROM `#{@tbl[:termrel]}`, `#{@tbl[:termtax]}` WHERE `#{@tbl[:termrel]}`.`object_id` = '#{post_id.to_i}' AND `#{@tbl[:termrel]}`.`term_taxonomy_id` = `#{@tbl[:termtax]}`.`term_taxonomy_id` AND `#{@tbl[:termtax]}`.`taxonomy` = '#{@conn.escape taxonomy}'").map { |e| e[:term_taxonomy_id] }
99
+ termtax_to_add -= currently_associated
100
+ end
101
+
102
+ termtax_to_add.each do |term|
103
+ @conn.query("INSERT INTO `#{@tbl[:termrel]}` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES ('#{post_id.to_i}', '#{termtax_rel[term].to_i}', '0')")
104
+ end
105
+ end
106
+ end
107
+
@@ -0,0 +1,29 @@
1
+ # Encoding: UTF-8
2
+
3
+ class WordPress::Options < WordPress::Base
4
+ def [](key)
5
+ v = nil
6
+ @conn.query("SELECT `option_value` FROM `#{@tbl[:options]}` WHERE `option_name`='#{@conn.escape key}' LIMIT 1").each do |row|
7
+ v = row[:option_value]
8
+ end
9
+ v
10
+ end
11
+
12
+ def []=(key, value)
13
+ old_value = nil
14
+ @conn.query("SELECT `option_value` FROM `#{@tbl[:options]}` WHERE `option_name`='#{@conn.escape key}' LIMIT 1").each do |row|
15
+ old_value = row[:option_value]
16
+ end
17
+ if !value.nil? and !old_value.nil? and value != old_value
18
+ # Update operation.
19
+ @conn.query("UPDATE `#{@tbl[:options]}` SET `option_value`='#{@conn.escape value}' WHERE `option_name`='#{@conn.escape key}'")
20
+ elsif value.nil? and !old_value.nil?
21
+ # New value nil, old value not. Delete operation.
22
+ @conn.query("DELETE FROM `#{@tbl[:options]}` WHERE `option_name`='#{@conn.escape key}'")
23
+ elsif !value.nil? and old_value.nil?
24
+ # New value non-nil, old value nil. Insert operation.
25
+ @conn.query("INSERT INTO `#{@tbl[:options]}` (`option_name`, `option_value`, `autoload`) VALUES ('#{@conn.escape key}', '#{@conn.escape value.to_s}', 'no')")
26
+ end
27
+ value
28
+ end
29
+ end
@@ -0,0 +1,5 @@
1
+ class WordPress
2
+ module Version
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'wordpress/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "ruby-wordpress"
8
+ spec.version = WordPress::Version::VERSION
9
+ spec.authors = ["Keitaroh Kobayashi"]
10
+ spec.email = ["keita@kkob.us"]
11
+ spec.description = %q{A gem to interface with the WordPress database}
12
+ spec.summary = %q{A gem to interface with the WordPress database}
13
+ spec.homepage = "https://github.com/keichan34/ruby-wordpress"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_dependency "mysql2", "~> 0.3.11"
22
+ spec.add_dependency "unicode_utils", "~> 1.4.0"
23
+ spec.add_dependency "k-php-serialize", "~> 1.2.0"
24
+
25
+ spec.add_development_dependency "bundler", "~> 1.3"
26
+ spec.add_development_dependency "rake"
27
+ end
metadata ADDED
@@ -0,0 +1,124 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-wordpress
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Keitaroh Kobayashi
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-05-27 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mysql2
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: 0.3.11
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: 0.3.11
27
+ - !ruby/object:Gem::Dependency
28
+ name: unicode_utils
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ~>
32
+ - !ruby/object:Gem::Version
33
+ version: 1.4.0
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ~>
39
+ - !ruby/object:Gem::Version
40
+ version: 1.4.0
41
+ - !ruby/object:Gem::Dependency
42
+ name: k-php-serialize
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ~>
46
+ - !ruby/object:Gem::Version
47
+ version: 1.2.0
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: 1.2.0
55
+ - !ruby/object:Gem::Dependency
56
+ name: bundler
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ~>
60
+ - !ruby/object:Gem::Version
61
+ version: '1.3'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ~>
67
+ - !ruby/object:Gem::Version
68
+ version: '1.3'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - '>='
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - '>='
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: A gem to interface with the WordPress database
84
+ email:
85
+ - keita@kkob.us
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - .gitignore
91
+ - Gemfile
92
+ - LICENSE.txt
93
+ - README.md
94
+ - Rakefile
95
+ - lib/wordpress.rb
96
+ - lib/wordpress/base.rb
97
+ - lib/wordpress/options.rb
98
+ - lib/wordpress/version.rb
99
+ - ruby-wordpress.gemspec
100
+ homepage: https://github.com/keichan34/ruby-wordpress
101
+ licenses:
102
+ - MIT
103
+ metadata: {}
104
+ post_install_message:
105
+ rdoc_options: []
106
+ require_paths:
107
+ - lib
108
+ required_ruby_version: !ruby/object:Gem::Requirement
109
+ requirements:
110
+ - - '>='
111
+ - !ruby/object:Gem::Version
112
+ version: '0'
113
+ required_rubygems_version: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - '>='
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ requirements: []
119
+ rubyforge_project:
120
+ rubygems_version: 2.0.3
121
+ signing_key:
122
+ specification_version: 4
123
+ summary: A gem to interface with the WordPress database
124
+ test_files: []