Difference between revisions of "Extension:RecordAdmin"

From Organic Design wiki
(render results as a table showing field values)
(add reset button and links back to new search)
Line 11: Line 11:
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
 
if (!defined('MEDIAWIKI')) die('Not an entry point.');
  
define('RECORDADMIN_VERSION','0.0.3, 2007-10-18');
+
define('RECORDADMIN_VERSION','0.0.5, 2007-10-19');
  
 
$wgRecordAdminCategory = 'Records';
 
$wgRecordAdminCategory = 'Records';
Line 49: Line 49:
 
global $wgOut, $wgRequest, $wgRecordAdminCategory;
 
global $wgOut, $wgRequest, $wgRecordAdminCategory;
 
$this->setHeaders();
 
$this->setHeaders();
$type = $wgRequest->getText('wpType') or $type = $param;
+
$type     = $wgRequest->getText('wpType') or $type = $param;
$record = $wgRequest->getText('wpRecord');
+
$record   = $wgRequest->getText('wpRecord');
$title = Title::makeTitle(NS_SPECIAL, 'RecordAdmin');
+
$title   = Title::makeTitle(NS_SPECIAL, 'RecordAdmin');
$wpTitle = trim($wgRequest->getText('wpTitle'));
+
$wpTitle = trim($wgRequest->getText('wpTitle'));
  
 +
$wgOut->addHTML("<div class='center'><a href='".$title->getLocalURL()."/$type'>New $type search</a> | "
 +
. "<a href='".$title->getLocalURL()."'>Select another record type</a></div><br>\n"
 +
);
 +
 
# Get posted form values if any
 
# Get posted form values if any
 
$posted = array();
 
$posted = array();
Line 72: Line 76:
 
if (empty($type)) {
 
if (empty($type)) {
 
$wgOut->addWikiText("== Select the type of record to search for ==\n");
 
$wgOut->addWikiText("== Select the type of record to search for ==\n");
 
+
 
$options = '';
 
$options = '';
 
$dbr  = &wfGetDB(DB_SLAVE);
 
$dbr  = &wfGetDB(DB_SLAVE);
Line 92: Line 96:
 
if (count($posted) && $wgRequest->getText('wpCreate')) {
 
if (count($posted) && $wgRequest->getText('wpCreate')) {
 
$t = Title::newFromText($wpTitle);
 
$t = Title::newFromText($wpTitle);
if ($t->exists()) $wgOut->addHTML("<div class='errorbox'>Sorry, \"$wpTitle\" already exists!</div>\n");
+
if (is_object($t)) {
else {
+
if ($t->exists()) $wgOut->addHTML("<div class='errorbox'>Sorry, \"$wpTitle\" already exists!</div>\n");
 +
else {
  
# Attempt to create the article
+
# Attempt to create the article
$article = new Article($t);
+
$article = new Article($t);
$summary = "[[Special:RecordAdmin/$type|RecordAdmin]]: New $type created";
+
$summary = "[[Special:RecordAdmin/$type|RecordAdmin]]: New $type created";
$text = '';
+
$text = '';
foreach ($posted as $k => $v) if ($v) $text .= "| $k = $v\n";
+
foreach ($posted as $k => $v) if ($v) $text .= "| $k = $v\n";
$text = $text ? "{{"."$type\n$text}}" : "{{"."$type}}";
+
$text = $text ? "{{"."$type\n$text}}" : "{{"."$type}}";
$success = $article->doEdit($text, $summary, EDIT_NEW);
+
$success = $article->doEdit($text, $summary, EDIT_NEW);
+
# Report success or error
+
# Report success or error
if ($success) $wgOut->addHTML("<div class='successbox'>\"$wpTitle\" created successfully</div>\n");
+
if ($success) $wgOut->addHTML("<div class='successbox'>\"$wpTitle\" created successfully</div>\n");
else $wgOut->addHTML("<div class='errorbox'>An error occurred while attempting to create the $type!</div>\n");
+
else $wgOut->addHTML("<div class='errorbox'>An error occurred while attempting to create the $type!</div>\n");
}
+
}
 +
} else $wgOut->addHTML("<div class='errorbox'>Bad title!</div>\n");
 
$wgOut->addHTML("<br><br><br><br>\n");
 
$wgOut->addHTML("<br><br><br><br>\n");
 
}
 
}
Line 122: Line 128:
 
$wgOut->addHTML($form);
 
$wgOut->addHTML($form);
 
$wgOut->addHTML(wfElement('input', array('type' => 'hidden', 'name' => 'wpType', 'value' => $type)));
 
$wgOut->addHTML(wfElement('input', array('type' => 'hidden', 'name' => 'wpType', 'value' => $type)));
$wgOut->addHTML('<br><hr><br>');
+
$wgOut->addHTML('<br><hr><br><table width="100%"><tr>'
$wgOut->addHTML(
+
.'<td>'.wfElement('input', array('type' => 'submit', 'name' => 'wpFind', 'value' => "Search")).'</td>'
wfElement('input', array('type' => 'submit', 'name' => 'wpFind', 'value' => "Search")). ' ' .
+
.'<td>'.wfElement('input', array('type' => 'submit', 'name' => 'wpCreate', 'value' => "Create")).'</td>'
wfElement('input', array('type' => 'submit', 'name' => 'wpCreate', 'value' => "Create new $type"))
+
.'<td width="100%" align="left">'.wfElement('input', array('type' => 'reset', 'value' => "Reset")).'</td>'
. '</form>'
+
.'</tr></table></form>'
 
);
 
);
 
 
Line 210: Line 216:
 
$wgOut->addHTML(wfElement('input', array('type' => 'hidden', 'name' => 'wpType', 'value' => $type)));
 
$wgOut->addHTML(wfElement('input', array('type' => 'hidden', 'name' => 'wpType', 'value' => $type)));
 
$wgOut->addHTML(wfElement('input', array('type' => 'hidden', 'name' => 'wpRecord', 'value' => $record)));
 
$wgOut->addHTML(wfElement('input', array('type' => 'hidden', 'name' => 'wpRecord', 'value' => $record)));
$wgOut->addHTML('<br><hr><br>');
+
$wgOut->addHTML('<br><hr><br><table width="100%"><tr>'
$wgOut->addHTML(wfElement('input', array('type' => 'submit', 'value' => 'Save')).'</form>');
+
.'<td>'.wfElement('input', array('type' => 'submit', 'value' => "Save")).'</td>'
 +
.'<td width="100%" align="left">'.wfElement('input', array('type' => 'reset', 'value' => "Reset")).'</td>'
 +
.'</tr></table></form>'
 +
);
 
}
 
}
 
 

