Skip to content

Commit

Permalink
CopyPatrol: use lang columns to add support for other wikipedias
Browse files Browse the repository at this point in the history
  • Loading branch information
MusikAnimal committed Oct 17, 2016
1 parent 51fb0f2 commit 0d155f7
Showing 1 changed file with 6 additions and 21 deletions.
27 changes: 6 additions & 21 deletions tasks/copypatrol.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,25 @@ def self.run
end

def self.fetch_records(last_id)
query("SELECT id, page_title FROM #{copyright_table} WHERE id > ?", last_id).to_a
query("SELECT id, page_title FROM copyright_diffs WHERE lang = '#{@mb.lang}' AND id > ?", last_id).to_a
end

# Are there any wikiprojects for this title?
def self.wikiprojects?(page_title)
query("SELECT COUNT(*) AS count FROM #{wikiproject_table} WHERE wp_page_title = ?", page_title).to_a.first['count'] > 0
query("SELECT COUNT(*) AS count FROM wikiprojects " \
"WHERE wp_lang = '#{@mb.lang}' AND wp_page_title = ?", page_title).to_a.first['count'] > 0
end

def self.write_wikiprojects(wikiprojects, record)
wikiprojects.each do |wikiproject|
# use underscores instead of spaces, to be consistent
query("INSERT INTO #{wikiproject_table} VALUES(NULL, ?, ?)",
record['page_title'], wikiproject.tr(' ', '_')
query("INSERT INTO wikiprojects VALUES(NULL, ?, ?, ?)",
record['page_title'], wikiproject.tr(' ', '_'), @mb.lang
)
end
end

def self.parse_wikiprojects(page_title)

# mw:API:Revisions, and convert to Nokogiri markup
talk_markup = Nokogiri::HTML(
# Talk: namespace is always normalized to whatever it is for the given wiki
Expand All @@ -84,25 +84,10 @@ def self.parse_wikiprojects(page_title)
talk_markup.css('td b a')
.collect { |link| link.attributes['href'].value.sub('/wiki/', '') }
.select { |link| link =~ /^Projet:/ && !link.include?('/') && !link.include?('#') }
.map { |link| link.sub(/^Projet:/, '') }
end
end

# get the wikiprojects table name for the given wiki
def self.wikiproject_table
{
'en.wikipedia' => 'wikiprojects',
'fr.wikipedia' => 'wikiprojects_frwiki'
}[@mb.opts[:project]]
end

# get the EranBot table name for the given wiki
def self.copyright_table
{
'en.wikipedia' => 'copyright_diffs',
'fr.wikipedia' => 'copyright_diffs_frwiki'
}[@mb.opts[:project]]
end

def self.query(sql, *values)
puts sql
statement = @client.prepare(sql)
Expand Down

1 comment on commit 0d155f7

@kaldari
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

+1, looks good to me, haven't tested.

Please sign in to comment.