291de1eeb8f69d02f3a1aa8c013584fdbc1cc402
[lhc/web/wiklou.git] / maintenance / initStats.php
1 <?php
2
3 /**
4 * Maintenance script to re-initialise or update the site statistics table
5 *
6 * @package MediaWiki
7 * @subpackage Maintenance
8 * @author Brion Vibber
9 * @author Rob Church <robchur@gmail.com>
10 * @licence GNU General Public Licence 2.0 or later
11 */
12
13 $options = array( 'help', 'update', 'noviews' );
14 require_once( 'commandLine.inc' );
15 echo( "Refresh Site Statistics\n\n" );
16 $dbr =& wfGetDB( DB_SLAVE );
17 $fname = 'initStats';
18
19 if( isset( $options['help'] ) ) {
20 showHelp();
21 exit();
22 }
23
24 echo( "Counting total edits..." );
25 $edits = $dbr->selectField( 'revision', 'COUNT(*)', '', $fname );
26 $edits += $dbr->selectField( 'archive', 'COUNT(*)', '', $fname );
27 echo( "{$edits}\nCounting number of articles..." );
28
29 global $wgContentNamespaces;
30 $good = $dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $wgContentNamespaces, 'page_is_redirect' => 0, 'page_len > 0' ), $fname );
31 echo( "{$good}\nCounting total pages..." );
32
33 $pages = $dbr->selectField( 'page', 'COUNT(*)', '', $fname );
34 echo( "{$pages}\nCounting number of users..." );
35
36 $users = $dbr->selectField( 'user', 'COUNT(*)', '', $fname );
37 echo( "{$users}\nCounting number of admins..." );
38
39 $admin = $dbr->selectField( 'user_groups', 'COUNT(*)', array( 'ug_group' => 'sysop' ), $fname );
40 echo( "{$admin}\nCounting number of images..." );
41
42 $image = $dbr->selectField( 'image', 'COUNT(*)', '', $fname );
43 echo( "{$image}\n" );
44
45 if( !isset( $options['noviews'] ) ) {
46 echo( "Counting total page views..." );
47 $views = $dbr->selectField( 'page', 'SUM(page_counter)', '', $fname );
48 echo( "{$views}\n" );
49 }
50
51 echo( "\nUpdating site statistics..." );
52
53 $dbw =& wfGetDB( DB_MASTER );
54 $values = array( 'ss_total_edits' => $edits,
55 'ss_good_articles' => $good,
56 'ss_total_pages' => $pages,
57 'ss_users' => $users,
58 'ss_admins' => $admin,
59 'ss_images' => $image );
60 $conds = array( 'ss_row_id' => 1 );
61 $views = array( 'ss_total_views' => isset( $views ) ? $views : 0 );
62
63 if( isset( $options['update'] ) ) {
64 $dbw->update( 'site_stats', $values, $conds, $fname );
65 } else {
66 $dbw->delete( 'site_stats', $conds, $fname );
67 $dbw->insert( 'site_stats', array_merge( $values, $conds, $views ), $fname );
68 }
69
70 echo( "done.\n\n" );
71
72 function showHelp() {
73 echo( "Re-initialise the site statistics tables.\n\n" );
74 echo( "Usage: php initStats.php [--update|--noviews]\n\n" );
75 echo( " --update : Update the existing statistics (preserves the ss_total_views field)\n" );
76 echo( "--noviews : Don't update the page view counter\n\n" );
77 }
78
79 ?>