root/trunk/mw_plugin.php

Revision 558, 10.7 kB (checked in by padams, 3 days ago)

fixed several bugs in serviceUser
renamed 'Administrator' role to 'admin' to maintain backwards compatability
fixed bogus error msg when applying module updates successfully

Line 
1<?php
2
3//
4// Open Web Analytics - An Open Source Web Analytics Framework
5//
6// Copyright 2006 Peter Adams. All rights reserved.
7//
8// Licensed under GPL v2.0 http://www.gnu.org/copyleft/gpl.html
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16// $Id$
17//
18
19require_once('owa_env.php');
20require_once(OWA_BASE_CLASSES_DIR.'owa_php.php');
21require_once "$IP/includes/SpecialPage.php";
22
23/* MEDIAWIKI GLOBALS */
24global $wgCachePages, $wgDBtype, $wgDBname, $wgDBserver, $wgDBuser, $wgDBpassword, $wgUser, $wgServer, $wgScriptPath, $wgScript;
25
26/* OWA's MEDIAWIKI CONFIGURATION OVERRIDES */
27
28// Public folder URI
29define('OWA_PUBLIC_URL', $wgServer.$wgScriptPath.'/extensions/owa/public/');
30
31// Build OWA's Mediawiki specific config overrides array
32$owa_config = array();
33$wiki_url = $wgScriptPath;
34
35// OWA DATABASE CONFIGURATION
36// Will use Mediawiki's config valuesunless there are values present in an OWA config file.
37// OWA uses this to setup it's own DB connection seperate from the one that Mediawiki uses.
38$owa_config['db_type'] = $wgDBtype;
39$owa_config['db_name'] = $wgDBname;
40$owa_config['db_host'] = $wgDBserver;
41$owa_config['db_user'] = $wgDBuser;
42$owa_config['db_password'] = $wgDBpassword;
43
44$owa_config['report_wrapper'] = 'wrapper_mediawiki.tpl';
45$owa_config['images_url'] = OWA_PUBLIC_URL.'i/';
46$owa_config['images_absolute_url'] = $owa_config['images_url'];
47$owa_config['main_url'] = $wgScriptPath.'/index.php?title=Special:Owa';
48$owa_config['main_absolute_url'] = $wgServer.$owa_config['main_url'];
49$owa_config['action_url'] = $wgServer.$wgScriptPath.'/index.php?action=owa&owa_specialAction';
50$owa_config['log_url'] = $wgServer.$wgScriptPath.'/index.php?action=owa&owa_logAction=1';
51$owa_config['link_template'] = '%s&%s';
52//$owa_config['authentication'] = 'mediawiki';
53$owa_config['site_id'] = md5($wgServer.$wiki_url);
54$owa_config['is_embedded'] = true;
55$owa_config['delay_first_hit'] = true;
56
57// Turn MediaWiki Caching Off
58global $wgCachePages, $wgCacheEpoch;
59$wgCachePages = false;
60$wgCacheEpoch = 'date +%Y%m%d%H%M%S';
61
62// Register Extension with MediaWiki
63$wgExtensionFunctions[] = 'owa_main';
64$wgExtensionCredits['other'][] = array( 'name' => 'Open Web Analytics for MediaWiki', 
65                                                                                'author' => 'Peter Adams <peter@openwebanalytics.com>', 
66                                                                                'url' => 'http://www.openwebanalytics.com' );
67                                                                               
68$wgExtensionCredits['specialpage'][] = array('name' => 'Open Web Analytics for MediaWiki', 
69                                                                                         'author' => 'Peter Adams', 
70                                                                                         'url' => 'http://www.openwebanalytics.com',
71                                                                                         'description' => 'Open Web Analytics for MedaWiki');
72
73//Load Special Page
74$wgAutoloadClasses['SpecialOwa'] = __FILE__;
75$wgSpecialPages['Owa'] = 'SpecialOwa';
76$wgHooks['LoadAllMessages'][] = 'SpecialOwa::loadMessages';
77$wgHooks['UnknownAction'][] = 'owa_actions';
78
79
80/**
81 * OWA Singleton Method
82 *
83 * Makes a singleton instance of OWA using the config array
84 */
85function owa_factory() {
86
87        global $owa_config;
88       
89        static $owa;
90       
91        if(!empty($owa)):
92                return $owa;
93        else:
94                $owa = new owa_php($owa_config);
95                return $owa;
96        endif;
97
98}
99
100/**
101 * Main Mediawiki Extension method
102 *
103 * sets up OWA to be triggered for various hooks/actions
104 */
105function owa_main() {
106
107        global $wgHooks;
108
109        // Hook for logging Article Page Views
110        $wgHooks['ArticlePageDataAfter'][] = 'owa_logArticle';
111        $wgHooks['SpecialPageExecuteAfterPage'][] = 'owa_logSpecialPage';
112        $wgHooks['CategoryPageView'][] = 'owa_logCategoryPage';
113       
114        // Hooks for adding page tracking tags
115        $wgHooks['ArticlePageDataAfter'][] = 'owa_footer';
116        $wgHooks['SpecialPageExecuteAfterPage'][] = 'owa_footer';
117        $wgHooks['CategoryPageView'][] = 'owa_footer';
118       
119       
120        //SpecialPage::addPage(new OwaSpecialPage());
121       
122    return;
123}
124
125/**
126 * Hook for OWA special actions
127 *
128 * This uses mediawiki's 'unknown action' hook to trigger OWA's special action handler.
129 * This is setup by adding 'action=owa' to the URLs for special actions. There is
130 * probably a better way to do this so that the OWA namespace is preserved.
131 *
132 * @TODO figure out how to register this method to be triggered only when 'action=owa' instead of
133 *               for all unknown mediawiki actions.
134 * @param object $specialPage
135 * @url http://www.mediawiki.org/wiki/Manual:MediaWiki_hooks/UnknownAction
136 * @return false
137 */
138function owa_actions() {
139       
140        global $wgOut;
141       
142        $owa = owa_factory();
143    owa_set_priviledges();
144       
145
146        $wgOut->disable();
147        $owa->handleSpecialActionRequest();
148       
149        return false;
150
151}
152
153/**
154 * OWA Priviledges
155 *
156 * Populates OWA requestion container with info about the current mediawiki user.
157 * This info is needed by OWA authentication system as well as to add dimensions
158 * requests that are logged.
159 */
160function owa_set_priviledges() {
161
162        global $wgUser;
163       
164        $owa = owa_factory();
165       
166        /*
167
168        $owa->params['caller']['mediawiki']['user_data'] = array(
169       
170                                        'user_level'    => $wgUser->mGroups,
171                                        'user_ID'               => $wgUser->mName,
172                                        'user_login'    => $wgUser->mName,
173                                        'user_email'    => $wgUser->mEmail,
174                                        'user_identity' => $wgUser->mRealName,
175                                        //'user_password'       => $wgUser->mPassword
176                                        );
177                                       
178        $owa->params['u'] = 'xxxxx'.$wgUser->mName;
179        $owa->params['p'] = 'xxxxxxx';//$wgUser->mPassword;
180       
181        */
182       
183        if ($owa->config['do_not_log_admins'] == true):
184                if (strtolower($wgUser->mGroups) == 'sysop' || 'bureaucrat' || 'developer'):
185                        $owa->params['do_not_log'] = true;
186                endif;
187        endif;         
188       
189        // preemptively set the current user info and mark as authenticated so that
190        // downstream controllers don't have to authenticate
191        $cu =&owa_coreAPI::getCurrentUser();
192        $cu->setUserData('user_id', $wgUser->mName);
193        $cu->setUserData('email_address', $wgUser->mEmail);
194        $cu->setUserData('real_name', $wgUser->mRealName);
195        $cu->setRole(owa_translate_role($wgUser->mGroups));
196        $cu->setAuthStatus(true);
197
198        return;
199}
200
201function owa_translate_role($role_array) {
202
203        if (in_array("*", $level)):
204                $owa_role = 'everyone';
205        elseif (in_array("user", $level)):
206                $owa_role = 'viewer';
207        elseif (in_array("autoconfirmed", $level)):
208                $owa_role = 'viewer';
209        elseif (in_array("emailconfirmed", $level)):
210                $owa_role = 'viewer';
211        elseif (in_array("bot", $level)):
212                $owa_role = 'viewer';
213        elseif (in_array("sysop", $level)):
214                $owa_role = 'admin';
215        elseif (in_array("bureaucrat", $level)):
216                $owa_role = 'admin';
217        elseif (in_array("developer", $level)):
218                $owa_role = 'admin';
219        endif;
220       
221        return $owa_role;
222
223}
224
225/**
226 * Logs Special Page Views
227 *
228 * @param object $specialPage
229 * @return boolean
230 */
231function owa_logSpecialPage(&$specialPage) {
232       
233        global $wgUser, $wgOut;
234       
235        $app_params['user_name']= $wgUser->mName;
236    $app_params['user_email'] = $wgUser->mEmail;
237    $app_params['page_title'] = $wgOut->mPagetitle;
238    $app_params['page_type'] = 'Special Page';
239
240        // Log the request
241        $owa = owa_factory();
242        $owa->log($app_params);
243       
244        return true;
245}
246
247/**
248 * Logs Category Page Views
249 *
250 * @param object $categoryPage
251 * @return boolean
252 */
253function owa_logCategoryPage(&$categoryPage) {
254       
255        global $wgUser, $wgOut;
256       
257        $app_params['user_name']= $wgUser->mName;
258    $app_params['user_email'] = $wgUser->mEmail;
259    $app_params['page_title'] = $wgOut->mPagetitle;
260    $app_params['page_type'] = 'Category';
261       
262        // Log the request
263        $owa = owa_factory();
264        $owa->log($app_params);
265       
266        return true;
267}
268
269/**
270 * Logs Article Page Views
271 *
272 * @param object $article
273 * @return boolean
274 */
275function owa_logArticle(&$article) {
276
277        global $wgUser, $wgOut, $wgTitle;
278       
279        $wgTitle->invalidateCache();
280        $wgOut->enableClientCache(false);
281       
282       
283        // Setup Application Specific Properties to be Logged with request
284        $app_params['user_name']= $wgUser->mName;
285    $app_params['user_email'] = $wgUser->mEmail;
286    $app_params['page_title'] = $article->mTitle->mTextform;
287    $app_params['page_type'] = 'article';
288   
289        // Log the request
290        $owa = owa_factory();
291        $owa->log($app_params);
292       
293        return true;
294       
295}
296
297/**
298 * Adds helper page tags to Article Pages if they are needed
299 *
300 * @param object $article
301 * @return boolean
302 */
303function owa_footer(&$article) {
304       
305        global $wgOut;
306        $owa = owa_factory();
307        $tags = $owa->placeHelperPageTags(false);
308       
309        $wgOut->addHTML($tags);
310               
311        return true;
312}
313
314
315/**
316 * OWA Special Page Class
317 *
318 * Enables OWA to be accessed through a Mediawiki special page.
319 */
320class SpecialOwa extends SpecialPage {
321
322        function SpecialOwa() {
323                SpecialPage::SpecialPage('Owa','',true);
324                self::loadMessages();
325        }
326
327        function execute() {
328                global $wgRequest, $wgOut, $wgUser, $wgSitename, $wgScriptPath, $wgScript, $wgServer;
329               
330                $this->setHeaders();
331                $owa = owa_factory();
332                # Get request data from, e.g.
333               
334                        // sets authentication priviledges
335                        owa_set_priviledges();
336               
337                $params = array();
338               
339                // check to see that owa in installed.
340                if (empty($owa->config['install_complete'])):
341
342                        $site_url = $wgServer.$wgScriptPath;
343                       
344                        $params = array('site_id' => md5($site_url), 
345                                                        'name' => $wgSitename,
346                                                        'domain' => $site_url, 
347                                                        'description' => '',
348                                                        'do' => 'base.installStartEmbedded');
349                                                       
350                elseif (empty($owa->params['do'])):
351                        if (empty($owa->params['view'])):
352                                $params['do'] = 'base.reportDashboard';
353                        endif;
354                endif;
355               
356                                $page = $owa->handleRequest($params);
357
358                                // switch for output scenario
359                                if (empty($owa->config['install_complete'])):
360                                        return $wgOut->addHTML($page);                                 
361                                else:
362                                        $wgOut->disable();
363                                        echo $page;
364                                        return;
365                                endif;
366               
367        }
368
369        function loadMessages() {
370                static $messagesLoaded = false;
371            global $wgMessageCache;
372               
373                        if ( $messagesLoaded ) return;
374                       
375                        $messagesLoaded = true;
376                       
377                        // this should be the only msg defined by mediawiki
378                        $allMessages = array(
379                                 'en' => array( 
380                                         'owa' => 'Open Web Analytics'
381                                         )
382                                );
383
384
385                        // load msgs in to mediawiki cache
386                        foreach ( $allMessages as $lang => $langMessages ) {
387                                   $wgMessageCache->addMessages( $langMessages, $lang );
388                        }
389                       
390                        return true;
391        }
392       
393}
394
395
396
397?>
Note: See TracBrowser for help on using the browser.