Jump to content

re: Attempt at an extension to MediaWiki


dwees

Recommended Posts

Hi there,

I successfully added an extension to my MediaWiki site using some code someone else created.

I am trying to modify this code so I can also include an extension to write quizzes directly from MediaWiki.

So far I've managed to create code that starts the ball rolling, but I have some error in the middle and its killing me trying to find it.  I also have noticed that I could probably cut the code down a bunch if I defined some functions along the way, but I can do that later and I don't think it's the cause of the problem.

Here is the type of XML file I want to produce:

[code]<?xml version="1.0"?>
<QUIZ>

<TITLE>Internet Quiz</TITLE>
<QUESTION>
<TEXT>A set of guidelines that allow different types of devices to communicate with each is called a:</TEXT>
<CHOICES>Modem, Protocol, Language, Process</CHOICES>
<ANSWER>Protocol</ANSWER>
</QUESTION>

<QUESTION>
<TEXT>The equation &#60;tex&#62;x^2+y^2=z^2&#60;/tex&#62; is not an example of what kind of equation:</TEXT>
<CHOICES>Quadratic equation, Diophantine equation, Linear Equation, Non-Linear Equation</CHOICES>
<ANSWER>Linear Equation</ANSWER>
</QUESTION>

<QUESTION>
<TEXT>DHCP is used to automcatically assign _____ to each device.</TEXT>
<CHOICES>MAC Address, Host Name, IP Address, URL</CHOICES>
<ANSWER>IP Address</ANSWER>
</QUESTION>
</QUIZ>[/code]

Note the character strings are in there so they get parsed correctly by MediaWiki into tag wrappers.

