r92942 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r92941‎ | r92942 | r92943 >
Date:12:06, 23 July 2011
Author:ankitgarg833
Status:deferred (Comments)
Tags:
Comment:
new PageSchemas/specials/EditSchema.php
For editing/creating page schema through Form.
Uses a lot of juery and code from SF_UTils.
Modified paths:
  • /trunk/extensions/PageSchemas/specials/EditSchema.php (added) (history)

Diff [purge]

Index: trunk/extensions/PageSchemas/specials/EditSchema.php
@@ -0,0 +1,211 @@
 2+<?php
 3+/**
 4+ * Displays an interface to let users create all pages based on xml
 5+ *
 6+ * @author Ankit Garg
 7+ */
 8+
 9+class EditSchema extends IncludableSpecialPage {
 10+ function __construct() {
 11+ parent::__construct( 'EditSchema' );
 12+ wfLoadExtensionMessages('EditSchema');
 13+ }
 14+ public static function addJavascript() {
 15+ global $wgOut;
 16+
 17+ PageSchemas::addJavascriptAndCSS();
 18+
 19+ // TODO - this should be in a JS file
 20+ $template_name_error_str = wfMsg( 'sf_blank_error' );
 21+ $jsText =<<<END
 22+<script type="text/javascript">
 23+var fieldNum = 1;
 24+var templateNum = 1;
 25+function createTemplateAddField(template_num) {
 26+ fieldNum++;
 27+ newField = jQuery('#starterField').clone().css('display', '').removeAttr('id');
 28+ newHTML = newField.html().replace(/starter/g, fieldNum);
 29+ newField.html(newHTML);
 30+ newField.find(".deleteField").click( function() {
 31+ // Remove the encompassing div for this instance.
 32+ jQuery(this).closest(".fieldBox")
 33+ .fadeOut('fast', function() { jQuery(this).remove(); });
 34+ });
 35+ jQuery('#fieldsList_'+template_num).append(newField);
 36+}
 37+function createAddTemplate() {
 38+ templateNum++;
 39+ newField = jQuery('#starterTemplate').clone().css('display', '').removeAttr('id');
 40+ newHTML = newField.html().replace(/starter/g, templateNum);
 41+ newField.html(newHTML);
 42+ newField.find(".deleteTemplate").click( function() {
 43+ // Remove the encompassing div for this instance.
 44+ jQuery(this).closest(".templateBox")
 45+ .fadeOut('fast', function() { jQuery(this).remove(); });
 46+ });
 47+ jQuery('#templatesList').append(newField);
 48+}
 49+
 50+
 51+jQuery(document).ready(function() {
 52+ jQuery(".deleteField").click( function() {
 53+ // Remove the encompassing div for this instance.
 54+ jQuery(this).closest(".fieldBox")
 55+ .fadeOut('fast', function() { jQuery(this).remove(); });
 56+ });
 57+ jQuery(".deleteTemplate").click( function() {
 58+ // Remove the encompassing div for this instance.
 59+ jQuery(this).closest(".templateBox")
 60+ .fadeOut('fast', function() { jQuery(this).remove(); });
 61+ });
 62+});
 63+</script>
 64+
 65+END;
 66+ $wgOut->addScript( $jsText );
 67+ }
 68+
 69+ function execute( $category ) {
 70+ global $wgRequest, $wgOut;
 71+ global $wgSkin;
 72+ $this->setHeaders();
 73+ $text_1 = '<p>This category does not exist yet. Create this category and its page schema: </p>';
 74+ $text_2 = '<p>This category exists, but does not have a page schema. Create schema:" </p>';
 75+ if ( $category != "" ) {
 76+ $title = Title::newFromText( $category, NS_CATEGORY );
 77+ $pageId = $title->getArticleID();
 78+ $dbr = wfGetDB( DB_SLAVE );
 79+ //get the result set, query : slect page_props
 80+ $res = $dbr->select( 'page_props',
 81+ array(
 82+ 'pp_page',
 83+ 'pp_propname',
 84+ 'pp_value'
 85+ ),
 86+ array(
 87+ 'pp_page' => $pageId,
 88+ )
 89+ );
 90+ //first row of the result set
 91+ $row = $dbr->fetchRow( $res );
 92+ if( $row == null ){
 93+ //Create form here
 94+ self::addJavascript();
 95+ $text = "";
 96+ $text .= '<p>This category does not exist yet. Create this category and its page schema: </p>';
 97+ $text .= ' <form id="createPageSchemaForm" action="" method="post">' . "\n";
 98+ $text .= '<p>Name of schema: <input type="text" /> </p> ';
 99+ $text .= '<div id="templatesList">';
 100+ $text .= '<div class="templateBox" >';
 101+ $text .= '<fieldset style="background: #ddd;"><legend>Template</legend> ';
 102+ $text .= '<p>Name: <input type="text" name="t_name_1"/></p> ';
 103+ $text .= '<p><input type="checkbox" name="is_multiple_1"/> Allow multiple instances of this template</p> ';
 104+ $text .= '<div id="fieldsList_1">';
 105+ $text .= '<div class="fieldBox" >';
 106+ $text .= '<fieldset style="background: #bbb;"><legend>Field</legend>
 107+ <p>Field name: <input size="15" name="name_1">
 108+ Display label: <input size="15" name="label_1">
 109+ </p>
 110+ <p><input type="checkbox" name="is_list_1"/>
 111+ This field can hold multiple values
 112+ </p>
 113+ <p>Additional XML:
 114+ <textarea rows=4 style="width: 100%" name="add_xml_1"></textarea>
 115+ </p>
 116+ <input type="button" value="Remove field" class="deleteField" /> </div>
 117+ <div class="fieldBox" id="starterField" style="display: none">
 118+ <p>Field name: <input size="15" name="name_starter">
 119+ Display label: <input size="15" name="label_starter">
 120+ </p>
 121+ <p><input type="checkbox" name="is_list_starter" /> This field can hold a list of values, separated by commas
 122+ &#160;&#160; <p>Additional XML:
 123+ <textarea rows=4 style="width: 100%" name="add_xml_starter"></textarea>
 124+ </p>
 125+ <input type="button" value="Remove field" class="deleteField" />
 126+</p>
 127+</div>
 128+</div>
 129+ </fieldset> ';
 130+ $add_field_button = Xml::element( 'input',
 131+ array(
 132+ 'type' => 'button',
 133+ 'value' => 'Add Field',
 134+ 'onclick' => "createTemplateAddField(1)"
 135+ )
 136+ );
 137+ $text .= Xml::tags( 'p', null, $add_field_button ) . "\n";
 138+ $text .= '<hr />
 139+ <p>Additional XML:
 140+ <textarea rows=4 style="width: 100%" name="t_add_xml_1"></textarea>
 141+ </p>
 142+ <p><input type="button" value="Remove template" class="deleteTemplate" /></p>
 143+ </fieldset> </div>';
 144+ $text .= '<div class="templateBox" id="starterTemplate" style="display: none">
 145+<fieldset style="background: #ddd;">
 146+<legend>Template</legend>
 147+<p>Name: <input type="text" name="t_name_starter"/></p>
 148+<p><input type="checkbox" name="is_multiple_starter"/> Allow multiple instances of this template</p>
 149+<div id="fieldsList_starter">
 150+</div>
 151+ <p><input type="button" value="Add Field" onclick="createTemplateAddField(starter)" /></p>
 152+
 153+<hr />
 154+ <p>Additional XML:
 155+ <textarea rows=4 style="width: 100%" name="t_add_xml_starter"></textarea>
 156+ </p>
 157+ <p><input type="button" value="Remove template" class="deleteTemplate" /></p>
 158+ </fieldset>
 159+ </div>
 160+ </div>
 161+ <hr /> ';
 162+ $add_template_button = Xml::element( 'input',
 163+ array(
 164+ 'type' => 'button',
 165+ 'value' => 'Add Template',
 166+ 'onclick' => "createAddTemplate()"
 167+ )
 168+ );
 169+ $text .= Xml::tags( 'p', null, $add_template_button ) . "\n";
 170+ $text .= ' <hr />
 171+ <p><input type="submit" value="Save" /></p> ';
 172+ $text .= ' </form>';
 173+ $wgOut->addHTML( $text );
 174+ }else{
 175+ if( ($row[1] == 'PageSchema') && ($row[2] != null )){
 176+
 177+ }else{
 178+ $wgOut->addHTML($text_2);
 179+ }
 180+ }
 181+ }else {
 182+ $cat_titles = array();
 183+ $count_title = 0;
 184+ $text = "";
 185+ $dbr = wfGetDB( DB_SLAVE );
 186+ //get the result set, query : slect page_props
 187+ $res = $dbr->select( 'page_props',
 188+ array(
 189+ 'pp_page',
 190+ 'pp_propname',
 191+ 'pp_value'
 192+ ),
 193+ array(
 194+ 'pp_propname' => 'PageSchema'
 195+ )
 196+ );
 197+ while ( $row = $dbr->fetchRow( $res ) ) {
 198+ if( $row[2] != null ){
 199+ $page_id_cat = $row[0];
 200+ if( Title::newFromId($page_id_cat)->getNamespace() == NS_CATEGORY){
 201+ $cat_text = Title::newFromId($page_id_cat)->getText();
 202+ $generatePagesPage = SpecialPage::getTitleFor( 'EditSchema' );
 203+ $url = $generatePagesPage ->getFullURL() . '/' . $cat_text;
 204+ $text .= '<a href='.$url.'>'.$cat_text.' </a> <br /> ';
 205+ }
 206+ }
 207+ }
 208+ $dbr->freeResult( $res );
 209+ $wgOut->addHTML( $text );
 210+ }
 211+ }
 212+}

Comments

#Comment by Nikerabbit (talk | contribs)   18:01, 23 July 2011

svn eol-style, messages not i18ned

#Comment by Reedy (talk | contribs)   22:36, 23 July 2011
$dbr->freeResult( $res );
</pre

is not needed

<pre>
+			while ( $row = $dbr->fetchRow( $res ) ) {

Use

+ foreach( $res as $row ) {

#Comment by Reedy (talk | contribs)   22:37, 23 July 2011
$dbr->freeResult( $res );

is not needed

+			while ( $row = $dbr->fetchRow( $res ) ) {

Use

+ foreach( $res as $row ) {

Status & tagging log