Extension:RPED/RPED body.php

<?php

/** * Remote Page Existence Detection (RPED) extension by Tisane * URL: http://www.mediawiki.org/wiki/Extension:RemotePageExistenceDetection * * 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. You can also redistribute it and/or * modify it under the terms of the Creative Commons Attribution 3.0 license. * * This extension provides the Special Page used to import data into the page title table. * * Two tables are used - a control table and a page title table. The page title table consists of the * primary key and the page titles that are read from the file. If it takes more than 40 seconds to * read the file, then the page saves the file pointer to the control table and reloads the page, so * it can continue reading from the file. That one field is the only thing in the control table. If * you have a large page title file, it will take several minutes to read it. You can tweak the * $sleepNumber, $sleepTime and $secondsBeforeReload as needed to get the performance necessary. If * there is a malfunction in mid-upload, you may need to manually drop the control table. Bear in * mind this special page is open to all users. If it strips too many characters off the end of the * rows in our input file, then adjust $stripThisManyCharactersFromEnd. */

class RPED extends SpecialPage { function __construct { parent::__construct( 'RPED' ); wfLoadExtensionMessages('RPED'); SpecialPage::SpecialPage( 'RPED' ); }	function execute( $par ) { global $wgRequest, $wgOut,$wgUser,$wgServer,$wgScriptPath; $displayMainMenu=true; if (!$this->userCanExecute( $wgUser )) { $this->displayRestrictionError; return; }		if (!$wgUser->isAllowed( 'RPED' ) ){ $this->displayRestrictionError; return; }	// Initialize variables $sleepNumber=1000; /* Go to sleep every x records read from the file (to give the server a rest		from queries)*/ $sleepTime=1; // Go to sleep for x seconds $stripThisManyCharactersFromEnd=2; /* Strip this many characters from the end of each row read from the file*/ $secondsBeforeReload=40; // Reload the page after x seconds and keep reading from the file $page_title_database_name='page_title_db'; // Default name for this database $yourHost='localhost'; $yourUsername='wikiuser2'; $yourPassword='password'; $centralServerURL='http://localhost/mw/Extensions/RPED/RPEDCentralServer.php?'; $this->setHeaders; # Get request data from parameters $param = $wgRequest->getText('param'); // Initialize database $con = mysql_connect($yourHost,$yourUsername,$yourPassword); /* This will fail unless you have supplied the correct data*/ if (!$con){ die('Could not connect: ' . mysql_error); }	// Create the page title database $sql='CREATE DATABASE '.$page_title_database_name; mysql_query($sql,$con); mysql_select_db($page_title_database_name, $con); // Select the page title database $this->createControlTable($con);

// See what's in the control table $myQuery='SELECT * FROM control_table'; $result=mysql_query($myQuery,$con); // Get the entire control table $matches=mysql_num_rows ( $result  ); if ($matches>0){ // If the control table isn't empty... //$timingResult=mysql_fetch_field($result); // Get the first row $result2=mysql_fetch_field($result); // Get the second row $result3=(int)$result2; // Convert to an integer if ($result3>0){ $this->readTheFile(				$fpointer=$result3,				$con=$con,				$stripThisManyCharactersFromEnd=$stripThisManyCharactersFromEnd,				$sleepNumber=$sleepNumber,				$sleepTime=$sleepTime,				$secondsBeforeReload=$secondsBeforeReload); // Read the file in from where we left off }		}	if (isset($_POST['yescleardatabase'])){ // If we've confirmed to clear the database... $sql="DROP TABLE page_title_table"; mysql_query($sql,$con); 	// Drop the page title table $sql="DROP TABLE control_table"; mysql_query($sql,$con); 	// Drop the control table // Create a new page title table $this->createpagetitleTable($con); }	elseif (isset($_POST['cleardatabase'])){ // If we've selected to clear the database... $outputText=' Are you sure that you want to clear the database?

findCurrentUrl.'"   '; $wgOut->addHTML($outputText); $displayMainMenu=false; }	elseif (isset($_POST['requestall'])){ // If we've selected data from the central server... $newPassword=''; for ($count=0; $count<32; $count++){ // A 32-digit randomly-generated password $newPassword.=rand(0,9); }		$sql="DROP TABLE password_table"; mysql_query($sql,$con); 	// Drop the password table $sql = "CREATE TABLE password_table(			password text(1000)			)"; mysql_query($sql,$con); $sql="INSERT INTO password_table (password) VALUES ('".$newPassword."')"; mysql_query($sql,$con); $centralServerURL.=$wgServer.$wgScriptPath.'|'.$newPassword; // Give the Server the password $response=file_get_contents($centralServerURL); $wgOut->addWikiText($response); }	elseif (isset($_POST['unsubscribe'])){ // If we've asked to unsubscribe... $newPassword=''; for ($count=0; $count<32; $count++){ // A 32-digit randomly-generated password $newPassword.=rand(0,9); }		$sql="DROP TABLE password_table"; mysql_query($sql,$con); 	// Drop the password table $sql = "CREATE TABLE password_table(			password text(1000)			)"; mysql_query($sql,$con); $sql="INSERT INTO password_table (password) VALUES ('".$newPassword."')"; mysql_query($sql,$con); $centralServerURL.=$wgServer.$wgScriptPath.'|'.$newPassword.'|'."unsubscribe"; $response=file_get_contents($centralServerURL); $wgOut->addWikiText($response); }	elseif (isset($_POST['uploadfile'])){ // If we've selected to read from a file... // Create the page title table $this->createpagetitleTable($con); $this->readTheFile( // Start at the beginning of the file		$fpointer=0,		$con=$con,		$stripThisManyCharactersFromEnd=$stripThisManyCharactersFromEnd,		$sleepNumber=$sleepNumber,$sleepTime=$sleepTime,		$secondsBeforeReload=$secondsBeforeReload); // Read the file in from where we left off }	elseif (isset($_POST['changepassword'])){ $newPassword = $_POST["newPassword"]; $confirmPassword = $_POST["confirmPassword"]; if ($newPassword==''){ $outputText=' The new password cannot be blank.

';			$wgOut->addHTML($outputText); }		elseif ($newPassword!=$confirmPassword){ $outputText=' The new password and confirm password did not match.

';			$wgOut->addHTML($outputText); }		else{ $sql="DROP TABLE password_table"; mysql_query($sql,$con); 	// Drop the password table $sql = "CREATE TABLE password_table(				password text(1000)			)"; mysql_query($sql,$con); $sql="INSERT INTO password_table (password) VALUES ('".$newPassword."')"; mysql_query($sql,$con); $outputText=' Password successfully changed!

';			$wgOut->addHTML($outputText); }	}	// ************** Main menu *********** if ($displayMainMenu==true){ // $outputText=' // Change password:

// findCurrentUrl.'" // enctype="multipart/form-data"> // New password:     

// Confirm password: 

//  //  //  //  // Upload page title file:

// Filename: 

//  // ';			$outputText=' findCurrentUrl.'" enctype="multipart/form-data">    ';		$wgOut->addHTML($outputText); }	}	function createpagetitleTable($con) // Create the page title table {	$sql = "CREATE TABLE page_title_table(		p_ID int NOT NULL AUTO_INCREMENT,		PRIMARY KEY(p_ID),		page_title text(1000)	)"; mysql_query($sql,$con); $sql="CREATE INDEX 'pageind' on 'page_title_table' ('page_title')"; mysql_query($sql,$con); }

function createControlTable($con){ global $wgOut; // Create the control table, which will have only one column $sql = "CREATE TABLE control_table(			offset_number text(1000)		)"; mysql_query($sql,$con); }		function readTheFile($fpointer,$con,$stripThisManyCharactersFromEnd,$sleepNumber,$sleepTime,		$secondsBeforeReload){ global $wgOut; $startTime=time; // Initial time; 40 seconds later, we will reload the page. //$wgOut->addWikiText('Reading file...'); //ini_set ( 'max_execution_time' , 300  ); // No need for this // This will fail unless you have created an "upload" folder in your mediawiki folder. move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]); $file=fopen("upload/" . $_FILES["file"]["name"],"r"); if ($fpointer>0){ fseek($file,$fpointer); // Go to the offset point in the file }		while(!feof($file)){ // Go until the end of the file $myFgets=fgets($file); // Get the next line from the file if (substr($myFgets, strlen($myFgets) - 2  ,2  )=='\n'){ // If it ends in a newline... $myFgets=substr ( $myFgets , 0  ,strlen($myFgets) - 2  ); // Strip the newline off -2 }			if (substr($myFgets, strlen($myFgets) - 1  ,1  )=='\n'){ // If it ends in a newline... $myFgets=substr ( $myFgets , 0  ,strlen($myFgets) - 1  ); // Strip the newline off -1 }			while (substr($myFgets, strlen($myFgets) - 1  ,1  )==' '){ // If it ends in a space... $myFgets=substr ( $myFgets , 0  ,strlen($myFgets) - 1  ); // Strip the space }			// Strip the last characters $myFgets=substr ( $myFgets , 0  ,strlen($myFgets) - $stripThisManyCharactersFromEnd  ); // Is this newline messing us up? $filesql="INSERT INTO page_title_table (page_title) VALUES ('".$myFgets."')"; /*if ($count2>100) // Send a query every this often {				mysql_query($filesql,$con); // Save page title to the database $filesql=''; $count2=0; }				$count2++; */ $count++; if ($count>$sleepNumber) // Give it some time to catch up! {				sleep($sleepTime); $count=0; }			mysql_query($filesql,$con); // Save what remains in the buffer if (time-$startTime>$secondsBeforeReload){ // At 40 seconds, reload! $sql="DELETE FROM control_table"; mysql_query($sql,$con); // Clear the control table $fpointer=ftell($file); // Find the current offset in the file // Query to save the fpointer to the control table $sql="INSERT INTO control_table (offset_number) VALUES ('".$fpointer."')"; //$wgOut->addWikiText($sql); mysql_query($sql,$con); // Execute query to save the fpointer to the control table // Figure out our current URL and go there again $pageURL = 'Location: '.$this->findCurrentUrl; header($pageURL); // Reload this page }		}		//mysql_query($filesql,$con); // Save page title to the database $sql="DROP TABLE control_table"; mysql_query($sql,$con); 	// Drop the control table fclose($file); $wgOut->addWikiText("File successfully uploaded! "); /*		$handle = fopen("upload/" . $_FILES["file"]["name"],"r"); $count=0; $arraycount=0; while ($userinfo = fscanf($handle, "%s\n")) { if ($count>1000){ mysql_query($filesql[$arraycount],$con); $arraycount++; $count=0; }			list ($name) = $userinfo; $filesql[arraycount].= "INSERT INTO page_title_table (page_title) VALUES ('".$name."\n')"; $wgOut->addWikiText($filesql[arraycount]); $count++; }		// $wgOut->addWikiText($filesql[$arraycount]); mysql_query($filesql[$arraycount],$con);*/ mysql_close($con); }	Function findCurrentUrl {		// Figure out our current URL and go there again $pageURL = 'http'; if (isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"] == "on"){ $pageURL .= "s"; }		$pageURL .= "://"; if ($_SERVER["SERVER_PORT"] != "80") { $pageURL .= $_SERVER["SERVER_NAME"].":".$_SERVER["SERVER_PORT"].$_SERVER["REQUEST_URI"]; } else { $pageURL .= $_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"]; }		return $pageURL; } }