We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 35844
    • 15 Posts
    So I've been following a tutorial posted here by Steven Lasch on setting up page statistics in MODx Evolution. I thought it would be quite simple to port it to Revolution, but I've hit a snag.

    I can't for the life of me figure out the database queries, according to the Revo Wiki the Evo database ($modx->db->query and similar) classes, have all depreciated and use xPDO XML definitions.

    Do I try to set up xPDO definitions? ...or use traditional PHP?

    <?php
    /* -----------------------------------------------------------------------------
    :: Snippet:  PageTrackerProcess 1.0.0
    ----------------------------------------------------------------------------- */
    
    // get necessary information
    $page = $_POST['page'];  
    $referrer = $_POST['referrer'];  
    $ip = $_SERVER['REMOTE_ADDR'];
    
    // Variable setup
    $views = 0;  
    $ips = '';  
    $referrers = '';
    
    $sql = $modx->query( "SELECT * FROM `devdef_stats` WHERE page = '" . $page . "'" );
    $resultArray = $modx->db->makeArray( $sql );
    foreach($resultArray as $item) {
        if($resultArray) {
    
        // Get this information  
        $views = $item['views'];  
        $ips = $item['ip'].' ';  
        if( $item['referrer'] )  
            $referrers = $item['referrer'].' ';  
            // Set a flag to state that old information was found  
            $flag = true;
    }
    }
    
    // Create arrays for all referrers and ip addresses  
    $ref_num = array();  
    $ip_num = array();
    
    // Find each referrer  
    $values = split( ' ', $referrers );  
      
    // Set a regular expression string to parse the referrer  
    $regex = '%(\d+)\((.*?)\)%';  
      
    // Loop through each referrer  
    foreach( $values as $value ) {  
        // Find the number of referrals and the URL of the referrer  
        preg_match( $regex, $value, $matches );  
      
        // If the two exist  
        if( $matches[1] && $matches[2] ) {  
            // Set the corresponding value in the array ( referrer link -> number of referrals )  
            $ref_num[$matches[2]] = intval( $matches[1] );
    }
    }  
      
    // If there is a referrer on this visit  
    if( $referrer ) {  
        // Add it to the array  
        $ref_num[$referrer]++;
    }
      
    // Get the IPs  
    $values = split( ' ', $ips );  
      
    // Repeat the same process as above  
    foreach( $values as $value ) {  
        // Find the information  
        preg_match( $regex, $value, $matches );  
      
        // Make sure it exists  
        if( $matches[1] && $matches[2] ) {  
            // Add it to the array  
            $ip_num[$matches[2]] = intval( $matches[1] );
    }
    }  
      
    // Update the array with the current IP.  
    $ip_num[$ip]++;
    
    // Reset the $ips string  
    $ips = '';  
      
    // Loop through all the information  
    foreach( $ip_num as $key => $val ) {  
        # Append it to the string (separated by a space)  
        $ips .= $val.'('.$key.') ';  
    }  
      
    // Trim the String  
    $ips = trim( $ips );  
      
    // Reset the $referrers string  
    $referrers = '';  
      
    // Loop through all the information  
    foreach( $ref_num as $key => $val ) {  
        # Append it  
        $referrers .= $val.'('.$key.') ';  
    }  
      
    // Trim the string  
    $referrers = trim( $referrers );
    
    // Update the number of views  
    $views++;
    
    // If we did obtain information from the database  
    // (the database already contains some information about this page)  
    if( $flag ) {  
        // Update it
        $fields = array(
            'ip' => $ips,
            'views' => $views,
            'referrer' => $referrers,
            'page' => $page
        );
        $modx->db->update($fields, 'devdef_stats', 'page="' . $page . '"');
    } else {
        $dbTable = array();
        $dbTable['ip'] = $ips;
        $dbTable['views'] = $views;
        $dbTable['referrer'] = $referrers;
        $dbTable['page'] = $page;
        $dbQuery = $modx->db->insert($dbTable, 'devdef_stats' );
    };


    Unless I've misunderstood the markup differences, I believe the following needs to be adapted:

    $modx->db->makeArray( $sql );

    $modx->db->update($fields, 'devdef_stats', 'page="' . $page . '"');

    $dbQuery = $modx->db->insert($dbTable, 'devdef_stats' );


    Could someone give me a hand understanding the changes that need to me made? I've read up on Reverse Engineering xPDO classes from tables, but I feel far too confused to even make an attempt.

    Greatly appreciated.

    This question has been answered by opengeek. See the first response.

    • discuss.answer
      • 22303 MODX Staff
      • 10,725 Posts
      Quote from: RLB at Oct 11, 2011, 08:05 AM
      So I've been following a tutorial posted I can't for the life of me figure out the database queries, according to the Revo Wiki the Evo database ($modx->db->query and similar) classes, have all depreciated and use xPDO XML definitions.

      Do I try to set up xPDO definitions? ...or use traditional PHP?

      Could someone give me a hand understanding the changes that need to me made? I've read up on Reverse Engineering xPDO classes from tables, but I feel far too confused to even make an attempt.

      Greatly appreciated.

      You just need to use the PDO methods that xPDO (which MODX itself extends) makes available for you. This is not that difficult and you don't need to worry about reverse-engineering classes to represent your tables unless you want to create an object model for taking care of the little details. You can make that happen at a later phase as you come to better understand xPDO.

      For now focus on understanding PDO and you should be able to adapt your queries easily. If not, you might be able to use the dbapi plugin to support your use of the legacy MODX DBAPI methods.
        • 35844
        • 15 Posts
        Thanks for the input, I'll research it later tonight and see how I get on. I've never worked with PHP inside a CMS or codebase before so there's been quite a strong learning curve.
          • 35844
          • 15 Posts
          I ended up using your DBAPI plugin to adapt the script, I'll have to take further time to look into PDO objects properly. Thanks for your help.