r59233 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r59232‎ | r59233 | r59234 >
Date:01:10, 19 November 2009
Author:nad
Status:deferred
Tags:
Comment:
move some globals into the object and prevent queries during job execution
Modified paths:
  • /trunk/extensions/RecordAdmin/RecordAdmin.php (modified) (history)
  • /trunk/extensions/RecordAdmin/RecordAdmin_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/RecordAdmin/RecordAdmin_body.php
@@ -16,16 +16,97 @@
1717 var $guid = '';
1818 var $quid = '';
1919 var $filter = array();
 20+ var $acturl = '';
 21+ var $done = false;
2022
2123 function __construct() {
 24+ global $wgHooks, $wgParser, $wgRequest, $wgRecordAdminTag, $wgRecordAdminCategory,
 25+ $wgRecordAdminTag, $wgRecordAdminTableMagic, $wgRecordAdminDataMagic;
 26+
2227 # Name to use for creating a new record either via RecordAdmin or a public form
2328 # todo: should add a hook here for custom default-naming
2429 $this->guid = strftime( '%Y%m%d', time() ) . '-' . substr( strtoupper( uniqid('', true) ), -5 );
2530 wfLoadExtensionMessages ( 'RecordAdmin' );
2631 SpecialPage::SpecialPage( 'RecordAdmin', 'recordadmin', true, false, 'default', true );
 32+
 33+ # Make recordID's of articles created with public forms available via recordid tag
 34+ $wgParser->setHook( $wgRecordAdminTag, array( $this, 'expandTag' ) );
 35+
 36+ # Add the parser-functions
 37+ $wgParser->setFunctionHook( $wgRecordAdminTableMagic, array( $this, 'expandTableMagic' ) );
 38+ $wgParser->setFunctionHook( $wgRecordAdminDataMagic, array( $this, 'expandDataMagic' ) );
 39+
 40+ # Check if posting a public creation form
 41+ $title = Title::newFromText( $wgRequest->getText( 'title' ) );
 42+ if ( is_object( $title ) && $title->getNamespace() != NS_SPECIAL && $wgRequest->getText( 'wpType' ) && $wgRequest->getText( 'wpCreate' ) )
 43+ $this->createRecord();
 44+
 45+ # A minimal hook so we know if the page has been rendered or not
 46+ # (so that record tables don't execute when run from the job-queue - looking for a better way to do this)
 47+ $wgHooks['BeforePageDisplay'][] = $this;
 48+
 49+ # Add some hooks if the current title is a record
 50+ if ( is_object( $title ) ) {
 51+ $types = array();
 52+ $id = $title->getArticleID();
 53+ $dbr = &wfGetDB( DB_SLAVE );
 54+ $cat = $dbr->addQuotes( $wgRecordAdminCategory );
 55+ $cl = $dbr->tableName( 'categorylinks' );
 56+ $tl = $dbr->tableName( 'templatelinks' );
 57+ $res = $dbr->select( $cl, 'cl_from', "cl_to = $cat" );
 58+ while ( $row = $dbr->fetchRow( $res ) ) $types[] = 'tl_title = ' . $dbr->addQuotes( Title::newFromID( $row[0] )->getText() );
 59+ $dbr->freeResult( $res );
 60+ $uses = join( ' OR ', $types );
 61+ if ( $uses && $row = $dbr->selectRow( $tl, 'tl_title', "tl_from = $id AND ($uses)" ) ) {
 62+ global $wgRecordAdminEditWithForm, $wgRecordAdminAddTitleInfo;
 63+ $this->type = $row->tl_title;
 64+
 65+ # Add title info
 66+ if ( $wgRecordAdminAddTitleInfo ) $wgHooks['OutputPageBeforeHTML'][] = $this;
 67+
 68+ # Add an "edit with form" action link
 69+ if ( $wgRecordAdminEditWithForm ) {
 70+ $wgHooks['SkinTemplateTabs'][] = $this;
 71+ $qs = "wpType={$this->type}&wpRecord=" . $title->getPrefixedText();
 72+ $this->acturl = Title::makeTitle( NS_SPECIAL, 'RecordAdmin' )->getLocalURL( $qs );
 73+ }
 74+ }
 75+ }
2776 }
2877
2978 /**
 79+ * Store the fact that this hook has executed so we don't run record tables from job queue
 80+ */
 81+ function onBeforePageDisplay( &$out, $skin = false ) {
 82+ return $this->done = true;
 83+ }
 84+
 85+ /**
 86+ * Add record type info below title
 87+ */
 88+ function onOutputPageBeforeHTML( &$out, &$text ) {
 89+ $text = '<div class="recordadmin-typeinfo">' . wfMsg( 'recordadmin-typeinfo', $this->type ) . "</div>\n" . $text;
 90+ return true;
 91+ }
 92+
 93+ /**
 94+ * Add action link
 95+ */
 96+ function onSkinTemplateTabs( &$skin, &$actions ) {
 97+ $tmp = array();
 98+ foreach ( $actions as $k => $v ) {
 99+ $tmp[$k] = $v;
 100+ if ( $k == 'edit' ) $tmp['editwithform'] = array(
 101+ 'text' => wfMsg( 'recordadmin-editwithform' ),
 102+ 'class' => false,
 103+ 'href' => $this->acturl
 104+ );
 105+ }
 106+ $actions = $tmp;
 107+ return true;
 108+ }
 109+
 110+ /**
30111 * Override SpecialPage::execute()
31112 */
32113 function execute( $param ) {
@@ -244,6 +325,9 @@
245326 function getRecords( $type, $posted, $wpTitle = '', $invert = false, $orderby = 'created desc' ) {
246327 global $wgRequest;
247328
 329+ # If the page is already rendered, don't run this query
 330+ if ( $this->done ) return array();
 331+
248332 # Generate a unique id for this set of parameters
249333 $this->quid = md5( var_export( array( $type, $posted ), true ) );
250334
Index: trunk/extensions/RecordAdmin/RecordAdmin.php
@@ -11,7 +11,7 @@
1212 * @licence GNU General Public Licence 2.0 or later
1313 */
1414
15 -define( 'RECORDADMIN_VERSION', '0.8.11, 2009-11-18' );
 15+define( 'RECORDADMIN_VERSION', '0.9.0, 2009-11-19' );
1616
1717 $wgRecordAdminUseNamespaces = false; # Whether record articles should be in a namespace of the same name as their type
1818 $wgRecordAdminCategory = 'Records'; # Category containing record types
@@ -31,9 +31,6 @@
3232 $wgGroupPermissions['sysop']['recordadmin'] = true;
3333 $wgAvailableRights[] = 'recordadmin';
3434
35 -$wgExtensionFunctions[] = 'wfSetupRecordAdmin';
36 -$wgHooks['LanguageGetMagic'][] = 'wfRecordAdminLanguageGetMagic';
37 -
3835 $wgExtensionCredits['specialpage'][] = array(
3936 'path' => __FILE__,
4037 'name' => 'Record administration',
@@ -44,93 +41,16 @@
4542 'version' => RECORDADMIN_VERSION,
4643 );
4744
48 -/**
49 - * Called from $wgExtensionFunctions array when initialising extensions
50 - */
51 -function wfSetupRecordAdmin() {
52 - global $wgSpecialRecordAdmin, $wgTitle, $wgParser, $wgHooks, $wgRequest, $wgRecordAdminCategory,
53 - $wgRecordAdminTag, $wgRecordAdminTableMagic, $wgRecordAdminDataMagic, $wgRecordAdminEditWithForm;
54 -
55 - # Make a global singleton so methods are accessible as callbacks etc
 45+$wgExtensionFunctions[] = 'efSetupRecordAdmin';
 46+function efSetupRecordAdmin() {
 47+ global $wgSpecialRecordAdmin;
5648 $wgSpecialRecordAdmin = new SpecialRecordAdmin();
57 -
58 - # Make recordID's of articles created with public forms available via recordid tag
59 - $wgParser->setHook( $wgRecordAdminTag, array( $wgSpecialRecordAdmin, 'expandTag' ) );
60 -
61 - # Add the parser-functions
62 - $wgParser->setFunctionHook( $wgRecordAdminTableMagic, array( $wgSpecialRecordAdmin, 'expandTableMagic' ) );
63 - $wgParser->setFunctionHook( $wgRecordAdminDataMagic, array( $wgSpecialRecordAdmin, 'expandDataMagic' ) );
64 -
65 - # Check if posting a public creation form
66 - $title = Title::newFromText( $wgRequest->getText( 'title' ) );
67 - if ( is_object( $title ) && $title->getNamespace() != NS_SPECIAL && $wgRequest->getText( 'wpType' ) && $wgRequest->getText( 'wpCreate' ) )
68 - $wgSpecialRecordAdmin->createRecord();
69 -
70 - # Add some hooks if the current title is a record
71 - if ( is_object( $title ) ) {
72 - $types = array();
73 - $id = $title->getArticleID();
74 - $dbr = &wfGetDB(DB_SLAVE);
75 - $cat = $dbr->addQuotes( $wgRecordAdminCategory );
76 - $cl = $dbr->tableName( 'categorylinks' );
77 - $tl = $dbr->tableName( 'templatelinks' );
78 - $res = $dbr->select( $cl, 'cl_from', "cl_to = $cat" );
79 - while ( $row = $dbr->fetchRow( $res ) ) $types[] = 'tl_title = ' . $dbr->addQuotes( Title::newFromID( $row[0] )->getText() );
80 - $dbr->freeResult( $res );
81 - $uses = join( ' OR ', $types );
82 - if ( $uses && $row = $dbr->selectRow( $tl, 'tl_title', "tl_from = $id AND ($uses)" ) ) {
83 - global $wgRecordAdminEditWithForm, $wgRecordAdminActionUrl, $wgRecordAdminCurrentType, $wgRecordAdminAddTitleInfo;
84 - $wgRecordAdminCurrentType = $row->tl_title;
85 -
86 - # Add title info
87 - if ( $wgRecordAdminAddTitleInfo ) {
88 - $wgHooks['OutputPageBeforeHTML'][] = 'wfRecordAdminAddTypeInfo';
89 - }
90 -
91 - # Add an "edit with form" action link
92 - if ( $wgRecordAdminEditWithForm ) {
93 - $wgHooks['SkinTemplateTabs'][] = 'wfRecordAdminEditWithForm';
94 - $qs = "wpType=$wgRecordAdminCurrentType&wpRecord=" . $title->getPrefixedText();
95 - $wgRecordAdminActionUrl = Title::makeTitle( NS_SPECIAL, 'RecordAdmin' )->getLocalURL( $qs );
96 - }
97 - }
98 - }
99 -
10049 }
10150
102 -/**
103 - * Setup parser-function magic
104 - */
105 -function wfRecordAdminLanguageGetMagic( &$magicWords, $langCode = 0 ) {
 51+$wgHooks['LanguageGetMagic'][] = 'efRecordAdminLanguageGetMagic';
 52+function efRecordAdminLanguageGetMagic( &$magicWords, $langCode = 0 ) {
10653 global $wgRecordAdminTableMagic, $wgRecordAdminDataMagic;
10754 $magicWords[$wgRecordAdminTableMagic] = array( $langCode, $wgRecordAdminTableMagic );
10855 $magicWords[$wgRecordAdminDataMagic] = array( $langCode, $wgRecordAdminDataMagic );
10956 return true;
11057 }
111 -
112 -/**
113 - * Add action link
114 - */
115 -function wfRecordAdminEditWithForm( &$skin, &$actions ) {
116 - global $wgRecordAdminActionUrl;
117 - $tmp = array();
118 - foreach ( $actions as $k => $v ) {
119 - $tmp[$k] = $v;
120 - if ( $k == 'edit' ) $tmp['editwithform'] = array(
121 - 'text' => wfMsg( 'recordadmin-editwithform' ),
122 - 'class' => false,
123 - 'href' => $wgRecordAdminActionUrl
124 - );
125 - }
126 - $actions = $tmp;
127 - return true;
128 -}
129 -
130 -/**
131 - * Add record type info below title
132 - */
133 -function wfRecordAdminAddTypeInfo( &$out, &$text ) {
134 - global $wgRecordAdminCurrentType;
135 - $text = '<div class="recordadmin-typeinfo">' . wfMsg( 'recordadmin-typeinfo', $wgRecordAdminCurrentType ) . "</div>\n" . $text;
136 - return true;
137 -}

Status & tagging log