Revision as of 23:11, 18 October 2008

<?php /**

* Extension:RecordAdmin - MediaWiki extension
*
Info.svg These are the MediaWiki extensions we're using and/or developing. Please refer to the information on the mediawiki.org wiki for installation and usage details. Extensions here which have no corresponding mediawiki article are either not ready for use or have been superseded. You can also browse our extension code in our local Subversion repository or our GitHub mirror.

Template:PhpCategory:Extensions created with Template:SpecialPage

* @package MediaWiki
* @subpackage Extensions
* @author Aran Dunkley User:Nad
* @licence GNU General Public Licence 2.0 or later
*/

if (!defined('MEDIAWIKI')) die('Not an entry point.');

define('RECORDADMIN_VERSION','0.0.5, 2007-10-19');

$wgRecordAdminCategory = 'Records';

$wgExtensionFunctions[] = 'wfSetupRecordAdmin';

$wgExtensionCredits['specialpage'][] = array( 'name' => 'Special:RecordAdmin', 'author' => 'User:Nad', 'description' => 'A special page for finding and editing record articles using a form', 'url' => 'http://www.organicdesign.co.nz/Extension:SpecialExample', 'version' => RECORDADMIN_VERSION );

require_once "$IP/includes/SpecialPage.php";

/**

* Define a new class based on the SpecialPage class
*/

