Alexander.lehmann has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/107589


Change subject: initial code commit Change-Id: 
I90cd3534e5fd210c315feb27b3b7be808d8cc6ad
......................................................................

initial code commit
Change-Id: I90cd3534e5fd210c315feb27b3b7be808d8cc6ad
---
A PubSubHubbub.body.php
A PubSubHubbub.php
A PublishChangesJob.php
A lib/publisher.php
4 files changed, 235 insertions(+), 0 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/PubSubHubbub 
refs/changes/89/107589/1

diff --git a/PubSubHubbub.body.php b/PubSubHubbub.body.php
new file mode 100644
index 0000000..c175c96
--- /dev/null
+++ b/PubSubHubbub.body.php
@@ -0,0 +1,54 @@
+<?php
+/**
+ * This file is part of the PubSubHubbub Extension to MediaWiki
+ * https://www.mediawiki.org/wiki/Extension:PubSubHubbub
+ *
+ * @section LICENSE
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ * 
+ * @file
+ */
+
+class PubSubHubbub {
+
+       public static function onPageContentSaveComplete( $article, $user, 
$content, $summary,
+                       $isMinor, $section, $flags, $revision, $status, 
$baseRevId) {
+               if (!is_null($baseRevId->value['revision'])) {
+                       $jobParams = array();
+                       $title = $article->getTitle();
+                       $job = new PublishChangesJob( $title, $jobParams );
+                       $jobQueueGroup = JobQueueGroup::singleton()->push( $job 
);
+                       if ( !$jobQueueGroup ) {
+                               wfLogWarning( "Failed to acquire a 
JobQueueGroup for $job" );
+                       }
+               }
+               return false;
+       }
+
+       private static function insertLinkHeader( $page ) {
+               global $wgRequest, $wgPubSubHubbubHubURL;
+               $wgRequest->response()->header( "Link: <" . 
$wgPubSubHubbubHubURL . ">; rel=\"hub\", <"
+                       . $page->getTitle()->getFullURL() . "&action=raw>; 
rel=\"self\"", false );
+       }
+
+       public static function onRawPageViewBeforeOutput( $rawPage, $text ) {
+               self::insertLinkHeader( $rawPage );
+       }
+
+       public static function onArticlePageDataAfter( $wikiPage, $row ) {
+               self::insertLinkHeader( $wikiPage );
+       }
+}
diff --git a/PubSubHubbub.php b/PubSubHubbub.php
new file mode 100644
index 0000000..0ff9688
--- /dev/null
+++ b/PubSubHubbub.php
@@ -0,0 +1,50 @@
+<?php
+/**
+ * This file is part of the PubSubHubbub Extension to MediaWiki
+ * https://www.mediawiki.org/wiki/Extension:PubSubHubbub
+ *
+ * @section LICENSE
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ * 
+ * @file
+ */
+
+if ( !defined( 'MEDIAWIKI' ) ) {
+       die( 'Not an entry point.' );
+}
+
+global $wgHooks;
+global $wgJobClasses;
+global $wgAutoloadClasses;
+
+$wgExtensionCredits['other'][] = array(
+       'path' => __FILE__,
+       'name' => 'PubSubHubbub',
+       'author' => array( 'BP2013N2' ),
+       'url' => 'https://www.mediawiki.org/wiki/Extension:PubSubHubbub',
+       'descriptionmsg' => 'This is a PubSubHubbub implementation',
+       'version'  => 0.1,
+);
+
+$dir = __DIR__ . '/';
+
+$wgAutoloadClasses['PubSubHubbub'] = $dir . 'PubSubHubbub.body.php';
+$wgAutoloadClasses['PublishChangesJob'] = $dir . 'PublishChangesJob.php';
+
+$wgHooks['PageContentSaveComplete'][] = 
'PubSubHubbub::onPageContentSaveComplete';
+$wgHooks['RawPageViewBeforeOutput'][] = 
'PubSubHubbub::onRawPageViewBeforeOutput';
+$wgHooks['ArticlePageDataAfter'][] = 'PubSubHubbub::onArticlePageDataAfter';
+$wgJobClasses['PublishChanges'] = 'PublishChangesJob';
diff --git a/PublishChangesJob.php b/PublishChangesJob.php
new file mode 100644
index 0000000..e7bac26
--- /dev/null
+++ b/PublishChangesJob.php
@@ -0,0 +1,45 @@
+<?php
+/**
+ * This file is part of the PubSubHubbub Extension to MediaWiki
+ * https://www.mediawiki.org/wiki/Extension:PubSubHubbub
+ *
+ * @section LICENSE
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * http://www.gnu.org/copyleft/gpl.html
+ * 
+ * @file
+ */
+
+require_once(__DIR__ . '/lib/publisher.php');
+
+class PublishChangesJob extends Job {
+
+       public function __construct( $title, $params ) {
+                       parent::__construct( 'PublishChanges', $title, $params 
);
+       }
+
+       public function run() {
+               global $wgPubSubHubbubHubURL;
+               $publisher = new Publisher( $wgPubSubHubbubHubURL );
+               if ( $publisher->publish_update( $this->title->getFullURL() . 
"&action=raw" ) ) {
+                       return true;
+               } else {
+                       //TODO: Trigger some error.
+                       $error = $publisher->last_response();
+                       return false;
+               }
+       }
+
+}
diff --git a/lib/publisher.php b/lib/publisher.php
new file mode 100644
index 0000000..0bf0ea8
--- /dev/null
+++ b/lib/publisher.php
@@ -0,0 +1,86 @@
+<?php
+
+// a PHP client library for pubsubhubbub
+// as defined at http://code.google.com/p/pubsubhubbub/
+// written by Josh Fraser | joshfraser.com | j...@eventvue.com
+// Released under Apache License 2.0
+
+class Publisher {
+       
+       protected $hub_url;
+       protected $last_response;
+       
+       // create a new Publisher
+       public function __construct($hub_url) {
+               
+               if (!isset($hub_url))
+                       throw new Exception('Please specify a hub url');
+               
+               if (!preg_match("|^https?://|i",$hub_url)) 
+                       throw new Exception('The specified hub url does not 
appear to be valid: '.$hub_url);
+                       
+               $this->hub_url = $hub_url;
+       }
+
+       // accepts either a single url or an array of urls
+       public function publish_update($topic_urls, $http_function = false) {
+               if (!isset($topic_urls))
+                       throw new Exception('Please specify a topic url');
+               
+               // check that we're working with an array
+               if (!is_array($topic_urls)) {
+                       $topic_urls = array($topic_urls);
+               }
+               
+               // set the mode to publish
+               $post_string = "hub.mode=publish";
+               // loop through each topic url 
+               foreach ($topic_urls as $topic_url) {
+
+                       // lightweight check that we're actually working w/ a 
valid url
+                       if (!preg_match("|^https?://|i",$topic_url)) 
+                               throw new Exception('The specified topic url 
does not appear to be valid: '.$topic_url);
+                       
+                       // append the topic url parameters
+                       $post_string .= "&hub.url=".urlencode($topic_url);
+               }
+               
+               // make the http post request and return true/false
+               // easy to over-write to use your own http function
+               if ($http_function)
+                       return $http_function($this->hub_url,$post_string);
+               else
+                       return $this->http_post($this->hub_url,$post_string);
+       }
+
+       // returns any error message from the latest request
+       public function last_response() {
+               return $this->last_response;
+       }
+       
+       // default http function that uses curl to post to the hub endpoint
+       private function http_post($url, $post_string) {
+               
+               // add any additional curl options here
+               $options = array(CURLOPT_URL => $url,
+                                                CURLOPT_POST => true,
+                                                CURLOPT_POSTFIELDS => 
$post_string,
+                                                CURLOPT_USERAGENT => 
"PubSubHubbub-Publisher-PHP/1.0");
+
+               $ch = curl_init();
+               curl_setopt_array($ch, $options);
+
+               $response = curl_exec($ch);
+               $this->last_response = $response;
+               $info = curl_getinfo($ch);
+
+               curl_close($ch);
+               
+               // all good
+               if ($info['http_code'] == 204) 
+                       return true;
+               return false;   
+       }
+}
+
+?>
\ No newline at end of file

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

Gerrit-MessageType: newchange
Gerrit-Change-Id: I90cd3534e5fd210c315feb27b3b7be808d8cc6ad
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/PubSubHubbub
Gerrit-Branch: master
Gerrit-Owner: Alexander.lehmann <alexander.lehm...@student.hpi.uni-potsdam.de>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to