Samwilson has uploaded a new change for review. ( 
https://gerrit.wikimedia.org/r/338708 )

Change subject: Convert to extension registration
......................................................................

Convert to extension registration

Add extension.json and refactor the main code into a Hooks class.

Change-Id: I5de7bd3d9779bd6bd8452ea48076abf613c67099
---
M ExternalArticles.php
A extension.json
A src/Hooks.php
3 files changed, 96 insertions(+), 61 deletions(-)


  git pull 
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/ExternalArticles 
refs/changes/08/338708/1

diff --git a/ExternalArticles.php b/ExternalArticles.php
index 74f3cc3..5130b97 100644
--- a/ExternalArticles.php
+++ b/ExternalArticles.php
@@ -54,67 +54,7 @@
        'url' => 'https://www.mediawiki.org/wiki/Extension:ExternalArticles'
 );
 $wgExtensionMessagesFiles['ExternalArticles'] = dirname( __FILE__ ) . 
'/ExternalArticles.i18n.php';
-$wgHooks['EditFormPreloadText'][] = 'ExternalArticles_EditFormPreloadText';
+$wgHooks['EditFormPreloadText'][] = 
'ExternalArticles_EditFormPreloadText::preload';
 
 $wgMessagesDirs['ExternalArticles'] = __DIR__ . '/i18n';
 $wgExtensionMessagesFiles['ExternalArticles'] = __DIR__ . 
