# HG changeset patch # User Ludovic Chabant # Date 1357423652 28800 # Node ID e2585a9da30aba61673508d2577c0f259a80e199 # Parent 1b72f32b19a7515c67e7f0167cc51739cb505859 Added support for redirects between wiki pages. diff -r 1b72f32b19a7 -r e2585a9da30a wikked/static/js/wikked/app.js --- a/wikked/static/js/wikked/app.js Sat Jan 05 11:58:51 2013 -0800 +++ b/wikked/static/js/wikked/app.js Sat Jan 05 14:07:32 2013 -0800 @@ -30,10 +30,15 @@ 'special/:page': "showSpecialPage" }, readPage: function(path) { + path_clean = this.stripQuery(path); + no_redirect = this.getQueryVariable('no_redirect', path); var view = new Views.PageReadView({ el: $('#app'), - model: new Models.PageReadModel({ path: path }) + model: new Models.PageReadModel({ path: path_clean }) }); + if (no_redirect) { + view.model.set('no_redirect', true); + } view.model.setApp(this); view.model.fetch(); this.navigate('/read/' + path); @@ -146,13 +151,30 @@ view.model.fetch(); this.navigate('/special/' + page); }, - getQueryVariable: function(variable) { - var query = window.location.search.substring(1); - var vars = query.split("&"); + stripQuery: function(url) { + q = url.indexOf("?"); + if (q < 0) + return url; + return url.substring(0, q); + }, + getQueryVariable: function(variable, url) { + if (url === undefined) { + url = window.location.search.substring(1); + } else { + q = url.indexOf("?"); + if (q < 0) + return false; + url = url.substring(q + 1); + } + var vars = url.split("&"); for (var i = 0; i < vars.length; i++) { var pair = vars[i].split("="); if (pair[0] == variable) { - return unescape(pair[1]); + if (pair.length > 1) { + return unescape(pair[1]); + } else { + return true; + } } } return false; diff -r 1b72f32b19a7 -r e2585a9da30a wikked/static/js/wikked/models.js --- a/wikked/static/js/wikked/models.js Sat Jan 05 11:58:51 2013 -0800 +++ b/wikked/static/js/wikked/models.js Sat Jan 05 14:07:32 2013 -0800 @@ -188,6 +188,22 @@ var PageReadModel = exports.PageReadModel = MasterPageModel.extend({ urlRoot: '/api/read/', action: 'read', + initialize: function() { + PageReadModel.__super__.initialize.apply(this, arguments); + this.on('change', this._onChange, this); + }, + _onChange: function() { + if (this.getMeta('redirect') && !this.get('no_redirect')) { + var oldPath = this.get('path'); + this.set('path', this.getMeta('redirect')); + this.fetch({ + success: function(model) { + model.set('redirected_from', oldPath); + } + }); + this.app.navigate('/read/' + this.getMeta('redirect'), { replace: true }); + } + }, _onChangePath: function(path) { PageReadModel.__super__._onChangePath.apply(this, arguments); this.footer.addExtraUrl('Pages Linking Here', '/#/inlinks/' + path, 1); diff -r 1b72f32b19a7 -r e2585a9da30a wikked/static/tpl/read-page.html --- a/wikked/static/tpl/read-page.html Sat Jan 05 11:58:51 2013 -0800 +++ b/wikked/static/tpl/read-page.html Sat Jan 05 14:07:32 2013 -0800 @@ -3,6 +3,12 @@ {{#ifnot meta.notitle}}

{{meta.title}}

{{/ifnot}} + {{#if redirected_from}} + Redirected from {{redirected_from}} + {{/if}} + {{#if meta.redirect}} + Redirects to {{meta.redirect}} + {{/if}} {{content}} diff -r 1b72f32b19a7 -r e2585a9da30a wikked/wiki.py --- a/wikked/wiki.py Sat Jan 05 11:58:51 2013 -0800 +++ b/wikked/wiki.py Sat Jan 05 14:07:32 2013 -0800 @@ -95,6 +95,15 @@ self.url = url self._meta = None + self._promoted_meta = [ + 'title', + 'redirect', + 'notitle' + ] + self._coerce_promoted_meta = { + 'redirect': Page.title_to_url + } + @property def title(self): self._ensureMeta() @@ -141,6 +150,8 @@ for name in self._promoted_meta: if name in self._meta['user']: meta[name] = self._meta['user'][name] + if name in self._coerce_promoted_meta: + meta[name] = self._coerce_promoted_meta[name](meta[name]) return meta def getHistory(self): @@ -202,12 +213,6 @@ self.wiki.logger.debug("Updated cached %s for page '%s'." % (cache_key, self.url)) self.wiki.cache.write(cache_key, data) - _promoted_meta = [ - 'title', - 'redirect', - 'notitle' - ] - @staticmethod def title_to_url(title): return re.sub(r'[^A-Za-z0-9_\.\-\(\)/]+', '-', title.lower())