Index: trunk/extensions/ShortUrl/ShortUrl.functions.php |
— | — | @@ -0,0 +1,82 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Functions used for decoding/encoding ids in ShortUrl Extension |
| 5 | + * |
| 6 | + * @file |
| 7 | + * @ingroup Extensions |
| 8 | + * @author Yuvi Panda, http://yuvi.in |
| 9 | + * @copyright © 2011 Yuvaraj Pandian (yuvipanda@yuvi.in) |
| 10 | + * @licence Modified BSD License |
| 11 | + */ |
| 12 | + |
| 13 | +if ( !defined( 'MEDIAWIKI' ) ) { |
| 14 | + exit( 1 ); |
| 15 | +} |
| 16 | + |
| 17 | +/* stolen from http://www.php.net/manual/en/function.base64-encode.php#63543 */ |
| 18 | +function urlsafe_b64encode($string) { |
| 19 | + $data = base64_encode($string); |
| 20 | + $data = str_replace(array('+','/','='),array('-','_',''),$data); |
| 21 | + return $data; |
| 22 | +} |
| 23 | + |
| 24 | +/* stolen from http://www.php.net/manual/en/function.base64-encode.php#63543 */ |
| 25 | +function urlsafe_b64decode($string) { |
| 26 | + $data = str_replace(array('-','_'),array('+','/'),$string); |
| 27 | + $mod4 = strlen($data) % 4; |
| 28 | + if ($mod4) { |
| 29 | + $data .= substr('====', $mod4); |
| 30 | + } |
| 31 | + return base64_decode($data); |
| 32 | +} |
| 33 | + |
| 34 | +function shorturlEncode ( $title ) { |
| 35 | + global $wgMemc; |
| 36 | + |
| 37 | + $id = null; |
| 38 | + $id = $wgMemc->get( wfMemcKey( 'shorturls', 'title', $title->getFullText() ) ); |
| 39 | + if( !$id ) { |
| 40 | + $dbr = wfGetDB( DB_SLAVE ); |
| 41 | + $query = $dbr->select( |
| 42 | + 'shorturls', |
| 43 | + array( 'su_id' ), |
| 44 | + array( 'su_namespace' => $title->getNamespace(), 'su_title' => $title->getText() ), |
| 45 | + __METHOD__); |
| 46 | + if( $dbr->numRows( $query ) > 0 ) { |
| 47 | + $entry = $dbr->fetchRow( $query ); |
| 48 | + $id = $entry['su_id']; |
| 49 | + } else { |
| 50 | + $dbw = wfGetDB( DB_MASTER ); |
| 51 | + $row_data = array( |
| 52 | + 'su_id' => $dbw->nextSequenceValue( 'shorturls_id_seq' ), |
| 53 | + 'su_namespace' => $title->getNamespace(), |
| 54 | + 'su_title' => $title->getText() |
| 55 | + ); |
| 56 | + $dbw->insert( 'shorturls', $row_data ); |
| 57 | + $id = $dbw->insertId(); |
| 58 | + } |
| 59 | + $wgMemc->set( wfMemcKey( 'shorturls', 'title', $title->getFullText() ), $id, 0 ); |
| 60 | + } |
| 61 | + return urlsafe_b64encode( $id ); |
| 62 | +} |
| 63 | + |
| 64 | +function shorturlDecode ( $data ) { |
| 65 | + global $wgMemc; |
| 66 | + |
| 67 | + $id = intval(urlsafe_b64decode ( $data )); |
| 68 | + $entry = $wgMemc->get( wfMemcKey( 'shorturls', 'id', $id) ); |
| 69 | + if ( !$entry ) { |
| 70 | + $dbr = wfGetDB( DB_SLAVE ); |
| 71 | + $query = $dbr->select( |
| 72 | + 'shorturls', |
| 73 | + array( 'su_namespace', 'su_title' ), |
| 74 | + array( 'su_id' => $id ), |
| 75 | + __METHOD__ |
| 76 | + ); |
| 77 | + |
| 78 | + $entry = $dbr->fetchRow($query); |
| 79 | + $wgMemc->set( wfMemcKey( 'shorturls', 'id', $id ), $entry, 0 ); |
| 80 | + } |
| 81 | + return Title::makeTitle( $entry['su_namespace'], $entry['su_title'] ); |
| 82 | + |
| 83 | +} |
Property changes on: trunk/extensions/ShortUrl/ShortUrl.functions.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 84 | + native |
Index: trunk/extensions/ShortUrl/schemas/shorturls.sql |
— | — | @@ -0,0 +1,12 @@ |
| 2 | +-- Replace /*_*/ with the proper prefix |
| 3 | +-- Replace /*$wgDBTableOptions*/ with the correct options |
| 4 | + |
| 5 | +CREATE TABLE IF NOT EXISTS /*_*/shorturls ( |
| 6 | + su_id integer NOT NULL AUTO_INCREMENT, |
| 7 | + su_namespace integer NOT NULL, |
| 8 | + su_title varchar(255) NOT NULL, |
| 9 | + PRIMARY KEY (su_id) |
| 10 | +) /*$wgDBTableOptions*/; |
| 11 | + |
| 12 | +CREATE INDEX /*i*/su_id ON shorturls (su_id); |
| 13 | +CREATE INDEX /*i*/su_title ON shorturls (su_namespace, su_title); |
Property changes on: trunk/extensions/ShortUrl/schemas/shorturls.sql |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 14 | + native |
Index: trunk/extensions/ShortUrl/install.settings |
— | — | @@ -0,0 +1 @@ |
| 2 | +require_once( "{{path}}/ShortUrl.php" ); |
Index: trunk/extensions/ShortUrl/ShortUrl.i18n.php |
— | — | @@ -0,0 +1,24 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Internationalisation file for ShortUrl extension. |
| 5 | + * |
| 6 | + * @file |
| 7 | + * @ingroup Extensions |
| 8 | + * @author Yuvi Panda, yuvi.in |
| 9 | + * @copyright © Yuvaraj Pandian <yuvipanda@yuvi.in> |
| 10 | + * @license Modified BSD License |
| 11 | + */ |
| 12 | + |
| 13 | +$messages = array(); |
| 14 | + |
| 15 | +/** English |
| 16 | + * @author Yuvi Panda |
| 17 | + */ |
| 18 | +$messages['en'] = array( |
| 19 | + 'shorturl' => 'Short Url', |
| 20 | + 'shorturl-desc' => '[[Special:ShortUrl|Short Url for redirects]]', |
| 21 | + 'shorturl-not-found' => 'Sorry, the URL you are looking for is not found (No article with ID $1 exists)', |
| 22 | + 'shorturl-toolbox-title' => 'Copy this short link for sharing', |
| 23 | + 'shorturl-toolbox-text' => 'Short URL' |
| 24 | +); |
| 25 | + |
Property changes on: trunk/extensions/ShortUrl/ShortUrl.i18n.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 26 | + native |
Index: trunk/extensions/ShortUrl/ShortUrl.php |
— | — | @@ -0,0 +1,40 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Setup for ShortUrl extension, a special page that provides redirects to articles |
| 5 | + * via their page IDs |
| 6 | + * |
| 7 | + * @file |
| 8 | + * @ingroup Extensions |
| 9 | + * @author Yuvi Panda, http://yuvi.in |
| 10 | + * @copyright © 2011 Yuvaraj Pandian (yuvipanda@yuvi.in) |
| 11 | + * @licence Modified BSD License |
| 12 | + */ |
| 13 | + |
| 14 | +if( !defined( 'MEDIAWIKI' ) ) { |
| 15 | + echo( "This file is an extension to the MediaWiki software and cannot be used standalone.\n" ); |
| 16 | + die( 1 ); |
| 17 | +} |
| 18 | + |
| 19 | +// Extension credits that will show up on Special:Version |
| 20 | +$wgExtensionCredits['specialpage'][] = array( |
| 21 | + 'path' => __FILE__, |
| 22 | + 'name' => 'ShortUrl', |
| 23 | + 'author' => 'Yuvi Panda', |
| 24 | + 'url' => 'http://www.mediawiki.org/wiki/Extension:ShortUrl', |
| 25 | + 'descriptionmsg' => 'shorturl-desc', |
| 26 | +); |
| 27 | + |
| 28 | +// Set up the new special page |
| 29 | +$dir = dirname( __FILE__ ) . '/'; |
| 30 | +$wgExtensionMessagesFiles['ShortUrl'] = $dir . 'ShortUrl.i18n.php'; |
| 31 | + |
| 32 | + |
| 33 | +$wgAutoloadClasses['ShortUrlHooks'] = $dir . 'ShortUrl.hooks.php'; |
| 34 | +$wgAutoloadClasses['SpecialShortUrl'] = $dir . 'SpecialShortUrl.php'; |
| 35 | +$wgSpecialPages['ShortUrl'] = 'SpecialShortUrl'; |
| 36 | + |
| 37 | +$wgHooks['SkinTemplateToolboxEnd'][] = 'ShortUrlHooks::AddToolboxLink'; |
| 38 | +$wgHooks['LoadExtensionSchemaUpdates'][] = 'ShortUrlHooks::SetupSchema'; |
| 39 | + |
| 40 | +// Configuration |
| 41 | +$wgShortUrlPrefix = '/wiki/Special:ShortUrl/'; |
Property changes on: trunk/extensions/ShortUrl/ShortUrl.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 42 | + native |
Index: trunk/extensions/ShortUrl/SpecialShortUrl.php |
— | — | @@ -0,0 +1,50 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Setup for ShortUrl extension, a special page that provides redirects to articles |
| 5 | + * via their page IDs |
| 6 | + * |
| 7 | + * @file |
| 8 | + * @ingroup Extensions |
| 9 | + * @author Yuvi Panda, http://yuvi.in |
| 10 | + * @copyright © 2011 Yuvaraj Pandian (yuvipanda@yuvi.in) |
| 11 | + * @licence Modified BSD License |
| 12 | + */ |
| 13 | + |
| 14 | +if( !defined( 'MEDIAWIKI' ) ) { |
| 15 | + echo( "not a valid entry point.\n" ); |
| 16 | + die( 1 ); |
| 17 | +} |
| 18 | + |
| 19 | +require_once "ShortUrl.functions.php"; |
| 20 | + |
| 21 | +/** |
| 22 | + * Provides the contact form |
| 23 | + * @ingroup SpecialPage |
| 24 | + */ |
| 25 | +class SpecialShortUrl extends SpecialPage { |
| 26 | + |
| 27 | + /** |
| 28 | + * Constructor |
| 29 | + */ |
| 30 | + public function __construct() { |
| 31 | + parent::__construct( 'ShortUrl' ); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Main execution function |
| 36 | + * |
| 37 | + * @param $par Mixed: Parameters passed to the page |
| 38 | + */ |
| 39 | + public function execute( $par ) { |
| 40 | + global $wgOut, $wgRequest; |
| 41 | + |
| 42 | + $title = shorturlDecode( $par ); |
| 43 | + if ( $title ) { |
| 44 | + $wgOut->redirect( $title->getFullURL(), '301' ); |
| 45 | + return; |
| 46 | + } |
| 47 | + // Wrong ID |
| 48 | + $notfound = Html::element( 'p', array(), wfMsg ( 'shorturl-not-found', $id ) ); |
| 49 | + $wgOut->addHTML( $notfound ); |
| 50 | + } |
| 51 | +} |
Property changes on: trunk/extensions/ShortUrl/SpecialShortUrl.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 52 | + native |
Index: trunk/extensions/ShortUrl/ShortUrl.hooks.php |
— | — | @@ -0,0 +1,41 @@ |
| 2 | +<?php |
| 3 | +/** |
| 4 | + * Hooks for ShortUrl for adding link to toolbox |
| 5 | + * |
| 6 | + * @file |
| 7 | + * @ingroup Extensions |
| 8 | + * @author Yuvi Panda, http://yuvi.in |
| 9 | + * @copyright © 2011 Yuvaraj Pandian (yuvipanda@yuvi.in) |
| 10 | + * @licence Modified BSD License |
| 11 | + */ |
| 12 | + |
| 13 | +if ( !defined( 'MEDIAWIKI' ) ) { |
| 14 | + exit( 1 ); |
| 15 | +} |
| 16 | + |
| 17 | +require_once "ShortUrl.functions.php"; |
| 18 | + |
| 19 | +class ShortUrlHooks { |
| 20 | + public static function AddToolboxLink( &$tpl ) { |
| 21 | + global $wgOut, $wgShortUrlPrefix; |
| 22 | + if ( ! $wgOut->getTitle()->equals( Title::newMainPage() ) ) { |
| 23 | + $title = $wgOut->getTitle(); |
| 24 | + $shortId = shorturlEncode( $title ); |
| 25 | + $shortURL = $wgShortUrlPrefix . $shortId; |
| 26 | + $html = Html::rawElement( 'li', array( 'id' => 't-shorturl' ), |
| 27 | + Html::Element( 'a', array( 'href' => $shortURL, 'title' => wfMsg( 'shorturl-toolbox-title') ), wfMsg ( 'shorturl-toolbox-text' ) ) |
| 28 | + ); |
| 29 | + |
| 30 | + echo $html; |
| 31 | + } |
| 32 | + return true; |
| 33 | + } |
| 34 | + |
| 35 | + public static function SetupSchema( DatabaseUpdater $du ) { |
| 36 | + $base = dirname( __FILE__ ) . '/schemas'; |
| 37 | + $du->addExtensionTable( "shorturls", "$base/shorturls.sql"); |
| 38 | + return true; |
| 39 | + } |
| 40 | + |
| 41 | +} |
| 42 | + |
Property changes on: trunk/extensions/ShortUrl/ShortUrl.hooks.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 43 | + native |
Index: trunk/extensions/ShortUrl/README |
— | — | @@ -0,0 +1,24 @@ |
| 2 | +-------------------------------------------------------------------------- |
| 3 | +README for the ShortUrl extension |
| 4 | +Copyright © 2011 Yuvi Panda |
| 5 | +Licenses: GNU General Public Licence (GPL) |
| 6 | +-------------------------------------------------------------------------- |
| 7 | + |
| 8 | +The ShortUrl extension implements a URL Shortener |
| 9 | +creates a special page Special:Contact, which is similar to |
| 10 | +Special:Emailuser, but it has a fixed recipient, and can be used |
| 11 | +anonymously. |
| 12 | + |
| 13 | +<http://mediawiki.org/wiki/Extension:ShortUrl> |
| 14 | + |
| 15 | +== Installing == |
| 16 | + |
| 17 | +Copy the ShortUrl directory into the extensions folder of your |
| 18 | +MediaWiki installation. Then add the following lines to your |
| 19 | +LocalSettings.php file (near the end): |
| 20 | + |
| 21 | + require_once( "$IP/extensions/ShortUrl/ShortUrl.php" ); |
| 22 | + |
| 23 | +== Configuration == |
| 24 | + |
| 25 | +There are no configuration options available as of now. |