@@ -111,7 +111,74 @@ module.exports = {
111
111
*/
112
112
113
113
findByName : function ( req , res ) {
114
- return res . redirect ( 302 , "https://rdocumentation.org" + req . path ) ;
114
+ var packageName = req . param ( 'name' ) ;
115
+ var packageVersion = req . param ( 'version' ) ;
116
+ var topic = req . param ( 'topic' ) ;
117
+ var key = 'view_topic_' + packageName + '_' + packageVersion + '_' + topic ;
118
+ if ( topic . endsWith ( '.html' ) ) topic = topic . replace ( '.html' , '' ) ;
119
+
120
+
121
+ RedisService . getJSONFromCache ( key , res , RedisService . DAILY , function ( ) {
122
+ var canonicalPromise = Topic . findByNameInPackage ( packageName , topic ) . then ( function ( t ) {
123
+ if ( t === null || t === undefined ) return null ;
124
+ return t . uri ;
125
+ } ) ;
126
+
127
+ var examplesPromise = Example . findPackageExamples ( packageName , topic ) ;
128
+
129
+ var topicPromise = Topic . findOnePopulated ( { name : topic } , {
130
+ include : [ {
131
+ model : PackageVersion ,
132
+ as : 'package_version' ,
133
+ where : { package_name : packageName , version : packageVersion } ,
134
+ include : [ { model : Package , as : 'package' , attributes : [ 'name' , 'latest_version_id' , 'type_id' ] } ,
135
+ { model : Collaborator , as : 'maintainer' } ]
136
+ } , ]
137
+ } ) . then ( function ( topicInstance ) {
138
+ if ( topicInstance === null ) {
139
+ return Topic . findByAliasInPackage ( packageName , topic , packageVersion ) . then ( function ( topicInstance ) {
140
+ if ( ! topicInstance ) return null ;
141
+ else return { redirect_uri : topicInstance . uri } ;
142
+ } ) ;
143
+ }
144
+ else {
145
+ return topicInstance ;
146
+ }
147
+ } ) . then ( function ( topicInstance ) {
148
+ if ( topicInstance === null ) return null ;
149
+ else if ( topicInstance . redirect_uri ) return topicInstance ;
150
+ else return TopicService . processHrefs ( topicInstance )
151
+ . then ( function ( topic ) {
152
+ topic . pageTitle = topic . name + ' function' ;
153
+ topic . type = 'topic' ;
154
+ return topic ;
155
+ } ) ;
156
+ } ) ;
157
+
158
+ var dclPromise = PackageService . isDCLSupported ( packageName , packageVersion )
159
+
160
+ return Promise . join ( topicPromise , examplesPromise , canonicalPromise , dclPromise , function ( topicJSON , examples , canonicalLink , dcl ) {
161
+ if ( topicJSON === null ) return null ;
162
+ topicJSON . canonicalLink = canonicalLink ;
163
+ var userExamples = examples . sort ( function ( example1 , example2 ) {
164
+ const compare = PackageService . compareVersions ( 'desc' ) ;
165
+ const compareValue = compare ( example1 . topic . package_version . version , example2 . topic . package_version . version ) ;
166
+ if ( compareValue === 0 ) return example2 . created_at . getTime ( ) - example1 . created_at . getTime ( ) ;
167
+ else return compareValue ;
168
+ } ) ;
169
+ topicJSON . dcl = dcl || ( topicJSON . package_version && topicJSON . package_version . package . type_id === 4 ) ; //in the list or in base r
170
+ // We decode html entities again (as some older packages didn't have this fix in place)
171
+ topicJSON . examples = htmlEntities . decode ( topicJSON . examples )
172
+ topicJSON . user_examples = userExamples ;
173
+ return topicJSON ;
174
+ } ) ;
175
+ } ) . then ( function ( topicJSON ) {
176
+ if ( topicJSON === null ) return res . rstudio_redirect ( 301 , '/packages/' + encodeURIComponent ( packageName ) + '/versions/' + encodeURIComponent ( packageVersion ) ) ;
177
+ else if ( topicJSON . redirect_uri ) return res . rstudio_redirect ( 301 , topicJSON . redirect_uri ) ;
178
+ return res . ok ( topicJSON , 'topic/show.ejs' ) ;
179
+ } ) . catch ( function ( err ) {
180
+ return res . negotiate ( err ) ;
181
+ } ) ;
115
182
} ,
116
183
117
184
/**
@@ -155,7 +222,36 @@ module.exports = {
155
222
*/
156
223
157
224
findById : function ( req , res ) {
158
- return res . redirect ( 302 , "https://rdocumentation.org" + req . path ) ;
225
+ var id = req . param ( 'id' ) ;
226
+ var key = 'view_topic_' + id ;
227
+
228
+
229
+ RedisService . getJSONFromCache ( key , res , RedisService . DAILY , function ( ) {
230
+
231
+
232
+ return Topic . findOnePopulated ( { id : id } , {
233
+ } ) . then ( function ( topicInstance ) {
234
+ if ( topicInstance === null ) return null ;
235
+ else {
236
+ return Topic . findByNameInPackage ( topicInstance . package_version . package_name , topicInstance . name ) . then ( function ( t ) {
237
+ return TopicService . processHrefs ( topicInstance )
238
+ . then ( function ( topic ) {
239
+ topic . pageTitle = topic . name + ' function' ;
240
+ topic . canonicalLink = t . uri ;
241
+ return topic ;
242
+ } ) ;
243
+ } ) ;
244
+ }
245
+
246
+ } ) ;
247
+
248
+ } ) . then ( function ( topicJSON ) {
249
+ if ( topicJSON === null ) return res . notFound ( ) ;
250
+ return res . rstudio_redirect ( 301 , topicJSON . uri ) ;
251
+ } ) . catch ( function ( err ) {
252
+ return res . negotiate ( err ) ;
253
+ } ) ;
254
+
159
255
} ,
160
256
161
257
/**
0 commit comments