'/ExternalArticles.i18n.php';
-
-// @todo: change this so each setting is set to it's default if it is not 
defined.
-//       Currently, if anything is overridden, all must be defined.
-if ( !isset( $eagRules ) || is_null( $eagRules ) ) {
-       $eagRules = array();
-       $eagRules['onpreload'] = true;
-       $eagRules['url'] = 'https://en.wikipedia.org/w/index.php?title=';
-
-       // @todo: remove assumption of English.
-       $eagRules['rule'] = '/^Template:.*$/'; // 
http://us3.php.net/manual/en/function.preg-match.php
-} else {
-       // @todo: validate $eagRules URL's, etc...
-}
-
-
-/**
- * Preload text from a remote wiki into the edit form. Called when edit page 
for
- * a new article is shown.
- *
- * @global OutputPage $wgOut OutputPage object for HTTP response
- * @global array $eagRules ExternalArticles configuration array
- * @param string $text Text with which to prefill the edit form
- * @param Title $title Title of the new page
- * @return boolean
- */
-function ExternalArticles_EditFormPreloadText( &$text, &$title ) {
-       global $wgOut, $eagRules;
-
-       $pagename = $title->getPrefixedURL();
-       $url = $eagRules['url'] . $pagename . '&action=raw';
-       $ismatch = preg_match( $eagRules['rule'], $pagename ) > 0;
-
-       if ( defined( 'EXTERNALARTICLES_DEBUG' ) ) {
-               if ( $ismatch ) {
-                       $wgOut->addWikiText( "URL: $url<br />" );
-               } else {
-                       $wgOut->addWikiText( "Page title does not match 
rule.<br />" );
-               }
-       }
-
-       if ( $eagRules['onpreload'] && $ismatch && empty( $text ) ) {
-               $options = array(
-                       'followRedirects' => true,
-               );
-               $httpRequest = MWHttpRequest::factory( $url, $options );
-               $status = $httpRequest->execute();
-               if ( !$status->isOK() ) {
-                       if ( defined( 'EXTERNALARTICLES_DEBUG' ) ) {
-                               $wgOut->addWikiText( "Failed to fetch external 
page: " . $status->getWikiText() );
-                       }
-                       return false;
-               }
-               $wgOut->wrapWikiMsg('<div class="success">$1</div>', 
array('externalarticles-article-loaded', $url));
-               $text = $httpRequest->getContent();
-
-               return true;
-       }
-       return true;
-}
-
diff --git a/extension.json b/extension.json
new file mode 100644
index 0000000..6b3332a
--- /dev/null
+++ b/extension.json
@@ -0,0 +1,24 @@
+{
+       "name": "External Articles",
+       "version": "0.2.0",
+       "author": [
+               "Nathan Perry",
+               "Alvinos",
+               "Sam Wilson"
+       ],
+       "url": "http://www.mediawiki.org/wiki/Extension:ExternalArticles";,
+       "descriptionmsg": "externalarticles-desc",
+       "type": "extension",
+       "AutoloadClasses": {
+               "MediaWiki\\Extensions\\ExternalArticles\\Hooks": 
"src/Hooks.php"
+       },
+       "MessagesDirs": {
+               "ExternalArticles": [
+                       "i18n"
+               ]
+       },
+       "Hooks": {
+               "EditFormPreloadText": 
"MediaWiki\\Extensions\\ExternalArticles\\Hooks::onEditFormPreloadText"
+       },
+       "manifest_version": 2
+}
diff --git a/src/Hooks.php b/src/Hooks.php
new file mode 100644
index 0000000..709373e
--- /dev/null
+++ b/src/Hooks.php
@@ -0,0 +1,71 @@
+<?php
+
+namespace MediaWiki\Extensions\ExternalArticles;
+
+use MWHttpRequest;
+use OutputPage;
+use Title;
+
+class Hooks {
+
+       /**
+        * Preload text from a remote wiki into the edit form. Called when edit 
page for
+        * a new article is shown.
+        *
+        * @global OutputPage $wgOut OutputPage object for HTTP response
+        * @global array $eagRules ExternalArticles configuration array
+        * @param string &$text Text with which to prefill the edit form
+        * @param Title &$title Title of the new page
+        * @return boolean
+        */
+       function onEditFormPreloadText( &$text, Title &$title ) {
+               global $wgOut, $eagRules;
+
+               // @todo: change this so each setting is set to it's default if 
it is not defined.
+               // Currently, if anything is overridden, all must be defined.
+               if ( !isset( $eagRules ) || is_null( $eagRules ) ) {
+                       $eagRules = [];
+                       $eagRules['onpreload'] = true;
+                       $eagRules['url'] = 
'https://en.wikipedia.org/w/index.php?title=';
+
+                       // @todo: remove assumption of English.
+                       $eagRules['rule'] = '/^Template:.*$/';
+               } else {
+                       // @todo: validate $eagRules URL's, etc...
+               }
+
+               $pagename = $title->getPrefixedURL();
+               $url = $eagRules['url'] . $pagename . '&action=raw';
+               $ismatch = preg_match( $eagRules['rule'], $pagename ) > 0;
+
+               if ( defined( 'EXTERNALARTICLES_DEBUG' ) ) {
+                       if ( $ismatch ) {
+                               $wgOut->addWikiText( "URL: $url<br />" );
+                       } else {
+                               $wgOut->addWikiText( "Page title does not match 
rule.<br />" );
+                       }
+               }
+
+               if ( $eagRules['onpreload'] && $ismatch && empty( $text ) ) {
+                       $options = [
+                               'followRedirects' => true,
+                       ];
+                       $httpRequest = MWHttpRequest::factory( $url, $options );
+                       $status = $httpRequest->execute();
+                       if ( !$status->isOK() ) {
+                               if ( defined( 'EXTERNALARTICLES_DEBUG' ) ) {
+                                       $wgOut->addWikiText( "Failed to fetch 
external page: " . $status->getWikiText() );
+                               }
+
+                               return false;
+                       }
+                       $wgOut->wrapWikiMsg( '<div class="success">$1</div>',
+                               [ 'externalarticles-article-loaded', $url ] );
+                       $text = $httpRequest->getContent();
+
+                       return true;
+               }
+
+               return true;
+       }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/338708
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I5de7bd3d9779bd6bd8452ea48076abf613c67099
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/ExternalArticles
Gerrit-Branch: master
Gerrit-Owner: Samwilson <[email protected]>

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

Reply via email to