http://www.mediawiki.org/wiki/Special:Code/MediaWiki/89168
Revision: 89168 Author: yuvipanda Date: 2011-05-30 13:56:58 +0000 (Mon, 30 May 2011) Log Message: ----------- Stopped polluting the global namespace with utility functions. Modified Paths: -------------- trunk/extensions/ShortUrl/ShortUrl.hooks.php trunk/extensions/ShortUrl/ShortUrl.php trunk/extensions/ShortUrl/SpecialShortUrl.php Added Paths: ----------- trunk/extensions/ShortUrl/ShortUrl.utils.php Removed Paths: ------------- trunk/extensions/ShortUrl/ShortUrl.functions.php Deleted: trunk/extensions/ShortUrl/ShortUrl.functions.php =================================================================== --- trunk/extensions/ShortUrl/ShortUrl.functions.php 2011-05-30 13:53:41 UTC (rev 89167) +++ trunk/extensions/ShortUrl/ShortUrl.functions.php 2011-05-30 13:56:58 UTC (rev 89168) @@ -1,79 +0,0 @@ -<?php -/** - * Functions used for decoding/encoding ids in ShortUrl Extension - * - * @file - * @ingroup Extensions - * @author Yuvi Panda, http://yuvi.in - * @copyright © 2011 Yuvaraj Pandian (yuvipa...@yuvi.in) - * @licence Modified BSD License - */ - -if ( !defined( 'MEDIAWIKI' ) ) { - exit( 1 ); -} - -/** - * @param $title Title - * @return mixed|string - */ -function shorturlEncode ( $title ) { - global $wgMemc; - - $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->getDBkey() ), - __METHOD__ ); - if ( $dbr->numRows( $query ) > 0 ) { - $entry = $dbr->fetchObject( $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->getDBkey() - ); - $dbw->insert( 'shorturls', $row_data ); - $id = $dbw->insertId(); - } - $wgMemc->set( wfMemcKey( 'shorturls', 'title', $title->getFullText() ), $id, 0 ); - } - return base_convert( $id, 10, 36 ); -} - -/** - * @param $data string - * @return Title - */ -function shorturlDecode ( $data ) { - global $wgMemc; - - $id = intval( base_convert ( $data, 36, 10 ) ); - $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 ); // Less overhead on memcaching - $wgMemc->set( wfMemcKey( 'shorturls', 'id', $id ), $entry, 0 ); - } - return Title::makeTitle( $entry['su_namespace'], $entry['su_title'] ); -} - -/** - * @param $title Title - * @return True if a shorturl needs to be displayed - */ -function needsShortUrl( $title ) { - return $title->exists() && ! $title->equals( Title::newMainPage() ); -} Modified: trunk/extensions/ShortUrl/ShortUrl.hooks.php =================================================================== --- trunk/extensions/ShortUrl/ShortUrl.hooks.php 2011-05-30 13:53:41 UTC (rev 89167) +++ trunk/extensions/ShortUrl/ShortUrl.hooks.php 2011-05-30 13:56:58 UTC (rev 89168) @@ -13,8 +13,6 @@ exit( 1 ); } -require_once "ShortUrl.functions.php"; - class ShortUrlHooks { /** * @param $tpl @@ -22,15 +20,15 @@ */ public static function AddToolboxLink( &$tpl ) { global $wgOut, $wgShortUrlPrefix; - if ( $wgShortUrlPrefix == NULL) { + if ( $wgShortUrlPrefix == null ) { $urlPrefix = SpecialPage::getTitleFor( 'ShortUrl' )->getFullURL() . '/'; } else { $urlPrefix = $wgShortUrlPrefix; } $title = $wgOut->getTitle(); - if ( needsShortUrl( $title ) ) { - $shortId = shorturlEncode( $title ); + if ( ShortUrlUtils::needsShortUrl( $title ) ) { + $shortId = ShortUrlUtils::EncodeTitle( $title ); $shortURL = $urlPrefix . $shortId; $html = Html::rawElement( 'li', array( 'id' => 't-shorturl' ), Html::Element( 'a', array( @@ -47,12 +45,12 @@ /** * @param $out OutputPage - * @param $text the HTML text to be added + * @param $text string the HTML text to be added */ public static function OutputPageBeforeHTML( &$out, &$text ) { global $wgOut; $title = $wgOut->getTitle(); - if ( needsShortUrl( $title ) ) { + if ( ShortUrlUtils::needsShortUrl( $title ) ) { $wgOut->addModules( 'ext.shortUrl' ); } return true; Modified: trunk/extensions/ShortUrl/ShortUrl.php =================================================================== --- trunk/extensions/ShortUrl/ShortUrl.php 2011-05-30 13:53:41 UTC (rev 89167) +++ trunk/extensions/ShortUrl/ShortUrl.php 2011-05-30 13:56:58 UTC (rev 89168) @@ -29,6 +29,7 @@ $wgExtensionMessagesFiles['ShortUrl'] = $dir . 'ShortUrl.i18n.php'; $wgExtensionAliasesFiles['ShortUrl'] = $dir . 'ShortUrl.alias.php'; +$wgAutoloadClasses['ShortUrlUtils'] = $dir . 'ShortUrl.utils.php'; $wgAutoloadClasses['ShortUrlHooks'] = $dir . 'ShortUrl.hooks.php'; $wgAutoloadClasses['SpecialShortUrl'] = $dir . 'SpecialShortUrl.php'; $wgSpecialPages['ShortUrl'] = 'SpecialShortUrl'; Copied: trunk/extensions/ShortUrl/ShortUrl.utils.php (from rev 89163, trunk/extensions/ShortUrl/ShortUrl.functions.php) =================================================================== --- trunk/extensions/ShortUrl/ShortUrl.utils.php (rev 0) +++ trunk/extensions/ShortUrl/ShortUrl.utils.php 2011-05-30 13:56:58 UTC (rev 89168) @@ -0,0 +1,85 @@ +<?php +/** + * Functions used for decoding/encoding ids in ShortUrl Extension + * + * @file + * @ingroup Extensions + * @author Yuvi Panda, http://yuvi.in + * @copyright © 2011 Yuvaraj Pandian (yuvipa...@yuvi.in) + * @licence Modified BSD License + */ + +if ( !defined( 'MEDIAWIKI' ) ) { + exit( 1 ); +} + +/** + * Utility functions for encoding and decoding short URLs + */ +class ShortUrlUtils { + + /** + * @param $title Title + * @return mixed|string + */ + public static function EncodeTitle ( $title ) { + global $wgMemc; + + $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->getDBkey() ), + __METHOD__ ); + if ( $dbr->numRows( $query ) > 0 ) { + $entry = $dbr->fetchObject( $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->getDBkey() + ); + $dbw->insert( 'shorturls', $row_data ); + $id = $dbw->insertId(); + } + $wgMemc->set( wfMemcKey( 'shorturls', 'title', $title->getFullText() ), $id, 0 ); + } + return base_convert( $id, 10, 36 ); + } + + /** + * @param $data string + * @return Title + */ + public static function DecodeURL ( $urlfragment ) { + global $wgMemc; + + $id = intval( base_convert ( $urlfragment, 36, 10 ) ); + $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 ); // Less overhead on memcaching + $wgMemc->set( wfMemcKey( 'shorturls', 'id', $id ), $entry, 0 ); + } + return Title::makeTitle( $entry['su_namespace'], $entry['su_title'] ); + } + + /** + * @param $title Title + * @return True if a shorturl needs to be displayed + */ + public static function NeedsShortUrl( $title ) { + return $title->exists() && ! $title->equals( Title::newMainPage() ); + } +} Modified: trunk/extensions/ShortUrl/SpecialShortUrl.php =================================================================== --- trunk/extensions/ShortUrl/SpecialShortUrl.php 2011-05-30 13:53:41 UTC (rev 89167) +++ trunk/extensions/ShortUrl/SpecialShortUrl.php 2011-05-30 13:56:58 UTC (rev 89168) @@ -15,8 +15,6 @@ die( 1 ); } -require_once "ShortUrl.functions.php"; - /** * Provides the actual redirection * @ingroup SpecialPage @@ -38,7 +36,7 @@ public function execute( $par ) { global $wgOut; - $title = shorturlDecode( $par ); + $title = ShortUrlUtils::DecodeURL( $par ); if ( $title ) { $wgOut->redirect( $title->getFullURL(), '301' ); return; _______________________________________________ MediaWiki-CVS mailing list MediaWiki-CVS@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-cvs