The code I have so far is (and I've tried to comment a fair bit):
[code]<?php

#apd_set_pprof_trace();
# Main wiki script; see design.doc
#

$wgRequestTime = microtime();

//unset( $IP );
@ini_set( 'allow_url_fopen', 0 ); # For security...
if( !file_exists( '../LocalSettings.php' ) ) {
  if ( file_exists( '../config/LocalSettings.php' ) ) {
    die( "To complete the installation, move <tt>config/LocalSettings.php</tt> to the parent directory.\n" );
  } else {
    die( "You'll have to <a href='config/index.php'>set the wiki up</a> first!" );
  }
}

# Valid web server entry point, enable includes.
# Please don't move this line to includes/Defines.php. This line essentially defines
# a valid entry point. If you put it in includes/Defines.php, then any script that includes
# it becomes an entry point, thereby defeating its purpose.
define( 'MEDIAWIKI', true );

require_once( '../includes/Defines.php' );
require_once( '../LocalSettings.php' );
require_once( '../includes/Setup.php' );

wfProfileIn( 'main-misc-setup' );
OutputPage::setEncodings(); # Not really used yet

# Debug statement for user levels
# print_r($wgUser);

# If the user is not logged in, the Namespace:title of the article must be in
# the Read array in order for the user to see it. (We have to check here to
# catch special pages etc. We check again in Article::view())
if ( !is_null( $wgTitle ) && !$wgTitle->userCanRead() ) {
$wgOut->loginToUse();
$wgOut->output();
exit;
}

if ($wgUser->getID() != 0) {
$wgOut->setArticleFlag( false );

$wgTitle = Title::makeTitle( NS_SPECIAL, "Custom" );

$wgOut->setArticleRelated( false );
$wgOut->setRobotPolicy( "noindex,follow" );



////////////////////////////////////////////////////////////
//
// xmlQuizCreator v1 - a simple quiz creation script
//
////////////////////////////////////////////////////////////
//
// This script allows you to to create questions for xmlQuiz v1.1 (which was originally
// created by Jon Thomas <http://www.fromthedesk.com/>
//
// Author: David Wees <http://www.unitorganizer.com/davidwees/>
// Last Modified: July 4th, 2006
//
// You may freely use, modify, and distribute this script.
////////////////////////////////////////////////////////////

//
// Collect data from previous post sessions
//

import_request_variables("p", "post_");

// Set the title of the page
$wTitle = "New Quiz";

// get Session variables.  Should switch this to actual global session variables
// but I'm worried that I'll pick the same variable name as somewhere else in MediaWiki

if (isset($_SESSION['formtype_quizxml'])) {
  $form = $_SESSION['formtype_quizxml'];
$formtype = 0 + $form;
} else {
  $formtype = "-1";
}
if (isset($SESSION['quizname_quizxml'])) {
  $quizname = $_SESSION['quizname_quizxml'];
} else {
  $quizname = " ";
}

if (isset($SESSION['number_quizxml'])) {
  $numb = $_SESSION['number_quizxml'];
$number = 0 + $numb;
} else {
  $number = 0;
}

if (isset($SESSION['question_quizxml'])) {
  $question = $_SESSION['question_quizxml'];
} else {
  $question = "none";
}

//if (isset($SESSION['question_confirm'])) {
//  $question_check = $_SESSION['question_confirm'];
//} else {
//  $question = "No";
//}

//
// Get the name of the quiz from the user if the formtype is -1.
//

if ($formtype == -1) {

        $wtext = "== Enter Quiz Name: ==\n\n";
        $text = "<form action=\"$PHP_SELF\" method=\"post\">\n";
$text .= "<input type=\"text\" name=\"quizname\" size=15>\n";
        $text .= "<input type=\"submit\" name=\"submit_quizname\" >\n";
        $text .= "</form>\n";
$quiz_name = $_POST['quizname'];

If (isset($quiz_name)) {
    // Create the XML format
    $stringAPP = "<?xml version=\"1.0\"?>\n"."<QUIZ>\n"."<TITLE>"."$quiz_name"."</TITLE>\n";
    $quiz = "$quiz_name".".xml";

    // Open the file to write
    $fh = fopen("$quiz", 'w') or die("can't create file");

    // Add the strings in XML format
    fwrite($fh, "$stringAPP");
    fclose($fh);

//Store session variables.
$_SESSION['formtype_quizxml'] = '0';
$_SESSION['quizname_quizxml'] = $quizname;
include ('quiz_creator.php');
           
        }

} elseif ($formtype == 0)  {

$wtext = "== Enter Number of Questions: ==\n\n";
        $text = "<form action=\"$PHP_SELF\" method=\"post\">\n";
$text .= "<input type=\"text\" name=\"numberquestions\" size=15>\n";
        $text .= "<input type=\"submit\" name=\"submitnumber\">\n";
        $text .= "</form>\n";


$number_questions = $_POST['numberquestions'];

if (isset($number_questions)) {

$number_questions = 0 + $number_questions;
$_SESSION['formtype_quizxml'] = $number_questions;
$SESSION['number_quizxml'] = $number_questions;
include ('quiz_creator.php');
}

}  elseif ($number <= $formtype){

      $wtext = "== Enter Question: ==\n\n";
    $text = "<form action=\"$PHP_SELF\" method=\"post\">\n";
$text .= "<textarea rows=\"5\" cols=\"50\" name=\"question\"></textarea>\n";
$text .= "<input type=\"submit\" name=\"submitnumber\">\n";
$text .= "</form>\n";

$question = $_POST['question'];
if (isset($question)){
 
  $stringAPP = "<QUESTION>\n"."<TEXT>"."$question"."</TEXT>\n";
$quizname = $SESSION['quizname_quizxml'].".xml";
        // Open the file to write
  $fh = fopen("$quizname", 'a') or die("can't open file");
  // Add the strings in XML format
  fwrite($fh, "$stringAPP");
  fclose($fh);

  $number = $number + 1;

  $_SESSION['number_quizxml'] = $number;
  include ('quiz_creator.php');
}
   
// Close if statement above.
} //else {
      //$_SESSION['formtype_quizxml'] = '-1';
            //$_SESSION['quizname_quizxml'] = 'start';
//$_SESSION['number_quizxml'] = '0';
//$_SESSION['question_confirm'] = "no";
//unset($_SESSION['question_quizxml']);


// $wgOut->setPageTitle($wTitle);

// $wgOut->addWikiText( "<br clear=all>\n" );
//$wgOut->addWikiText( $wtext );
//$wgOut->addWikiText( "<br clear=all>\n" );


// $wgOut->addHTML( $text );
// $wgOut->output();
// exit;
//}

$wgOut->setPageTitle($wTitle);

$wgOut->addWikiText( "<br clear=all>\n" );
$wgOut->addWikiText( $wtext );
$wgOut->addWikiText( "<br clear=all>\n" );


$wgOut->addHTML( $text );
$wgOut->output();
exit;

} else {
$wgOut->loginToUse();
$wgOut->output();
exit;
}
?>[/code]

Entering the quiz works, and entering the number of questions seems to work, but as soon as the program starts to append to the xml file two errors show up. 

1.  The xml file has the wrong name, instead of quizname.xml it's just .xml
2.  The program stops appending the tags when it runs out of memory.

You can check out my program at www.unitorganizer.com/mediawiki.  There are also log in issues with the program, it apparently tries to log me in twice.

Dave
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.