class SpecialRecordAdmin extends SpecialPage {

function __construct() { SpecialPage::SpecialPage( 'RecordAdmin', # name as seen in links etc 'sysop', # user rights required true, # listed in special:specialpages false, # function called by execute() - defaults to wfSpecial{$name} false, # file included by execute() - defaults to Special{$name}.php, only used if no function false # includable ); }

/** * Override SpecialPage::execute() */ function execute($param) { global $wgOut, $wgRequest, $wgRecordAdminCategory; $this->setHeaders(); $type = $wgRequest->getText('wpType') or $type = $param; $record = $wgRequest->getText('wpRecord'); $title = Title::makeTitle(NS_SPECIAL, 'RecordAdmin'); $wpTitle = trim($wgRequest->getText('wpTitle'));

$wgOut->addHTML("

<a href='".$title->getLocalURL()."/$type'>New $type search</a> | " . "<a href='".$title->getLocalURL()."'>Select another record type</a>


\n"

);

# Get posted form values if any $posted = array(); foreach ($_POST as $k => $v) if (ereg('^ra_(.+)$', $k, $m)) $posted[$m[1]] = $v;

# Read in the form for this record type if one has been selected # - extract only the content from between the form tags and remove the submit input if ($type) { $form = new Article(Title::newFromText("Form:$type")); $form = $form->getContent(); $form = preg_replace('#<input.+?type=[\'"]?submit["\']?.+?/(input| *)>#', , $form); $form = preg_replace('#^.+?<form.+?>#s', , $form); $form = preg_replace('#</form>.+?$#s', , $form); $form = preg_replace('#name=[\'"](.+?)["\']#s', 'name="ra_$1"', $form); }

# If no type selected, render select list of record types from Category:Records if (empty($type)) { $wgOut->addWikiText("== Select the type of record to search for ==\n");

$options = ; $dbr = &wfGetDB(DB_SLAVE); $cl = $dbr->tableName('categorylinks'); $cat = $dbr->addQuotes($wgRecordAdminCategory); $res = $dbr->select($cl, 'cl_from', "cl_to = $cat", __METHOD__, array('ORDER BY' => 'cl_sortkey')); while ($row = $dbr->fetchRow($res)) $options .= '<option>'.Title::newFromID($row[0])->getText().'</option>';

$wgOut->addHTML(wfElement('form', array('action' => $title->getLocalURL('action=submit'), 'method' => 'post'), null)); $wgOut->addHTML("<select name='wpType'>$options</select> "); $wgOut->addHTML(wfElement('input', array('type' => 'submit', 'value' => 'Submit')).'</form>'); }

# Record type known, but no record selected, render form for searching or creating elseif (empty($record)) { $wgOut->addWikiText("== Find or Create a $type record ==\n");

# Process Create submission if (count($posted) && $wgRequest->getText('wpCreate')) { $t = Title::newFromText($wpTitle); if (is_object($t)) {

if ($t->exists()) $wgOut->addHTML("

Sorry, \"$wpTitle\" already exists!

\n");

else {

# Attempt to create the article $article = new Article($t); $summary = "RecordAdmin: New $type created"; $text = ; foreach ($posted as $k => $v) if ($v) $text .= "| $k = $v\n"; $text = $text ? "Template:"."$type\n$text" : "Template:"."$type"; $success = $article->doEdit($text, $summary, EDIT_NEW);

# Report success or error

if ($success) $wgOut->addHTML("

\"$wpTitle\" created successfully

\n"); else $wgOut->addHTML("

An error occurred while attempting to create the $type!

\n");

}

} else $wgOut->addHTML("

Bad title!

\n");

$wgOut->addHTML("



\n"); }

# Populate the form with the posted values foreach ($posted as $k => $v) $form = str_replace("name=\"ra_$k\"", "name=\"ra_$k\" value='$v'", $form);

# Render the form $wgOut->addHTML(wfElement('form', array('action' => $title->getLocalURL('action=submit'), 'method' => 'post'), null)); $wgOut->addHTML( 'Record Name: ' . wfElement('input', array('name' => 'wpTitle', 'size' => 30, 'value' => $wpTitle)) );

$wgOut->addHTML("\n



\n");

$wgOut->addHTML($form); $wgOut->addHTML(wfElement('input', array('type' => 'hidden', 'name' => 'wpType', 'value' => $type)));

$wgOut->addHTML('



' .'' .'' .'' .'
'.wfElement('input', array('type' => 'submit', 'name' => 'wpFind', 'value' => "Search")).''.wfElement('input', array('type' => 'submit', 'name' => 'wpCreate', 'value' => "Create")).''.wfElement('input', array('type' => 'reset', 'value' => "Reset")).'

</form>'

);

# Process Find submission if (count($posted) && $wgRequest->getText('wpFind')) { $wgOut->addWikiText("
\n== Search results ==\n");

# Select records which use the template and exhibit a matching title and other fields $records = array(); $dbr = &wfGetDB(DB_SLAVE); $tl = $dbr->tableName('templatelinks'); $ty = $dbr->addQuotes($type); $res = $dbr->select($tl, 'tl_from', "tl_namespace = 10 AND tl_title = $ty", __METHOD__); while ($row = $dbr->fetchRow($res)) { $t = Title::newFromID($row[0]); if (empty($wpTitle) || eregi($wpTitle, $t)) { $a = new Article($t); $text = $a->getContent(); $match = true; $r = array($t); foreach ($posted as $k => $v) { $i = preg_match("|$k\s*=\s*(.+)\s*$|mi", $text, $m); if ($v && !($i && eregi($v, $m[1]))) $match = false; $r[$k] = isset($m[1]) ? $m[1] : ; } if ($match) $records[$t->getPrefixedText()] = $r; } }

# Render a table of the matching records if (count($records)) { ksort($records);

$table = "

\n"; foreach ($posted as $k => $v) $table .= ""; $table .= "\n"; $stripe = ; foreach ($records as $k => $r) { $stripe = $stripe ?  : ' class="stripe"'; $table .= "<tr$stripe>\n"; foreach ($posted as $k => $v) $table .= ""; $table .= "\n"; } $table .= "
$type$k
<a href='".$r[0]->getLocalURL()."'>".$r[0]->getText()."</a>"; $table .= "(<a href='".$title->getLocalURL("wpType=$type&wpRecord=$k")."'>edit</a>)".$r[$k]."

\n";

$wgOut->addHTML($table); } else $wgOut->addWikiText("No matching records found!\n"); } }

# A specific record has been selected, render form for updating else { $wgOut->addWikiText("== Editing \"$record\" ==\n"); $article = new Article(Title::newFromText($record)); $text = $article->fetchContent();

# Update article if form posted if (count($posted)) {

# Get the location and length of the record braces to replace foreach ($this->examineBraces($text) as $brace) if ($brace['NAME'] == $type) $braces = $brace;

# Attempt to save the article $summary = "RecordAdmin: $type properties updated"; $replace = ; foreach ($posted as $k => $v) if ($v) $replace .= "| $k = $v\n"; $replace = $replace ? "Template:"."$type\n$replace" : "Template:"."$type"; $text = substr_replace($text, $replace, $braces['OFFSET'], $braces['LENGTH']); $success = $article->doEdit($text, $summary, EDIT_UPDATE);

# Report success or error

if ($success) $wgOut->addHTML("

$type updated successfully

\n"); else $wgOut->addHTML("

An error occurred during update!

\n");

$wgOut->addHTML("



\n"); }

# Populate the form with the current values in the article preg_match_all("|\|\s*(.+?)\s*=\s*(.+?)$|m", $text, $m); foreach ($m[1] as $i => $k) $form = eregi_replace("name=\"ra_$k\"", "name=\"ra_$k\" value='{$m[2][$i]}'", $form);

# Render the form $wgOut->addHTML(wfElement('form', array('action' => $title->getLocalURL('action=submit'), 'method' => 'post'), null)); $wgOut->addHTML($form); $wgOut->addHTML(wfElement('input', array('type' => 'hidden', 'name' => 'wpType', 'value' => $type))); $wgOut->addHTML(wfElement('input', array('type' => 'hidden', 'name' => 'wpRecord', 'value' => $record)));

$wgOut->addHTML('



' .'' .'' .'
'.wfElement('input', array('type' => 'submit', 'value' => "Save")).''.wfElement('input', array('type' => 'reset', 'value' => "Reset")).'

</form>'

); }

}

/** * Return array of braces used and the name, position, length and depth * See http://www.organicdesign.co.nz/MediaWiki_code_snippets */ function examineBraces(&$content) { $braces = array(); $depths = array(); $depth = 1; $index = 0; while (preg_match('/\\{\\{\\s*([#a-z0-9_]*)|\\}\\}/is', $content, $match, PREG_OFFSET_CAPTURE, $index)) { $index = $match[0][1]+2; if ($match[0][0] == '}}') { $brace =& $braces[$depths[$depth-1]]; $brace['LENGTH'] = $match[0][1]-$brace['OFFSET']+2; $brace['DEPTH'] = $depth--; } else { $depths[$depth++] = count($braces); $braces[] = array( 'NAME' => $match[1][0], 'OFFSET' => $match[0][1] ); } } return $braces; }

}

/**

* Called from $wgExtensionFunctions array when initialising extensions
*/

function wfSetupRecordAdmin() { global $wgLanguageCode, $wgMessageCache;

# Add the messages used by the specialpage if ($wgLanguageCode == 'en') { $wgMessageCache->addMessages(array( 'recordadmin' => 'Record administration' )); }

# Add the specialpage to the environment SpecialPage::addPage(new SpecialRecordAdmin()); }