http://www.mediawiki.org/wiki/Special:Code/MediaWiki/88817

Revision: 88817
Author:   yuvipanda
Date:     2011-05-25 19:00:18 +0000 (Wed, 25 May 2011)
Log Message:
-----------
Initial Commit of new extension (ShortUrl)

Added Paths:
-----------
    trunk/extensions/ShortUrl/README
    trunk/extensions/ShortUrl/ShortUrl.functions.php
    trunk/extensions/ShortUrl/ShortUrl.hooks.php
    trunk/extensions/ShortUrl/ShortUrl.i18n.php
    trunk/extensions/ShortUrl/ShortUrl.php
    trunk/extensions/ShortUrl/SpecialShortUrl.php
    trunk/extensions/ShortUrl/install.settings
    trunk/extensions/ShortUrl/schemas/
    trunk/extensions/ShortUrl/schemas/shorturls.sql

Added: trunk/extensions/ShortUrl/README
===================================================================
--- trunk/extensions/ShortUrl/README                            (rev 0)
+++ trunk/extensions/ShortUrl/README    2011-05-25 19:00:18 UTC (rev 88817)
@@ -0,0 +1,24 @@
+--------------------------------------------------------------------------
+README for the ShortUrl extension
+Copyright © 2011 Yuvi Panda
+Licenses: GNU General Public Licence (GPL)
+--------------------------------------------------------------------------
+
+The ShortUrl extension implements a URL Shortener
+creates a special page Special:Contact, which is similar to
+Special:Emailuser, but it has a fixed recipient, and can be used
+anonymously.
+
+<http://mediawiki.org/wiki/Extension:ShortUrl>
+
+== Installing ==
+
+Copy the ShortUrl directory into the extensions folder of your 
+MediaWiki installation. Then add the following lines to your
+LocalSettings.php file (near the end):
+
+  require_once( "$IP/extensions/ShortUrl/ShortUrl.php" );
+
+== Configuration ==
+
+There are no configuration options available as of now.

Added: trunk/extensions/ShortUrl/ShortUrl.functions.php
===================================================================
--- trunk/extensions/ShortUrl/ShortUrl.functions.php                            
(rev 0)
+++ trunk/extensions/ShortUrl/ShortUrl.functions.php    2011-05-25 19:00:18 UTC 
(rev 88817)
@@ -0,0 +1,82 @@
+<?php
+/**
+ * Functions used for decoding/encoding ids in ShortUrl Extension
+ *
+ * @file
+ * @ingroup Extensions
+ * @author Yuvi Panda, http://yuvi.in
+ * @copyright © 2011 Yuvaraj Pandian ([email protected])
+ * @licence Modified BSD License
+ */
+
+if ( !defined( 'MEDIAWIKI' ) ) {
+       exit( 1 );
+}
+
+/* stolen from http://www.php.net/manual/en/function.base64-encode.php#63543 */
+function urlsafe_b64encode($string) {
+           $data = base64_encode($string);
+                   $data = 
str_replace(array('+','/','='),array('-','_',''),$data);
+                   return $data;
+}
+
+/* stolen from http://www.php.net/manual/en/function.base64-encode.php#63543 */
+function urlsafe_b64decode($string) {
+           $data = str_replace(array('-','_'),array('+','/'),$string);
+                   $mod4 = strlen($data) % 4;
+                   if ($mod4) {
+                                       $data .= substr('====', $mod4);
+                                                   }
+                           return base64_decode($data);
+}
+
+function shorturlEncode ( $title ) {
+    global $wgMemc;
+
+    $id = null; 
+    $id = $wgMemc->get( wfMemcKey( 'shorturls', 'title', $title->getFullText() 
) );
+    if( !$id ) {
+        $dbr = wfGetDB( DB_SLAVE );
+        $query = $dbr->select(
+            'shorturls',
+            array( 'su_id' ),
+            array( 'su_namespace' => $title->getNamespace(), 'su_title' => 
$title->getText() ),
+            __METHOD__);
+        if( $dbr->numRows( $query ) > 0 ) {
+            $entry = $dbr->fetchRow( $query );
+            $id = $entry['su_id'];
+        } else {
+            $dbw = wfGetDB( DB_MASTER );
+            $row_data = array(
+                'su_id' => $dbw->nextSequenceValue( 'shorturls_id_seq' ),
+                'su_namespace' => $title->getNamespace(),
+                'su_title' => $title->getText()
+            );
+            $dbw->insert( 'shorturls', $row_data );
+            $id = $dbw->insertId();        
+        }
+        $wgMemc->set( wfMemcKey( 'shorturls', 'title', $title->getFullText() 
), $id, 0 );
+    } 
+       return urlsafe_b64encode( $id );
+}
+
+function shorturlDecode ( $data ) {
+    global $wgMemc;
+
+    $id = intval(urlsafe_b64decode ( $data ));
+    $entry = $wgMemc->get( wfMemcKey( 'shorturls', 'id', $id) ); 
+    if ( !$entry ) {
+        $dbr = wfGetDB( DB_SLAVE );
+        $query = $dbr->select(
+            'shorturls',
+            array( 'su_namespace', 'su_title' ),
+            array( 'su_id' => $id ),
+            __METHOD__
+        );
+
+        $entry = $dbr->fetchRow($query);
+        $wgMemc->set( wfMemcKey( 'shorturls', 'id', $id ), $entry, 0 );
+    }
+    return Title::makeTitle( $entry['su_namespace'], $entry['su_title'] );
+
+}


