r89168 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r89167‎ | r89168 | r89169 >
Date:13:56, 30 May 2011
Author:yuvipanda
Status:ok
Tags:
Comment:
Stopped polluting the global namespace with utility functions.
Modified paths:
  • /trunk/extensions/ShortUrl/ShortUrl.functions.php (deleted) (history)
  • /trunk/extensions/ShortUrl/ShortUrl.hooks.php (modified) (history)
  • /trunk/extensions/ShortUrl/ShortUrl.php (modified) (history)
  • /trunk/extensions/ShortUrl/ShortUrl.utils.php (added) (history)
  • /trunk/extensions/ShortUrl/SpecialShortUrl.php (modified) (history)

Diff [purge]

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
187 + native
Index: trunk/extensions/ShortUrl/ShortUrl.php
@@ -29,6 +29,7 @@
3030 $wgExtensionMessagesFiles['ShortUrl'] = $dir . 'ShortUrl.i18n.php';
3131 $wgExtensionAliasesFiles['ShortUrl'] = $dir . 'ShortUrl.alias.php';
3232
 33+$wgAutoloadClasses['ShortUrlUtils'] = $dir . 'ShortUrl.utils.php';
3334 $wgAutoloadClasses['ShortUrlHooks'] = $dir . 'ShortUrl.hooks.php';
3435 $wgAutoloadClasses['SpecialShortUrl'] = $dir . 'SpecialShortUrl.php';
3536 $wgSpecialPages['ShortUrl'] = 'SpecialShortUrl';
Index: trunk/extensions/ShortUrl/SpecialShortUrl.php
@@ -15,8 +15,6 @@
1616 die( 1 );
1717 }
1818
19 -require_once "ShortUrl.functions.php";
20 -
2119 /**
2220 * Provides the actual redirection
2321 * @ingroup SpecialPage
@@ -38,7 +36,7 @@
3937 public function execute( $par ) {
4038 global $wgOut;
4139
42 - $title = shorturlDecode( $par );
 40+ $title = ShortUrlUtils::DecodeURL( $par );
4341 if ( $title ) {
4442 $wgOut->redirect( $title->getFullURL(), '301' );
4543 return;
Index: trunk/extensions/ShortUrl/ShortUrl.hooks.php
@@ -13,8 +13,6 @@
1414 exit( 1 );
1515 }
1616
17 -require_once "ShortUrl.functions.php";
18 -
1917 class ShortUrlHooks {
2018 /**
2119 * @param $tpl
@@ -22,15 +20,15 @@
2321 */
2422 public static function AddToolboxLink( &$tpl ) {
2523 global $wgOut, $wgShortUrlPrefix;
26 - if ( $wgShortUrlPrefix == NULL) {
 24+ if ( $wgShortUrlPrefix == null ) {
2725 $urlPrefix = SpecialPage::getTitleFor( 'ShortUrl' )->getFullURL() . '/';
2826 } else {
2927 $urlPrefix = $wgShortUrlPrefix;
3028 }
3129
3230 $title = $wgOut->getTitle();
33 - if ( needsShortUrl( $title ) ) {
34 - $shortId = shorturlEncode( $title );
 31+ if ( ShortUrlUtils::needsShortUrl( $title ) ) {
 32+ $shortId = ShortUrlUtils::EncodeTitle( $title );
3533 $shortURL = $urlPrefix . $shortId;
3634 $html = Html::rawElement( 'li', array( 'id' => 't-shorturl' ),
3735 Html::Element( 'a', array(
@@ -47,12 +45,12 @@
4846
4947 /**
5048 * @param $out OutputPage
51 - * @param $text the HTML text to be added
 49+ * @param $text string the HTML text to be added
5250 */
5351 public static function OutputPageBeforeHTML( &$out, &$text ) {
5452 global $wgOut;
5553 $title = $wgOut->getTitle();
56 - if ( needsShortUrl( $title ) ) {
 54+ if ( ShortUrlUtils::needsShortUrl( $title ) ) {
5755 $wgOut->addModules( 'ext.shortUrl' );
5856 }
5957 return true;

Status & tagging log