Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/xml import #2

Open
wants to merge 20 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions PubSubHubbubSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@
$wgAutoloadClasses['PubSubHubbubSubscriber\\Subscription'] = $dir . 'src/Subscription.php';
$wgAutoloadClasses['PubSubHubbubSubscriber\\SubscriptionHandler'] = $dir . 'src/SubscriptionHandler.php';
$wgAutoloadClasses['PubSubHubbubSubscriber\\SubscriberClient'] = $dir . 'src/SubscriberClient.php';
$wgAutoloadClasses['PubSubHubbubSubscriber\\ImportCallbacks'] = $dir . 'src/ImportCallbacks.php';

$wgAPIModules['pushcallback'] = 'PubSubHubbubSubscriber\\ApiSubscription';

$wgHooks['LoadExtensionSchemaUpdates'][] = 'PubSubHubbubSubscriber\\HookHandler::onLoadExtensionSchemaUpdates';
$wgHooks['UnitTestsList'][] = 'PubSubHubbubSubscriber\\HookHandler::onUnitTestsList';

2 changes: 1 addition & 1 deletion src/HookHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace PubSubHubbubSubscriber;

use DatabaseUpdater;
use WikiImporter;

class HookHandler {

Expand Down Expand Up @@ -37,5 +38,4 @@ public static function onUnitTestsList( &$files ) {
$files = array_merge( $files, glob( __DIR__ . '/../tests/phpunit/*Test.php' ) );
return true;
}

}
91 changes: 91 additions & 0 deletions src/ImportCallbacks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
<?php

namespace PubSubHubbubSubscriber;


use Title;
use User;
use WikiPage;
use WikiRevision;

class ImportCallbacks {

private $previousPageOutCallback = null;

/**
* @codeCoverageIgnore
* @param callable $previousPageOutCallback
*/
public function setPreviousPageOutCallback( $previousPageOutCallback ) {
$this->previousPageOutCallback = $previousPageOutCallback;
}

/**
* @param WikiRevision $revision
*/
public function deletionPage( WikiRevision $revision ) {
if ( $revision->getAction() != 'delete' ){
return;
}
$username = $revision->getUser();
if ( !empty( $username ) ) {
$user = User::newFromName( $username );
}
else {
$user = null;
}
$error = array();
$title = $revision->getTitle();
$wikipage = new WikiPage( $title );
$wikipage->doDeleteArticle( $revision->getComment(), false, 0, true, $error, $user );
}

/**
* @param Title $title
* @param $origTitle
* @param $revCount
* @param $sucCount
* @param $pageInfo
* @return bool
*/
function callOriginalPageOutCallback( Title $title, $origTitle, $revCount, $sucCount, $pageInfo ) {
if ( is_callable( $this->previousPageOutCallback) ) {
call_user_func_array( $this->previousPageOutCallback, func_get_args() );
}
return true;
}

/**
* @param Title $title
* @param $origTitle
* @param $revCount
* @param $sucCount
* @param $pageInfo
*/
public function createRedirect( Title $title, $origTitle, $revCount, $sucCount, $pageInfo ) {
if ( !array_key_exists( 'redirect', $pageInfo ) || $pageInfo['redirect'] == "" || $sucCount < 1 ) {
$this->callOriginalPageOutCallback( $title, $origTitle, $revCount, $sucCount, $pageInfo );
return;
}

$wikipage = new WikiPage( $title );
$redirectTitle = Title::newFromText( $pageInfo['redirect'] );
if ( $redirectTitle->exists() ){
$this->callOriginalPageOutCallback( $title, $origTitle, $revCount, $sucCount, $pageInfo );
return;
}

$dbw = wfGetDB( DB_MASTER );
$pageId = $wikipage->getId();
$currentRevision = $wikipage->getRevision();
$contentRevision = $currentRevision->getPrevious();
$currentRevisionID = $currentRevision->getId();
$contentRevisionID = $contentRevision->getId();

$dbw->delete( 'revision', array( 'rev_id' => $currentRevisionID ) );
$dbw->update( 'page', array( 'page_latest' => $contentRevisionID ), array( 'page_id' => $pageId ) );
$title->moveTo( $redirectTitle, false );

$this->callOriginalPageOutCallback( $title, $origTitle, $revCount, $sucCount, $pageInfo );
}
}
5 changes: 4 additions & 1 deletion src/SubscriptionHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,11 @@ public function handlePush( $topic, $hmacSignature, $file = "php://input" ) {
// Still need to return success according to specification.
return true;
}

$callbacks = new ImportCallbacks();
$importer = new WikiImporter( $source->value );
$importer->setLogItemCallback( array( &$callbacks, 'deletionPage' ) );
$callbacks->setPreviousPageOutCallback( $importer->setPageOutCallback(
array( &$callbacks, 'createRedirect' ) ) );
$importer->doImport();
return true;
} else {
Expand Down
Loading