Property changes on: trunk/extensions/ShortUrl/ShortUrl.functions.php
___________________________________________________________________
Added: svn:eol-style
   + native

Added: trunk/extensions/ShortUrl/ShortUrl.hooks.php
===================================================================
--- trunk/extensions/ShortUrl/ShortUrl.hooks.php                                
(rev 0)
+++ trunk/extensions/ShortUrl/ShortUrl.hooks.php        2011-05-25 19:00:18 UTC 
(rev 88817)
@@ -0,0 +1,41 @@
+<?php
+/**
+ * Hooks for ShortUrl for adding link to toolbox
+ *
+ * @file
+ * @ingroup Extensions
+ * @author Yuvi Panda, http://yuvi.in
+ * @copyright © 2011 Yuvaraj Pandian ([email protected])
+ * @licence Modified BSD License
+ */
+
+if ( !defined( 'MEDIAWIKI' ) ) {
+       exit( 1 );
+}
+
+require_once "ShortUrl.functions.php";
+
+class ShortUrlHooks {
+       public static function AddToolboxLink( &$tpl ) {
+               global $wgOut, $wgShortUrlPrefix;
+               if ( ! $wgOut->getTitle()->equals( Title::newMainPage() ) ) {
+                       $title = $wgOut->getTitle();
+                       $shortId = shorturlEncode( $title );
+                       $shortURL = $wgShortUrlPrefix . $shortId;
+                       $html = Html::rawElement( 'li', array( 'id' => 
't-shorturl' ),
+                               Html::Element( 'a', array( 'href' => $shortURL, 
'title' => wfMsg( 'shorturl-toolbox-title') ), wfMsg ( 'shorturl-toolbox-text' 
) )
+                       );
+
+                       echo $html;
+               }
+               return true;
+       }
+
+       public static function SetupSchema( DatabaseUpdater $du ) {
+               $base = dirname( __FILE__ ) . '/schemas';
+               $du->addExtensionTable( "shorturls", "$base/shorturls.sql");
+               return true;
+       }
+
+}
+


Property changes on: trunk/extensions/ShortUrl/ShortUrl.hooks.php
___________________________________________________________________
Added: svn:eol-style
   + native

Added: trunk/extensions/ShortUrl/ShortUrl.i18n.php
===================================================================
--- trunk/extensions/ShortUrl/ShortUrl.i18n.php                         (rev 0)
+++ trunk/extensions/ShortUrl/ShortUrl.i18n.php 2011-05-25 19:00:18 UTC (rev 
88817)
@@ -0,0 +1,24 @@
+<?php
+/**
+ * Internationalisation file for ShortUrl extension.
+ *
+ * @file
+ * @ingroup Extensions
+ * @author Yuvi Panda, yuvi.in
+ * @copyright © Yuvaraj Pandian <[email protected]>
+ * @license Modified BSD License 
+ */
+
+$messages = array();
+
+/** English
+ * @author Yuvi Panda
+ */
+$messages['en'] = array(
+       'shorturl' => 'Short Url',
+       'shorturl-desc' => '[[Special:ShortUrl|Short Url for redirects]]',
+       'shorturl-not-found' => 'Sorry, the URL you are looking for is not 
found (No article with ID $1 exists)',
+       'shorturl-toolbox-title' => 'Copy this short link for sharing',
+       'shorturl-toolbox-text' => 'Short URL'
+);
+


Property changes on: trunk/extensions/ShortUrl/ShortUrl.i18n.php
___________________________________________________________________
Added: svn:eol-style
   + native

