Jump to content

re: interesting debugging issue with MediaWiki extension


dwees

Recommended Posts

Hi folks,

Have an extension for MediaWiki that reads a file like this:

[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]

and creates a quiz out of it.  Problem is, creating XML files from scratch for 20 question multiple choice can be a bit tiresome.

So I've started writing an extension that will help create the files.  So far I'm trying to get it to create 1 question only, and still having some difficulties.  It's written in functions now so it's a bit easier to read.

It has some difficulty with the mixed form that I just can't see (maybe tomorrow I'll spot it).  Also, plan is to find a way
to allow the script to add as many questions as the user desires, and end when the users clicks submit with an empty textarea. 

Also, can anyone point me at some code validators to make this extension more secure?

Anyone willing to help me debug it?  Check it out at [url=http://www.unitorganizer.com/mediawiki]www.unitorganizer.com/mediawiki[/url] and click on [test3].  You'll need to create a login.

[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" );


// Define functions


// Create the quiz form.
function create_quiz_form (){
        $text .= "<form action=\"$PHP_SELF\"\" method=\"post\">\n";
return $text;
}

// Create the input button.
function create_input_button ($input_name, $type, $value){
$text .= "<input type=\"$type\" name=\"$input_name\" value=\"$value\" size=\"15\">\n";
return $text;
}

// Create the textarea.
function create_textarea ($textarea_name) {
$text .= "<textarea rows=\"5\" cols=\"50\" name=\"$textarea_name\"></textarea>\n";
return $text;
}

// Create the submit button.
function create_submit_button ($submit_name){
$text .= "<input type=\"submit\" name=\"$submit_name\" >\n";
        $text .= "</form>\n";
return $text;
}
//if (!$_POST['quizname']&&!$_POST['question']) {
if (!file_exists("temp.txt")){

$wtext = "==Name of quiz==\n";
$text .= "Write the name of the quiz below, and click submit.\n";
$text .= create_quiz_form ();
$text .= create_input_button ("quizname","text","");
$text .= create_submit_button ("submit_quizname");
echo $quizname;

if ($_POST['quizname']) {
    $quiz_name = $_POST['quizname'];
    $quiz_file_name = "$quiz_name".".xml";
    $stringAPP = "<?xml version=\"1.0\"?>\n"."<QUIZ>\n"."<TITLE>"."$quiz_name"."</TITLE>\n";

// Open the file to write
    $fh = fopen("$quiz_file_name", 'w') or die("can't create file");
    // Add the strings in XML format
    fwrite($fh, "$stringAPP");
    fclose($fh);

$fh = fopen("temp.txt", 'w') or die("can't create file");
    // Add the strings in XML format
    fwrite($fh, "$quiz_file_name");
    fclose($fh);

}
}
if (file_exists("temp.txt")) {
  $quiz_file_name = file_get_contents("temp.txt");
}
if (file_exists($quiz_file_name)) {
$wtext = "==Enter Questions==\n";
$text = "When you are done entering questions, click submit without.\n";
$text .= "entering anything into the textbook.  Below the question, enter\n";
$text .= "the choices for the question, and click on the one that is the answer.\n";
$text .= create_quiz_form ();
$text .= create_textarea ("question_textarea");
$text .= create_input_button ("question_choice1","text","choice_1");
$text .= create_input_button ("question_radio","radio","choice1");
$text .= create_input_button ("question_choice2","text","choice_2");
$text .= create_input_button ("question_radio","radio","choice2");
$text .= create_input_button ("question_choice3","text","choice_3");
$text .= create_input_button ("question_radio","radio","choice3");
$text .= create_input_button ("question_choice4","text","choice_3");
$text .= create_input_button ("question_radio","radio","choice3");
$text .= create_submit_button ("submit_questions");
// call_editor($wtext, $text);

if ($_POST['question_textarea']){
      $choice = $_POST['question_radio'];
$question = $_POST['question_textarea'];
$choice_1 = $_POST['question_choice_1'];
$choice_2 = $_POST['question_choice_2'];
$choice_3 = $_POST['question_choice_3'];
$choice_4 = $_POST['question_choice_4'];

$text = "<QUESTION>\n"."<TEXT>"."$question"."</TEXT>";
$text .= "<CHOICES>"."$choice_1, "."$choice_2, "."$choice_3, "."$choice_4, "."</CHOICES>\n";

if ($choice == "choice1"){
    $answer = $_POST['choice1'];
$text .= "<ANSWER>"."$answer"."</ANSWER>";
}
if ($choice == "choice2"){
                  $answer = $_POST['choice2'];
$text .= "<ANSWER>"."$answer"."</ANSWER>";
}
if ($choice == "choice3"){
    $answer = $_POST['choice3'];
$text .= "<ANSWER>"."$answer"."</ANSWER>";
}
if ($choice == "choice4"){
                  $answer = $_POST['choice4'];
$text .= "<ANSWER>"."$answer"."</ANSWER>";
              }
$text .= "</QUESTION>";
$quiz_file_name = file_get_contents("temp.txt");
$stringAPP = $text;
      $fh = fopen("$quiz_file_name", 'a') or die("can't create file");
      // Add the strings in XML format
      fwrite($fh, "$stringAPP");
      fclose($fh);

if (unlink("temp.txt")){
} else {
  echo "Delete of temp.txt failed!\n";
}

  }
}
$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]
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.