Changesets can be listed by changeset number.
The Git repository is here.
- Revision:
- 385
- Log:
Remove PATH_PREFIX stuff, since Apache+Passenger doesn't need it.
- Author:
- rool
- Date:
- Mon Mar 21 14:15:29 +0000 2011
- Size:
- 7891 Bytes
1 | class ArticlesController < ContentController |
2 | before_filter :verify_config |
3 | |
4 | layout :theme_layout, :except => [:comment_preview, :trackback] |
5 | |
6 | cache_sweeper :blog_sweeper |
7 | |
8 | cached_pages = [:index, :read, :permalink, :category, :find_by_date, :archives, :view_page, :tag, :author] |
9 | # If you're really memory-constrained, then consider replacing |
10 | # caches_action_with_params with caches_page |
11 | caches_action_with_params *cached_pages |
12 | session :new_session => false |
13 | |
14 | verify(:only => [:nuke_comment, :nuke_trackback], |
15 | :session => :user, :method => :post, |
16 | :render => { :text => 'Forbidden', :status => 403 }) |
17 | |
18 | @@hubssolib_permissions = HubSsoLib::Permissions.new({ |
19 | :read_and_comment => [ :admin, :webmaster, :privileged, :normal ] |
20 | }) |
21 | |
22 | def ArticlesController.hubssolib_permissions |
23 | @@hubssolib_permissions |
24 | end |
25 | |
26 | def index |
27 | # On Postgresql, paginate's default count is *SLOW*, because it does a join against |
28 | # all of the eager-loaded tables. I've seen it take up to 7 seconds on my test box. |
29 | # |
30 | # So, we're going to use the older Paginator class and manually provide a count. |
31 | # This is a 100x speedup on my box. |
32 | now = Time.now |
33 | count = this_blog.articles.count(:conditions => ['published = ? AND contents.published_at < ?', |
34 | true, now]) |
35 | @pages = Paginator.new self, count, this_blog.limit_article_display, params[:page] |
36 | @articles = this_blog.published_articles.find( :all, |
37 | :offset => @pages.current.offset, |
38 | :limit => @pages.items_per_page, |
39 | :conditions => ['contents.published_at < ?', now] ) |
40 | end |
41 | |
42 | def search |
43 | @articles = this_blog.published_articles.search(params[:q]) |
44 | render_paginated_index("No articles found...") |
45 | end |
46 | |
47 | def comment_preview |
48 | if params[:comment].blank? or params[:comment][:body].blank? |
49 | render :nothing => true |
50 | return |
51 | end |
52 | |
53 | set_headers |
54 | @comment = this_blog.comments.build(params[:comment]) |
55 | @controller = self |
56 | end |
57 | |
58 | def archives |
59 | @articles = this_blog.published_articles |
60 | end |
61 | |
62 | def read |
63 | display_article { this_blog.published_articles.find(params[:id]) } |
64 | end |
65 | |
66 | def read_and_comment |
67 | display_article(nil, 'read_and_comment') { |
68 | this_blog.published_articles.find(params[:id]) |
69 | } |
70 | end |
71 | |
72 | def permalink |
73 | display_article(this_blog.published_articles.find_by_permalink(*params.values_at(:year, :month, :day, :title))) |
74 | end |
75 | |
76 | def find_by_date |
77 | @articles = this_blog.published_articles.find_all_by_date(params[:year], params[:month], params[:day]) |
78 | render_paginated_index |
79 | end |
80 | |
81 | def error(message = "Record not found...") |
82 | @message = message.to_s |
83 | render :action => 'error' |
84 | end |
85 | |
86 | def author |
87 | render_grouping(User) |
88 | end |
89 | |
90 | def category |
91 | render_grouping(Category) |
92 | end |
93 | |
94 | def tag |
95 | render_grouping(Tag) |
96 | end |
97 | |
98 | # Receive comments to articles |
99 | def comment |
100 | unless request.xhr? || this_blog.sp_allow_non_ajax_comments |
101 | render_error("non-ajax commenting is disabled") |
102 | return |
103 | end |
104 | |
105 | unless hubssolib_logged_in? |
106 | render_error("You are not logged in; you cannot comment on articles") |
107 | return |
108 | end |
109 | |
110 | if request.post? |
111 | begin |
112 | @article = this_blog.published_articles.find(params[:id]) |
113 | params[:comment].merge!({:ip => request.remote_ip, |
114 | :published => true, |
115 | :user => session[:user], |
116 | :user_agent => request.env['HTTP_USER_AGENT'], |
117 | :referrer => request.env['HTTP_REFERER'], |
118 | :permalink => @article.permalink_url}) |
119 | @comment = @article.comments.build(params[:comment]) |
120 | @comment.author ||= 'Anonymous' |
121 | @comment.save! |
122 | add_to_cookies(:typoapp_author, @comment.author) |
123 | add_to_cookies(:typoapp_url, @comment.url) |
124 | |
125 | set_headers |
126 | render :partial => "comment", :object => @comment |
127 | rescue ActiveRecord::RecordInvalid |
128 | STDERR.puts @comment.errors.inspect |
129 | render_error(@comment) |
130 | end |
131 | end |
132 | end |
133 | |
134 | # Receive trackbacks linked to articles |
135 | def trackback |
136 | @error_message = catch(:error) do |
137 | if params[:__mode] == "rss" |
138 | # Part of the trackback spec... will implement later |
139 | # XXX. Should this throw an error? |
140 | elsif !(params.has_key?(:url) && params.has_key?(:id)) |
141 | throw :error, "A URL is required" |
142 | else |
143 | begin |
144 | settings = { :id => params[:id], |
145 | :url => params[:url], :blog_name => params[:blog_name], |
146 | :title => params[:title], :excerpt => params[:excerpt], |
147 | :ip => request.remote_ip, :published => true } |
148 | this_blog.ping_article!(settings) |
149 | rescue ActiveRecord::RecordNotFound, ActiveRecord::StatementInvalid |
150 | throw :error, "Article id #{params[:id]} not found." |
151 | rescue ActiveRecord::RecordInvalid |
152 | throw :error, "Trackback not saved" |
153 | end |
154 | end |
155 | nil |
156 | end |
157 | end |
158 | |
159 | def nuke_comment |
160 | Comment.find(params[:id]).destroy |
161 | render :nothing => true |
162 | end |
163 | |
164 | def nuke_trackback |
165 | Trackback.find(params[:id]).destroy |
166 | render :nothing => true |
167 | end |
168 | |
169 | def view_page |
170 | if(@page = Page.find_by_name(params[:name].to_a.join('/'))) |
171 | @page_title = @page.title |
172 | else |
173 | render :nothing => true, :status => 404 |
174 | end |
175 | end |
176 | |
177 | def markup_help |
178 | render :text => TextFilter.find(params[:id]).commenthelp |
179 | end |
180 | |
181 | private |
182 | |
183 | def add_to_cookies(name, value) |
184 | cookies[name] = { :value => value, :path => url_for(:controller => :articles, :action => :index, :only_path => true), |
185 | :expires => 6.weeks.from_now } |
186 | end |
187 | |
188 | def verify_config |
189 | if User.count == 0 |
190 | redirect_to :controller => "accounts", :action => "signup" |
191 | elsif ! this_blog.is_ok? |
192 | redirect_to :controller => "admin/general", :action => "redirect" |
193 | else |
194 | return true |
195 | end |
196 | end |
197 | |
198 | def display_article(article = nil, action = 'read') |
199 | begin |
200 | @article = block_given? ? yield : article |
201 | @comment = Comment.new |
202 | @page_title = @article.title |
203 | auto_discovery_feed :type => 'article', :id => @article.id |
204 | render :action => action |
205 | rescue ActiveRecord::RecordNotFound, NoMethodError => e |
206 | error("Post not found...") |
207 | end |
208 | end |
209 | |
210 | alias_method :rescue_action_in_public, :error |
211 | |
212 | def render_error(object = '', status = 500) |
213 | render(:text => (object.errors.full_messages.join(", ") rescue object.to_s), :status => status) |
214 | end |
215 | |
216 | def set_headers |
217 | headers["Content-Type"] = "text/html; charset=utf-8" |
218 | end |
219 | |
220 | def list_groupings(klass) |
221 | @grouping_class = klass |
222 | @groupings = klass.find_all_with_article_counters(1000) |
223 | render :action => 'groupings' |
224 | end |
225 | |
226 | def render_grouping(klass) |
227 | return list_groupings(klass) unless params[:id] |
228 | |
229 | @page_title = "#{klass.to_s.underscore} #{params[:id]}" |
230 | @articles = klass.find_by_permalink(params[:id]).articles.find_already_published rescue [] |
231 | auto_discovery_feed :type => klass.to_s.underscore, :id => params[:id] |
232 | render_paginated_index("Can't find posts with #{klass.to_prefix} '#{h(params[:id])}'") |
233 | end |
234 | |
235 | def render_paginated_index(on_empty = "No posts found...") |
236 | return error(on_empty) if @articles.empty? |
237 | |
238 | @pages = Paginator.new self, @articles.size, this_blog.limit_article_display, params[:page] |
239 | start = @pages.current.offset |
240 | stop = (@pages.current.next.offset - 1) rescue @articles.size |
241 | # Why won't this work? @articles.slice!(start..stop) |
242 | @articles = @articles.slice(start..stop) |
243 | render :action => 'index' |
244 | end |
245 | end |