Index: trunk/extensions/ShortUrl/ShortUrl.functions.php |
— | — | @@ -1,79 +0,0 @@ |
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 | | -/** |
18 | | - * @param $title Title |
19 | | - * @return mixed|string |
20 | | - */ |
21 | | -function shorturlEncode ( $title ) { |
22 | | - global $wgMemc; |
23 | | - |
24 | | - $id = $wgMemc->get( wfMemcKey( 'shorturls', 'title', $title->getFullText() ) ); |
25 | | - if ( !$id ) { |
26 | | - $dbr = wfGetDB( DB_SLAVE ); |
27 | | - $query = $dbr->select( |
28 | | - 'shorturls', |
29 | | - array( 'su_id' ), |
30 | | - array( 'su_namespace' => $title->getNamespace(), 'su_title' => $title->getDBkey() ), |
31 | | - __METHOD__ ); |
32 | | - if ( $dbr->numRows( $query ) > 0 ) { |
33 | | - $entry = $dbr->fetchObject( $query ); |
34 | | - $id = $entry->su_id; |
35 | | - } else { |
36 | | - $dbw = wfGetDB( DB_MASTER ); |
37 | | - $row_data = array( |
38 | | - 'su_id' => $dbw->nextSequenceValue( 'shorturls_id_seq' ), |
39 | | - 'su_namespace' => $title->getNamespace(), |
40 | | - 'su_title' => $title->getDBkey() |
41 | | - ); |
42 | | - $dbw->insert( 'shorturls', $row_data ); |
43 | | - $id = $dbw->insertId(); |
44 | | - } |
45 | | - $wgMemc->set( wfMemcKey( 'shorturls', 'title', $title->getFullText() ), $id, 0 ); |
46 | | - } |
47 | | - return base_convert( $id, 10, 36 ); |
48 | | -} |
49 | | - |
50 | | -/** |
51 | | - * @param $data string |
52 | | - * @return Title |
53 | | - */ |
54 | | -function shorturlDecode ( $data ) { |
55 | | - global $wgMemc; |
56 | | - |
57 | | - $id = intval( base_convert ( $data, 36, 10 ) ); |
58 | | - $entry = $wgMemc->get( wfMemcKey( 'shorturls', 'id', $id ) ); |
59 | | - if ( !$entry ) { |
60 | | - $dbr = wfGetDB( DB_SLAVE ); |
61 | | - $query = $dbr->select( |
62 | | - 'shorturls', |
63 | | - array( 'su_namespace', 'su_title' ), |
64 | | - array( 'su_id' => $id ), |
65 | | - __METHOD__ |
66 | | - ); |
67 | | - |
68 | | - $entry = $dbr->fetchRow( $query ); // Less overhead on memcaching |
69 | | - $wgMemc->set( wfMemcKey( 'shorturls', 'id', $id ), $entry, 0 ); |
70 | | - } |
71 | | - return Title::makeTitle( $entry['su_namespace'], $entry['su_title'] ); |
72 | | -} |
73 | | - |
74 | | -/** |
75 | | - * @param $title Title |
76 | | - * @return True if a shorturl needs to be displayed |
77 | | - */ |
78 | | -function needsShortUrl( $title ) { |
79 | | - return $title->exists() && ! $title->equals( Title::newMainPage() ); |
80 | | -} |
Index: trunk/extensions/ShortUrl/ShortUrl.utils.php |
— | — | @@ -0,0 +1,85 @@ |
| 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 | +/** |
| 18 | + * Utility functions for encoding and decoding short URLs |
| 19 | + */ |
| 20 | +class ShortUrlUtils { |
| 21 | + |
| 22 | + /** |
| 23 | + * @param $title Title |
| 24 | + * @return mixed|string |
| 25 | + */ |
| 26 | + public static function EncodeTitle ( $title ) { |
| 27 | + global $wgMemc; |
| 28 | + |
| 29 | + $id = $wgMemc->get( wfMemcKey( 'shorturls', 'title', $title->getFullText() ) ); |
| 30 | + if ( !$id ) { |
| 31 | + $dbr = wfGetDB( DB_SLAVE ); |
| 32 | + $query = $dbr->select( |
| 33 | + 'shorturls', |
| 34 | + array( 'su_id' ), |
| 35 | + array( 'su_namespace' => $title->getNamespace(), 'su_title' => $title->getDBkey() ), |
| 36 | + __METHOD__ ); |
| 37 | + if ( $dbr->numRows( $query ) > 0 ) { |
| 38 | + $entry = $dbr->fetchObject( $query ); |
| 39 | + $id = $entry->su_id; |
| 40 | + } else { |
| 41 | + $dbw = wfGetDB( DB_MASTER ); |
| 42 | + $row_data = array( |
| 43 | + 'su_id' => $dbw->nextSequenceValue( 'shorturls_id_seq' ), |
| 44 | + 'su_namespace' => $title->getNamespace(), |
| 45 | + 'su_title' => $title->getDBkey() |
| 46 | + ); |
| 47 | + $dbw->insert( 'shorturls', $row_data ); |
| 48 | + $id = $dbw->insertId(); |
| 49 | + } |
| 50 | + $wgMemc->set( wfMemcKey( 'shorturls', 'title', $title->getFullText() ), $id, 0 ); |
| 51 | + } |
| 52 | + return base_convert( $id, 10, 36 ); |
| 53 | + } |
| 54 | + |
| 55 | + /** |
| 56 | + * @param $data string |
| 57 | + * @return Title |
| 58 | + */ |
| 59 | + public static function DecodeURL ( $urlfragment ) { |
| 60 | + global $wgMemc; |
| 61 | + |
| 62 | + $id = intval( base_convert ( $urlfragment, 36, 10 ) ); |
| 63 | + $entry = $wgMemc->get( wfMemcKey( 'shorturls', 'id', $id ) ); |
| 64 | + if ( !$entry ) { |
| 65 | + $dbr = wfGetDB( DB_SLAVE ); |
| 66 | + $query = $dbr->select( |
| 67 | + 'shorturls', |
| 68 | + array( 'su_namespace', 'su_title' ), |
| 69 | + array( 'su_id' => $id ), |
| 70 | + __METHOD__ |
| 71 | + ); |
| 72 | + |
| 73 | + $entry = $dbr->fetchRow( $query ); // Less overhead on memcaching |
| 74 | + $wgMemc->set( wfMemcKey( 'shorturls', 'id', $id ), $entry, 0 ); |
| 75 | + } |
| 76 | + return Title::makeTitle( $entry['su_namespace'], $entry['su_title'] ); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @param $title Title |
| 81 | + * @return True if a shorturl needs to be displayed |
| 82 | + */ |
| 83 | + public static function NeedsShortUrl( $title ) { |
| 84 | + return $title->exists() && ! $title->equals( Title::newMainPage() ); |
| 85 | + } |
| 86 | +} |
Property changes on: trunk/extensions/ShortUrl/ShortUrl.utils.php |
___________________________________________________________________ |
Added: svn:eol-style |
1 | 87 | + native |
Index: trunk/extensions/ShortUrl/ShortUrl.php |
— | — | @@ -29,6 +29,7 @@ |
30 | 30 | $wgExtensionMessagesFiles['ShortUrl'] = $dir . 'ShortUrl.i18n.php'; |
31 | 31 | $wgExtensionAliasesFiles['ShortUrl'] = $dir . 'ShortUrl.alias.php'; |
32 | 32 | |
| 33 | +$wgAutoloadClasses['ShortUrlUtils'] = $dir . 'ShortUrl.utils.php'; |
33 | 34 | $wgAutoloadClasses['ShortUrlHooks'] = $dir . 'ShortUrl.hooks.php'; |
34 | 35 | $wgAutoloadClasses['SpecialShortUrl'] = $dir . 'SpecialShortUrl.php'; |
35 | 36 | $wgSpecialPages['ShortUrl'] = 'SpecialShortUrl'; |
Index: trunk/extensions/ShortUrl/SpecialShortUrl.php |
— | — | @@ -15,8 +15,6 @@ |
16 | 16 | die( 1 ); |
17 | 17 | } |
18 | 18 | |
19 | | -require_once "ShortUrl.functions.php"; |
20 | | - |
21 | 19 | /** |
22 | 20 | * Provides the actual redirection |
23 | 21 | * @ingroup SpecialPage |
— | — | @@ -38,7 +36,7 @@ |
39 | 37 | public function execute( $par ) { |
40 | 38 | global $wgOut; |
41 | 39 | |
42 | | - $title = shorturlDecode( $par ); |
| 40 | + $title = ShortUrlUtils::DecodeURL( $par ); |
43 | 41 | if ( $title ) { |
44 | 42 | $wgOut->redirect( $title->getFullURL(), '301' ); |
45 | 43 | return; |
Index: trunk/extensions/ShortUrl/ShortUrl.hooks.php |
— | — | @@ -13,8 +13,6 @@ |
14 | 14 | exit( 1 ); |
15 | 15 | } |
16 | 16 | |
17 | | -require_once "ShortUrl.functions.php"; |
18 | | - |
19 | 17 | class ShortUrlHooks { |
20 | 18 | /** |
21 | 19 | * @param $tpl |
— | — | @@ -22,15 +20,15 @@ |
23 | 21 | */ |
24 | 22 | public static function AddToolboxLink( &$tpl ) { |
25 | 23 | global $wgOut, $wgShortUrlPrefix; |
26 | | - if ( $wgShortUrlPrefix == NULL) { |
| 24 | + if ( $wgShortUrlPrefix == null ) { |
27 | 25 | $urlPrefix = SpecialPage::getTitleFor( 'ShortUrl' )->getFullURL() . '/'; |
28 | 26 | } else { |
29 | 27 | $urlPrefix = $wgShortUrlPrefix; |
30 | 28 | } |
31 | 29 | |
32 | 30 | $title = $wgOut->getTitle(); |
33 | | - if ( needsShortUrl( $title ) ) { |
34 | | - $shortId = shorturlEncode( $title ); |
| 31 | + if ( ShortUrlUtils::needsShortUrl( $title ) ) { |
| 32 | + $shortId = ShortUrlUtils::EncodeTitle( $title ); |
35 | 33 | $shortURL = $urlPrefix . $shortId; |
36 | 34 | $html = Html::rawElement( 'li', array( 'id' => 't-shorturl' ), |
37 | 35 | Html::Element( 'a', array( |
— | — | @@ -47,12 +45,12 @@ |
48 | 46 | |
49 | 47 | /** |
50 | 48 | * @param $out OutputPage |
51 | | - * @param $text the HTML text to be added |
| 49 | + * @param $text string the HTML text to be added |
52 | 50 | */ |
53 | 51 | public static function OutputPageBeforeHTML( &$out, &$text ) { |
54 | 52 | global $wgOut; |
55 | 53 | $title = $wgOut->getTitle(); |
56 | | - if ( needsShortUrl( $title ) ) { |
| 54 | + if ( ShortUrlUtils::needsShortUrl( $title ) ) { |
57 | 55 | $wgOut->addModules( 'ext.shortUrl' ); |
58 | 56 | } |
59 | 57 | return true; |