Added: trunk/extensions/ShortUrl/ShortUrl.php
===================================================================
--- trunk/extensions/ShortUrl/ShortUrl.php                              (rev 0)
+++ trunk/extensions/ShortUrl/ShortUrl.php      2011-05-25 19:00:18 UTC (rev 
88817)
@@ -0,0 +1,40 @@
+<?php
+/**
+ * Setup for ShortUrl extension, a special page that provides redirects to 
articles  
+ * via their page IDs
+ *
+ * @file
+ * @ingroup Extensions
+ * @author Yuvi Panda, http://yuvi.in
+ * @copyright © 2011 Yuvaraj Pandian ([email protected])
+ * @licence Modified BSD License
+ */
+
+if( !defined( 'MEDIAWIKI' ) ) {
+       echo( "This file is an extension to the MediaWiki software and cannot 
be used standalone.\n" );
+       die( 1 );
+}
+
+// Extension credits that will show up on Special:Version
+$wgExtensionCredits['specialpage'][] = array(
+       'path' => __FILE__,
+       'name' => 'ShortUrl',
+       'author' => 'Yuvi Panda',
+       'url' => 'http://www.mediawiki.org/wiki/Extension:ShortUrl',
+       'descriptionmsg' => 'shorturl-desc',
+);
+
+// Set up the new special page
+$dir = dirname( __FILE__ ) . '/';
+$wgExtensionMessagesFiles['ShortUrl'] = $dir . 'ShortUrl.i18n.php';
+
+
+$wgAutoloadClasses['ShortUrlHooks'] = $dir . 'ShortUrl.hooks.php';
+$wgAutoloadClasses['SpecialShortUrl'] = $dir . 'SpecialShortUrl.php';
+$wgSpecialPages['ShortUrl'] = 'SpecialShortUrl';
+
+$wgHooks['SkinTemplateToolboxEnd'][] = 'ShortUrlHooks::AddToolboxLink';
+$wgHooks['LoadExtensionSchemaUpdates'][] = 'ShortUrlHooks::SetupSchema';
+
+// Configuration
+$wgShortUrlPrefix = '/wiki/Special:ShortUrl/';


Property changes on: trunk/extensions/ShortUrl/ShortUrl.php
___________________________________________________________________
Added: svn:eol-style
   + native

Added: trunk/extensions/ShortUrl/SpecialShortUrl.php
===================================================================
--- trunk/extensions/ShortUrl/SpecialShortUrl.php                               
(rev 0)
+++ trunk/extensions/ShortUrl/SpecialShortUrl.php       2011-05-25 19:00:18 UTC 
(rev 88817)
@@ -0,0 +1,50 @@
+<?php
+/**
+ * Setup for ShortUrl extension, a special page that provides redirects to 
articles  
+ * via their page IDs
+ *
+ * @file
+ * @ingroup Extensions
+ * @author Yuvi Panda, http://yuvi.in
+ * @copyright © 2011 Yuvaraj Pandian ([email protected])
+ * @licence Modified BSD License
+ */
+
+if( !defined( 'MEDIAWIKI' ) ) {
+       echo( "not a valid entry point.\n" );
+       die( 1 );
+}
+
+require_once "ShortUrl.functions.php";
+
+/**
+ * Provides the contact form
+ * @ingroup SpecialPage
+ */
+class SpecialShortUrl extends SpecialPage {
+
+       /**
+        * Constructor
+        */
+       public function __construct() {
+               parent::__construct( 'ShortUrl' );
+       }
+
+       /**
+        * Main execution function
+        *
+        * @param $par Mixed: Parameters passed to the page
+        */
+       public function execute( $par ) {
+        global $wgOut, $wgRequest;
+        
+        $title = shorturlDecode( $par );
+        if ( $title ) {
+            $wgOut->redirect( $title->getFullURL(), '301' );
+            return;
+        }
+               // Wrong ID
+               $notfound = Html::element( 'p', array(), wfMsg ( 
'shorturl-not-found', $id ) );
+               $wgOut->addHTML( $notfound );
+       }
+}


Property changes on: trunk/extensions/ShortUrl/SpecialShortUrl.php
___________________________________________________________________
Added: svn:eol-style
   + native

Added: trunk/extensions/ShortUrl/install.settings
===================================================================
--- trunk/extensions/ShortUrl/install.settings                          (rev 0)
+++ trunk/extensions/ShortUrl/install.settings  2011-05-25 19:00:18 UTC (rev 
88817)
@@ -0,0 +1 @@
+require_once( "{{path}}/ShortUrl.php" );

Added: trunk/extensions/ShortUrl/schemas/shorturls.sql
===================================================================
--- trunk/extensions/ShortUrl/schemas/shorturls.sql                             
(rev 0)
+++ trunk/extensions/ShortUrl/schemas/shorturls.sql     2011-05-25 19:00:18 UTC 
(rev 88817)
@@ -0,0 +1,12 @@
+-- Replace /*_*/ with the proper prefix
+-- Replace /*$wgDBTableOptions*/ with the correct options
+
+CREATE TABLE IF NOT EXISTS /*_*/shorturls (    
+    su_id integer NOT NULL AUTO_INCREMENT,
+    su_namespace integer NOT NULL,
+    su_title varchar(255) NOT NULL,
+    PRIMARY KEY (su_id)
+) /*$wgDBTableOptions*/;
+
+CREATE INDEX /*i*/su_id ON shorturls (su_id);
+CREATE INDEX /*i*/su_title ON shorturls (su_namespace, su_title);


Property changes on: trunk/extensions/ShortUrl/schemas/shorturls.sql
___________________________________________________________________
Added: svn:eol-style
   + native


_______________________________________________
MediaWiki-CVS mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs

Reply via email to