Typo3: Count the number of Pages, Content, and Words in your install

May 17, 2010

Ever wonder how many pages, words or tt_content element you have in your Typo3 install? I am working with two large Typo3 sites and I was curious so I wrote a little piece of code. It works really well and is super quick. Here is what the output looks like:

Counting Pages
SELECT COUNT(uid) AS num_pages FROM `pages`Counting Pages Not Deleted
SELECT COUNT(uid) AS num_pages FROM `pages` WHERE deleted=’0′

Counting Content
SELECT COUNT(uid) AS num_content FROM `tt_content`

Counting Words
SELECT bodytext FROM `tt_content`

Totals
Num Pages: 4505
Num Pages Not Deleted: 2948
Num Content: 5152
Num Words: 28,394,588

If you would like to implement this yourself I provided the code below, instead of reinventing the wheel yourself.

(I put mine in the typo3conf folder for ease of use)

Counting Pages';
echo '
'.$sql_count_pages; $result = $mysqli->query($sql_count_pages); $result = $result->fetch_assoc(); $num_pages = $result['num_pages']; echo 'Counting Pages Not Deleted'; echo '
'.$sql_count_pages_not_deleted; $result = $mysqli->query($sql_count_pages_not_deleted); $result = $result->fetch_assoc(); $num_pages_not_deleted = $result['num_pages']; echo '

Counting Content'; echo '
'.$sql_count_content; $result = $mysqli->query($sql_count_content); $result = $result->fetch_assoc(); $num_content = $result['num_content']; echo '

Counting Words'; echo '
'.$sql_bodytext; $result = $mysqli->query($sql_bodytext); while($r = $result->fetch_assoc()) { $num_words += str_word_count($r['bodytext']); } echo '


Totals'; echo '
Num Pages: ' . $num_pages; echo '
Num Pages Not Deleted: ' . $num_pages_not_deleted; echo '
Num Content: ' . $num_content; echo '
Num Words: ' . number_format($num_words, 0, '.', ','); $mysqli->close(); ?>

3 thoughts on “Typo3: Count the number of Pages, Content, and Words in your install

  1. Paul heimann (October 13, 2010)

    To count the words without additionally using PHP just use:

    SELECT SUM( LENGTH( bodytext) ) FROM `tt_content` where deleted=’0′

    Because content elements in the tt_contens table can be deleted seperatly as well you should always use the WHERE-clause deleted=’0′.

    1. blobaugh (October 13, 2010)

      Paul, great! Thanks for the tip. I believe using LENGTH() will give you the wrong count though. LENGTH() returns the number of bytes in a string, not the number of words, additionally it does not even return the number of characters. Some characters have 2 bytes.

      Excluding deleted items is generally a good thing to do, however in this case I wanted to know what everything in the database was because I still made it all, deleted or not.

  2. Eduardo (December 8, 2010)

    Thanks!!! I was about to reinvent the wheel. Great. Thanks a lot for sharing it.