Jump to content

Community

Developer Documentation

core/Sitemap

What it is

A Sitemap extension allows your application to add content to the sitemap that the Invision Community software builds. Content items that are properly mapped in a ContentRouter extension will automatically be added, depending upon permissions, so you only need to add content not mapped through a content router extension. Member profiles are an example of non-content-item content added to the sitemap through a Sitemap extension.

How to use

When you generate the extension, 4 methods and one class property will be added to the class template by default.

    /**
     * @brief    Recommended Settings
     */
    public $recommendedSettings = array();

You can define the default recommended settings for settings defined in the settings() method (outlined below) using the class property. The property is an array with keys being the setting keys and the values being the recommended setting values.

    /**
     * Add settings for ACP configuration to the form
     *
     * @return    array
     */
    public function settings()
    {
    }

The settings() method should return an array with keys as the setting keys, and the values being form helper objects where the form helper name is the setting key. For example:

    /**
     * Add settings for ACP configuration to the form
     *
     * @return    array
     */
    public function settings()
    {
        return array( 'my_setting' => new \IPS\Helpers\Form\Number( 'my_setting', \IPS\Settings::i()->my_setting, TRUE ) );
    }

    /**
     * Get the sitemap filename(s)
     *
     * @return    array
     */
    public function getFilenames()
    {
        return array( 'sitemap_' . mb_strtolower('{class}') );
    }

The getFilenames() method returns a list of possible filenames generated by the Sitemap extension. You should not include more than \IPS\Sitemap::MAX_PER_FILE links in a single sitemap file, so in situations where you may have more than this many links to include, you will need to split the links amongst multiple sitemap files. For example:

    /**
     * Get the sitemap filename(s)
     *
     * @return    array
     */
    public function getFilenames()
    {
        /* Get a count of how many files we'll be generating */
        $count = \IPS\Db::i()->select( 'COUNT(*)', 'my_database_table' )->first();
        $count = ( $count > \IPS\Sitemap::MAX_PER_FILE ) ? ceil( $count / \IPS\Sitemap::MAX_PER_FILE ) : 1;

        /* Generate the file names */
        $files    = array();
        for( $i=1; $i <= $count; $i++ )
        {
            $files[]    = "sitemap_myapp_" . $i;
        }
        /* Return */
        return $files;
    }

Here we select the total number of records we will be including, then we divide it by the number of records to include per-file, and then we generate names for each file as "sitemap_myapp_1", "sitemap_myapp_2" and so on.

    /**
     * Generate the sitemap
     *
     * @param    string            $filename    The sitemap file to build (should be one returned from getFilenames())
     * @param    \IPS\Sitemap    $sitemap    Sitemap object reference
     * @return    void
     */
    public function generateSitemap( $filename, $sitemap )
    {
    }

Finally, the generateSitemap() file generates the actual sitemap file. First, you will need to take the filename to determine which results we are including. For instance, if the filename is "sitemap_myapp_2", then we know that our start offset in our database query should be "2 * \IPS\Sitemap::MAX_PER_FILE" and our offset limit should be "\IPS\Sitemap::MAX_PER_FILE".

You will then want to fetch the results (typically by looping over a database result set) and push the results into an array temporarily before sending the values to buildSitemapFile().

    /**
     * Generate the sitemap
     *
     * @param    string            $filename    The sitemap file to build (should be one returned from getFilenames())
     * @param    \IPS\Sitemap    $sitemap    Sitemap object reference
     * @return    void
     */
    public function generateSitemap( $filename, $sitemap )
    {
        /* Which file are we building? */
        $_info        = explode( '_', $filename );
        $index        = array_pop( $_info ) - 1;
        $entries      = array();
        $start        = \IPS\Sitemap::MAX_PER_FILE * $index;
        $limit        = \IPS\Sitemap::MAX_PER_FILE;
        /* Retrieve the members */
        foreach( \IPS\Db::i()->select( '*', 'my_database_table', NULL, 'id ASC', array( $start, $limit ) ) as $row )
        {
            $entries[]    = array(
                'url'    => \IPS\Http\Url::internal( "app=myapp&module=module&controller=controller&id={$row['id']}", 'front', 'seotemplate', $row['seo_name'] )
            );
        }
        
        /* Build the file */
        $sitemap->buildSitemapFile( $filename, $entries );
    }


  Report Document


×
×
  • Create New...