r58736 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r58735‎ | r58736 | r58737 >
Date:23:19, 7 November 2009
Author:ashley
Status:ok
Tags:
Comment:
cleanup to ExternalPages
Modified paths:
  • /trunk/extensions/ExternalPages/ExternalPages.alias.php (modified) (history)
  • /trunk/extensions/ExternalPages/ExternalPages.i18n.php (modified) (history)
  • /trunk/extensions/ExternalPages/ExternalPages_body.php (modified) (history)
  • /trunk/extensions/ExternalPages/SpecialExternalPages.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ExternalPages/ExternalPages.alias.php
@@ -2,7 +2,8 @@
33 /**
44 * Aliases for Special:ExternalPages
55 *
6 - * @addtogroup Extensions
 6+ * @file
 7+ * @ingroup Extensions
78 */
89
910 $aliases = array();
Index: trunk/extensions/ExternalPages/ExternalPages_body.php
@@ -5,76 +5,73 @@
66 exit( 1 );
77 }
88
9 -$base = dirname( __FILE__ );
10 -
119 /**
1210 * Special page allows retrieval and display of pages from remote WMF sites
1311 * with year, lang and project specifable
1412 */
1513 class ExternalPages extends SpecialPage {
1614
17 - private $mYear = '';
18 - private $mLang = '';
19 - private $mProject = '';
20 - private $mPage = false;
21 - private $mPageURL = '';
22 - private $mPageText = false;
23 - private $mFromCache = false;
 15+ private $mYear = '';
 16+ private $mLang = '';
 17+ private $mProject = '';
 18+ private $mPage = false;
 19+ private $mPageURL = '';
 20+ private $mPageText = false;
 21+ private $mFromCache = false;
2422
2523 // adjust these as needed to change cache expiry
2624 const EP_SMAXAGE = 600;
2725 const EP_MAXAGE = 600;
2826 const EP_MEMCACHE_EXP = 600;
2927
30 - function __construct() {
31 - SpecialPage::SpecialPage( 'ExternalPages' );
 28+ public function __construct() {
 29+ parent::__construct( 'ExternalPages' );
3230 wfLoadExtensionMessages( 'ExternalPages' );
33 - }
34 - /*
35 - * entry point (retrieve parsed page, convert rel links to full
36 - * urls that direct to the remote site
37 - * $par would be the subpage. we don't need it
38 - */
39 - function execute( $par ) {
 31+ }
 32+
 33+ /**
 34+ * Entry point (retrieve parsed page, convert rel links to full
 35+ * URLs that direct to the remote site
 36+ * $par would be the subpage. we don't need it
 37+ */
 38+ public function execute( $par ) {
4039 global $wgUser, $wgRequest;
41 -
 40+
4241 wfLoadExtensionMessages( 'ExternalPages' );
4342 $this->setHeaders();
44 - if ( ! $this->parseParams() ) {
 43+
 44+ if ( !$this->parseParams() ) {
4545 return( false );
4646 }
47 - if ( ! $this->userCanExecute( $wgUser ) ) {
 47+ if ( !$this->userCanExecute( $wgUser ) ) {
4848 $this->displayRestrictionError();
4949 return( false );
5050 }
51 -
 51+
5252 $this->constructURL();
5353 $this->retrieveExternalPage();
54 -
55 - }
56 -
57 - /*
58 - * process parameters of the request
59 - */
60 - private function parseParams() {
 54+ }
 55+
 56+ /**
 57+ * Process parameters of the request
 58+ */
 59+ private function parseParams() {
6160 global $wgRequest, $wgServer;
62 -
63 - if (!$wgRequest->getVal( 'EPyear') ) {
64 - $this->mYear=false;
65 - }
66 - else {
 61+
 62+ if ( !$wgRequest->getVal( 'EPyear' ) ) {
 63+ $this->mYear = false;
 64+ } else {
6765 $this->mYear = $wgRequest->getInt( 'EPyear' );
6866 // if this code is still being used 50 years from now, replace it :-P
69 - if (! (( $this->mYear > 2000 ) && ( $this->mYear < 2050 ))) {
 67+ if (! ( ( $this->mYear > 2000 ) && ( $this->mYear < 2050 ) ) ) {
7068 ExternalPagesErrors::showError( 'externalpages-bad-year' );
7169 return( false );
7270 }
7371 }
74 -
 72+
7573 if ( !$wgRequest->getVal( 'EPlanguage' ) ) {
76 - $this->mLang=false;
77 - }
78 - else {
 74+ $this->mLang = false;
 75+ } else {
7976 $this->mLang = $wgRequest->getVal( 'EPlanguage' );
8077 $knownLanguages = Language::getLanguageNames( false );
8178 if ( !array_key_exists( $this->mLang, $knownLanguages ) ) {
@@ -86,47 +83,46 @@
8784 if ( !$wgRequest->getVal( 'EPproject' ) ) {
8885 ExternalPagesErrors::showError( 'externalpages-no-project' );
8986 return( false );
90 - }
91 - else {
 87+ } else {
9288 $this->mProject = $wgRequest->getVal( 'EPproject' );
93 - // for initial fundraiser rollout, just allow pages from one project. this
94 - // can be generalized later
 89+ // for initial fundraiser rollout, just allow pages from one project.
 90+ // This can be generalized later
9591 if ( 'wikimediafoundation.org' != $this->mProject ) {
9692 ExternalPagesErrors::showError( 'externalpages-bad-project' );
97 - return(false);
 93+ return( false );
9894 }
9995 }
10096
10197 if ( !$wgRequest->getVal( 'EPpage' ) ) {
10298 ExternalPagesErrors::showError( 'externalpages-no-page' );
103 - return(false);
 99+ return( false );
104100 }
105101 $this->mPage = $wgRequest->getVal( 'EPpage' );
106102 // strictly speaking this may behave differently on the local wiki, oh well
107 - if ( ! Title::newFromText( $this->mPage ) ) {
 103+ if ( !Title::newFromText( $this->mPage ) ) {
108104 ExternalPagesErrors::showError( 'externalpages-bad-page' );
109 - return(false);
 105+ return( false );
110106 }
111107 return( true );
112 - }
 108+ }
113109
114 - private function constructURL() {
115 - $url = "http://" . $this->mProject . "/w/api.php?action=parse&page=";
116 - $title = ( $this->mYear ? $this->mYear."/" : "" ) . $this->mPage;
117 - $title .= $this->mLang ? "/".$this->mLang : "" ;
 110+ private function constructURL() {
 111+ $url = 'http://' . $this->mProject . '/w/api.php?action=parse&page=';
 112+ $title = ( $this->mYear ? $this->mYear . '/' : '' ) . $this->mPage;
 113+ $title .= $this->mLang ? '/' . $this->mLang : '';
118114 $title = urlencode( $title );
119115 $url = $url . $title . '&format=php';
120116 $this->mPageURL = $url;
121 - }
 117+ }
122118
123 - public function cacheHeaders() {
 119+ public function cacheHeaders() {
124120 global $wgRequest;
125121
126122 $smaxage = self::EP_SMAXAGE;
127123 $maxage = self::EP_MAXAGE;
128124
129125 $public = ( session_id() == '' );
130 -
 126+
131127 if ( $public ) {
132128 $wgRequest->response()->header( "Cache-Control: public, s-maxage=$smaxage, max-age=$maxage" );
133129 } else {
@@ -135,100 +131,97 @@
136132 $time = time() + self::EP_MAXAGE;
137133 $wgRequest->response()->header( 'Expires: ' . gmdate( 'D, d M Y H:i:s', $time ) . ' GMT' );
138134 return( true );
139 - }
 135+ }
140136
141 - private function getCacheKey( $string ) {
 137+ private function getCacheKey( $string ) {
142138 return( wfMemcKey( 'externalpages', $string ) );
143 - }
 139+ }
144140
145 - private function getPageFromCache() {
 141+ private function getPageFromCache() {
146142 global $wgMemc;
147 -
 143+
148144 wfProfileIn( __METHOD__ );
149145
150 - if ( ! $this->mPageURL ) {
 146+ if ( !$this->mPageURL ) {
151147 $this->constructURL();
152148 }
153149
154150 $this->mPageText = $wgMemc->get( $this->getCacheKey( $this->mPageURL ) );
155 - if ( ! $this->mPageText ) {
 151+ if ( !$this->mPageText ) {
156152 wfDebugLog( 'ExternalPages', "Remote Page Text: cache miss for {$this->mPageURL} " );
157153 wfProfileOut( __METHOD__ );
158154 return( false );
159155 }
160156 wfProfileOut( __METHOD__ );
161157 return( true );
162 - }
 158+ }
163159
164 - private function savePageToCache() {
 160+ private function savePageToCache() {
165161 global $wgMemc;
166162
167163 wfDebugLog( 'ExternalPages', "Saving text {$this->mPageURL} to cache." );
168164 $wgMemc->set( $this->getCacheKey( $this->mPageURL ), $this->mPageText, self::EP_MEMCACHE_EXP );
169 - }
 165+ }
170166
171 - private function retrieveExternalPage() {
 167+ private function retrieveExternalPage() {
172168 global $wgOut, $wgRequest, $wgHooks;
173 -
174 - if ( ! $this->mPageURL ) {
 169+
 170+ if ( !$this->mPageURL ) {
175171 $this->constructURL();
176172 }
177173
178174 // try from cache first
179175 $this->getPageFromCache();
180176
181 - if ( ! $this->mPageText ) {
 177+ if ( !$this->mPageText ) {
182178 $serializedText = Http::get( $this->mPageURL );
183179
184180 if ( empty( $serializedText ) ) {
185181 ExternalPagesErrors::showError( 'externalpages-bad-url' );
186 - return(false);
187 - }
188 - else {
 182+ return( false );
 183+ } else {
189184 $text = unserialize( $serializedText );
190185 }
191186
192187 if ( isset( $text['parse'] ) && ( isset( $text['parse']['text'] ) ) ) {
193188 $this->mPageText = $text['parse']['text']['*'];
194 - $absurl = '<a href="http://'.$this->mProject."/";
 189+ $absurl = '<a href="http://' . $this->mProject . '/';
195190 $this->mPageText = str_replace( '<a href="https://www.mediawiki.org/', $absurl, $this->mPageText );
196191 }
197192 $this->savePageToCache();
198 - }
199 - else {
 193+ } else {
200194 wfDebugLog( 'ExternalPages', "Retrieved {$this->mPageURL} from cache." );
201195 }
202196
203197 if ( $this->mPageText ) {
204198 $wgHooks['CacheHeadersAfterSet'][] = array( $this, 'cacheHeaders' );
205199 $wgOut->addHTML( $this->mPageText );
206 - }
207 - else {
 200+ } else {
208201 ExternalPagesErrors::showError( 'externalpages-bad-url-data' );
209202 return( false );
210203 }
211204 return;
212 - }
 205+ }
213206 }
214207
215 -/*
216 - * error handler for some formatting of error messages
 208+/**
 209+ * Error handler for some formatting of error messages
217210 */
218211 class ExternalPagesErrors {
219212
220 - static function showError( $errorText='externalpages-error-generic', $phpErrorText=false ) {
 213+ static function showError( $errorText = 'externalpages-error-generic', $phpErrorText = false ) {
221214 global $wgOut;
222 -
 215+
223216 $args = func_get_args();
224217
225218 array_shift( $args );
226 - $msg = wfMsg( $errorText, $args );
 219+ $msg = wfMsg( $errorText, $args );
227220
228 - $wgOut->addWikiText( "<div class=\"errorbox\" style=\"float:none;\">" .
229 - $msg .
230 - "</div>" );
 221+ $wgOut->addWikiText(
 222+ '<div class="errorbox" style="float:none;">' .
 223+ $msg .
 224+ '</div>'
 225+ );
231226 }
232227
233 -}
234 -
235 -?>
 228+}
\ No newline at end of file
Index: trunk/extensions/ExternalPages/ExternalPages.i18n.php
@@ -1,24 +1,28 @@
22 <?php
33 /**
4 - * Internationalizations for extension ExternalPages.
 4+ * Internationalizations for ExternalPages extension.
55 *
6 - * @addtogroup Extensions
 6+ * @file
 7+ * @ingroup Extensions
78 */
89
910 $messages = array();
1011
 12+/** English
 13+ * @author Ariel Glenn
 14+ */
1115 $messages['en'] = array(
12 - 'externalpages' => 'External Pages',
13 - 'externalpages-desc' => 'Retrieves and displays pages from remote WMF sites',
14 - 'externalpages-bad-year' => 'Bad year specified',
15 - 'externalpages-bad-language' => 'Bad language specified',
16 - 'externalpages-bad-project' => 'Bad project specified',
17 - 'externalpages-no-project' => 'No project specified',
18 - 'externalpages-bad-page' => 'Bad page specified',
19 - 'externalpages-no-page' => 'No page specified',
20 - 'externalpages-error-generic' => 'Error encountered',
21 - 'externalpages-bad-url' => 'Failed to retrieve URL',
22 - 'externalpages-bad-url-data' => 'Failed to retrieve page contents',
 16+ 'externalpages' => 'External Pages',
 17+ 'externalpages-desc' => 'Retrieves and displays pages from remote WMF sites',
 18+ 'externalpages-bad-year' => 'Bad year specified',
 19+ 'externalpages-bad-language' => 'Bad language specified',
 20+ 'externalpages-bad-project' => 'Bad project specified',
 21+ 'externalpages-no-project' => 'No project specified',
 22+ 'externalpages-bad-page' => 'Bad page specified',
 23+ 'externalpages-no-page' => 'No page specified',
 24+ 'externalpages-error-generic' => 'Error encountered',
 25+ 'externalpages-bad-url' => 'Failed to retrieve URL',
 26+ 'externalpages-bad-url-data' => 'Failed to retrieve page contents',
2327 );
2428
2529 /** Message documentation (Message documentation)
Index: trunk/extensions/ExternalPages/SpecialExternalPages.php
@@ -1,12 +1,12 @@
22 <?php
33 /**
44 * A Special Page extension to retrieve and display a page
5 - * from a specified external WMF site, with optional year,
6 - * project
7 - * and language parameters
 5+ * from a specified external WMF site, with optional year,
 6+ * project and language parameters
87 *
9 - * @addtogroup Extensions
10 - *
 8+ * @file
 9+ * @ingroup Extensions
 10+ * @version 0.1
1111 * @author Ariel Glenn <ariel@wikimedia.org>
1212 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 3.0 or later
1313 */
@@ -14,11 +14,12 @@
1515 if ( !defined( 'MEDIAWIKI' ) ) {
1616 echo <<<EOT
1717 To install the ExternalPages extension, put the following line in LocalSettings.php:
18 -require_once( "\$IP/extensions/SpecialExternalPages.php" );
 18+require_once( "\$IP/extensions/ExternalPages/SpecialExternalPages.php" );
1919 EOT;
20 - exit(1);
 20+ exit( 1 );
2121 }
2222
 23+// Extension credits that will show up on Special:Version
2324 $wgExtensionCredits['specialpage'][] = array(
2425 'name' => 'ExternalPages',
2526 'version' => '0.1',
@@ -29,7 +30,6 @@
3031 );
3132
3233 $dir = dirname( __FILE__ ) . '/';
33 -
3434 $wgExtensionMessagesFiles['ExternalPages'] = $dir . 'ExternalPages.i18n.php';
3535 $wgExtensionAliasesFiles['ExternalPages'] = $dir . 'ExternalPages.alias.php';
3636
@@ -42,11 +42,9 @@
4343 function externalPagesLocalizedPageName( &$specialPageArray, $code ) {
4444 wfLoadExtensionMessages( 'ExternalPages' );
4545 $text = wfMsg( 'externalpages' );
46 -
 46+
4747 # Convert from title in text form to DBKey and put it into the alias array:
4848 $title = Title::newFromText( $text );
4949 $specialPageArray['ExternalPages'][] = $title->getDBKey();
5050 return true;
51 -}
52 -
53 -?>
 51+}
\ No newline at end of file

Status & tagging log