Merge from SerbianVariants branch, trunk 16500 vs SerbianVariants 16523
[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 echo( "{$edits}\nCounting number of articles..." );
27
28 global $wgContentNamespaces;
29 $good = $dbr->selectField( 'page', 'COUNT(*)', array( 'page_namespace' => $wgContentNamespaces, 'page_is_redirect' => 0, 'page_len > 0' ), $fname );
30 echo( "{$good}\nCounting total pages..." );
31
32 $pages = $dbr->selectField( 'page', 'COUNT(*)', '', $fname );
33 echo( "{$pages}\nCounting number of users..." );
34
35 $users = $dbr->selectField( 'user', 'COUNT(*)', '', $fname );
36 echo( "{$users}\nCounting number of admins..." );
37
38 $admin = $dbr->selectField( 'user_groups', 'COUNT(*)', array( 'ug_group' => 'sysop' ), $fname );
39 echo( "{$admin}\nCounting number of images..." );
40
41 $image = $dbr->selectField( 'image', 'COUNT(*)', '', $fname );
42 echo( "{$image}\n" );
43
44 if( !isset( $options['noviews'] ) ) {
45 echo( "Counting total page views..." );
46 $views = $dbr->selectField( 'page', 'SUM(page_counter)', '', $fname );
47 echo( "{$views}\n" );
48 }
49
50 echo( "\nUpdating site statistics..." );
51
52 $dbw =& wfGetDB( DB_MASTER );
53 $values = array( 'ss_total_edits' => $edits,
54 'ss_good_articles' => $good,
55 'ss_total_pages' => $pages,
56 'ss_users' => $users,
57 'ss_admins' => $admin,
58 'ss_images' => $image );
59 $conds = array( 'ss_row_id' => 1 );
60 $views = array( 'ss_total_views' => isset( $views ) ? $views : 0 );
61
62 if( isset( $options['update'] ) ) {
63 $dbw->update( 'site_stats', $values, $conds, $fname );
64 } else {
65 $dbw->delete( 'site_stats', $conds, $fname );
66 $dbw->insert( 'site_stats', array_merge( $values, $conds, $views ), $fname );
67 }
68
69 echo( "done.\n\n" );
70
71 function showHelp() {
72 echo( "Re-initialise the site statistics tables.\n\n" );
73 echo( "Usage: php initStats.php [--update|--noviews]\n\n" );
74 echo( " --update : Update the existing statistics (preserves the ss_total_views field)\n" );
75 echo( "--noviews : Don't update the page view counter\n\n" );
76 }
77
78 ?>